将 ul 中的文本与浮动 li 居中

发布于 2024-09-30 15:50:57 字数 416 浏览 0 评论 0原文

我有一个 UL,他的 LI 是正确浮动的。我想将 UL 中的文本居中对齐,但我发现如果我不指定 UL 的宽度,它会认为它是 100%,并且不会将 LI 居中对齐。
但是,我无法为 UL 指定特定宽度,因为 LI 中的数据是动态的。
另外,由于 UL 的宽度为 100%,因此将 UL 包裹在 DIV 中并为 DIV 指定 text-align:center 规则并没有帮助。
有没有 CSS 唯一的方法让 UL 居中它的 LI?
具有上述 LI 的演示页面为 此处

I have a UL who's LIs are floated right. I would like to center align the text in the UL, but I see that if I don't specify the UL's width, it thinks that it's 100%, and doesn't center align the LIs.
However, I cannot give the UL a specific width because the data in the LIs is dynamic.
Also, because the UL's width is 100%, it doesn't help to wrap the UL in a DIV and give the DIV a rule of text-align:center.
Is there a CSS only way to get the UL to center it's LIs?
A demo page with the said LI is here

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

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

发布评论

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

评论(2

娇俏 2024-10-07 15:50:57

如果您需要 lis 的动态显式宽度,您可以使用内联块。

#list { text-align:center; }
#list li { display:inline-block; }

您必须针对 IE 进行解决

If you need dynamic explicit widths for the lis you can use inline-block.

#list { text-align:center; }
#list li { display:inline-block; }

You'll have to work around for IE.

命比纸薄 2024-10-07 15:50:57

我找到了 非常简单的解决方案,在 FF 和 IE 中都适用于我:

CSS:

#footer{text-align:center;list-style:none}
#footer li{display:inline}

HTML:

<ul id=”footer”>
<li><a href=”1.asp”>Link 1</a></li>
<li><a href=”2n.asp”>Link 2</a></li>
<li><a href=”3.asp”>Link 3</a></li>
</ul>

I found a very simple solution, that works for me in both FF and in IE:

CSS:

#footer{text-align:center;list-style:none}
#footer li{display:inline}

HTML:

<ul id=”footer”>
<li><a href=”1.asp”>Link 1</a></li>
<li><a href=”2n.asp”>Link 2</a></li>
<li><a href=”3.asp”>Link 3</a></li>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文