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

Place a character as background in a div

我想创建以下布局:

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

问题是文本现在位于内部带有“+”的框下方。我怎样才能把文本放在盒子里。我尝试将 position: relative 用于 p 标签,但这不起作用。

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

这是更简单的方法:

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

这对你有用吗?

可以使用Flexbox,只需要在父元素上设置justify-content: space-betweenflex-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>

您还可以在伪元素上使用 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>

你必须给你的 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;
}

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

您可以直接在第二段生成内容,而不是在 .drop-box 容器中生成内容:

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

当然,将 class 应用于您的段落可能会更容易、更清晰:

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/

我会在段落上设置行高并在十字上设置绝对位置,如下所示:

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

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

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

正如你的标题所暗示的,为什么不在 parent 上用 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>