Reputation: 551
I want to create the following layout:
+----------------------------------+
| TITLE |
| |
| + |
| |
| (drop here) |
+----------------------------------+
using the following HTML:
<div class="drop-box">
<p>TITLE</p>
<p>(drop here)</p>
</div>
and CSS:
.drop-box {
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
}
.drop-box:before {
content: "+";
width: 100%;
height: 100%;
font-size: 3em;
text-align: center;
line-height: 150px;
display: block;
}
Problem is that the text is now placed below the box with the '+' inside. How can I get the text to be placed inside the box. I've tried to use position: relative
for the p
tags but that doesn't work.
I've created a pen for this here: http://codepen.io/kdbruin/pen/ryMjgb
Upvotes: 4
Views: 2154
Reputation: 537
Instead of generating content on your .drop-box
container, you can do it directly on the second paragraph:
.drop-box p:nth-child(2)::before {
content: "+";
display: block;
font-size: 3em;
text-align: center;
}
Of course, it will probably be easier and cleaner to apply class to your paragraph:
HTML:
<div class="drop-box">
<p>TITLE</p>
<p class="has-plus-before">(drop here)</p>
</div>
CSS:
.has-plus-before {
content: "+";
display: block;
font-size: 3em;
}
Then for keep a clean vertical alignement, i suggest to use some flexbox properties on your container:
.drop-box {
display: flex;
justify-content: center;
flex-direction: column;
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
}
Take a look to this demo: https://jsfiddle.net/julienvanderkluft/1mxwep20/
Upvotes: 1
Reputation: 39342
As your title suggested, why won't draw +
as background on parent with linear-gradient()
?
.drop-box {
background-image: linear-gradient(black, black),
linear-gradient(black, black);
background-repeat: no-repeat;
background-size: 30px 2px, 2px 30px;
background-position: center center;
display: flex;
flex-direction: column;
justify-content: space-between;
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
}
<div class="drop-box">
<p>TITLE</p>
<p>(drop here)</p>
</div>
Upvotes: 0
Reputation: 67778
Use abolute position on the p tags, with according top/bottom/left parameters and z-index -1 (and add position: relative;
to .drop-box
for the absolute positioning to work):
.drop-box {
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
position: relative;
}
.drop-box:before {
content: "+";
width: 100%;
height: 100%;
font-size: 3em;
text-align: center;
line-height: 150px;
display: block;
}
.drop-box > p {
position: absolute;
z-index: -1;
}
.drop-box > p:first-child {
top: 10%;
left: 50%;
transform: translateX(-50%);
}
.drop-box > p:nth-child(2) {
bottom: 10%;
left: 50%;
transform: translateX(-50%);
}
<div class="drop-box">
<p>TITLE</p>
<p>(drop here)</p>
</div>
http://codepen.io/anon/pen/xqEqmg
Upvotes: 1
Reputation: 122077
You can use Flexbox
, you just need to set justify-content: space-between
and flex-direction: column
on parent element, and then change order of pseudo-element and last p element.
.drop-box {
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
display: flex;
justify-content: space-between;
flex-direction: column;
}
.drop-box:before {
content: "+";
font-size: 3em;
order: 1;
}
p {
margin: 0;
}
.drop-box p:last-child {
order: 2;
}
<div class="drop-box">
<p>TITLE</p>
<p>(drop here)</p>
</div>
You could also use position: absolute
on pseudo-element to remove it from elements flow and center it with transform: translate()
.drop-box {
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
display: flex;
flex-direction: column;
position: relative;
}
p {
margin: 0;
}
.drop-box:before {
content: "+";
font-size: 3em;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
p:last-child {
margin-top: auto;
}
<div class="drop-box">
<p>TITLE</p>
<p>(drop here)</p>
</div>
Upvotes: 1
Reputation: 5176
I would set a line-height on the paragraps and set absolute position on the cross, like this:
.drop-box {
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
position:relative;
}
.drop-box p{
line-height:50px;
}
.drop-box:before {
content: "+";
width: 100%;
height: 100%;
font-size: 3em;
line-height: 150px;
position:absolute;
left:0px;
}
<div class="drop-box">
<p>TITLE</p>
<p>(drop here)</p>
</div>
Upvotes: 1
Reputation: 1042
Insert the "+" after p tag
.drop-box {
width: 300px;
height: 150px;
border: 1px solid black;
text-align: center;
}
.drop-box p:first-of-type:after {
content: "+";
font-size: 2em;
text-align: center;
display: block;
margin: 16px 0;
}
<div class="drop-box">
<p>TITLE</p>
<p>(drop here)</p>
</div>
Upvotes: 1
Reputation: 4923
You have to give your drop-box
container a fixed height.
There are many ways to center align content with CSS. In my opinion the best way is to use flex-box
. Very easy to implement and very flexible.
HTML
<div class="drop-box">Title<br>+<br>(drop here)</div>
CSS
.drop-box {
align-items: center;
display: flex;
flex-direction: column;
height: 150px;
justify-content: center;
text-align: center;
}
Upvotes: 0
Reputation: 792
Here is easier way:
HTML:
<div class="drop-box">
<p>TITLE</p>
<p>+</p>
<p>(drop here)</p>
</div>
CSS:
.drop-box {
width: 300px;
border: 1px solid black;
text-align: center;
}
Does this work for you?
Upvotes: 0