开发者

How to apply CSS to elements that have multiple classes?

开发者 https://www.devze.com 2023-03-12 05:50 出处:网络
Is there a way to apply a style to elements that contain multiple classes? Example <div class="foo"> foo</div>

Is there a way to apply a style to elements that contain multiple classes?

Example


<div class="foo"     > foo     </div>
<div class="foo bar" > foo bar </div>
<div class="bar"     > bar     </div>

I only want to modify the styling of the element that contains class "foo bar". The XPath information would be开发者_运维百科 something like //div[contains(@class,"foo") and contains(@class,"bar")]. Any ideas for pure CSS, w/o having to create a unique class?


Like this:

.foo { background: red; }
.bar { background: blue; }
.foo.bar { background: purple; }


Solutions


  1.  .foo.bar { ... }
    
  2.  [class="foo bar"] { ... }             // explicit class name
    
  3.  [class~="foo"][class~="bar"] { ... }  // inclusive class name
    
0

精彩评论

暂无评论...
验证码 换一张
取 消