简体   繁体   English

为什么我的<a>元素</a>不在我的<a>中心</a> <li> <a>导航元素?</a>

[英]Why aren't my <a> elements centered within my <li> navigation elements?

I am creating a website using a mobile-first approach. 我正在使用移动优先方法创建网站。 I am currently styling the navigation bar, which is comprised of a ul with five li elements and an a element within each li . 我目前正在设计导航栏的样式,该导航栏由一个包含五个li元素的ul和每个li内的a element组成。 For the mobile layout, I want the navigation to be perfectly centered. 对于移动版式,我希望导航能够完美居中。 The nav element and the li elements appear to be perfectly centered; nav元素和li元素似乎完美地居中; however, the a elements are not centered within each li ... They are skewed toward the right. 但是, a元素不在每个li居中。它们向右倾斜。 How can I correct this? 我该如何纠正?

Here is my HTML: 这是我的HTML:

<nav>
    <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="programs.html">Programs</a></li>
        <li><a href="about.html">About Us</a></li>
        <li><a href="why.html">Why</a></li>
        <li><a href="contact.html">Contact Us</a></li>
    </ul>
</nav>

And here is my CSS: 这是我的CSS:

nav {
    width: 15%;
    margin: 0 auto;
    padding-top: 0.5em;
}

nav ul {
     list-style-type: none;
}

nav li {
    max-width: 100%;
    margin: 1em;
    text-align: center;
    border-radius: 10px;
}

nav a {
    display: inline-block;
    width: 100%;
    margin: 0 auto;
    padding: 0.5em;
    color: inherit;
    text-decoration: none;
}

And here is an image of what the nav currently looks like in the browser (Chrome): 以下是浏览器(Chrome)中当前nav的图像:

垂直导航按钮组的图像,其中每个按钮内的文本居中放置不正确

设置li的marginpadding为0; padding为0。

Add the following inline or in an external style sheet to nav a 将以下内联或外部样式表添加到nav a

margin: 0px;
text-align: center;

Try this : 尝试这个 :

nav ul {
    display: block;
    overflow: hidden;
    padding: 0px;
    list-style-type: none;
  }

nav a {
     display: block;
     width: 100%;
     margin: 0 auto;
     color: inherit;
     text-decoration: none;
     overflow: hidden;
     }

And use max-width on the tag not simple width 并在标签上使用max-width而不是简单的宽度

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

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