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

Viewed 1262

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

3 Answers

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.

You can easily do this with one element for each:

#dropdown {
  background: #3f51b5;
  padding: 10px 50px;
  overflow:hidden;
}

#dot {
  width: 10px;
  height: 30px;
  background: 
   radial-gradient(5px 5px at center, white 57%, transparent 61%) top/10px 10px;
  float: right;
}

#bar {
  width: 25px;
  height: 22px;
  margin: 4px 0;
  background: 
    linear-gradient(#fff,#fff) top/100% 3px,
    linear-gradient(#fff,#fff) center/100% 3px,
    linear-gradient(#fff,#fff) bottom/100% 3px;
  background-repeat:no-repeat;
  float:left;
}
<div id="dropdown">
  <div id="bar"></div>
  <div id="dot"></div>
</div>

Here is another idea:

#dropdown {
  background: #3f51b5;
  padding: 10px 50px;
  overflow:hidden;
}

#dot {
  width: 5px;
  height: 5px;
  background:#fff;
  box-shadow:
    0 10px 0 #fff,
    0 20px 0 #fff;
  border-radius:50%;
  float: right;
}

#bar {
  width: 25px;
  height: 23px;
  padding:7px 0;
  margin: 4px 0;
  border-top:3px solid #fff;
  border-bottom:3px solid #fff;
  background:#fff content-box;
  float:left;
  box-sizing:border-box;
}
<div id="dropdown">
  <div id="bar"></div>
  <div id="dot"></div>
</div>

Also like this with pseudo element:

#dropdown {
  background: #3f51b5;
  padding: 10px 50px;
  overflow:hidden;
}

#dot {
  width: 5px;
  height: 5px;
  margin: 15px 0;
  background:#fff;
  border-radius:50%;
  float: right;
  position:relative;
}
#dot:before,
#dot:after{
  content:"";
  position:absolute;
  height:inherit;
  width:100%;
  left:0;
  background:inherit;
  border-radius:inherit;
  top:-8px;
}
#dot:after {
  bottom:-8px;
  top:auto;
}
#bar {
  width: 25px;
  height: 3px;
  margin: 15px 0;
  background:#fff;
  float:left;
  box-sizing:border-box;
  position:relative;
}
#bar:before,
#bar:after{
  content:"";
  position:absolute;
  height:inherit;
  width:100%;
  left:0;
  background:inherit;
  top:-8px;
}
#bar:after {
  bottom:-8px;
  top:auto;
}
<div id="dropdown">
  <div id="bar"></div>
  <div id="dot"></div>
</div>

Use something like if you want to check for first specific dot

#dropdown div:first-child { 
  position:relative;
  top:4px
}
Related