Skip to main content

How do you set styles by class?

To set styles by class, a dot . is placed before the class name. In HTML the class is specified through the class attribute, and in CSS through a selector with a dot.


Example

HTML:

html
<p class="title">Heading</p>

CSS:

css
.title { color: blue; font-size: 24px; }

The styles will apply to all elements that have the class title.


Features

  • one class can be used multiple times

  • an element can have several classes:

    html
    <p class="title big">Text</p>
  • a single class selector can select many elements on a page


Summary: to style elements by class, CSS uses a selector with a dot before the class name, and the rule applies to all elements with that class.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

How do you set styles by class?: CSS Interview Question