label
s are inline elements. This means they will be displayed, from the page's point of view, just as a "letter" would.
In HTML
, if you put a letter on each line and the parent wraps normally, new lines will be rendered by the browser into spaces. They are "smart" spaces, as in, only one of two or more subsequent such spaces will be rendered.
This is, basically, what happens with the new lines between your labels. They are rendered as spaces. To fix it, you have three options:
1. remove all spaces/new-lines between your elements
body {margin: 0;}
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
position: relative
}
input[type="radio"]:checked + label::before {
content: url("http:://lorempixel.com/10/10");
position: absolute;
left: 15px
}
#img1, #img3, #img5 {
width: 100px;
height:100px;
}
<div class="buttons"><input type="radio" name="a" value="1.0" id="b" /><label for="b"><img id="img1" src="http://lorempixel.com/10/10"></label><input type="radio" name="a" value="2.0" id="c" /><label for="c"><img id="img3" src="http://lorempixel.com/10/10"></label><input type="radio" name="a" value="3.0" id="d" /><label for="d"><img id="img5" src="http://lorempixel.com/10/10"></label></div>
Alternatively, you can just comment those spaces/new-lines:
body {margin: 0;}
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
position: relative
}
input[type="radio"]:checked + label::before {
content: url("http:://lorempixel.com/10/10");
position: absolute;
left: 15px
}
#img1, #img3, #img5 {
width: 100px;
height:100px;
}
<div class="buttons"><!--
--><input type="radio" name="a" value="1.0" id="b" /><!--
--><label for="b"><img id="img1" src="http://lorempixel.com/10/10"></label><!--
--><input type="radio" name="a" value="2.0" id="c" /><!--
--><label for="c"><img id="img3" src="http://lorempixel.com/10/10"></label><!--
--><input type="radio" name="a" value="3.0" id="d" /><!--
--><label for="d"><img id="img5" src="http://lorempixel.com/10/10"></label><!--
--></div>
2. Float your labels to the left
This will not remove the spaces, but it groups your labels glued to the left. The spaces are still rendered, after the labels:
input[type="radio"] + label {
float: left;
}
body {margin: 0;}
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
position: relative
}
input[type="radio"]:checked + label::before {
content: url("http:://lorempixel.com/10/10");
position: absolute;
left: 15px
}
#img1, #img3, #img5 {
width: 100px;
height:100px;
}
input[type="radio"] + label {
float: left;
}
<div class="buttons">
<input type="radio" name="a" value="1.0" id="b" />
<label for="b"><img id="img1" src="http://lorempixel.com/10/10"></label>
<input type="radio" name="a" value="2.0" id="c" />
<label for="c"><img id="img3" src="http://lorempixel.com/10/10"></label>
<input type="radio" name="a" value="3.0" id="d" />
<label for="d"><img id="img5" src="http://lorempixel.com/10/10"></label>
</div>
Updated fiddle
3. Alternatively, you can just give .buttons
a font-size
of 0
making those pesky spaces have 0 × 0 px. However, if you have rendered text inside .buttons
, you should give the immediate children of .buttons
a "normal" font-size:
.buttons {
font-size: 0;
}
.buttons>* {
font-size: 1em;
}
body { margin: 0; }
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
position: relative
}
input[type="radio"]:checked + label::before {
content: url("http:://lorempixel.com/10/10");
position: absolute;
left: 15px
}
#img1, #img3, #img5 {
width: 100px;
height:100px;
}
.buttons {
font-size: 0;
}
.buttons>* {
font-size: 1em;
}
<div class="buttons">
<input type="radio" name="a" value="1.0" id="b" />
<label for="b"><img id="img1" src="http://lorempixel.com/10/10"></label>
<input type="radio" name="a" value="2.0" id="c" />
<label for="c"><img id="img3" src="http://lorempixel.com/10/10"></label>
<input type="radio" name="a" value="3.0" id="d" />
<label for="d"><img id="img5" src="http://lorempixel.com/10/10"></label>
</div>