【问题标题】:Change text align after first line在第一行之后更改文本对齐
【发布时间】:2017-10-24 16:32:48
【问题描述】:

我有一个跨越多行的文本。第一行应该从左到右,但是任何不适合第一行的多余文本都应该右对齐。

这是我要描述的内容(包含 3 个两行段落):

1. Lorem ipsum dolor sit amet, consectetur adipiscing    |
                                                    elit.|
2. Sed do eiusmod tempor incididunt ut labore et dolore  |
                                            magna aliqua.|
3. Ut enim ad minim veniam, quis nostrud exercitation ul-|
                     lamco laboris nisi ut aliquip ex ea.|

我已经尝试过这个简单的解决方案:

div {
  text-align: right;
}

div::first-line {
  text-align: left;
}

但这不起作用。强制块显示到第一行 - div::first-line {display: block;} - 也无济于事。

将第一行或每隔一行关闭到单独的span 或其他东西以进行操作或使其全部为预格式化文本不是一种选择;它应该会自动进行。

有没有办法用 CSS 实现这一点?

【问题讨论】:

    标签: html css text-alignment


    【解决方案1】:

    如果您将每一行包装为单独的div,则可以使用text-align-last 属性:

    div {
       text-align: left;
       text-align-last: right;
       margin-bottom:10px;
    }
    <div>Fungus is common in temperate and tropical regions around the world. When young, it seems a puffball; in maturity, the outer layer of fruit body tissue splits open in a star shape, similar in appearance to the earthstars.</div>
    <div>The fungus grows in mutual symbiosis with roots of various trees, especially in sandy soils. It can open up its rays to expose the spore sac in response to increased humidity</div>
    <div>The rays have an irregularly cracked surface, while the spore case is pale brown and smooth with an irregular slit or tear at the top.</div>

    【讨论】:

    • 谢谢,这个text-align-last 属性非常接近我想要实现的目标。
    • @lipao 很高兴做出贡献。这是一个艰难的过程,但我已经接近了。
    【解决方案2】:

    由于您的方法是强制块显示,您可以尝试使用文本缩进的替代方法。

    p {
      margin-left: 3em; 
      text-indent: -3em;
    }
    

    它并没有按照语法将第一行文本之后的行完全对齐,但我认为这可能是您想要实现的结果。

    这里是sn-p的代码:

    p { 
      text-align-last: right;
      margin-left: 3em; 
      text-indent: -3em 
     }
    <p>I am a very long
    sentence where my second line is longer.sentence where my second line is 
     longer. sentence where my second line is longer.sentence where my second line  is longer. sentence where my second line is longer.sentence where my second line  is longer. sentence where my second line is longer.sentence where my second line  is longer. sentence where my second line is longer.sentence where my second line  is longer. sentence where my second line is longer.</p>
    <p>I am a very long
    sentence where my second line is longer.sentence where my second line is longer.sentence where my second line is longer.sentence where my second line is longer.sentence where my second line is longer.sentence where my second line is longer.sentence where my second line is longer.sentence where my second line is longer.</p>

    这是fiddle

    【讨论】:

    • 我实际上也打算实现它,因为我不希望多余的行(从右侧运行)延伸到第一行。但正如你承认的那样,它并不能正确解决问题。
    • @lipao 由于目前不存在逐行 CSS 选择器,因此您可以结合使用 Koby 的解决方案和我的解决方案来实现纯 CSS 方法。我已经更新了代码 sn-p 和我的小提琴。或者,如果您对 JS 方法持开放态度,您可以查看 Lining.js zencode.in/lining.js
    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多