The problem is that the background is in elements inside the UL and not in the UL and these elements are floating right.
I think that because of the effect of the image you want the background to limit the area of the links, so you can do it in two ways, using display: inline-block;
with overflow: hidden;
(similar to @Bacco):
In both example I moved the background to ul
.navbar{
list-style-type:none;
padding:0px;
margin:0px;
font-weight:bold;
border-radius:5px;
display: inline-block;
background-color:#1A1A1A;
}
.navbar li{
display: inline-block;
padding:10px;
}
.navbar a{
text-decoration:none;
color:#E8142D
}
<ul class="navbar">
<li><a href="#home">Início</a></li>
<li><a href="#artigos">Artigos</a></li>
<li><a href="#contato">Contato</a></li>
<li><a href="#sobre">Sobre</a></li>
</ul>
However, elements inline or with display: inline-*
usually generate a small margin below the elements (not always, depending on the situation and / or use of the font), this can be a bit annoying to solve, so you can use float
no ul
and a pseudo-element ( ::after
or :after
for older browsers) with clear:both;
.navbar{
list-style-type:none;
padding:0px;
margin:0px;
font-weight:bold;
border-radius:5px;
float:left;
background-color:#1A1A1A;
}
.navbar::after, .navbar:after{
clear: both;
content: " ";
height: 0;
display: block;
}
.navbar li{
float:left;
padding:10px;
}
.navbar a{
text-decoration:none;
color:#E8142D
}
<ul class="navbar">
<li><a href="#home">Início</a></li>
<li><a href="#artigos">Artigos</a></li>
<li><a href="#contato">Contato</a></li>
<li><a href="#sobre">Sobre</a></li>
</ul>
<div style="background: #fc0;">oi</div>
I talked to @Bacco, I think that two solutions that can be applied to resolve the spacing in inline-block
would be font-size:0
(aside from the examples that bacco mentioned) and create a parent element, this way it will not generate the extra space and will not stick to the side of another element, for example:
.navbar {
font-size: 0;
}
.navbar ul {
font-size: 10pt; /*ou font-size: initial;*/
list-style-type:none;
padding:0px;
margin:0px;
font-weight:bold;
border-radius:5px;
display: inline-block;
background-color:#1A1A1A;
}
.navbar li{
display: inline-block;
padding:10px;
}
.navbar a{
text-decoration:none;
color:#E8142D
}
<div class="navbar">
<ul>
<li><a href="#home">Início</a></li>
<li><a href="#artigos">Artigos</a></li>
<li><a href="#contato">Contato</a></li>
<li><a href="#sobre">Sobre</a></li>
</ul>
</div>
<div style="background: #fc0;">oi</div>
And with the float it would look like this:
.navbar ul {
list-style-type:none;
padding:0px;
margin:0px;
font-weight:bold;
border-radius:5px;
float:left;
background-color:#1A1A1A;
}
.navbar::after, .navbar:after, /*aplica quebra no para o element pai*/
.navbar ul::after, .navbar ul:after { /*aplica quebra no para o UL*/
clear: both;
content: " ";
height: 0;
display: block;
}
.navbar li{
float:left;
padding:10px;
}
.navbar a{
text-decoration:none;
color:#E8142D
}
<div class="navbar">
<ul>
<li><a href="#home">Início</a></li>
<li><a href="#artigos">Artigos</a></li>
<li><a href="#contato">Contato</a></li>
<li><a href="#sobre">Sobre</a></li>
</ul>
</div>
<div style="background: #fc0;">oi</div>