CSS layout styling

Página 23

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:

# 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.

Next page of the Free Ebook:

24Responsive styling in CSS

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text