0

I have a div with this class:

<div class="hero-section width-80%"> A Hero Image </div>

CSS:

<style>
.width-80%{
max-width: 80%;
}
</style>

Does a percent sign allowed to use in class value ? If I want to use any percent class with "%" symbol, How can I use a percent sign in css?

  • 2
    [Which characters are valid in CSS class names/selectors?](https://stackoverflow.com/questions/448981/which-characters-are-valid-in-css-class-names-selectors) - It isn't valid, but you can seemingly escape it. – Álvaro González Jan 07 '23 at 11:49

1 Answers1

3

Yes, you'd have to escape it in your selector:

.width-80\% {
  max-width: 80%;
  
  color: red;
}
<div class="hero-section width-80%">A Hero Image</div>

But it is not valid by the CSS specification (as commented by Álvaro González)

Nora Söderlund
  • 1,148
  • 2
  • 18