html - 如何使复选框位于框中并包含文本

标签 html css

我正在尝试创建三个隐藏复选框,以便当您单击每个框时,新信息会显示在其下方。

现在我有 3 个复选框:Click1、Click2、Click3。您单击的每个单词都会在其下方打开文本(分别命名为 Content1、Content2、Content3)。

有没有办法让每个点击文本都在框中?因此,您单击的不仅仅是纯文本,而是包含文本的框。

这是我的代码笔: http://codepen.io/arielelliott/pen/XmwBaP

HTML:

<label for="box1">Click1</label>
<input type="checkbox" id="box1" name="demo">
<label for="box2">Click2</label>
<input type="checkbox" id="box2" name="demo">
<label for="box3">Click3</label>
<input type="checkbox" id="box3" name="demo">
<div class="new1"><p>Content1</p></div>
<div class="new2"><p>Content2</p></div>
<div class="new3"><p>Content3</p></div>

CSS:

/* Default State */
body{
  background-color: #f2f2f2;
}
.new1, .new2, .new3{
  display: none;
}

/* Checkbox */
input[type="checkbox"] {
  display:none;
  visibility:hidden;
}

/* Toggled State */
input[type=checkbox]#box1:checked ~ div.new1{
  display:block;
  width: 200px;
  background-color: #fff;
}
input[type=checkbox]#box2:checked ~ div.new2 {
  display:block;
  width: 200px;
  background-color: #fff;
}
input[type=checkbox]#box3:checked ~ div.new3 {
  display:block;
  width: 200px;
  background-color: #fff;
}
input[type=checkbox]#box1 ~ div.new2 {
  display: none;
}
input[type=checkbox]#box2 ~ div.new1 {
  display: none;
}
input[type=checkbox]#box3 ~ div.new1, div.new2 {
  display: none;
}

最佳答案

您可以使用不同的 CSS 属性以多种方式实现此目的:borderpaddingwidthheight ...

我刚刚将其中一些添加到您的 CSS 中,并且还添加了一个类来设置标签样式 (toggle-control),因为您应该尽可能避免使用元素选择器。

请注意,我更改了标签和复选框的顺序,以便复选框位于将触发它们的标签之前。这样您还可以根据复选框状态设置标签样式。

/* Default State */

body {
  background-color: #f2f2f2;
  font: normal .75rem/100% Sans-Serif;
}

p {
  margin: 0;
}

p + p {
  margin: .5rem 0 0 0;
}

/* Pane */

.pane {
  display: none;
  background-color: #fff;
  overflow: hidden;
  clear: both;
  border: 1px solid #EEE;
  border-radius: 2px;
  margin: .25rem 0;
  padding: .5rem;
  text-align: justify;
}

/* Checkbox */

input[type="checkbox"] {
  display: none;
  visibility: hidden;
}

/* Toggled State */

#box1:checked ~ #pane1,
#box2:checked ~ #pane2,
#box3:checked ~ #pane3 {
  display: block;
}

/* Toggle Control */

.toggle-control {
  position: relative;
  float: left;
  margin: 0 .25rem .25rem 0;
  background: #50F;
  color: #FFF;
  padding: .5rem 1rem .5rem 2rem;
  border-radius: 2px;
  box-shadow: 0 .25rem 1rem -.5rem #000;
  border: 1px solid #20C;
  transition: box-shadow ease-in .125s;
  cursor: pointer;
  text-transform: uppercase;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
}

.toggle-control:hover {
  box-shadow: 0 .0 1rem -.5rem #000;
}

input[type="checkbox"] + .toggle-control:before {
  content: "";
  position: absolute;
  width: .5rem;
  height: .5rem;
  border: 1px solid #20C;
  left: .75rem;
  top: 50%;
  margin: -.25rem 0 0 0;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  border-radius: 100%;
}

input[type="checkbox"]:checked + .toggle-control:before {
  background: #FEF;
  border: 1px solid #60F;
  box-shadow: 0 0 1rem #FFF;
}
<input type="checkbox" id="box1" name="demo[]">
<label class="toggle-control" for="box1">Show 1</label>

<input type="checkbox" id="box2" name="demo[]">
<label class="toggle-control" for="box2">Show 2</label>

<input type="checkbox" id="box3" name="demo[]">
<label class="toggle-control" for="box3">Show 3</label>

<div class="pane" id="pane1">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<div class="pane" id="pane2">
  <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
</div>

<div class="pane" id="pane3">
  <p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
</div>

如果您使用 JavaScript 来显示内容,则可以分别删除复选框和标签上的 idfor 属性,因为您可以将复选框放在标签内 ( implicit labels )。

关于html - 如何使复选框位于框中并包含文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33903185/

相关文章:

javascript - 实现拖拽提交表单

javascript - ng-mouseover 上的 Angular JS 更改类

javascript - 阿拉伯语句子开头的拉丁数字

html - HTML 文本周围的颜色

html - 具有相同高度的 Flex 盒子内部元素

javascript - Jquery 拖放 - 在其他容器之前插入拖动的 div 容器

javascript - 如何在页面重新加载时保持 jquery 脚本更改?

ios - iPad:纵向和横向模式之间的时间差

javascript - 在父 div 中居中几个 div

css - Bootstrap 按钮颜色不起作用,但在 jsfiddle 中工作