Styling Elements in CSS

Capítulo 22

Estimated reading time: 4 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Styling elements in CSS is one of the main ways to bring life and personality to a website. With CSS, you can change the color, size, font, and many other visual aspects of HTML elements.

Selectors

Before you start styling elements, it's important to understand selectors. Selectors are used to identify the elements that will be styled. There are several types of selectors in CSS, such as type, class, ID, and attribute selectors.

Type selectors are used to style all elements of a given type, such as all paragraphs or all images. Class selectors are used to style elements that have a specific class, such as all elements with the class "highlight". ID selectors are used to style elements that have a specific ID, such as an element with the ID "logo".

Attribute selectors are used to style elements that have a specific attribute, such as an element with the "href" attribute.

Properties

Once you've selected the elements to style, it's time to set the properties. Properties are used to change the visual aspects of elements such as color, size, font, margin, padding, and many others.

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

Some of the most common properties in CSS are:

  • color: sets the text color
  • font-size: sets the font size
  • font-family: defines the font
  • background-color: sets the background color
  • margin: sets the margin
  • padding: sets the inner spacing
  • border: defines the border

There are many other properties in CSS, and it's important to know each one well to be able to style elements the way you want.

Cascade and Specificity

One of the most important features of CSS is cascading. This means that properties defined in one selector can be overridden by properties defined in another selector. For example, if one selector sets the text color to red and another selector sets the text color to blue, the text color will be blue.

In addition, specificity is also important. This means that some selectors carry more weight than others when it comes to overriding properties. For example, an ID selector carries more weight than a class selector.

Pseudo-classes and Pseudo-elements

In addition to normal selectors, CSS also has pseudo-classes and pseudo-elements. Pseudo-classes are used to style elements in certain states, such as when the mouse is over the element or when the element is clicked. Pseudo-elements are used to style specific parts of an element, such as the first character of a paragraph or the content before an element.

Some of the most common pseudo-classes in CSS are:

  • :hover: styles the element when the mouse is over it
  • :active: styles the element when it is clicked
  • :visited: styles the element when it has already been visited

Some of the most common pseudo-elements in CSS are:

  • ::before: styles the content before the element
  • ::after: styles the content after the element
  • ::first-letter: styles the first letter of the element
  • ::first-line: styles the first line of the element

Conclusion

Styling elements in CSS is a fundamental part of web development. With CSS, it is possible to give life and personality to a website, and it is important to know well the selectors, the properties, the cascade and the specificity to be able to style the elements in the desired way.

In addition, pseudo-classes and pseudo-elements are powerful tools for styling elements into specific states and specific parts of an element.

Now answer the exercise about the content:

_What is the role of selectors in CSS?

You are right! Congratulations, now go to the next page

You missed! Try again.

The role of selectors in CSS is to identify the elements that will be styled. They allow developers to target specific HTML elements so that styles can be applied. This is evident from the text, which explains that selectors are used to pinpoint the elements for styling, using different types such as type, class, ID, and attribute selectors.

Next chapter

CSS layout styling

Arrow Right Icon
Free Ebook cover Complete CSS course
63%

Complete CSS course

4.5

(2)

35 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.