简体   繁体   中英

LESS hide first element with class in DIV

I'm currently trying to hide a button using LESS. My HTML looks the following way:

<div class="targets">
   <div class="target">
      <div class="form-group">
         <div class="col-md-12">
            <button type="button" class="btn-delete" />
         </div>
      </div>
   </div>
</div>

The problem: I use jQuery to add a "target" div to "targets" div, the html looks like this after the DOM manipulation:

<div class="targets">
   <div class="target">
      <div class="form-group">
         <div class="col-md-12">
            <button type="button" class="btn-delete" />
         </div>
      </div>
   </div>
   <div class="target">
      <div class="form-group">
         <div class="col-md-12">
            <button type="button" class="btn-delete" />
         </div>
      </div>
   </div>
   <div class="target">
      <div class="form-group">
         <div class="col-md-12">
            <button type="button" class="btn-delete" />
         </div>
      </div>
   </div>
</div>

I always want to hide the first button with the class btn-delete. I use LESS.

My current LESS looks like this:

.targets {
    button[type="button"] {
        .btn-delete {
            visibility: hidden;
        }
    }
}

Unfortunately my LESS coding doesn't work as I expected.

Do you know how to solve this issue, thus to always hide the first .btn-delete button with LESS Css?

Thanks! :)

As you are targeting the button with btn-delete class, You need & operator

.targets {
    button[type="button"] {
        &.btn-delete {
            visibility: hidden;
        }
    }
}

OR , Traditional way

.targets {
    button[type="button"].btn-delete {
            visibility: hidden;
    }
}

Its applying the visibility to a element with a class inside the button so

.targets {
  button[type="button"].btn-delete {
    visibility: hidden;
  }
}

Would look for button[type="button"] with the class .btn-delete.

Hello use below code it work properly

.targets{
    .target:first-child{  
       button[type="button"] {
        &.btn-delete{ 
          visibility: hidden;
        }
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM