CSS选择器,它带有所有label.control-label,除了带有.floating-labels类的表单

时间:2015-03-17 11:06:13

标签: html css css3 twitter-bootstrap twitter-bootstrap-3

我有这段代码:

label.control-label{
    font-weight: bold;
}
label.control-label::after{
    content: ":";
}

这使得输入的Bootswatch Paper's标签变为粗体,并在标签后添加:,并且效果非常好。然后我找到了一种创建浮动标签的方法:Paper description and example of floating labels并且,为了防止其他形式的行为更改,我将类.floating-labels添加到表单之一并创建了这个CSS:

/* Based on Ryan Walters solution http://jsfiddle.net/RyanWalters/z9ymd852/ 

*/

/* --- material floating label --- */

form.floating-labels .form-group {
    display: flex;
    height: 55px;
}

form.floating-labels .control-label {
    font-size: 16px;
    font-weight: 400;
    opacity: 0.4;
    pointer-events: none;
    position: absolute;
    transform: translate3d(0, 22px, 0) scale(1);
    transform-origin: left top;
    transition: 240ms;
}

form.floating-labels .form-group.focused .control-label {
    opacity: 1;
    transform: scale(0.75);
}

form.floating-labels .form-control {
    align-self: flex-end;
}

form.floating-labels .form-control::-webkit-input-placeholder {
    color: transparent;
    transition: 240ms;
}

form.floating-labels .form-control:focus::-webkit-input-placeholder {
    transition: none;
}

form.floating-labels .form-group.focused .form-control::-webkit-input-placeholder {
    color: #bbb;
}

它也有效并且不会使没有.floating-labels类的表单混乱,除了一个错误 - :也被添加到浮动标签,这看起来有点奇怪。所以我想创建一个CSS选择器,它将label.control-label中分配的所有form.floating-labels类分开。我创建了这个CSS,但解决方案无法正常工作

label.control-label:not(form.floating-labels label.control-label){
    font-weight: bold;
}
label.control-label:not(form.floating-labels label.control-label)::after {
    content: ":";
}

如果有人帮助我实现CSS选择器,我将非常高兴,除了label.control-label内的所有form.floating-labels之外,我将非常高兴 - 谢谢你提前。 代码段:

label.control-label:not(form.floating-labels label.control-label){
  font-weight: bold;
}

label.control-label:not(form.floating-labels label.control-label)::after{
  content: ":";
}
<form>
  <label class='control-label'>Should has ':' and be bold</label>
    <input>
 </form>
<form class='floating-labels'>
  <label class='control-label'>Should NOT be bold</label>
    <input>
 </form>
解决方案,基于@DiegoLópez的回答:

form:not(.floating-labels) label.control-label{
    font-weight: bold;
}
form:not(.floating-labels) label.control-label::after{
    content: ":";
}

2 个答案:

答案 0 :(得分:2)

使用您已使用的>

选择:not()选择器
form:not(.floating-labels) > label.control-label{
  font-weight: bold;
}

form:not(.floating-labels) > label.control-label::after{
  content: ":";
}

答案 1 :(得分:1)

您需要使用以下CSS来执行此操作。

form.floating-labels label.control-label {
    font-weight: normal;
}
form.floating-labels label.control-label::after {
    content:"";
}

以下是一个工作代码段。

label.control-label {
  font-weight: bold;
}
label.control-label::after {
  content: ":";
}
form.floating-labels label.control-label {
  font-weight: normal;
}
form.floating-labels label.control-label::after {
  content: "";
}
<form>
  <label class='control-label'>Should has ':' and be bold</label>
  <input>
</form>
<form class='floating-labels'>
  <label class='control-label'>Should NOT be bold</label>
  <input>
</form>