【问题标题】:Justifying the last line of text using CSS使用 CSS 对齐最后一行文本
【发布时间】:2011-08-05 01:36:47
【问题描述】:

我有一个 CSS 帮助器类,旨在强制“文本”的最后一行(或在预期用途中,内联块 div)与其余部分一样对齐。

这是我得到的代码:

.justify-all-lines
{
    text-align: justify;
    /* IE-only properties that make the :after below unnecessary (we need this because IE 6/7 don't support :after, though) but if they both apply it'll be fine */
    -ms-text-justify: distribute-all-lines;
    text-justify: distribute-all-lines;
}

.justify-all-lines > *:last-child:after
{
    display: inline-block;
    width: 100%;
    content: 'hello';
}

.blocky
{
    display: inline-block;
    /* Make inline block work in IE 6/7 */
    zoom: 1;
    *display: inline;
}

这是打算这样使用的:

<div class="justify-all-lines">
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
    <div class="blocky">There is stuff in here.</div>
</div>

但是,我看到“hello”出现在最后一个“blocky”div 内部,而不是最后一个“blocky”div 之后。我做错了什么?

【问题讨论】:

  • .justify-all-lines &gt; *:last-child:after { /* stuff for IE6/7 */ } 很奇怪。 IE6不支持&gt;,IE6/7都不支持:last-child:after
  • 您目前正在测试哪些浏览器?你能做一个jsFiddle 演示来展示这个问题吗?
  • @thirtydot 我正在使用 dotless (dotlesscss.org),这只是复制和粘贴异常,因为我为了这篇文章的目的将 mixin 转换为内联 CSS。认为比 LESS 更多的人会熟悉纯 CSS。我会修复它。 ;)
  • “我看到 'hello' 出现在最后一个 'blocky' div 里面”——它应该这样做。它不会创建新元素,但(至少显然)将内容附加到指定元素。
  • 这是一个显示问题的 jsFiddle 演示:jsfiddle.net/Rk79Y

标签: css


【解决方案1】:

工作解决方案:

.justify-all-lines
{
    /* This element will need layout for the text-justify
     * to take effect in IE7 (and possibly previous versions);
     * this will force it, for more info Google "hasLayout in IE"
     */
    overflow: hidden;
    text-align: justify;

    /* For IE6 to IE7 since they don't support :after */
    -ms-text-justify: distribute-all-lines; /* IE8+ */
    text-justify: distribute-all-lines; /* IE5+ */
}

.justify-all-lines:after
{
    /*
     * We don't need IE6 and IE7 inline-block hack support here
     * since they don't support :after anyways (the text-justify
     * properties for them are above)... IE8 and above have native
     * inline-block support so for IE8+, both the text-justify and
     * :after will take effect but it doesn't have any negative
     * effects since this element is invisible
     */
    display: inline-block;
    width: 100%;
    content: '.';
    font-size: 0;
    height: 0;
    line-height: 0;
    visibility: hidden;
}

【讨论】:

  • jsfiddle.net/Rk79Y/6。似乎在 IE7 中不起作用,但是我的解决方案也没有(除非我像我链接到的答案中那样调整它)。否则没关系。
  • 嗯,同样的代码在我的网站上有效,但在 jsFiddle 中无效。我想知道它是否对 CSS 有所帮助...
  • text-justify 是在 IE5 中添加的,而 -ms-text-justify 是在 IE8 中添加的,所以它肯定可以正常工作...
  • 有布局!有布局!!显然.justify-all-lines 需要布局才能使text-justify 工作。我在我的网站上的 CSS 中有overflow:hidden,它触发了 hasLayout,但在 jsFiddle 中没有。这就解释了。非常感谢您指出这一点,因为我不会注意到!工作 jsFiddle 链接:jsfiddle.net/Rk79Y/9
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
相关资源
最近更新 更多