简体   繁体   English

如何将可点击区域应用于整个Div,而不仅仅是标签?

[英]How Can I Make the Clickable Area be Applied to Whole Div, not Just Label?

I have been banging my head against the wall on this one for a while and can't seem to find how to do this. 我一直在这个墙上敲我的头一段时间,似乎无法找到如何做到这一点。 I have a card with two rows (card-body) classes. 我有一张卡有两排(卡体)类。 each row represents a radio button (input) with a corresponding label. 每行代表一个带有相应标签的单选按钮(输入)。 When the user clicks the radio button I want the background to change colors. 当用户单击单选按钮时,我希望背景更改颜色。 Right now that is working, but the background is only working for the label and not the whole div that contains the card row. 现在它正在工作,但背景只适用于标签,而不是包含卡行的整个div。 I'm looking for a HTML/CSS-only answer, as I think it's probably something simple that I'm doing wrong. 我正在寻找一个只支持HTML / CSS的答案,因为我认为这可能是一件很简单的事情,我做错了。 Thanks! 谢谢!

CSS CSS

.card {
    position: relative;
    display: flex;
    flex-direction: column;
    min-width: 0;
    word-wrap: break-word;
    background-color: #fff;
    background-clip: border-box;
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 0.25rem;
}
.card-body {
    flex: 1 1 auto;
    padding: 1.25rem;
}
.pick input[type="radio"] {
  display: none;
  /* comment this line to see the radio buttons */
}

.pick label {
  display: inline-block;
  background-color: red;
}

.pick input[type="radio"]:checked+label {
  background-color: blue;
}
.card {
    border-style: solid;
    border-width: 5px;
}
label {
  display: block;
  width: 100%;
}

HTML HTML

<div class="card">
  <div class="card-body pick" for="i1">
    <input type="radio" name="g1" id="i1" value="you" data-target="r65">
    <label class="ben" for="i1" id="r1">you</label>
  </div>
  <div class="card-body pick" for="i2">
    <input type="radio" name="g1" id="i2" value="me" data-target="r65">
    <label class="ben" for="i2" id="r2">me</label>
  </div>
</div>

JSFiddle: https://jsfiddle.net/j3mskfjv/11/ JSFiddle: https ://jsfiddle.net/j3mskfjv/11/

You need to set your labels to display: block with width of 100%, like so: 您需要设置要显示的标签:宽度为100%的块,如下所示:

 .card { position: relative; display: flex; flex-direction: column; min-width: 0; word-wrap: break-word; background-color: #fff; background-clip: border-box; border: 1px solid rgba(0, 0, 0, 0.12); border-radius: 0.25rem; } .card-body { flex: 1 1 auto; } .pick input[type="radio"] { display: none; /* comment this line to see the radio buttons */ } .pick label { display: inline-block; background-color: red; } .pick input[type="radio"]:checked+label { background-color: blue; } .card { border-style: solid; border-width: 5px; } .pick { display: flex; } label { display: block; flex: 1; padding: 1.25rem; } 
 <div class="card"> <div class="card-body pick" for="i1"> <input type="radio" name="g1" id="i1" value="you" data-target="r65"> <label class="ben" for="i1" id="r1">you</label> </div> <div class="card-body pick" for="i2"> <input type="radio" name="g1" id="i2" value="me" data-target="r65"> <label class="ben" for="i2" id="r2">me</label> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM