【问题标题】:style width on ::before div::before div 上的样式宽度
【发布时间】:2015-03-09 11:40:25
【问题描述】:

我需要一些帮助来设置我的标题样式。我在css counter-increment unwanted reset when skipping :before 有一个关于 css 计数器增量的问题,我让计数器工作,现在我的问题是样式,我希望它是这样的。

1.a 标题 1
10.a 标题 2

div.first:before
{
    width: auto;
    display: inline-block;
    content: '1.a.\00a0';
}
div.second:before
{
    width: auto;
    display: inline-block;
    content: '10.a.\00a0';
}

div.first {  
}
div.second {
}

h4.counter {
}
<div class="first">
    <h4 class="counter">foo</h4>
</div>

<div class="second">
    <h4 class="counter">foo</h4>
</div>

【问题讨论】:

  • 嘿@Sanna Widell,尝试为h4 标签添加样式!

标签: html css


【解决方案1】:

h 元素是块级元素,因此它们覆盖 100% 宽度并清除两侧。

您需要将显示从block 更改为inline,然后它会对齐。

例子:

div.first:before
{
    width: auto;
    display: inline-block;
    content: '1.a.\00a0';
}
div.second:before
{
    width: auto;
    display: inline-block;
    content: '10.a.\00a0';
}

div.first {  
}
div.second {
}

h4.counter {
    display:inline
}
<div class="first">
    <h4 class="counter">foo</h4>
</div>

<div class="second">
    <h4 class="counter">foo</h4>
</div>

所以这个先前的答案是一个对齐的文本示例:Align list items with custom CSS content before bullets

【讨论】:

  • 效果很好!但是当我有不止一行时,我希望在其他行上有​​缩进(第一行除外)。那可能吗?所以第二行的文本与第一行的文本在同一点开始。
【解决方案2】:

试试这样:DEMO

CSS:

div.first:before {
    display: inline-block;
    content:'1.a.\00a0';
}
div.second:before {
    display: inline-block;
    content:'10.a.\00a0';
}
div.first, div.second {
    display: block;
}
h4.counter {
    display: inline-block;
}

【讨论】:

    【解决方案3】:

    可以添加到 CSS 文件中的非常简单的小类:

    h4.counter{
        margin : -1.5% 0 0 3%;
        /*below seems to be hard-codednot so useful still can checkout:*/
        /*margin: -20px 0 0 35px;*/
    }
    

    它会按照您的意愿工作。

    【讨论】:

    • 嘿@Sanna Widell,这是有用的解决方案吗?
    • 你好@Victor,我之前尝试过,但我希望标题中的文本在计数器之后浮动。
    • 上例中的 Header 是什么?
    • 在我的示例中,“foo”是文本,我希望我看起来像 @Stewartside 发布的内容,但需要进行一些修改。我希望第二行(如果文本需要多行)有缩进,所以它从第一行的同一点开始。
    • 你真的检查过我的代码吗? bcuz,即使在多行文本中,它也会提供具有完全相同缩进的标题
    猜你喜欢
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多