悬停与复选框黑客

时间:2016-03-02 13:49:29

标签: html css checkbox hover

我正在尝试创建一个框,当按下按钮时,文本仅显示为“保留”,因为它现在显示“Free Space!Reserved”,因为我使用了:after函数。是否有任何方法可以创建一个悬停功能,以便当盒子为绿色时显示“单击此处保留”并在盒子为红色时悬停“我的”?

代码位于jsFiddle及以下位置:

<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl">Free Space</label>

.mouseover1 {

background-color: green;
color: white;
display: inline-block;
padding: 5% ;
text-align: center;
width: 20%;
}

#btnControl {
 display: none;
}

.mouseover1:active {
margin-left: 2px;
box-shadow: 0px 0px 1px #000;
outline: 1px solid black;
}

.mouseover1:hover {
background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
color:black;
}

#btnControl:checked + label:after {
content: 'Reserved!';
}

input[type=checkbox]:checked ~ .mouseover1 {
background-color: red;
}

5 个答案:

答案 0 :(得分:1)

要更改标签内的文字,请先将其包装在div

<input type="checkbox" id="btnControl"/>
<div class="container">
  <label class="mouseover1 new-label" for="btnControl"><Span>Free Space</Span></label>
</div>

确保添加&#34;新标签&#34;到标签类 然后按如下方式更改CSS:

.mouseover1 {

    background-color: green;
    color: white;
    display: inline-block;
    padding: 5% ;
    text-align: center;
    width: 20%;
}

#btnControl {
    display: none;
}

.mouseover1:active {
    margin-left: 2px;
    box-shadow: 0px 0px 1px #000;
    outline: 1px solid black;
}

.mouseover1:hover {
    background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
    color:black;
}

#btnControl:checked + label:after {
    content: 'Reserved!';
}

input[type=checkbox]:checked ~ .mouseover1 {
    background-color: red;
}
.container:hover .new-label span{
    display: none;
}
.container:hover .new-label:after{
    content: 'Click Here To Reserve';
}

答案 1 :(得分:1)

只需添加文本范围并添加类。

CSS

#btnControl:checked + .mouseover1 span {
  display: none;
}

在这里:jsFiddle

答案 2 :(得分:0)

您可以使用font-size:

&#13;
&#13;
.mouseover1 {
    
    background-color: green;
    color: white;
    display: inline-block;
    padding: 5% ;
    text-align: center;
    width: 20%;
}

#btnControl {
    display: none;
}

.mouseover1:active {
    margin-left: 2px;
    box-shadow: 0px 0px 1px #000;
    outline: 1px solid black;
}

.mouseover1:hover {
    background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
    color:black;
}
#btnControl:checked + label {
    font-size:0;
}
#btnControl:checked + label:after {
    content: 'Reserved!';
    font-size:1rem;
}

input[type=checkbox]:checked ~ .mouseover1 {
	background-color: red;
}
&#13;
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl">Free Space</label>
&#13;
&#13;
&#13;

或float和text-indent

&#13;
&#13;
    .mouseover1 {
      background-color: green;
      color: white;
      display: inline-block;
      padding: 5%;
      text-align: center;
      width: 20%;
    }
    #btnControl {
      display: none;
    }
    .mouseover1:active {
      margin-left: 2px;
      box-shadow: 0px 0px 1px #000;
      outline: 1px solid black;
    }
    .mouseover1:hover {
      background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
      color: black;
    }
    #btnControl:checked + label {
      text-indent: -999px;
      ;
    }
    #btnControl:checked + label:after {
      content: 'Reserved!';
      float: left;
      width: 100%;
      text-indent: 0;
    }
    input[type=checkbox]:checked ~ .mouseover1 {
      background-color: red;
    }
&#13;
<input type="checkbox" id="btnControl" />
<label class="mouseover1" for="btnControl">Free Space</label>
&#13;
&#13;
&#13;

或您认为相关的任何其他方法。最好是用javascript来改变文字。

答案 3 :(得分:0)

将文字包裹在span中可以让您轻松隐藏它。悬停文本(&#34;点击此处保留/我的&#34;)可以添加另一个伪元素

&#13;
&#13;
.mouseover1 {
    
    background-color: green;
    color: white;
    display: inline-block;
    padding: 5% ;
    text-align: center;
    width: 20%;
}

#btnControl {
    display: none;
}

.mouseover1:active {
    margin-left: 2px;
    box-shadow: 0px 0px 1px #000;
    outline: 1px solid black;
}

.mouseover1:hover {
    background-image: linear-gradient(to bottom, #c3e3fa, #a5defb);
    color:black;
}

label {
  position: relative;
}

#btnControl:checked + label:after {
    content: 'Reserved!';
}

#btnControl:checked + label span {
  display: none;
}

#btnControl + label:before {
    content: 'Click here to reserve';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0,0,0,.8);
    color: #fff;
    opacity: 0;
    transition: all .2s ease;
}

#btnControl + label:hover:before {
    opacity: 1;
}

#btnControl:checked + label:before {
    content: 'Mine!';
}

input[type=checkbox]:checked ~ .mouseover1 {
	background-color: red;
}
&#13;
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl"><span>Free Space</span></label>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

我确实使用:before来显示/隐藏原始文字。

single button jsfiddle

multiple buttons jsfiddle

body {
  max-width:100%;
  max-height:100vh;
  font-family: arial, sans-serif;
  font-weight: bold;
}

label {
  position:absolute;
  right:0;
  left:0;
  margin:auto;
}

.mouseover1 {    
    background-color: yellowgreen;
    color: white;
    display: inline-block;
    padding: 5% ;
    text-align: center;
    width: 20%;
}

#btnControl {
    display: none;
}

.mouseover1:active {
    box-shadow: 0px 0px 1px #000;
    outline: 2px solid darkred;
}

.mouseover1:hover {
    background: yellowgreen;
    color: white;
}

#btnControl:checked + label:after {
    content: 'Reserved';
}

#btnControl + label:before {
    content: 'Free Space';
}

#btnControl:checked + label:before {
    content: '';
}

#btnControl:checked:hover + label:after {
    content: 'Mine!';
}

#btnControl:hover + label:before {
    content: 'Click here!';
}

#btnControl:checked:hover + label:before {
    content: '';
}

input[type=checkbox]:checked + .mouseover1 {
	background-color: tomato;
}
<input type="checkbox" id="btnControl"/>
<label class="mouseover1" for="btnControl"></label>