【问题标题】:How do I make ::before fill the height?如何在填充高度之前制作 ::before?
【发布时间】:2015-09-30 10:22:48
【问题描述】:

我有以下 HTML:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="brokencss.css">
</head>
<body>
    <div class="datapoint">
    <span class="label">
        This is a test.
        </span>
    </div>
</body>
</html>

和 CSS:

body {
    background-color: rgb(0,30,60);
    margin-left:40px;
    margin-right:40px;
    margin-top:20px;
    color: rgb(250,240,250);
    font-family: 'Verdana';
    font-size: 35px;
}

.datapoint::before {
    content: ">>";
    background-color:black;
    color: white;
    height:100%;
}

.datapoint {
    margin-top: 15px;
    max-width: 20em;
    background-color: red;
    height:100%;
}

.label {
    font-family:'Courier New';
    font-size:60px;
}

呈现为:http://imgur.com/0oVTuGw

我希望带有“>>”的黑色 ::before 块填充 .datapoint div 的高度,但我找不到这样做的方法。我将两者的高度都设置为 100%,所以我有点困惑为什么这不起作用。

【问题讨论】:

标签: css


【解决方案1】:

我找到了比我提供的更好的解决方案:

  1. 我将::beforelabel 元素显示为inline-block

  2. 我将label元素的line-height设置为等于font-size

  3. 我将::before 元素的line-height 设置为相同的值。

  4. vertical-align: top; 添加到::before 元素。

body {
    background-color: rgb(0,30,60);
    margin-left:40px;
    margin-right:40px;
    margin-top:20px;
    color: rgb(250,240,250);
    font-family: 'Verdana';
    font-size: 35px;
}

.datapoint::before {
    content: ">>";
    background-color:black;
    color: white;
    font-size: 35px;
    display: inline-block;
    line-height: 60px;
    vertical-align: top;
}

.datapoint {
    margin-top: 15px;
    background-color: red;
    max-width: 20em;
    white-space: nowrap;
}

.label {
    font-family:'Courier New';
    font-size: 60px;
    line-height: 60px;
    display: inline-block;
    margin-left: 15px;
}
<div class="datapoint">
        <span class="label">
            This is a test.
        </span>
    </div>

【讨论】:

    【解决方案2】:

    建立在弗拉基米尔在他的评论中创建的小提琴之上,只需要添加填充。看到这个fiddle illustrating a solution

    white-space:nowrap;
    

    上面的 CSS 将强制文本保持在一行,因为任何新行都不会遵守填充权。我希望这会有所帮助。

    position: absolute; 是让您的元素拉伸高度的原因。有关 :before 伪元素的更多信息,请参阅this question

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 2019-02-16
      • 2019-06-11
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 2012-06-22
      相关资源
      最近更新 更多