user3003810
user3003810

Reputation: 961

horizontal line between circles with transparent background

I'm trying to draw a horizontal line between empty circles (no background), How can I draw a line from one circle to other to match exactly without entering the other circle or not reach it?

I did the sample using codepen

#wizard {
  background-color: #eee;
  display: inline-block;
  padding: 15px;
}
#wizard .step {
  display: inline-block;
  width: 40px;
  height: 40px;
  background-color: transparent;
  border: 1px solid #000;
  border-radius: 50%;
  text-align: center;
  padding: 2px;
  margin-right: 3em;
  margin-left: 3em;
  position: relative;
}
#wizard .step:after {
  content: "";
  display: block;
  height: 1px;
  background-color: #000;
  position: absolute;
  left: auto;
  right: -100%;
  width: 100%;
  top: 50%;
}
#wizard .step:last-child:after {
  display: none;
}
#wizard .step span {
  padding: 11px;
  display: block;
}
<div id="wizard">
  <div class="step active">
    <span>1</span>
  </div>
  <div class="step">
    <span>2</span>
  </div>
  <div class="step">
    <span>3</span>
  </div>
</div>

Upvotes: 2

Views: 350

Answers (2)

Rounin
Rounin

Reputation: 29463

If you're going to absolutely position your #wizard .step::after pseudo-element, you need to know 2 things:

  1. Where it should begin
  2. How long it should be

You can see where it should begin, given that your #wizard .step has a border of 1px, a padding of 2px and a content width of 40px.

1px + 2px + 40px + 2px + 1px = 46px

So your position styles need to include left: 46px:

#wizard .step::after {
position: absolute;
top: 50%;
left: 46px;
}

As for how long it needs to be, it needs to span the margin-right of the current #wizard .step and then the margin-left of the next #wizard .step.

Since these are both 3em, you can give it your #wizard .step::after pseudo-element a width of 6em.

Putting it all together:

#wizard {
  background-color: #eee;
  display:inline-block;
  padding:15px;
}

#wizard .step {
  display: inline-block;
  width: 40px;
  height: 40px;
  background-color: transparent;
  border: 1px solid #000;
  border-radius: 50%;
  text-align: center;
  padding: 2px;
  margin-right: 3em;
  margin-left: 3em;
  position: relative;
}

#wizard .step::after {
  content: '';
  display: block;
  position: absolute;
  top: 50%;
  left: 46px;
  width: 6em;
  height: 1px;
  background-color: #000;
}

#wizard .step:last-child::after {
  display:none;
}

#wizard .step span {
    padding: 11px;
    display: block;
}
<div id="wizard">
  <div class="step active">
    <span>1</span>
  </div>
  <div class="step">
    <span>2</span>
  </div>
  <div class="step">
    <span>3</span>
  </div>
</div>


N.B. It's really not clear to me why, but though the result from the approach above should be flawless, the actual result includes tiny gaps - giving every impression of being from a 1950s text-book.

If you want to eliminate these gaps, use the following style declarations, instead:

#wizard .step::after {
left: 45px;
width: 6.3em;
}

Upvotes: 3

fredrover
fredrover

Reputation: 3025

Since your margin-right and margin-left are both 3em, try using 6em instead of 100%

 left:auto;
 right:-6em;
 width:6em;

That leaves a little space, but you can tweak it:

right:-6.3em;
width:6.3em;

Upvotes: 4

Related Questions