简体   繁体   English

如何将DIV中的超链接居中?

[英]How to center hyperlinks within a DIV?

I have a set of hyperlinks (using the a tag) that are placed within a DIV and I would like them to center within that DIV. 我有一组放在DIV中的超链接(使用a标签),我希望它们在DIV中居中。 I have tried putting text-align (Italicized) on this DIV and have also tried margin 0 auto (Italicized) for the hyperlinks but they still stick firmly to the left. 我已经尝试将text-align(斜体)放在这个DIV上,并且还尝试使用margin 0 auto(斜体)作为超链接,但它们仍然牢牢地贴在左边。

Below is the HTML and the CSS code. 下面是HTML和CSS代码。

Thanks for the help! 谢谢您的帮助!

HTML HTML

<div id="navigation">
        <a class="nav" href="index.html">Home</a>
        <a class="nav" href="Page 1.html">Page 1</a>
        <a class="nav" href="Page 2.html">Page 2</a>
        <a class="nav" href="Page 3.html">Page 3</a>
        <a class="nav" href="Page 4.html">Page 4</a>
</div>

CSS CSS

#navigation {
width: 100%;
height: 40px;
border: #000 solid 1px;
    *text-align: center;*
}

.nav {
text-align: center;
text-decoration: none;
display: block;
float: left;
width: 80px;
height: 40px;
background-color: #0F0; 
    *margin: 0 auto;*
}

Remove float:left from .nav class and add display:inline-block .nav类中删除float:left并添加display:inline-block

.nav {
text-align: center;
text-decoration: none;
display: inline-block;
width: 80px;
height: 40px;
background-color: #0F0; 
}

Js Fiddle Demo Js Fiddle演示

Remove the float. 移除浮子。

Floated elements can not be centered using margin: 0 auto; 使用margin: 0 auto;不能使浮动元素居中margin: 0 auto; . Adding float to a element makes it a block element. 将float添加到元素使其成为块元素。 text-align: center doesn't affect block elements. text-align: center不影响块元素。 It does affect inline-block though. 它确实会影响内联块。

删除Float:离开我们的a(.nav)并将Text-align:center添加到div中。

你可以通过很多方式实现这一目标,但我认为你应该阅读Joshua Johnson在设计小屋中的这篇文章 ,这样你就可以了解更多关于如何集中任何内容的信息

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

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