Free Ebook cover Complete CSS course

Complete CSS course

4.5

(2)

35 pages

CSS layout styling

Capítulo 23

Estimated reading time: 4 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Styling layouts in CSS

CSS is a style language used to define the appearance of an HTML document. With it, you can define colors, fonts, sizes, spacing and other visual elements that make up a layout. Styling layouts in CSS is an essential task for creating a nice and professional looking website or web application.

CSS Selectors

To style an HTML element with CSS, it is necessary to select it using a selector. There are several types of selectors, such as tag selector, class selector, id selector, attribute selector, among others.

The tag selector is used to select all elements of a certain type, for example:

body {
  font-family: Arial, sans-serif;
}

The class selector is used to select elements that have a specific class, for example:

.highlight {
  background-color: yellow;
}

The id selector is used to select elements that have a specific id, for example:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

# header {
  border-bottom: 1px solid black;
}

CSS Properties

CSS properties are used to define the style of a selected element. There are several properties available, for example:

  • background-color: sets the background color of the element;
  • color: sets the color of the element's text;
  • font-family: defines the font family used by the element;
  • font-size: defines the font size used by the element;
  • margin: defines the margins of the element;
  • padding: defines the inner spacing of the element;
  • border: defines the border of the element;
  • width: sets the width of the element;
  • height: sets the height of the element;
  • display: defines how the element should be displayed (block, inline, inline-block, etc);
  • float: defines how the element should be positioned in relation to other elements;
  • position: defines how the element should be positioned on the page (static, relative, absolute, fixed);
  • z-index: defines the stacking order of the positioned elements;
  • pacity: sets the opacity of the element;
  • transition: defines the transition of a CSS property;
  • animation: defines a CSS animation.

Cascade and Specificity

When several CSS rules are applied to the same element, it is necessary to determine which one should prevail. This is done through cascading and specificity.

The cascade defines that the last declared CSS rules have priority over the previous ones. For example:

p {
  color: red;
}

P {
  color: blue;
}

In this case, all paragraphs will be colored blue, as the second rule overrides the first.

Specificity defines that more specific CSS rules take precedence over less specific ones. For example:

p {
  color: red;
}

# header p {
  color: blue;
}

In this case, all paragraphs will be colored red, except those within the element with id "header", which will be colored blue.

Conclusion

Styling layouts in CSS is a fundamental task for creating a nice and professional looking website or web application. Using selectors and CSS properties correctly, it is possible to define colors, fonts, sizes, spacing and other visual elements that make up a layout. Additionally, it's important to understand cascading and specificity to ensure CSS rules are correctly applied to selected elements.

Now answer the exercise about the content:

_What is the CSS property used to define the background color of an element?

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

You missed! Try again.

The correct CSS property to define the background color of an element is background-color. This property allows you to set the background color of the specified element, as mentioned in the text.

Next chapter

Responsive styling in CSS

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