0

I have set the inner div to style="margin:auto;", yet the home icon is not centered.

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
      body {
      color: white;
      background-color: #1E1B1B;
      border-color: grey;
      }
      .nav-col {
          background-color: transparent;
      margin: 10px;
      padding: 10px;
      font-size: 30px;
      border: 1px solid grey;
      height: 700px;
      width: 150px;
      }

    </style>
  </head>

  <body>
      <div class="nav-col">
    <div style="margin:auto;"><i class="fa fa-home"></i></div>
      </div>  
  </body>
</html>

This is what I am seeing:

enter image description here

ggorlen
  • 44,755
  • 7
  • 76
  • 106

2 Answers2

1

If you inspect the page, you will notice that your div is taking up the entire width. You can use text-align to align your icon instead (see snippet below).

Also, setting margin: auto will set all margins to auto (top, bottom, left, right). If you just want left/right, you can write: margin: 0px auto (source)

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <style>
      body {
      color: white;
      background-color: #1E1B1B;
      border-color: grey;
      }
      .nav-col {
          background-color: transparent;
      margin: 10px;
      padding: 10px;
      font-size: 30px;
      border: 1px solid grey;
      height: 700px;
      width: 150px;
      }

    </style>
  </head>

  <body>
      <div class="nav-col">
    <div style="text-align:center"><i class="fa fa-home"></i></div>
      </div>  
  </body>
</html>
msassen
  • 139
  • 2
  • 9
1

If you really want to use margin: auto;, then you need to set a width. width: min-content; should be enough.

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <style>
    body {
      color: white;
      background-color: #1E1B1B;
      border-color: grey;
    }
    
    .nav-col {
      background-color: transparent;
      margin: 10px;
      padding: 10px;
      font-size: 30px;
      border: 1px solid grey;
      height: 700px;
      width: 150px;
    }
  </style>
</head>

<body>
  <div class="nav-col">
    <div style="margin:auto; width: min-content;"><i class="fa fa-home"></i></div>
  </div>
</body>

</html>
Rojo
  • 2,749
  • 1
  • 13
  • 34