CSS 对齐div的最后一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4771304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:31:53  来源:igfitidea点击:

Justify the last line of a div?

cssjustifytext-align

提问by JCOC611

I don't think the "why?" of this question is important...but what I need to do is to text-align:justifythe last line of text from a DIV. Normally, the last line (or the first if there are no other lines, which is the current case) of a div isn't justified, but aligned left. I know it might not make sense at all, but I absolutely need the last line to be justified!

我不认为“为什么?” 这个问题很重要......但我需要做的是text-align:justifyDIV到最后一行文本。通常,div 的最后一行(或第一行,如果没有其他行,这是当前情况)不是对齐的,而是左对齐的。我知道这可能根本没有意义,但我绝对需要最后一行来证明!

回答by Kristin

Here's a cross-browser method that works in IE6+

这是一个适用于 IE6+ 的跨浏览器方法

It combines text-align-last: justify; which is supported by IE and a variation of the :after pseudo-content method. It includes a fix to remove extra space added after one line text elements.

它结合了 text-align-last: justify; 这是由 IE 和 :after 伪内容方法的变体支持的。它包括删除一行文本元素后添加的额外空间的修复。

CSS:

CSS:

p, h1{
   text-align: justify;
   text-align-last: justify;
}

p:after, h1:after{
   content: "";
   display: inline-block;
   width: 100%;
}

If you have one line of text, use this to prevent the blank line the above CSS will cause

如果您有一行文本,请使用它来防止上述 CSS 导致的空行

h1{
   text-align: justify;
   text-align-last: justify;
   height: 1em;
   line-height: 1;
}

h1:after{
   content: "";
   display: inline-block;
   width: 100%;
}

More details: http://kristinlbradley.wordpress.com/2011/09/15/cross-browser-css-justify-last-line-paragraph-text/

更多详情:http: //kristinlbradley.wordpress.com/2011/09/15/cross-browser-css-justify-last-line-paragraph-text/

回答by thirtydot

This is the cleanest hack I could come up with/find. Your mileage may vary.

这是我能想出/找到的最干净的黑客。你的旅费可能会改变。

I tested my example in IE8, Firefox, Chrome, Opera, Safari.

我在 IE8、Firefox、Chrome、Opera、Safari 中测试了我的示例。

IE7 does not implement the :afterpseudo-class, so it won't work there.

IE7 没有实现:after伪类,所以它不会在那里工作。

If you need IE7 support, it would probably work to stick the " ___"inside an extraneous spanat the end of the div(use JS?).

如果您需要 IE7 支持,它可能会在(使用 JS?)的末尾粘贴" ___"一个无关span的内容div

Live Demo(see code)

现场演示见代码

CSS:

CSS:

div {
    width: 618px;        
    text-align: justify
}
div:after {
    content: " __________________________________________________________";
    line-height: 0;
    visibility: hidden
}

HTML:

HTML:

<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean dui dolor, bibendum quis accumsan porttitor, fringilla sed libero. Phasellus felis ante, egestas at adipiscing lobortis, lobortis id mi. Praesent pulvinar dictum purus. Duis rhoncus bibendum vehicula. Vestibulum mollis, metus a consectetur semper, urna enim sollicitudin lacus, vel imperdiet turpis nisl at metus. Integer iaculis pretium dui, a viverra dolor lobortis pellentesque. Aliquam quis felis nec purus semper interdum. Nam ac dolor in sem tincidunt egestas. Ut nisl tortor, laoreet eu vestibulum id, bibendum at ipsum. Maecenas elementum bibendum orci, ac eleifend felis tincidunt in. Fusce elementum lacinia feugiat.
</div>

Unfortunately, it seems to make the diva line taller than it needs to be.

不幸的是,它似乎使div一条线比它需要的要高。

回答by Jacqui

This method worked for me:

这种方法对我有用:

This trick is called "Ben Justification" *

这个技巧叫做“Ben Justification” *

Well, it's not quite all with css, you need to add a little extra to the end of the line. The markup for the heading above is...

好吧,CSS 并不是全部,您需要在行尾添加一些额外的内容。上面标题的标记是...

<h1 id="banner">
    How to justify a single line of text with css<span> &nbsp;</span>
</h1>

The little addition at the end of the line triggers the justification of the line by starting a new line. The additional content (<span> &nbsp;</span>) is forced onto a new line because the space is made 1000px wide (with word-spacing) and &nbsp;is treated like a word. The additional line does not display because the fontsize is set to 0px.

行尾的少量添加通过开始新行来触发行的对齐。附加内容 ( <span> &nbsp;</span>) 被强制换行,因为空格宽度为 1000 像素(带有字间距)并且&nbsp;被视为一个单词。附加行不会显示,因为字体大小设置为 0px。

Update, 23-Jan-11: I've just updated the markup to include a space after the   within the span and setting the font size to 1px for the span: this is needed for IE8. Thanks to Mamoun Gadir for pointing out IE's problems.

11 年 1 月 23 日更新:我刚刚更新了标记,在 span 之后包含一个空格并将字体大小设置为 span 的 1px:这是 IE8 所需要的。感谢 Mamoun Gadir 指出 IE 的问题。

The css for the heading above is...

上面标题的css是...

h1#banner {
border: 1px solid #666;
display: block;
width: 100%;
height: 1em;
font-size: 1em;
line-height: 1em;
margin: 20px auto;
padding: 0px ;
text-align: justify ;
}

h1#banner span {
font-size: 1px ;
word-spacing: 1000px;
}

*Unless evidence demonstrates that this technique has been published before, I hereby name this technique "Ben Justification" and declare it free for all to use.

*除非有证据表明此技术之前已发布过,否则我在此将这种技术命名为“Ben Justification”并声明它可供所有人免费使用。

Ben Clay, Jan 2010.

本·克莱,2010 年 1 月。

Source: http://www.evolutioncomputing.co.uk/technical-1001.html

来源:http: //www.evolutioncomputing.co.uk/technical-1001.html

回答by kander

CSS3 offers a solution for this in the form of text-align-last, see http://www.w3.org/TR/2010/WD-css3-text-20101005/#text-align-last

CSS3 以 的形式为此提供了解决方案text-align-last,请参阅http://www.w3.org/TR/2010/WD-css3-text-20101005/#text-align-last

回答by Donovan

Let's say I'm working with divs that "for some reason" (there is one pretty good reason) can only have one line....and I want to justify them.

假设我正在使用“出于某种原因”(有一个很好的理由)只能有一行的 div ......我想证明它们是合理的。

Why don't you use text-align: justify;? It does justify every line. Even if there is just one line.

你为什么不使用text-align: justify;它确实证明了每一行。即使只有一行。

Edit: I think you are looking for this, as scragz said. Anyway, this works only in IE 5.5 and IE 6.

编辑:我认为你正在寻找这个,正如 scragz 所说。无论如何,这仅适用于 IE 5.5 和 IE 6。

回答by scragz

There is a CSS3IE 5.5/6 (thanks, CSS3.com, NOT!) property called text-justifythat can do what you want with:

有一个CSS3IE 5.5/6(谢谢,CSS3.com,不是!)属性被调用text-justify,它可以做你想做的事情:

text-justify: distribute-all-lines;

Not sure about browser support.What a ripoff.

不确定浏览器支持。这(商品质量)真是骗人。

回答by Destralak

I had an issue where some of the above answers worked, but added a visible new line at the bottom - which was unacceptable, because the box had a background color. I fixed the justification with the code below:

我遇到了一个问题,上面的一些答案有效,但在底部添加了一个可见的新行 - 这是不可接受的,因为该框有背景颜色。我用下面的代码修正了理由:

jQuery:

jQuery:

$(".justify").each(function(index, element) {
    var original_height = $(this).height();
    $(this).append("<span style='display: inline-block;visibility: hidden;width:100%;'></span>");
    $(this).height(original_height);
});

HTML:

HTML:

<div class="justify" style="width:100px; background-color:#CCC">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>

回答by robert4

When the !DOCTYPE is HTML5, Kristin's and Jacqui's solutions cause an extra newline, which is hard to remove. If it bothers you (like me), here's yet another idea:

当 !DOCTYPE 是 HTML5 时,Kristin 和 Jacqui 的解决方案会导致额外的换行符,这很难删除。如果它困扰你(像我一样),这是另一个想法:

<div style="display: flex;">
  Lorem <span style="flex:1;"></span>
  ipsum <span style="flex:1;"></span>
  dolor <span style="flex:1;"></span>
  sit...
</div>

It has another drawbacks: works for single-line text only, and requires modification of the content as shown above, which isn't always feasible/practical. But there are some situations when this is not problem (like in my case). It can even be useful to have control over the positions where the extra space will be inserted. Styles are inline for brevity only, of course. I tested it with recent FFox/IE only.

它还有另一个缺点:仅适用于单行文本,并且需要修改如上所示的内容,这并不总是可行/实用。但是在某些情况下这不是问题(例如在我的情况下)。控制插入额外空间的位置甚至很有用。当然,样式是内联的,只是为了简洁。我仅使用最近的 FFox/IE 对其进行了测试。

回答by abvgd

If the closing div tag is followed by a line break or if the div tag makes use of an equivalent bottom padding/margin, then you can simply replace this with a concluding invisible line of non-breaking spaces such as this:

如果结束 div 标签后跟换行符,或者 div 标签使用等效的底部填充/边距,那么您可以简单地将其替换为结束的不可见的不间断空格行,例如:

<div>This last line is now for all intents and purposes justified &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>

While this may not be the prettiest solution, it's probably the safest cross-browser solution as it doesn't rely on any kind of non-standard tweak. Just make sure that there's enough "nbsp" characters to always cause a line break while keeping within a safe margin from the maximum allowed on a single line.

虽然这可能不是最漂亮的解决方案,但它可能是最安全的跨浏览器解决方案,因为它不依赖于任何类型的非标准调整。只需确保有足够的“nbsp”字符以始终导致换行,同时与单行允许的最大值保持在安全边距内。

As for why one would want this, well, I can give my own rationale to whom it may be of interest; there are situations where you want to divide up a continuous text block or text paragraph in order to insert images, tables or as in my case custom footnotes right before a page break. If you do that, then you want the last line right before the break to be justified as the text will resume right after the arbitrary break. In my case, the footnotes are of variable length so I have to enter them manually and therefore I also need to manually divide the text block and manually force alignment of the last line before the break. I'm not aware of any automatized solution that's also cross-browser compatible, but adding a bunch of non-breaking spaces does the job for me at least...

至于为什么有人会想要这个,好吧,我可以给出自己可能感兴趣的理由;在某些情况下,您想分割一个连续的文本块或文本段落,以便在分页符之前插入图像、表格或在我的情况下自定义脚注。如果这样做,那么您希望在中断之前的最后一行是合理的,因为文本将在任意中断之后立即恢复。就我而言,脚注的长度可变,因此我必须手动输入它们,因此我还需要手动分割文本块并在中断前手动强制对齐最后一行。我不知道有任何自动化的解决方案也是跨浏览器兼容的,但是添加一堆不间断的空间至少对我来说是这样的......