search

Home  >  Q&A  >  body text

How to center an image added to a menu using HTML CSS?

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粉980815259P粉980815259240 days ago362

reply all(2)I'll reply

  • P粉215292716

    P粉2152927162024-04-02 12:56:13

    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.

    reply
    0
  • P粉198814372

    P粉1988143722024-04-02 12:15:07

    You must add display:flex; like this:

    .navbar ul{display:flex;}

    reply
    0
  • Cancelreply