如何使文字居中对齐?
当前,justify不在最后一行的中心。
Answers:
您可以使用该text-align-last
物业
.center-justified {
text-align: justify;
text-align-last: center;
}
这是一个兼容性表:https : //developer.mozilla.org/en-US/docs/Web/CSS/text-align-last#Browser_compatibility。
在除Safari(Mac和iOS)之外的所有浏览器(包括Internet Explorer )中均可使用。
同样在Internet Explorer中,仅适用于text-align: justify
(无其他值text-align
),start
并且end
不受支持。
text-align: justify
左行最后对齐不是默认行为吗?这不是OP为何要问如何居中的原因吗?:S
对于希望获得既集中又合理的文本的人们,下面的方法应该起作用:
<div class="center-justified">...lots and lots of text...</div>
使用以下CSS规则(width
根据需要调整属性):
.center-justified {
text-align: justify;
margin: 0 auto;
width: 30em;
}
这是现场演示。
text-align: justify;
确保文本填满div
其中所包含的全部宽度。margin: 0 auto;
实际上是四个规则的简写:
margin-top
和margin-bottom
规则。因此整个意思是margin-top: 0; margin-bottom: 0
,即没有高于或低于的边距div
。margin-left
和margin-right
规则。因此,此规则导致margin-left: auto; margin-right: auto
。这是一个聪明的地方:它告诉浏览器获取两侧可用的任何空间,然后在左右两侧均匀分配。结果是居中文本。width: 30em;
,限制了的宽度div
。仅当宽度受到限制时,才留有一些空白margin: auto
可分配。没有此规则,div
它将占据所有可用的水平空间,并且您将失去居中效果。它与此代码一起工作
text-align: justify; text-align-last: center;
<div class="centered">
<p style="text-align: justify;">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam id dolor id nibh ultricies vehicula ut id elit. Etiam porta sem malesuada magna mollis euismod. Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, vel scelerisque</p>nisl consectetur et.</div>
通过将内容包装在div标记中并应用属性text-align:center,可以实现结果。在div标签之后,我立即将内容包装在一个段落标签中,并应用了text-align:justify属性。为了使最后一行居中,我从段落标签中排除了它,然后将其回退到div标签中应用的属性。您只需要确定最后一行上要多少个单词的策略即可。我包括了小提琴的演示。希望这可以帮助。
计算文本行的长度,并创建一个与文本行大小相同的块。将块居中。如果您有两条线,则如果长度不同,则需要两个块。如果您不希望块中有多余的空间,则可以使用span标签和br标签。您也可以使用pre标记在块内格式化。
您可以执行以下操作:style ='text-align:center;'
有关垂直的信息,请参见:http : //www.w3schools.com/cssref/pr_pos_vertical-align.asp
这是块和网页布局的最佳方法,请转到此处学习flex自2009年开始的新标准。 。http://www.w3.org/TR/2014/WD-css-flexbox-1-20140325/#证明内容属性
w3schools也有很多flex示例。
解决方案(不是最好的,但在某些情况下仍然可以使用) 固定宽度的非动态文本的文本“拉伸”到倒数第二行末尾的空间很小的情况,请使用full。在段落的末尾添加一些符号(以其长度作为实验)并将其隐藏;适用于段落的绝对位置,或仅使用填充/标记来校正自由空间。
文本居中对齐的良好兼容性/跨浏览器方式。
示例(前面的段落):
.paragraph {
width:455px;
text-align:justify;
}
.center{
display:block;
text-align:center;
margin-top:-17px;
}
<div class="paragraph">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam id dolor id nibh ultricies vehicula ut id elit. Etiam porta sem malesuada magna mollis euismod. Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna,<br><center>vel scelerisque nisl consectetur et.</center></div>
修复后:
.paragraph {
width:455px;
text-align:justify;
position:relative;
}
.center{
display:block;
text-align:center;
margin-top:-17px;
}
.paragraph b{
opacity:0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: alpha(opacity=0);
-moz-opacity: 0;
-khtml-opacity: 0;
}
<div class="paragraph">Cras justo odio, dapibus ac facilisis in, egestas eget quam. Nullam id dolor id nibh ultricies vehicula ut id elit. Etiam porta sem malesuada magna mollis euismod. Nullam id dolor id nibh ultricies vehicula ut id elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, <b>__</b><br><div class="center">vel scelerisque nisl consectetur et.</div></div>
您还可以通过HTML + JS将元素分成两个部分。
HTML:
<div class='justificator'>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a
type specimen book.
</div>
JS:
function justify() {
// Query for elements search
let arr = document.querySelectorAll('.justificator');
for (let current of arr) {
let oldHeight = current.offsetHeight;
// Stores cut part
let buffer = '';
if (current.innerText.lastIndexOf(' ') >= 0) {
while (current.offsetHeight == oldHeight) {
let lastIndex = current.innerText.lastIndexOf(' ');
buffer = current.innerText.substring(lastIndex) + buffer;
current.innerText = current.innerText.substring(0, lastIndex);
}
let sibling = current.cloneNode(true);
sibling.innerText = buffer;
sibling.classList.remove('justificator');
// Center
sibling.style['text-align'] = 'center';
current.style['text-align'] = 'justify';
// For devices that do support text-align-last
current.style['text-align-last'] = 'justify';
// Insert new element after current
current.parentNode.insertBefore(sibling, current.nextSibling);
}
}
}
document.addEventListener("DOMContentLoaded", justify);
这是带有div和p标签的示例
function justify() {
// Query for elements search
let arr = document.querySelectorAll('.justificator');
for (let current of arr) {
let oldHeight = current.offsetHeight;
// Stores cut part
let buffer = '';
if (current.innerText.lastIndexOf(' ') >= 0) {
while (current.offsetHeight == oldHeight) {
let lastIndex = current.innerText.lastIndexOf(' ');
buffer = current.innerText.substring(lastIndex) + buffer;
current.innerText = current.innerText.substring(0, lastIndex);
}
let sibling = current.cloneNode(true);
sibling.innerText = buffer;
sibling.classList.remove('justificator');
// Center
sibling.style['text-align'] = 'center';
// For devices that do support text-align-last
current.style['text-align-last'] = 'justify';
current.style['text-align'] = 'justify';
// Insert new element after current
current.parentNode.insertBefore(sibling, current.nextSibling);
}
}
}
justify();
p.justificator {
margin-bottom: 0px;
}
p.justificator + p {
margin-top: 0px;
}
<div class='justificator'>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<p class='justificator'>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p><p>Some other text</p>