10. Layout and positioning with CSS

Página 56

Chapter 10: Layout and positioning with CSS

To create an aesthetically pleasing and functional website, it is essential to have a good understanding of the concepts of layout and positioning in CSS. CSS, or Cascading Style Sheets, is a style sheet language used to describe the appearance of a document written in HTML. It's a crucial component of front-end development and is responsible for many aspects of a website's design, including layout, colors, fonts, and more.

10.1 Basic layout concepts

CSS layouts can be divided into two types: block layout and inline layout. Block elements, such as divs and paragraphs, start on a new line and take up all available horizontal space, while inline elements, like links and spans, only take up as much space as necessary and do not start on a new line.

CSS layouts can also be classified as fixed, fluid or responsive layouts. Fixed layouts have widths that are defined in pixels and do not change with the size of the browser window. Fluid layouts have widths that are set in percentages, allowing the layout to adjust to the size of the browser window. Responsive layouts use media queries to adjust the layout based on the device's screen size.

10.2 Positioning in CSS

Positioning in CSS is an important concept that allows you to control where elements are placed on the page. There are five values ​​for the 'position' property: static, relative, absolute, fixed and sticky.

The 'static' value is the default value and positions the element according to the normal flow of the document. The 'relative' value positions the element relative to its normal position. The 'absolute' value positions the element relative to the nearest parent element that has a position value other than 'static'. The 'fixed' value positions the element relative to the browser window. The 'sticky' value is a mix of 'relative' and 'fixed' and positions the element based on the user's scrolling.

10.3 Layouts with Flexbox and Grid

Flexbox and Grid are two modern CSS layout techniques that offer greater flexibility and control over the positioning of elements. Flexbox is ideal for one-dimensional layouts, while Grid is more suitable for two-dimensional layouts.

With Flexbox, you can easily align elements horizontally or vertically and distribute space between elements. The 'display' property is set to 'flex' on the parent element and several other properties such as 'flex-direction', 'justify-content' and 'align-items' can be used to control the layout of child elements.< /p>

With Grid, you can create complex layouts with rows and columns. The 'display' property is set to 'grid' on the parent element and the 'grid-template-columns', 'grid-template-rows' and 'grid-gap' properties are used to define the grid structure. The child elements are then positioned on the grid using the 'grid-column' and 'grid-row' properties.

10.4 Conclusion

Understanding the concepts of layout and positioning in CSS is fundamental to creating attractive and functional websites. By mastering these concepts, you will be able to create complex layouts with ease and precision. Remember that practice is the key to becoming proficient in CSS, so keep experimenting and building projects to improve your skills.

Now answer the exercise about the content:

What is the role of CSS in the front-end development of a website?

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

You missed! Try again.

Next page of the Free Ebook:

5711. Box model and padding, border and margin

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