我想显示一小段可能会换行两三行的文本内容。它位于一个高度可视的元素内,为了样式的目的,我希望这些行的长度尽可能相等,且更倾向于用一行而不是两行。
与其如下:
这是一个将换成两行的文本示例
。
我想要这个:
这是一个将换成两行的文本
示例。
但我不想限制文本容器的宽度:
这段文本足够长,只能在两行中刚好容纳。
它不应该换行为三行或更多行,因此不需要限制宽度。
是否有一种纯CSS或可接受的JavaScript方法可以实现?
我想显示一小段可能会换行两三行的文本内容。它位于一个高度可视的元素内,为了样式的目的,我希望这些行的长度尽可能相等,且更倾向于用一行而不是两行。
与其如下:
这是一个将换成两行的文本示例
。
我想要这个:
这是一个将换成两行的文本
示例。
但我不想限制文本容器的宽度:
这段文本足够长,只能在两行中刚好容纳。
它不应该换行为三行或更多行,因此不需要限制宽度。
是否有一种纯CSS或可接受的JavaScript方法可以实现?
CSS文本模块4属性:
text-wrap: balance;
我会做这个。 https://drafts.csswg.org/css-text-4/#text-wrap
此外,有一个包可以为现有浏览器实现 polyfill。
如果我理解你的问题(如果我理解的话,“使用对齐”这一主题并不完全适用),我认为你无法避免编写相当数量的 JavaScript。
我能想到的唯一方法是比较文本的宽度和容器的宽度,并进行相应的调整。
公式:单行文本宽度 = 文本宽度 / 四舍五入(文本宽度/容器宽度)
即,(可能需要一些调整以防止它截断单词)
var widthOfText = functionToGetWidthOfText(width); //==120
var widthOfContainer = container.width(); //==100
var containerWidth = widthOfText / ceil(widthOfText/widthOfContainer);
// containerWidth = 120 / ceil(120/100) == 120 / 2 == 60
var formattingTextContainer.width = containerWidth
// text would go inside the formattingTextContainer, which would
// give you 2 lines of equal, 60px width
我创建了一个例子,其中包括使用text-align: justify和不使用text-align: justify。
请看:
.center {width:200px; float:left; margin-left: 20px;}
.justify { text-align: justify; }
<!-- 4. -->
<section class="center">
<p class="justify">orem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</section>
<section class="center">
<p class="">orem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</section>
由于似乎没有现成的解决方案可用,我写了一个相对简单的jQuery函数来实现我想要的功能。以下是代码,以防其他人也在寻找。
它的工作原理是克隆我们想要调整的元素。为了防止将其注入到DOM时闪烁,克隆被隐藏起来。然后,我们逐像素使克隆变窄,直到必须换行为止。然后将在此之前的宽度设置为原始元素的宽度,并删除克隆元素。
仍有更好的方法可以实现此目标,欢迎提出改进建议。Codepen链接在此处:http://codepen.io/Kaivosukeltaja/pen/jWYqZN
function adjust() {
$('.quote').each(function() {
// Create an invisible clone of the element
var clone = $(this).clone().css({
visibility: 'hidden',
width: 'auto'
}).appendTo($(this).parent());
// Get the bigger width of the two elements to be our starting point
var goodWidth = Math.max($(this).width(), $(clone).width());
var testedWidth = goodWidth;
var initialHeight = $(clone).height();
// Make the clone narrower until it wraps again, causing height to increase
while($(clone).height() == initialHeight && testedWidth > 0) {
goodWidth = testedWidth;
testedWidth--;
$(clone).width(testedWidth);
}
// Set original element's width to last one before wrap
$(this).width(goodWidth);
// Remove the clone element
$(clone).remove();
});
}
$(window).resize(adjust);
$(document).ready(function() {
adjust();
});
body {
background-color: #f0f0f0;
}
.holder {
text-align: center;
margin: 2em auto;
}
.quote {
display: inline-block;
border: 1px solid #c0c0c0;
background-color: #fff;
padding: 1em;
font-size: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="holder">
<div class="quote">
If I want text to wrap, I want the lines as close to equal length as possible.
</div>
</div>
很抱歉,CSS中没有这样做的方法,所以我刚刚搜索了一下,在这里找到了在JavaScript中将大字符串拆分为n个块和计算字符串中的单词数。
我将把这视为一个挑战,并编写一个函数来解决它 :)
尝试使用正则表达式查找单词数并找到其一半长度,然后执行以下操作:
function chunkString(str, length) {
length = length/2;
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}
.....
text-align: justify
。
我不确定我是否理解了问题,但是看到您的声望,我害怕回答这个问题!:)
将文本放入 p 或 span 中,确保其为 display: block
,inline-block
也可以胜任。每个元素的宽度应为父元素的 50%。然后对子元素进行 text-align: justify
。
#center{
width : 100px;
text-align : justify;
}
<div id="center">This is an example of a piece of text that will wrap to two lines.This is an example of a piece of text that will wrap to two
lines.</div>
只需缩短包含此文本的元素的宽度:
div {
width: 210px;
}
<div>This is an example of a piece of text that will wrap to two lines.</div>
max-width
吗?你实际上尝试了什么?为什么不想限制文本容器? - Pogrindisspan
元素,计算它们的宽度,并在屏幕重新调整大小时手动添加换行符。我想先询问一下是否有更简单的解决方案。 @Bit:不需要对长单词进行特殊处理。只需注意字体是变宽的,因此我们无法仅通过文本就知道它们在屏幕上的宽度。 - Kaivosukeltaja