CSS Fundamentals: Beginner’s Guide to Web Styling
Sobre a Aula

Element Positioning

 

Hello, everyone! Welcome to the Element Positioning lesson.

In this lesson, we will learn how to use CSS properties to control the position of elements on the page. This will enable us to create more complex and customized layouts.

What is element positioning?

Element positioning is the process of controlling where an element will appear on the page. There are four types of element positioning:

  • Static positioning: The element is positioned according to the rules of the box model.
  • Relative positioning: The element is positioned relative to its original position.
  • Absolute positioning: The element is positioned relative to the position of the first parent element that does not have a relative or absolute positioning.
  • Fixed positioning: The element is positioned in the same place on the page, regardless of content or page scrolling.

CSS Properties for Element Positioning

There are many CSS properties that can be used to control the positioning of elements. Some of the most common CSS properties for element positioning include:

  • Position: Defines the type of positioning for the element.
  • Top: Defines the vertical position of the element.
  • Left: Defines the horizontal position of the element.
  • Right: Defines the horizontal position of the element from the right edge of the window.
  • Bottom: Defines the vertical position of the element from the bottom edge of the window.
  • Z-index: Defines the stacking order of elements.

Examples

Let’s see some examples of how to use CSS properties to control the positioning of elements.

Example 1:

The following CSS code positions the div element 100px below the top edge of the page:

div {
  position: absolute;
  top: 100px;
}

Example 2:

The following CSS code positions the div element 200px to the right of the left edge of the page:

div {
  position: absolute;
  left: 200px;
}

Example 3:

The following CSS code positions the div element in the center of the page both horizontally and vertically:

div {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Note

It’s important to use CSS selectors to control which CSS rules are applied to which elements. This will help keep your CSS organized and easy to maintain.

Summary

In this lesson, we learned about element positioning. We explored the four types of element positioning and saw some of the CSS properties that can be used to control element positioning.

In the next lesson, we will learn about CSS layouts.

Entrar na conversa
Rolar para cima