将一个角色作为背景放在div中

时间:2017-03-06 13:38:01

标签: html css pseudo-element

我想创建以下布局:

+----------------------------------+
|               TITLE              |
|                                  |
|                 +                |
|                                  |
|            (drop here)           |
+----------------------------------+

使用以下HTML:

<div class="drop-box">
  <p>TITLE</p>
  <p>(drop here)</p>
</div>

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

问题是文字现在放在方框下面,而且&#39; +&#39;内。如何将文本放在框内。我已尝试将position: relative用于p代码,但这并不起作用。

我在这里为此创建了一支笔:http://codepen.io/kdbruin/pen/ryMjgb

8 个答案:

答案 0 :(得分:1)

您可以使用Flexbox,只需在父元素上设置justify-content: space-betweenflex-direction: column,然后更改伪元素和最后一个p元素的顺序。

&#13;
&#13;
.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;
}
&#13;
<div class="drop-box">
  <p>TITLE</p>
  <p>(drop here)</p>
</div>
&#13;
&#13;
&#13;

您还可以在伪元素上使用position: absolute将其从元素流中移除,并将其与transform: translate()

对齐

&#13;
&#13;
.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;
}
&#13;
<div class="drop-box">
  <p>TITLE</p>
  <p>(drop here)</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分: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>

答案 2 :(得分: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/

答案 3 :(得分:1)

我会在paragraps上设置一个行高,并在十字架上设置绝对位置,如下所示:

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

答案 4 :(得分:1)

在p标签上使用绝对位置,使用顶部/底部/左侧参数和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>

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

答案 5 :(得分:0)

这是更简单的方法:

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

这对你有用吗?

答案 6 :(得分: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;
}

答案 7 :(得分: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>