简体   繁体   English

水平对齐复选框并水平标记?

[英]Align checkbox and label horizontally?

I am trying to create a legend inside a fixed-width div, containing coloured checkboxes and a label for each checkbox. 我正在尝试在固定宽度的div中创建图例,其中包含彩色复选框和每个复选框的标签。 My problem is that longer labels break over two lines and push the label down below the checkbox, which looks bad: 我的问题是,较长的标签会跨越两行,并将标签向下推至复选框下方,这看起来很糟:

在此处输入图片说明

How can I get the checkbox and the label on the same line, but retaining the line break, and ensuring that "France" and "territories" both have the same left indent, rather than "territories" having the same indent as the checkbox? 我如何在同一行上获得复选框和标签,但保留换行符,并确保“法国”和“领土”都具有相同的左缩进,而不是“领土”具有与复选框相同的缩进?

JSFiddle: https://jsfiddle.net/6yLd6j1b/10/ JSFiddle: https ://jsfiddle.net/6yLd6j1b/10/

Full code: 完整代码:

<div id='legend'>
<div class="legend-container" id="areas">

  <div class="checkbox france">
    <input type="checkbox" checked="checked" class="area_type" id="france">  
    <label for="france"></label><div class="tag">France and its overseas territories</div>
  </div>

  <div class="checkbox germany">
    <input type="checkbox" checked="checked" class="area_type" id="germany">
     <label for="germany"></label><div class="tag">Germany</div>
  </div>

</div>
</div>

#legend {
    width: 200px;
}
#legend div {
    margin-right: 5px;
}
.checkbox {
  margin: 0 0 1.2em 0;
  clear: both;
  cursor: pointer;
}
.checkbox .tag {
  display: block;
  float: left;
}
.checkbox .area_type {
  display: none;
}
.area_type + label {
  cursor: pointer;
  -webkit-appearance: none;
  background-color: #fafafa;
  border: 1px solid #cacece;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
  padding: 7px;
  display: block;
  float: left;
  position: relative;
  top: 2px;
  overflow: hidden;
  border-radius: 5px;
  margin-right: 5px;
}
.area_type:checked + label:after {
  width: 100%;
  height: 100%;
  content: "";
  position: absolute;
  left: 0px;
  top: 0px;
}
.france .area_type:checked + label:after {
  background-color: #2c98a0; 
}
.germany .area_type:checked + label:after {
  background-color: #38b2a3;
}

You can do it with the Flexbox if you add display: inline-flex and align-items: flex-start to the .checkbox class: 你可以用Flexbox的做,如果你添加display: inline-flexalign-items: flex-start.checkbox类:

 #legend { background-color: #ccc; border-radius: 3px; box-shadow: 0 1px 2px rgba(0,0,0,0.10); padding: 10px; position: absolute; top: 10px; right: 10px; z-index: 1; width: 200px; } #legend div { margin-right: 5px; } .checkbox { margin: 0 0 1.2em 0; clear: both; cursor: pointer; display: inline-flex; /* only takes the content's width, but in your case "display: flex" also works just fine */ align-items: flex-start; /* prevents the default vertical stretching of flex-items */ } .checkbox .tag { display: block; float: left; } .checkbox .area_type { display: none; } .area_type + label { cursor: pointer; -webkit-appearance: none; background-color: #fafafa; border: 1px solid #cacece; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05); padding: 7px; display: block; float: left; position: relative; top: 2px; overflow: hidden; border-radius: 5px; margin-right: 5px; } .area_type:checked + label:after { width: 100%; height: 100%; content: ""; position: absolute; left: 0px; top: 0px; } .france .area_type:checked + label:after { background-color: #2c98a0; } .germany .area_type:checked + label:after { background-color: #38b2a3; } 
 <div id='legend'> <div class="legend-container" id="areas"> <div class="checkbox france"> <input type="checkbox" checked="checked" class="area_type" id="france"> <label for="france"></label><div class="tag">France and its overseas territories</div> </div> <div class="checkbox germany"> <input type="checkbox" checked="checked" class="area_type" id="germany"> <label for="germany"></label><div class="tag">Germany</div> </div> </div> </div> 

I took the liberty of correctly wrapping the input[type="checkbox"] inside the label so that it toggles the checkbox for better user experience. 我自由选择了将input[type="checkbox"]正确包装在label内,以便切换复选框以获取更好的用户体验。 Obviously this required some markup changes as well. 显然,这也需要一些标记更改。

Bonus tip: I didn't want to mess any more than necessary with your initial code, but try using more semantic code and simpler logic - for example the .checkbox class should be applied on the checkbox itself and not around it. 温馨提示:我不想对您的初始代码造成不必要的混乱,但请尝试使用更多的语义代码和更简单的逻辑-例如, .checkbox类应应用于复选框本身而不是其周围。 Also with the new markup the <div class="checkbox france"> could become obsolete, since you might as well have used the label.country class to target specific elements. 同样,使用新的标记, <div class="checkbox france">也可能会过时,因为您可能已经使用了label.country类来定位特定元素。

HTML: HTML:

<div id='legend'>
  <div class="legend-container" id="areas">

    <div class="checkbox france">
      <label for="france">
        <input type="checkbox" checked="checked" class="area_type" id="france">  
        <span class="tag">France and its overseas territories</span>
      </label>
    </div>

    <div class="checkbox germany">
      <label for="germany">
        <input type="checkbox" checked="checked" class="area_type" id="germany">
        <span class="tag">Germany</span>
      </label>
    </div>

  </div>
</div>

CSS: CSS:

#legend {
  width: 200px;
}

#legend div {
  margin-right: 5px;
}

.checkbox {
  margin: 0 0 1.2em 0;
  clear: both;
  cursor: pointer;
}

.checkbox {
  position: relative;
}

.area_type {
  position: absolute;
  visibility: hidden;
  opacity: 0;
}

.area_type+span {
  display: block;
  margin-left: 25px;
}

.area_type+span:before {
  content: "";
  cursor: pointer;
  -webkit-appearance: none;
  background-color: #fafafa;
  border: 1px solid #cacece;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
  padding: 7px;
  display: block;
  overflow: hidden;
  border-radius: 5px;
  margin-right: 5px;
  position: absolute;
  left: 0;
  top: 2px;
}

.france .area_type:checked+span:before {
  background-color: #2c98a0;
}

.germany .area_type:checked+span:before {
  background-color: #38b2a3;
}

Here is a working fiddle: https://jsfiddle.net/y74qanps/ 这是一个工作的小提琴: https : //jsfiddle.net/y74qanps/

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

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