Put the character as background in the div

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;
}

The problem is that the text is now placed under the box with a "+" inside. How can I get the text to be placed inside the box. I tried to use position: relativefor tags p, but this does not work.

I created a pen for this here: http://codepen.io/kdbruin/pen/ryMjgb

+4
source share
8 answers

I would set the line height on the paragraphs and set the absolute position on the cross, for example:

.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>
Run codeHide result
+1
source

Flexbox, justify-content: space-between flex-direction: column , p.

.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>
Hide result

position: absolute , 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>
Hide result
+1

"+" p

.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>
Hide result
+1

.drop-box :

.drop-box p:nth-child(2)::before {
  content: "+";
  display: block;
  font-size: 3em;
  text-align: center;
}

, :

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;
}

, , flexbox :

.drop-box {
  display: flex;
  justify-content: center;
  flex-direction: column;
  width: 300px;
  height: 150px;
  border: 1px solid black;
  text-align: center;
}

: https://jsfiddle.net/julienvanderkluft/1mxwep20/

+1

p- top/bottom/left z-index -1 ( position: relative; .drop-box ):

.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>
Hide result

http://codepen.io/anon/pen/xqEqmg

+1

:

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;
}

?

0

drop-box . CSS. , - flex-box. .

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;
}
0

, + 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>
Hide result
0
source

Source: https://habr.com/ru/post/1671601/


All Articles