Vlad
Vlad

Reputation: 3764

background color over border color

I'm trying to make a guitar string over a fret effect with table cells and css however using z-index doesn't seem to work:

.guitar-string {
  border-right: #aaa9ad; /* silver */
  border-right-width: 3px; 
  border-right-style: solid;
  position: absolute;
  z-index: 0;
}

.guitar-string::after {
  content: "";
  display: block;
  width: 100%;
  height: var(--thickness);
  background-color: #efd5b8; /* guitar string colour */
  top: 50%;
  border-color: transparent;
  position: absolute;
  z-index: 1;
}

And it looks like this - which is wrong as the silver (fret) needs to be under the string: enter image description here

Upvotes: 0

Views: 33

Answers (1)

Amini
Amini

Reputation: 1792

Add to the width of the guitar string and add left: 0.

.container {
  display: flex;
}

.guitar-string {
  /* border-right: #aaa9ad;
  border-right-width: 3px; 
  border-right-style: solid; */
  border-right: 3px solid #aaa9ad;
  /* position: absolute */
  position: relative;
  z-index: 0;
  width: 100px;
  height: 100px;
}

.guitar-string::after {
  content: "";
  display: block;
  /* Plus the size of border-right 👇 */
  width: calc(100% + 3px);
  height: 3px;
  background-color: #efd5b8; /* guitar string colour */
  top: 50%;
  /* Also important 👇 */
  left: 0;
  border-color: transparent;
  position: absolute;
  z-index: 1;
}
<section class="container">
  <div class="guitar-string">6</div>
  <div class="guitar-string">6</div>
</section>

Upvotes: 1

Related Questions