如何使复选框位于框中并具有文本

时间:2015-11-24 20:28:22

标签: html css

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

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

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

这是我的codepen: 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;
}

4 个答案:

答案 0 :(得分:2)

您可以通过多种方式使用不同的CSS属性执行此操作:borderpaddingwidthheight ...

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

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

&#13;
&#13;
/* 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;
}
&#13;
<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>
&#13;
&#13;
&#13;

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

答案 1 :(得分:1)

您可以设置标签的样式,但要使它们更像盒子。例如,我刚刚将这个CSS添加到您已经拥有的内容中:

label {
  width:150px;
  height:50px;
  line-height:50px;
  border:2px solid #999;
  display:inline-block;
  text-align:center;
}
label:hover {
  background: #666;
  color: #eee;
}

/* Default State */

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

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

.style {
  background: #fff;
  margin: 10px 0 0 50px;
  width: 200px;
}
/* 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;
}
label {
  width: 150px;
  height: 50px;
  line-height: 50px;
  border: 2px solid #999;
  display: inline-block;
  text-align: center;
  border-radius:8px;
}
label:hover {
  background: #666;
  color: #eee;
}
<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>

答案 2 :(得分:0)

我认为最简单的答案是设置标签元素的样式。我使用带有teh属性的通配符,以防你想限制它影响页面上的其他标签元素,并且我添加了一个游标:指针规则和悬停状态,所以它的行为更像我认为你想要的acheive。

--date-format

答案 3 :(得分:0)

您需要为label元素添加样式(如果您愿意,也可以添加label:hover个样式):

&#13;
&#13;
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;
}

label {
display: inline-block;
margin: 6px;
padding: 6px;
font-weight: bold;
color: rgba(255, 255, 255, 1);
border-top: 2px solid rgba(191, 0, 0, 1);
border-right: 2px solid rgba(95, 0, 0, 1);
border-bottom: 2px solid rgba(95, 0, 0, 1);
border-left: 2px solid rgba(191, 0, 0, 1);
background-color: rgba(127, 0, 0, 1);
}

label:hover {
border-top: 2px solid rgba(0, 0, 191, 1);
border-right: 2px solid rgba(0, 0, 95, 1);
border-bottom: 2px solid rgba(0, 0, 95, 1);
border-left: 2px solid rgba(0, 0, 191, 1);
background-color: rgba(0, 0, 127, 1);
cursor: pointer;
}
&#13;
<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>
&#13;
&#13;
&#13;