【问题标题】:Align checkbox and label horizontally?水平对齐复选框和标签?
【发布时间】:2018-03-14 21:32:35
【问题描述】:

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

如何将复选框和标签放在同一行,但保留换行符,并确保“France”和“territories”都具有相同的左缩进,而不是“territories”具有相同的缩进复选框?

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

【问题讨论】:

    标签: css


    【解决方案1】:

    如果您将 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>

    【讨论】:

      【解决方案2】:

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

      额外提示:我不想在您的初始代码中弄乱不必要的,但请尝试使用更多语义代码和更简单的逻辑 - 例如应该应用 .checkbox 类复选框本身而不是它周围。此外,使用新标记时,&lt;div class="checkbox france"&gt; 可能会过时,因为您不妨使用 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/

      【讨论】:

        猜你喜欢
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-28
        • 2012-10-24
        • 1970-01-01
        • 2013-12-14
        • 1970-01-01
        相关资源
        最近更新 更多