How to center an image added to a menu using HTML CSS?
P粉980815259
P粉980815259 2024-04-01 20:53:26
0
2
391

I'm trying to make a menu like the one pictured below:

This is what I did:

How to center the items in the menu with the picture? This is my homework and although I try my best, I can't do it. Thanks in advance for your help.

body {
    font-family: Poppins;
    background-color: #F5F6F6;
}

.container {
    width: 70%;
    margin: 0 auto;
}

.top-space {
    height: 25px;
}

.navbar ul li {
    display: inline;
}

.navbar ul li a {
    text-decoration: none;
    line-height: 50px;
}

.navbar ul li img {
    height: 50px;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins">
    <link rel="stylesheet" href="./style/reset.css">
    <link rel="stylesheet" href="./style/style.css">
</head>

<body>
    <div class="container">
        <div class="top-space"></div>
        <div class="navbar">
            <ul>
                <li>
                    <a href=""><img src="./img/logo.png"> </a>
                </li>
                <li><a href="">adssdaads</a></li>
                <li><a href="">adssdaads</a></li>
                <li><a href="">adssdaads</a></li>
                <li><a href="">adssdaads</a></li>
            </ul>
        </div>
    </div>
</body>

</html>

I would be very grateful if you could tell me how the code you wrote works.

P粉980815259
P粉980815259

reply all(2)
P粉215292716

What I would do is give display flex to a wrapper for all navigation menu items, in this case ul. Then use align-content: center to vertically center the list items.

body {
    font-family: Poppins;
    background-color: #F5F6F6;
}

.container {
    width: 70%;
    margin: 0 auto;
}

.top-space {
    height: 25px;
}
.navbar ul {
  display: flex;
  align-content: center;
}
.navbar ul li {
    display: inline;
}

.navbar ul li a {
    text-decoration: none;
    line-height: 50px;
}

.navbar ul li img {
    height: 50px;
    width: 50px;
    
}



    
    
    
    Document
    
    
    



    


Hope this helps. If you don't let me know, I'll investigate further.

P粉198814372

You must add display:flex; like this:

.navbar ul{display:flex;}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template