如何定位复选框和文本

时间:2015-03-09 18:42:06

标签: jquery html css checkbox

我在使用相应的文字定位这些复选框时遇到了一些麻烦。

隐藏了实际的复选框,以便可以使用个人风格。

无论如何,这纯粹是一个CSS问题。文本应位于复选框的前面。

我一直把头撞在墙上,无法做到......

我错过了什么?

感谢。

JSFiddle

HTML:

<div id="navigation">
    <form id="catform" action="#">
        <div class="kat1">
            <label>
            <input id="catswitch1" type="checkbox" onclick="shCat(1);" checked="checked"/><label for="thing" style="cursor: pointer"></label>
        </div>
<div>ITEM 1</div>
        <div class="kat2">
            <label>
            <input id="catswitch2" type="checkbox" onclick="shCat(2);"/><label for="thing" style="cursor: pointer"></label>
        </div>
<div>ITEM 2</div>
        <div class="kat8">
            <label>
            <input id="catswitch8" type="checkbox" onclick="shCat(8);"/><label for="thing" style="cursor: pointer"></label>
        </div>
<div>ITEM 3</div>
    </form>
</div>

CSS:

body {
    font-family: Arial, Helvetica, san-serif;
    font-size: 11px;
    line-height: 15px;
}

#navigation {
    left: 10px; top: 10px;
    width: 120px;
    border: 1px solid #000;
}

#navigation div {
    padding: 1px 4px 3px 1px;
    border: 1px none #000;
    margin: 0 10px 4px 0;
    height: 17px;
    width: 100%;
}

input[type=checkbox] {
display:none;
}

input[type=checkbox] + label {
background: #FFF;
height: 11px;
width: 11px;
display:inline-block;
padding: 0 0 0 0px;
border: 1px solid #000;
}

input[type=checkbox]:checked + label {
background: #000;
height: 11px;
width: 11px;
display:inline-block;
padding: 0 0 0 0px;
border: 1px solid #000;
}

3 个答案:

答案 0 :(得分:1)

该文字放在新闻div内。 div占据了整个空间,特别是在一条新线上。摆脱它。而是将它们放在span中,也放在父级内,如下所示:

<div class="kat1">
  <label>
   <input id="catswitch1" type="checkbox" onclick="shCat(1);" checked="checked"/>
   <label for="thing" style="cursor: pointer"></label>
  </label>
  <span>ITEM 1</span>
</div>

body {
    font-family: Arial, Helvetica, san-serif;
    font-size: 11px;
    line-height: 15px;
}
#navigation {
    left: 10px;
    top: 10px;
    width: 120px;
    border: 1px solid #000;
}
#navigation div {
    padding: 1px 4px 3px 1px;
    border: 1px none #000;
    margin: 0 10px 4px 0;
    height: 17px;
    width: 100%;
}
input[type=checkbox] {
    display:none;
}
input[type=checkbox] + label {
    background: #FFF;
    height: 11px;
    width: 11px;
    display:inline-block;
    padding: 0 0 0 0px;
    border: 1px solid #000;
}
input[type=checkbox]:checked + label {
    background: #000;
    height: 11px;
    width: 11px;
    display:inline-block;
    padding: 0 0 0 0px;
    border: 1px solid #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navigation">
    <form id="catform" action="#">
        <div class="kat1">
            <label>
                <input id="catswitch1" type="checkbox" onclick="shCat(1);" checked="checked" />
                <label for="thing" style="cursor: pointer"></label> 
            </label>
          <span>ITEM 1</span>
        </div>
        <div class="kat2">
            <label>
                <input id="catswitch2" type="checkbox" onclick="shCat(2);" />
                <label for="thing" style="cursor: pointer"></label> 
            </label>
          <span>ITEM 2</span>
        </div>
        <div class="kat8">
            <label>
                <input id="catswitch8" type="checkbox" onclick="shCat(8);" />
                <label for="thing" style="cursor: pointer"></label>
            </label>
          <span>ITEM 3</span>
        </div>
        <div class="kat14">
            <label>
                <input id="catswitch14" type="checkbox" onclick="shCat(14);" />
                <label for="thing" style="cursor: pointer"></label>
            </label>
          <span>ITEM 4</span>
        </div>
        <div class="kat9">
            <label>
                <input id="catswitch9" type="checkbox" onclick="shCat(9);" />
                <label for="thing" style="cursor: pointer"></label> 
            </label>
          <span>ITEM 5</span>
        </div>
    </form>
</div>

答案 1 :(得分:0)

那样的东西?

http://jsfiddle.net/5u2o1y1h/1/

只需在<label>代码后添加 ITEM 文字。

答案 2 :(得分:0)

https://jsfiddle.net/b6aywnok/

CSS:

body {
    font-family: Arial, Helvetica, san-serif;
    font-size: 11px;
    line-height: 15px;
}

#navigation {
    left: 10px; top: 10px;
    width: 120px;
    border: 1px solid #000;
}

#navigation div {
    padding: 1px 4px 3px 1px;
    border: 1px none #000;
    margin: 0 10px 4px 0;
    height: 17px;
    width: 100%;
}

input[type=checkbox] {
display:none;
}

input[type=checkbox] + label {
background: #FFF;
height: 11px;
margin-left:100px;
width: 11px;
display:inline-block;
padding: 0 0 0 0px;
border: 1px solid #000;
}

input[type=checkbox]:checked + label {
background: #000;
height: 11px;
width: 11px;
display:inline-block;
padding: 0 0 0 0px;
border: 1px solid #000;
}

h3
{
margin-left:50px;
margin-top:-25px;
}

HTML:

<body>
<div id="navigation">
    <form id="catform" action="#">
        <div class="kat1">
            <label>
            <input id="catswitch1" type="checkbox" onclick="shCat(1);" checked="checked"/><label for="thing" style="cursor: pointer"></label>
        </div>
<h3>ITEM 1</h3>
        <div class="kat2">
            <label>
            <input id="catswitch2" type="checkbox" onclick="shCat(2);"/><label for="thing" style="cursor: pointer"></label>
        </div>
<h3>ITEM 2</h3>
        <div class="kat8">
            <label>
            <input id="catswitch8" type="checkbox" onclick="shCat(8);"/><label for="thing" style="cursor: pointer"></label>
        </div>
<h3>ITEM 3</h3>
    </form>
</div>
</body>