简体   繁体   English

文本对齐:证明不起作用

[英]text-align: justify not working

HTML HTML

<div id="menu">
    <a href="#">Commissions</a>
    <a href="#">Business Setup</a>
    <a href="#">Administrator</a>
    <a href="#">Content Management</a>
    <a href="#">Inventory</a>
    <a href="#">Communications Tools</a>
    <a href="#">Genealogy</a>
    <a href="#">Reports</a>
</div>

CSS CSS

#menu {
    width: 1000px;
    float: left;
    font-size: 9pt;
    text-align: justify;
}
#menu a {
    text-decoration: none;
    color: #0066cc;
    font-size: 9pt;
}
#menu a:hover {
    text-decoration: underline;
}

I want to make each links to have the whole width. 我想使每个链接具有整个宽度。 I tried to implement so with text-align: justify. 我试图用text-align:justify来实现。 But it's not working. 但它不起作用。 How can I do this? 我怎样才能做到这一点?

This will make evenly spaced divs(can be links or about any element). 这将使div均匀分布(可以是链接或大约任何元素)。 The issue you ran in to is that justify doesn't work on a single line of text (or the last line of text). 您遇到的问题是,对齐方式不适用于单行文本(或最后一行文本)。 That is why you need the :after psuedo element. 这就是为什么需要:after psuedo元素的原因。

Html: HTML:

<div class="wrapper">
  <div>This can be anything.</div>
  <div>This can be anything.</div>
  <div>This can be anything.</div>
  <div>This can be anything.</div>
  <div>This can be anything.</div>
  <div>This can be anything.</div>
</div>

Css: CSS:

.wrapper{
  text-align: justify;
}
.wrapper div{
  display: inline-block;
}
.wrapper:after{
  content: "";
  width: 100%;
  display: inline-block;
}

I'm not sure if I completely understand your question, but by the sounds of things you want justify to do something that it was not designed to do. 我不确定我是否完全理解您的问题,但是从声音的角度来看,您想证明做某事不是设计好的。

Only when a line of text wraps at the right edge of a container will the text be justified. 仅当一行文本环绕在容器的右边缘时,文本才被对齐。

This however, cannot really happen in your menu. 但是,这实际上不能在菜单中发生。

So instead of a justify fix (I say fix, although nothing is broken), I instead have another suggestion. 因此,我有一个建议,而不是一个合理的解决方案(我说是修复,尽管什么都没坏)。

From my understanding you want your links evenly spread across your div. 据我了解,您希望您的链接在整个div中平均分布。

The best way I can think of is to give the a elements a percentage based width based on the number of links and align to center instead of justify. 我能想到的最好方法是根据链接数为a元素提供基于百分比的宽度,并对齐中心而不是对齐。

For example: 例如:

#menu a {
    text-decoration: none;
    color: #0066cc;
    font-size: 9pt;
    width: 12%;
    display: inline-block;
    text-align: center;
}

I don't know if it is what you want but you can try it and see what you think. 我不知道这是不是您想要的,但是您可以尝试一下,看看您的想法。

No. All links have different length. 否。所有链接的长度都不同。 But length between these links should be the same. 但是这些链接之间的长度应该相同。

I have only a tables solution. 我只有一个表解决方案。 http://jsfiddle.net/Flack/Q7z6q/ http://jsfiddle.net/Flack/Q7z6q/

I know it's dirty and will be glad if someone comes with a better idea. 我知道这很脏,如果有人提出了更好的主意,我将很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM