【问题标题】:CSS cannot pad text within a nav bar defined as an unordered listCSS 不能在定义为无序列表的导航栏中填充文本
【发布时间】:2016-12-26 17:33:45
【问题描述】:

我有一个网站,其导航栏使用几个 div 和一个无序列表定义。

    <!--Nav Bar-->
    <div class="nav-blur"></div>
    <div class="nav">
        <img class="logo" src="img/rectangle.png">
        <ul class="links">
            <li class="link text"><a href="#">Home</a></li>
            <li class="link divider">&nbsp;|&nbsp;</li>
            <li class="link text"><a href="#">About Us</a></li>
            <li class="link divider">&nbsp;|&nbsp;</li>
            <li class="link text"><a href="#">Contact Us</a></li>
            <li class="link divider">&nbsp;|&nbsp;</li>
            <li class="link button"><a href="#">Get Started</a></li>
        </ul>
    </div>

目前,导航栏中的最后一个链接是一个按钮,与其他纯文本不同。

Nav Bar Example Image

这是我的 CSS 样式:

.nav-blur {
    width: 100%;
    height: 45px;
    position: fixed;
    top: 0px;
    -webkit-filter: blur(10px);     
    filter: blur(10px);
    background-color: rgba(0, 0, 0, 0.6);
}

.nav {
    width: 100%;
    height: 45px;
    background-color: rgba(0, 0, 0, 0.7);
    position: fixed;
    top: 0px;
}


.nav .logo {
    padding-left: 20px;
    padding-top: 1px;
    height: 95%;
    width: auto;
}

.nav .links {
    list-style-type: none;
    margin: 0;
    padding: 11px;
    padding-right: 30px;
    float: right;
}

.nav .divider {
    color: whitesmoke;
}

.nav .links .link {
    display: inline;
}

.nav .links .link a {
    color: whitesmoke;
    text-decoration: none;
    font-style: normal;
}

.nav .links .link a:hover {
    color: grey;
    text-decoration: none;
    font-style: normal;
}

.nav .links .button {
    background-color: rgba(103, 155, 193, 1);
    border-radius: 5px;
    box-shadow: 2px 2px 1px rgba(0, 0, 0, 1);
}

.nav .links .button:hover {
    background-color: rgba(103, 155, 193, 0.5);
    border-radius: 5px;
    box-shadow: 2px 2px 1px rgba(0, 0, 0, 1);
}

.nav .links .button a {
    padding-right: 7px;
    padding-left: 7px;
    padding-top: 5px;
    padding-bottom: 5px;
    text-align: center;
}

我目前的问题是,虽然我可以调整按钮内文本的左右填充,但我无法垂直调整填充。我尝试使用填充和边距添加我需要的间距,这两者都没有任何效果。

.nav .links .button a {
    padding-right: 7px;
    padding-left: 7px;
    padding-top: 5px;
    padding-bottom: 5px;
    text-align: center;
}

.nav .links .button a {
    padding-right: 7px;
    padding-left: 7px;
    margine-top: 5px;
    margine-bottom: 5px;
    text-align: center;
}

我还尝试将文本插入到一个单独的 div 中,该 div 管理 ro 彻底弄乱了我的导航栏。

我需要添加什么 CSS 才能为按钮内的文本提供一些垂直填充?

【问题讨论】:

    标签: html css padding navbar spacing


    【解决方案1】:

    inline 元素忽略垂直填充...这是默认的display 锚链接类型。

    将链接(和li)设置为display:inline-block

    .nav .links .link a {
        color: whitesmoke;
        text-decoration: none;
        font-style: normal;
        display:inline-block;
    }
    

    .nav .links {
      list-style-type: none;
      margin: 0;
      padding: 11px;
      padding-right: 30px;
      float: right;
      background: #000;
    }
    .nav .divider {
      color: whitesmoke;
    }
    .nav .links .link {
      display: inline-block;
    }
    .nav .links .link a {
      color: whitesmoke;
      text-decoration: none;
      font-style: normal;
      display: inline-block;
    }
    .nav .links .link a:hover {
      color: grey;
      text-decoration: none;
      font-style: normal;
    }
    .nav .links .button {
      background-color: rgba(103, 155, 193, 1);
      border-radius: 5px;
      box-shadow: 2px 2px 1px rgba(0, 0, 0, 1);
    }
    .nav .links .button:hover {
      background-color: rgba(103, 155, 193, 0.5);
      border-radius: 5px;
      box-shadow: 2px 2px 1px rgba(0, 0, 0, 1);
    }
    .nav .links .button a {
      padding: 7px;
      text-align: center;
    }
    <div class="nav">
      <ul class="links">
        <li class="link text"><a href="#">Home</a>
        </li>
        <li class="link divider">&nbsp;|&nbsp;</li>
        <li class="link text"><a href="#">About Us</a>
        </li>
        <li class="link divider">&nbsp;|&nbsp;</li>
        <li class="link text"><a href="#">Contact Us</a>
        </li>
        <li class="link divider">&nbsp;|&nbsp;</li>
        <li class="link button"><a href="#">Get Started</a>
        </li>
      </ul>
    </div>

    【讨论】:

    • 不幸的是,当我将链接更改为阻止而不是内联时,内容不再水平对齐导航,而是垂直对齐到窗口的右侧。
    • 修改为inline-block。 Displayblock 作用于提供li 的锚点是inline-block - jsfiddle.net/0qpau8Ld/1
    • 工作得很好!谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 2020-04-01
    • 2021-04-12
    相关资源
    最近更新 更多