水平对齐复选框和标签?

时间:2018-03-14 09:39:24

标签: css

我正在尝试在固定宽度的div中创建一个图例,其中包含彩色复选框和每个复选框的标签。我的问题是较长的标签会突破两行并将标签向下推到复选框下方,这看起来很糟糕:

enter image description here

如何在同一行显示复选框和标签,但保留换行符,并确保"法国"和"地区"两者都有相同的左缩进,而不是"地区"与复选框具有相同的缩进?

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

完整代码:

<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;
}

2 个答案:

答案 0 :(得分:2)

如果您将display: inline-flexalign-items: flex-start添加到.checkbox类,则可以使用 Flexbox 执行此操作:

#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>

答案 1 :(得分:0)

我冒昧地将input[type="checkbox"]正确包裹在label内,以便切换复选框以获得更好的用户体验。显然,这也需要一些标记更改。

额外提示:我不想用你的初始代码弄乱,而是尝试使用更多的语义代码和更简单的逻辑 - 例如.checkbox类应该是应用于复选框本身而不是它周围。使用新标记时,<div class="checkbox france">可能会过时,因为您可能已使用label.country类来定位特定元素。

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:

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

这是一个工作小提琴:https://jsfiddle.net/y74qanps/