文本对齐:证明不起作用

发布于 2024-10-08 00:24:32 字数 796 浏览 9 评论 0原文

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

#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;
}

我想让每个链接都具有整个宽度。我尝试用 text-align: justify 来实现。但这不起作用。我该怎么做?

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

#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. But it's not working. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箹锭⒈辈孓 2024-10-15 00:24:32

这将使 div 间隔均匀(可以是链接或任何元素)。您遇到的问题是 justify 不适用于单行文本(或最后一行文本)。这就是为什么您需要 :after psuedo 元素。

网页:

<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:

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

This will make evenly spaced divs(can be links or about any element). 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.

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:

.wrapper{
  text-align: justify;
}
.wrapper div{
  display: inline-block;
}
.wrapper:after{
  content: "";
  width: 100%;
  display: inline-block;
}
雨轻弹 2024-10-15 00:24:32

我不确定我是否完全理解你的问题,但从你想要证明做一些它不是设计要做的事情的声音来看。

仅当一行文本在容器的右边缘换行时,文本才会对齐。

然而,这不可能真正发生在您的菜单中。

因此,我没有提出合理的修复(我说修复,尽管没有任何问题),而是提出了另一个建议。

根据我的理解,您希望链接均匀分布在您的 div 中。

我能想到的最好的方法是根据链接的数量为 a 元素提供基于百分比的宽度,并对齐到中心而不是对齐。

例如:

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

我不知道这是否是你想要的,但你可以尝试一下,看看你的想法。

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.

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.

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.

请帮我爱他 2024-10-15 00:24:32

没有。所有链接都有不同的长度。但这些链接之间的长度应该相同。

我只有一个表解决方案。 http://jsfiddle.net/Flack/Q7z6q/

我知道它很脏,如果有人的话我会很高兴有一个更好的主意。

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/

I know it's dirty and will be glad if someone comes with a better idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文