I was trying to create a horizontal navbar using lists inside a container div, I came across confusing things.
When I try to place elements next to each other, if I dont use float at all, there is a gap between list items, but margin and padding wise it is fine.
body {
margin: 0px;
padding: 0px;
}
div {
text-align: center;
background-color: gray;
margin: 0px;
padding: 0px;
}
ul {
list-style-type: none;
margin: 0px;
padding: 0px;
display: inline-block;
}
ul li {
display: inline-block;
color: white;
background-color: blue;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div class="container">
<ul>
<li>Home</li>
<li>Youtube</li>
<li>Contact</li>
<li>Support</li>
</ul>
When I use float: left;
there is an unwanted padding in div or margin on list.
body {
margin: 0px;
padding: 0px;
}
div {
text-align: center;
background-color: gray;
margin: 0px;
padding: 0px;
}
ul {
list-style-type: none;
margin: 0px;
padding: 0px;
display: inline-block;
}
ul li {
display: inline-block;
float: left;
color: white;
background-color: blue;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div class="container">
<ul>
<li>Home</li>
<li>Youtube</li>
<li>Contact</li>
<li>Support</li>
</ul>
To solve that, I used font-size: 0px
and font-size: 16px
for <ul>
.
That solved my problem but I don't know if it's right thing to do and more importantly what causes the problem there.
body {
margin: 0px;
padding: 0px;
}
div {
font-size: 0px;
text-align: center;
background-color: gray;
margin: 0px;
padding: 0px;
}
ul {
font-size: 16px;
list-style-type: none;
margin: 0px;
padding: 0px;
display: inline-block;
}
ul li {
display: inline-block;
float: left;
color: white;
background-color: blue;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div class="container">
<ul>
<li>Home</li>
<li>Youtube</li>
<li>Contact</li>
<li>Support</li>
</ul>
Is it bad to use list items as navigation bars or I am using it wrong? I am curious what causes that gap at the bottom of the list.