简体   繁体   English

使用Bootstrap在导航栏的左侧和右侧添加填充的问题

[英]Issue with an adding padding to the left and right of the navbar using Bootstrap

Hey guys I have a problem with padding on navbar with Bootstrap because I am not able to add padding right and left to menu items like "Home" "Features" etc, but when I do padding-top and bottom it works? 大家好,我在Bootstrap导航栏上填充时遇到问题,因为我无法向菜单项(例如“ Home”,“ Features”等)左右添加填充,但是当我进行顶部和底部填充时,它可以工作吗?

anyone can check code and see why padding left and right to menu items doesn't have any effect, but if you add padding-top and bottom it works? 任何人都可以检查代码,看看为什么向菜单项左右填充没有任何作用,但是如果您添加padding-top和bottom可以起作用?

<nav class="navbar navbar-expand-lg navbar-light bg-orange">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
        <div class="navbar-nav">
            <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
            <a class="nav-item nav-link" href="#">Features</a>
            <a class="nav-item nav-link" href="#">Pricing</a>
            <a class="nav-item nav-link disabled" href="#">Disabled</a>
        </div>
    </div>
</nav>

CSS Code is Below CSS代码如下

.navbar{
    text-align: center;
    padding: 0;
    border-bottom: 4px solid black;
}
.navbar-brand{
    margin-left: 34px;
    font-size: 25px;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande',
    'Lucida Sans', Arial, sans-serif;
}
.bg-orange{
    background-color: yellow;
}
.navbar-nav{
    font-size: 25px;
    font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 
    'Lucida Sans', Arial, sans-serif;
}
.nav-item{
    transition: 0.3s;
    padding: 22px 0px 22px 0px;
}
.nav-item:hover{
    background: orange;
}

UPDATE: FIXED IT: Changed class of "nav-item nav-link" to "nav-item" 更新:已修复:将“ nav-item nav-link”的类更改为“ nav-item”

Bootstrap's css has padding-right: 0; Bootstrap的CSS具有padding-right: 0; declared on nav-link in _navbar.scss . _navbar.scss nav-link上_navbar.scss

You can either 你可以

  1. include your css file after bootstrap's on the page, or 在页面上进行引导后添加您的CSS文件,或者
  2. use !important to make your style declaration take precedence. 使用!important使样式声明优先。

Also, I think you have your css padding declaration mixed up. 另外,我认为您混合了css填充声明。 It works for me with 1 small change: 只需1个小更改,它便对我有效:

https://codepen.io/anon/pen/NebeMN https://codepen.io/anon/pen/NebeMN

Simply change padding: 22px 0 22px 0; 只需更改padding: 22px 0 22px 0; to padding: 0 22px 0 22px; padding: 0 22px 0 22px;

The order is: top, right, bottom, left. 顺序为:上,右,下,左。 See https://www.w3schools.com/css/css_padding.asp . 请参阅https://www.w3schools.com/css/css_padding.asp (FYI - This can be shortened to padding: 0 22px; ) (仅供参考-可以缩短为padding: 0 22px;

So finally you end up with padding: 0 22px !important; 所以最后您得到了padding: 0 22px !important; . However, I would recommend using option 1 above rather than using !important. 但是,我建议您使用上面的选项1,而不要使用!important。

So the answer to your question because it is using bootstrap's padding: 因此,您的问题的答案是因为它使用了引导程序的填充:

在此处输入图片说明


1. You need to rewrite it with !important tag. 1.您需要使用!important标签重写它。

padding: top, right, bottom, left !important;

The code above is just to show what values go where. 上面的代码仅用于显示值在哪里。


2. Another solution is to add new class 2.另一个解决方案是添加新类

.item{ padding-right:22px !imporant; }

and add this class to elements that you want to override and don't affect other elements with bootstrap's class. 并将该类添加到您要覆盖且不会影响带有bootstrap类的其他元素的元素。

maybe this will work? 也许这会工作?

  .navbar{ text-align: center; padding: 0; border-bottom: 4px solid black; height: 60px; } .navbar a{ padding: 10px; margin: 5px 0 0 0; float: left; } .navbar-toggler { margin: 20px 10px 0 10px; height: 25px; width: 25px; float: left; } .navbar-brand{ margin-left: 34px; font-size: 25px; font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; } .bg-orange{ background-color: yellow; } .navbar-nav{ font-size: 25px; font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; } .nav-item{ transition: 0.3s; padding: 32px 20px 22px 0; margin: 5px 0 0 40px; } .nav-item:hover{ background: orange; } 
 <nav class="navbar navbar-expand-lg navbar-light bg-orange"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> <div class="navbar-nav"> <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a> <a class="nav-item nav-link" href="#">Features</a> <a class="nav-item nav-link" href="#">Pricing</a> <a class="nav-item nav-link disabled" href="#">Disabled</a> </div> </div> </nav> 

It is the specificity of your selector. 这是您选择器的特异性

The default bootstrap styles for .nav-link are: .nav-link的默认引导样式为:

.navbar-expand-lg .navbar-nav .nav-link { ...

Your selector is just: 您的选择器只是:

.nav-link { ...

As you can see, the bootstrap one is more specific. 如您所见,引导程序之一更为具体。 You need to be more specific (without using !important!) 您需要更具体 (不使用!important!)。

Seeing as you have an id on your .navbar-collapse , you could do: 看到您在.navbar-collapse上有一个id时,可以执行以下操作:

#navbarNavAltMarkup a.nav-link { ...

That is using an ID , ID's are more specific than classes. 那就是使用ID ,ID比类更具体。

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

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