分组在另一个元素中时如何对齐图像而没有间距

比利

我正在尝试将这些图像并排对齐。不幸的是,图像之间存在可见的间隙。

在单击无线电图像时,如何在不更改功能的情况下摆脱它?

我尝试过将图像向左浮动,但是不幸的是,这不起作用

HTML:

<div class="buttons">
<input type="radio" name="a" value="1.0" id="b" />
<label for="b"><img id="img1" src="mypicone.png"></label>

<input type="radio" name="a" value="2.0" id="c" />
<label for="c"><img id="img3" src="mypictwo.png"></label>

<input type="radio" name="a" value="3.0" id="d" />
<label for="d"><img id="img5" src="mypicthree.png"></label>
</div>

CSS:

input[type="radio"] {
 display: none;
}
input[type="radio"]:checked + label {
 position: relative
}
input[type="radio"]:checked + label::before {
content: url("//lorempixel.com/10/10");
position: absolute;
left: 15px
}

小提琴:

https://jsfiddle.net/emeka/qbukpfn5/1/

labels是内联元素。这意味着它们将从页面的角度显示,就像显示“字母”一样。

在中HTML,如果您在每行上放一个字母,并且父行正常换行,浏览器会将新行呈现为空格。它们是“智能”空间,例如,将仅呈现两个或多个后续此类空间之一。

基本上,这就是标签之间的新行所发生的情况。它们被渲染为空间。要解决此问题,您可以使用三个选项:

1.删​​除元素之间的所有空格/换行符

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>

另外,您可以只注释那些空格/换行符:

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.将标签浮动到左侧

这不会删除空格,但是会将您粘贴在左侧的标签分组。在标签之后,空格仍会呈现:

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>

更新 fiddle

3.另外,您也可以只给.buttons一个font-size0

使那些讨厌的空间具有0×0 px。但是,如果您在中渲染了文本.buttons,则应给其直接子级.buttons使用“正常”的字体大小:

.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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在JPanel的右侧对齐一个元素,在左侧对齐另一个元素?

如何在没有另一个数组列表中的一个元素的情况下创建数组列表

如何在Java 8中按另一个元素将List的元素分组

左对齐div中的一个元素并将另一个元素居中

numpy:从另一个矩阵的所有元素中减去矩阵而没有循环

如何在导轨中没有另一个的情况下验证元素的存在

当元素中还有另一个类时,SCSS扩展类

当一个元素悬停并影响CSS中的另一个元素/类时,如何添加悬停效果?

绑定元素时如何从netbeans创建的另一个类访问私有列表

如何在没有id的另一个元素中查找元素

单击另一个元素时如何关闭另一个元素

在另一个AppDomain中调用Await时没有SynchronizationContext

如何将带有hr线的图像与另一个图像html CSS对齐?

在没有父元素的另一个元素之后添加一个元素

为什么我的图像没有显示在另一个 div 内的 div 中?

在另一个元素上对齐元素

在另一个图像周围对齐图像?

当另一个元素纯粹悬停在 css 中时,如何更改一个元素的样式属性?

从另一个类访问时,Populated ObservableCollection 中没有项目

如何使用语法“flex:x”将同一行中的一个元素在左侧对齐,另一个在中间对齐

Javascript将一个元素添加到另一个没有id的元素中

当另一个元素拥有一个元素时,如何给它自动对焦?

Selenium 通过 Id 获取元素,该元素在没有 Xpath 的另一个元素中

单击时如何获取同一div中另一个元素的值?

如何更改位于另一个元素的innerhtml中的图像的来源?

在进行mysql join时如何检查另一个表没有数据?

如何将向量中具有间隔的某些值设置为另一个向量

将鼠标悬停在另一个元素上时如何使元素显示,如果没有则隐藏它。并让它一直占用空间

如何将一个元素对齐在另一个元素的正下方但与侧面有一些间距?