Why can't you specify a background color when creating a navigation bar?

the code is as follows:

/* CSS */
body
{
    margin: 0;
}

-sharpheader
{
    margin: 0;
    padding: 0;
    background-color: -sharpc6f0eb;
}

-sharpheader ul
{
    list-style-type: none;
    padding: 0;
    margin: 0 400px;
    background-color: -sharpc6f0eb;
}

-sharpheader ul .logo
{
    float: left;
}

-sharpheader ul .logo a
{
    display: block;
    color: -sharp2b2b2b;
    padding: 14px 16px;
    text-align: center;
    text-decoration: none;
}

-sharpheader ul li:not(.logo)
{
    float: right;
}

-sharpheader ul li:not(.logo) a
{
    display: block;
    color: -sharp2b2b2b;
    padding: 14px 16px;
    text-align: center;
    text-decoration: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="style.css">
  <title>Title</title>
</head>
<body>
<div id="header">
  <ul>
    <li class="logo"><a href="-sharp">Logo</a></li>
    <li><a href="-sharp">1</a></li>
    <li><a href="-sharp">2</a></li>
    <li><a href="-sharp">3</a></li>
    <li><a href="-sharp">4</a></li>
    <li><a href="-sharp">5</a></li>
  </ul>
</div>
</body>
</html>

the effect is as follows:

clipboard.png

I just transferred from the software, and I think I can"t understand the layout of html&css. Shouldn"t div and ul automatically define height according to li? What F12 sees is zero.

I typed according to https://www.w3schools.com/css., and its ul did not specify a height, but its background color also came out, so I guessed that it adapted according to li, but this is not the case.

and [tag 1]. [tag 5] Why is the order out of order?

what"s wrong with the code I wrote? Please give me some advice.

Apr.08,2021

Just add a overflow:hidden to the

ul element. All li in
ul have the float attribute, which deviates from the document flow, causing the ul not to hold up the quilt element by default. When the (ul) height is auto, after adding the overflow:hidden attribute, it will calculate the (li) height of the floating element in it to decide how to crop the parent element. If the parent element height is auto, it will cut to the height of the child element by default.

The

tag li is in the wrong order, because if you give the following li the same float:right, it will move the element to the right of the line in turn, and if you find that an element already exists on the right, it will be placed next to it on the left.


cannot see the style written by your li, but I guess you are using float:right;. The float will deviate from the normal document stream


.

since it is just beginning to learn from the front end, I strongly recommend that landlords learn flex and grid , instead of looking at the floating positioning of more than a decade ago, the API design in those years was so poor that it was not intended to be "laid out" at all.

introduce the immutable theme of Web: layout-- Box,Flex,Grid . After reading it, the subject will take a look at the implementation of Bootstrap (the most popular front-end library), which is basically fine.

Menu