Aschab
Aschab

Reputation: 1399

CSS Correctly positioning pseudo element

I have a input type range inside a div and I'm using the pseudo element 'before' as a circle. My intention is for it to be like the thumb at the starting position: I have the following html:

<div class="range">
    <input type="range" name="" class="progress" value="0" max="100" min="0"/>
</div>

with the following css:

.range::before{
   content: '';
   display: inline-block;
   width: 15px;
   height: 15px;
   -moz-border-radius: 15px;
   -webkit-border-radius: 15px;
   border-radius: 15px;
   background-color: #69b6d5;
}

Here is a functioning fiddle

My intention is to make the before element at the same position as the beginning of the range.

Upvotes: 1

Views: 305

Answers (2)

kukkuz
kukkuz

Reputation: 42380

  1. Added inline-block to .range::before and input and aligned them vetically using vertical-align: middle.

  2. Set width of input to width: calc(100% - 15px). This 15px is the width of the .range::before element.

  3. Bring the .range::before over the yellow dot using transform: translate(100%, 0)

See demo below:

/* RANGE */

input[type=range] {
  -webkit-appearance: none;
  margin: 10px 0;
  width: calc(100% - 15px);
  display: inline-block;
  vertical-align: middle;
  padding: 0;
  border: none;
}
input[type=range]:focus {
  outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 3px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 0px 0px 0px #000000;
  background: #FFE000;
  border-radius: 7px;
  border: 0px solid #FFE000;
}
input[type=range]::-webkit-slider-thumb {
  box-shadow: 0px 0px 0px #FFE000;
  border: 2px solid #FFE000;
  height: 15px;
  width: 15px;
  border-radius: 15px;
  background: #FFE000;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -7px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
  background: #FFE000;
}
input[type=range]::-moz-range-track {
  width: 100%;
  height: 3px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 0px 0px 0px #000000;
  background: #FFE000;
  border-radius: 7px;
  border: 0px solid #FFE000;
}
input[type=range]::-moz-range-thumb {
  box-shadow: 0px 0px 0px #FFE000;
  border: 2px solid #FFE000;
  height: 15px;
  width: 15px;
  border-radius: 15px;
  background: #FFE000;
  cursor: pointer;
}
input[type=range]::-ms-track {
  width: 100%;
  height: 3px;
  cursor: pointer;
  animate: 0.2s;
  background: transparent;
  border-color: transparent;
  color: transparent;
}
input[type=range]::-ms-fill-lower {
  background: #FFE000;
  border: 0px solid #FFE000;
  border-radius: 14px;
  box-shadow: 0px 0px 0px #000000;
}
input[type=range]::-ms-fill-upper {
  background: #FFE000;
  border: 0px solid #FFE000;
  border-radius: 14px;
  box-shadow: 0px 0px 0px #000000;
}
input[type=range]::-ms-thumb {
  box-shadow: 0px 0px 0px #FFE000;
  border: 2px solid #FFE000;
  height: 15px;
  width: 15px;
  border-radius: 15px;
  background: #FFE000;
  cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
  background: #FFE000;
}
input[type=range]:focus::-ms-fill-upper {
  background: #FFE000;
}
.range {
  position: relative;
  display: table;
  margin: 0 auto;
  width: 50vw;
}
.range::before {
  content: '';
  display: inline-block;
  vertical-align: middle;
  transform: translate(100%, 0);
  width: 15px;
  height: 15px;
  -moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  border-radius: 15px;
  background-color: #69b6d5;
}
<div class="range">
  <input type="range" name="" class="progress" value="0" max="100" min="0" />
</div>

Let me know your feedback on this. Thanks!

Upvotes: 1

NonameSL
NonameSL

Reputation: 1475

It's a bit hacky, but how about adding this to .range::before:

.range::before {
    /* ... other css */
    position: absolute;
    margin-top: 11px;
}

JSFiddle

Upvotes: 0

Related Questions