Html 强制元素中的单行文本用 CSS 填充宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21199057/
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 00:33:35  来源:igfitidea点击:

Force single line of text in element to fill width with CSS

htmlcsstext

提问by Alix P?rry

I have a divelement that holds one line of text. How can I get this text to fill the width 100%?

我有一个div包含一行文本的元素。我怎样才能让这个文本填充宽度 100%?

text-align:justifyis only working on elements with several lines of text, and has no effect on the divwith single line. Manually adjusting the letter-spacing and word-spacing is ugly and ineffective.

text-align:justify仅适用于具有多行文本的元素,对div具有单行的元素没有影响。手动调整字母间距和字间距是丑陋且无效的。

Are there any other alternatives?

还有其他选择吗?

回答by mutil

Try this:

尝试这个:

div {
  text-align: justify;
}

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

jsFiddle

js小提琴

Take a look herefor more info.

看看这里了解更多信息。