Search code examples
htmlcssimageheaderalignment

Center logo in html


I have an logo in the header of the page and I want to make in centered. This is my html:

body {
  width: 90%;
  margin: 0 auto;
}
header {
  margin-top: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #b5b5b5;
}
.logo {
  width: 250px;
  height: 150px;
  text-align: center;
}
<body>
  <header>
    <div class="row">
      <div class="logo-row">
        <img src="resources/img/logo.png" alt="logo" class="logo">
      </div>
    </div>
  </header>


Solution

  • You need to add a width to the logo-row class and use margin: 0 auto.

    body {
        width: 90%;
        margin: 0 auto;
    }
    
    header {
        margin-top: 20px;
        padding-bottom: 10px;
        border-bottom: 2px solid #b5b5b5;
    }
    
    .logo-row {
        width: 250px;
        margin: 0 auto;
    }
    
    .logo {
        width: 100%;
        height: 150px;
        text-align: center;
    }
    <body>
      <header>
        <div class="row">
          <div class="logo-row">
            <img src="resources/img/logo.png" alt="logo" class="logo">
          </div>
        </div>
      </header>