Home > database >  I want to put the image on the left side of Home in the same line as the text
I want to put the image on the left side of Home in the same line as the text

Time:09-06

I am practicing CSS. In the image shown here -:

Image

I want to put the image in the navbar beside Home on Home's left side in the same line as Home, About, and Contact. But I don't know how to do it properly as the image is being displayed on the top and not on Home's left side. Here is my code -:

<!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>Practice 24</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
    li {
      list-style: none;
      display: inline;
      padding: 20px;
    }
    
    .container {
      background-color: black;
      color: white;
      padding: 20px;
    }
    
    .first {
      width: 100px;
      height: 100px;
      background-color: blue;
      border: 2px solid black;
      color: white;
    }
    
    .second {
      width: 100px;
      height: 100px;
      background-color: red;
      border: 2px solid black;
      color: white;
    }
    
    .boxContainer {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      flex-wrap: wrap;
      padding: 50px;
      margin: 50px;
    }
  </style>
</head>

<body>
  <header>
    <nav>
      <div >
        <img src="tank.jpg" width="100">
        <ul>
          <li>Home</li>
          <li>About</li>
          <li>Contact</li>
        </ul>
      </div>
    </nav>
  </header>
  <div >
    <div >Div1</div>
    <div >Div2</div>
  </div>
</body>

</html>

Any help would be great.

Thank You.

CodePudding user response:

You can use a flexbox on .container to make all items aligned on the same row.

Note that if you want to align ul to the right side, you can use margin-left: auto on ul (I'm using the class name .nav for ul).

.container {
   background-color: black;
   color: white;
   padding: 20px;
   display: flex; /*Create a flexbox*/
}

/*You can remove this, if you don't want to move navigation to the right side*/
.nav {
   margin-left: auto; /*Shift navigation items to the right side*/
}

<!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>Practice 24</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
    /*You can remove this, if you don't want to move navigation to the right side*/
    .nav {
      margin-left: auto; /*Shift navigation items to the right*/
    }
    
    li {
      list-style: none;
      display: inline;
      padding: 20px;
    }
    
    .container {
      background-color: black;
      color: white;
      padding: 20px;
      display: flex; /*Create a flexbox*/
    }
    
    .first {
      width: 100px;
      height: 100px;
      background-color: blue;
      border: 2px solid black;
      color: white;
    }
    
    .second {
      width: 100px;
      height: 100px;
      background-color: red;
      border: 2px solid black;
      color: white;
    }
    
    .boxContainer {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      flex-wrap: wrap;
      padding: 50px;
      margin: 50px;
    }
  </style>
</head>

<body>
  <header>
    <nav>
      <div >
        <img src="tank.jpg" width="100">
        <ul >
          <li>Home</li>
          <li>About</li>
          <li>Contact</li>
        </ul>
      </div>
    </nav>
  </header>
  <div >
    <div >Div1</div>
    <div >Div2</div>
  </div>
</body>

</html>

CodePudding user response:

Add display: flex; to the .container class

<!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>Practice 24</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
    li {
      list-style: none;
      display: inline;
      padding: 20px;
    }
    
    .container {
      background-color: black;
      color: white;
      padding: 20px;
      display: flex;
    }
    
    .first {
      width: 100px;
      height: 100px;
      background-color: blue;
      border: 2px solid black;
      color: white;
    }
    
    .second {
      width: 100px;
      height: 100px;
      background-color: red;
      border: 2px solid black;
      color: white;
    }
    
    .boxContainer {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      flex-wrap: wrap;
      padding: 50px;
      margin: 50px;
    }
  </style>
</head>

<body>
  <header>
    <nav>
      <div >
        <img src="tank.jpg" width="100">
        <ul>
          <li>Home</li>
          <li>About</li>
          <li>Contact</li>
        </ul>
      </div>
    </nav>
  </header>
  <div >
    <div >Div1</div>
    <div >Div2</div>
  </div>
</body>

</html>

  • Related