0

I have a form:

<div class="ttSliderFrmCnt">
    <form ref="form" class="ttSliderForm">
        <input max="480" min="30" name="slider" type="range" value={this.props.totalSeconds}/>
     </form>
</div>

I tried adding some custom styles, following this Css-tricks tutorial:

// Hide the default slider
input[type=range] {
  -webkit-appearance: none; /* Hides the slider so that custom slider can be made */
  width: 100%; /* Specific width is required for Firefox. */
  background: transparent; /* Otherwise white in Chrome */
}

input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
}

input[type=range]:focus {
  outline: none; /* Removes the blue border. You should probably do some kind of focus styling for accessibility reasons though. */
}

input[type=range]::-ms-track {
  width: 100%;
  cursor: pointer;

  /* Hides the slider so custom styles can be added */
  background: transparent;
  border-color: transparent;
  color: transparent;
}

// Style the thumb
/* Special styling for WebKit/Blink */
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  border: 1px solid $darkDivider;
  height: $bodyTextSize;
  width: $bodyTextSize;
  border-radius: 50%;
  background: $accentColor;
  margin-top: -5px; /* You need to specify a margin in Chrome, but in Firefox and IE it is automatic */
}

/* All the same stuff for Firefox */
input[type=range]::-moz-range-thumb {
  border: 1px solid $darkDivider;
  height: $titleTextSize;
  width: $titleTextSize;
  border-radius: 50%;
  background: $accentColor;
}

/* All the same stuff for IE */
input[type=range]::-ms-thumb {
  border: 1px solid $darkDivider;
  height: $titleTextSize;
  width: $titleTextSize;
  border-radius: 50%;
  background: $accentColor;
}

input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 4px;
  background: $accentColor;
  border-radius: 2px;
  border: 0.2px solid $darkDivider;
}

input[type=range]:focus::-webkit-slider-runnable-track {
  background: $accentColor;
}

input[type=range]::-moz-range-track {
  width: 100%;
  height: 4px;
  background: $accentColor;
  border-radius: 2px;
  border: 0.2px solid $darkDivider;
}

input[type=range]::-ms-track {
  width: 100%;
  height: 4px;
  background: transparent;
  border-color: transparent;
  border-width: 2px 0;
  color: transparent;
}
input[type=range]::-ms-fill-lower {
  background: $accentColor;
  border: 0.2px solid $darkDivider;
  border-radius: 2px;
}
input[type=range]:focus::-ms-fill-lower {
  background: $accentColor;
}
input[type=range]::-ms-fill-upper {
  background: $accentColor;
  border: 0.2px solid $darkDivider;
  border-radius: 2px;
}
input[type=range]:focus::-ms-fill-upper {
  background: $darkSecondaryText;
}

The variables used are either sizes in px, or color definitions and should not matter.

Now a curious thing happens, when I inspect this in chrome I get a margin I did not define on the input itself:

input[type="range" i] {
    -webkit-appearance: slider-horizontal;
    color: rgb(144, 144, 144);
    padding: initial;
    border: initial;
    margin: 2px;
}

Where does this come from and how can I override it?

Miha Šušteršič
  • 9,742
  • 25
  • 92
  • 163
  • Did you miss the title "user agent stylesheet" right beside it in the debugger which is the default styles? http://stackoverflow.com/questions/12582624/what-is-user-agent-stylesheet – epascarello Jan 06 '17 at 17:01
  • I did see it, but was confused by the selector - as far as I know it is not valid css? – Miha Šušteršič Jan 06 '17 at 17:07

1 Answers1

1

This is the user agent stylesheet of your browser. If you want to override the rule, simply define the rule in your CSS.

input[type="range"] {
    margin: 0px;
}
<div class="ttSliderFrmCnt">
    <form ref="form" class="ttSliderForm">
        <input max="480" min="30" name="slider" type="range" value={this.props.totalSeconds}/>
     </form>
</div>
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87