The problem is that the background is in elements within UL and not in UL and these elements are floating right.
I think by 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 of inline type 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 little boring to solve, so you can use float
in the 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 two solutions that can be applied to solve the spacing on inline-block
would be the font-size:0
(apart from the examples that Bacco quoted) and create a parent element, in this way it will not generate extra space and will not stick to the side of another elemnent, 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 the float 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>
I chose Allan Andrade’s answer. The other two have a lot of information and some CSS tags that I still don’t quite understand how it works like
clear
andinline-block
. Even so, in a few days I may need these answers as reference.– Ezequiel Barbosa