css - 将复选框左对齐

标签 css

我已经设置了复选框的样式,但出于某种原因,我无法让复选框顶部 float 在文本的左侧。我已经尝试过 floats inlines blocks 似乎没有任何效果。我能够将文本向右浮动,但它在方框和文本之间留下了很大的空隙

.regular-checkbox {
	display: none;
}

.regular-checkbox + label {
	background-color: #f0f0ee;
	border: 1px solid #dcdcda;
	box-shadow: none;
	padding: 9px;
	border-radius: 0px;
	display: inline-block;
	position: relative;
}

.regular-checkbox + label:active, .regular-checkbox:checked + label:active {
	box-shadow: none;	
}

.regular-checkbox:checked + label {
	background-color: #f0f0ee;
	border: 1px solid #dcdcda;
	box-shadow: none;
	color: #99a1a7;
}

.regular-checkbox:checked + label:after {
	content: '\2714';
	font-size: 15px;
	position: absolute;
	top: -4px;
	left: 3px;
	color: #99a1a7;
}


.tag {
	position: relative;
	top: 0px;
	display: block;
	float: left;
	font-size: 13px;
	color: #000;
}
    <div class="large-12 columns">
		<div>
			<div class="tag">Box 1</div>
			<input type="checkbox" id="checkbox-1-1" class="regular-checkbox"><label for="checkbox-1-1"></label>
		</div>
		<div>
			<div class="tag">Box 2</div>
			<input type="checkbox" id="checkbox-2-1" class="regular-checkbox"><label for="checkbox-2-1"></label>
		</div>
	  </div>

最佳答案

要么您可以更改您的 html 标记,即在 checkbox, label 之后添加 .tag 并轻松解决该问题,或者使用 css left 属性在 text 之前对齐 check-box

HTML 的变化可能是这样的,

.regular-checkbox {
  display: none;
}

.regular-checkbox + label {
  background-color: #f0f0ee;
  border: 1px solid #dcdcda;
  box-shadow: none;
  padding: 9px;
  border-radius: 0px;
  display: inline-block;
  position: relative;
}

.regular-checkbox + label:active,
.regular-checkbox:checked + label:active {
  box-shadow: none;
}

.regular-checkbox:checked + label {
  background-color: #f0f0ee;
  border: 1px solid #dcdcda;
  box-shadow: none;
  color: #99a1a7;
}

.regular-checkbox:checked + label:after {
  content: '\2714';
  font-size: 15px;
  position: absolute;
  top: -4px;
  left: 3px;
  color: #99a1a7;
}

.tag {
  position: relative;
  top: 0px;
  display: inline-block;
  font-size: 13px;
  color: #000;
  vertical-align: top;
}
<div class="large-12 columns">
  <div>
    <input type="checkbox" id="checkbox-1-1" class="regular-checkbox">
    <label for="checkbox-1-1"></label>
       <div class="tag">Box 1</div>
  </div>
  <div>
    <input type="checkbox" id="checkbox-2-1" class="regular-checkbox">
    <label for="checkbox-2-1"></label>
    <div class="tag">Box 2</div>
  </div>
</div>

这使用 CSS left 属性。

.regular-checkbox {
  display: none;
}

.regular-checkbox + label {
  background-color: #f0f0ee;
  border: 1px solid #dcdcda;
  box-shadow: none;
  padding: 9px;
  border-radius: 0px;
  display: inline-block;
  position: relative;
  left: -30px;
}

.regular-checkbox + label:active,
.regular-checkbox:checked + label:active {
  box-shadow: none;
}

.regular-checkbox:checked + label {
  background-color: #f0f0ee;
  border: 1px solid #dcdcda;
  box-shadow: none;
  color: #99a1a7;
}

.regular-checkbox:checked + label:after {
  content: '\2714';
  font-size: 15px;
  position: absolute;
  top: -4px;
  left: 3px;
  color: #99a1a7;
}

.tag {
  position: relative;
  top: 0px;
  display: block;
  float: left;
  font-size: 13px;
  color: #000;
  left: 25px;
}
<div class="large-12 columns">
  <div>
    <div class="tag">Box 1</div>
    <input type="checkbox" id="checkbox-1-1" class="regular-checkbox">
    <label for="checkbox-1-1"></label>
  </div>
  <div>
    <div class="tag">Box 2</div>
    <input type="checkbox" id="checkbox-2-1" class="regular-checkbox">
    <label for="checkbox-2-1"></label>
  </div>
</div>

关于css - 将复选框左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46024368/

相关文章:

css - 悬停时不触发过渡

javascript - 在加载 DIV 之前显示微调图像

javascript - 添加链接到按钮 Javascript

jquery - 删除和返回焦点表单元素中的背景

javascript - 动态追加 div 和类

jquery - 将 Twitter Bootstrap 从 940px fixed 更改为 960px fixed 有什么缺点吗?

javascript - 开始滚动时将 div 粘贴到页面顶部

javascript - 溢出-x :hidden; can still scroll when open at my phone

javascript - 如何使用 jquery 禁用第二列和第三列中存在的行表单元素

html - CSS 显示 :inline have no effect