Search code examples
htmlcssdropdown

How can I space three dots uniformly as a skinny hamburger menu icon?


I want the code to show up the 3 bars, and the 3 dots to eventually be dropdown options. For some reason the 1st out of the 3 dots does not want to be spaced correctly.

#dropdown {
  background: #3f51b5;
  padding: 30px;
  margin: 0px;
}

#dot {
  width: 5px;
  height: 5px;
  background: white;
  border-radius: 50%;
  float: right;
}

#bar {
  width: 25px;
  height: 3px;
  background: white;
  margin: 5px;
}
<div id="dropdown">
  <div id="dot"></div>
  <div id="bar"></div>
  <div id="dot"></div>
  <div id="bar"></div>
  <div id="dot"></div>
  <div id="bar"></div>
</div>

Picture of what is returned:

image


Solution

  • This is a hard thing to do with floats. A possible solution could be to wrap the dots and the bars within a div. Afterwards you can position those wrapping divs in the style you like. I used flexbox for this in the following snippet.

    #dropdown {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: #3f51b5;
      padding: 30px;
      margin: 0px;
    }
    
    .dot {
      width: 5px;
      height: 5px;
      margin: 5px;
      background: white;
      border-radius: 50%;
    }
    
    .bar {
      width: 25px;
      height: 3px;
      background: white;
      margin: 5px;
    }
    <div id="dropdown">
      <div>
        <div class="bar"></div>
        <div class="bar"></div>
        <div class="bar"></div>
      </div>
      <div>
        <div class="dot"></div>
        <div class="dot"></div>
        <div class="dot"></div>
      </div>
    </div>

    p.s.: you should use the keyword class instead of id for repeating elements. This might still work, but is considered bad practice and might break javascript implementations using that id.