当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to evenly align checkboxes in a vertical line next to list items?

问题描述:

I am trying to figure out a way to add a checkbox for every ol/li but with even vertical alignment. The checkboxes should be in a straight vertical line instead of in a zig zag manner.

  • Coffee

    • Nestle
    • Cadbury

  • Tea
  • Milk

Is this even possible?

Something like this:

网友答案:

I don't know exactly what you want to achieve but I hope this point you in right direction.

.my-ul{
    padding: 0;
    margin-left: -24px;  
}
.my-ul li{
    list-style: none;
}
<ul>
    <li>Coffee
        <ul class="my-ul">
            <li>
                <input id="nestle" type="checkbox">
                <label for="nestle">Nestle</label>
            </li>
            <li>
                <input id="cadbury" type="checkbox">
                <label for="cadbury">Cadbury</label>
            </li>
        </ul>
    </li>
    <li>Tea</li>
    <li>Milk</li>
</ul>
网友答案:

You can check with the below link.

Fiddle

    label {
    display: block;
    padding-left: 15px;
    text-indent: -15px;
}
input {
    width: 13px;
    height: 13px;
    padding: 0;
    margin:0;
    vertical-align: bottom;
    position: relative;
    top: -1px;
    *overflow: hidden;
}
网友答案:

You can check if the list item contains the input and add nopadding class to it.

$('li').has('input').addClass('nopadding');
ul {
  list-style: none;
  padding-left: 0;
}
ul li {
  padding-left: 40px;
}
ul > li > ul > li > input[type="checkbox"] {
  margin-right: 23px;
}
.nopadding {
  padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <input type="checkbox">A
  </li>
  <li>
    <input type="checkbox">B
    <ul>
      <li>
        <input type="checkbox">Sub Item 1</li>
      <li>Sub Item 2</li>
    </ul>
  </li>
  <li>
    <input type="checkbox">C
    <ul>
      <li>Sub Item 1</li>
      <li>
        <input type="checkbox">Sub Item 2</li>
      <li>Sub Item 3</li>
      <li>Sub Item 4</li>
    </ul>
  </li>
</ul>
分享给朋友:
您可能感兴趣的文章:
随机阅读: