Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add content if the element is not empty

Tags:

html

css

I want to add content (with :before selector) only if span has value.

If the span is empty I don't want to add content with :before selector.

How can I do it with css?

<!DOCTYPE html>
<html>

<head>
  <style>
    span.b::before {
      content: "\2022"
    }
    
    .span.b:empty {}
  </style>
</head>

<body>

  <div class="a">
    <span class="b">AAA</span>
  </div>

</body>

</html>
like image 871
javauser35 Avatar asked Sep 17 '25 05:09

javauser35


1 Answers

You were close. You're looking for:

span.b:not(:empty)::before {
  content:"\2022"
}

span.b:not(:empty)::before {
  content:"\2022"
}
<div class="a">
  <span class="b">AAA</span>
</div>
<div class="a">
  <span class="b"></span>
</div>
like image 92
tao Avatar answered Sep 19 '25 21:09

tao