3. Position

When we want to place elements on top of others, as if they were layers in Photoshop or similar image editing software, we can do it with position.

absolute

It is positioned relative to the document, so it follows the scroll.

.elemento {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

fixed

It is positioned relative to the viewport, so it ignores the scroll.

.elemento {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

More information

inset

To save you from writing each side separately (top, right, bottom and left), you have a style called inset that shortens its declaration. Its structure is the same as using margin or padding.

.elemento {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

It could be represented as:

.elemento {
  position: fixed;
  inset: 0;
}

Or if we only wanted top, right and left.

.elemento {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}

It could be represented as:

.elemento {
  position: fixed;
  inset: 0 0 auto;
}

Let's remember that 3 values are equivalent to top | left and right | bottom. Therefore I must drop bottom, and to do so we can use auto.

z-index

It indicates the depth, or Z axis. It starts at 0. The higher it is, the more it will be positioned above other elements, there is no defined limit. It is also possible to use negative numbers to place it below the document.

.elemento {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
}

More information

There are other position values like static and relative that were used in the past for layout. Nowadays we have more modern solutions such as flex or grid.

Activity 1

Create a header with its logo and menu that is always anchored to the top.

Activity 2

Create a page with a title and several paragraphs of Lorem text.

Next, design a div that takes up the whole screen and completely covers the previous content. In its center it must be written: "Loading... please wait".

Activity 3

Create a Cookies banner that is anchored to the bottom left.

It must contain:

  • Text: Do you accept our cookies?
  • Accept button.
  • Cancel button.
Activity 4

Create an alert asking the visitor to subscribe to the newsletter. It will block navigation until the user interacts.

It must have the following characteristics:

  • Form to add the email and a submit button.
  • Cross to close the alert.
  • Semi-transparent black background as a fill.
  • The form content will be in a box with a white background.
Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python

Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python

The HTML over WebSockets approach simplifies single-page application (SPA) development and lets you bypass learning a JavaScript rendering framework such as React, Vue, or Angular, moving the logic to Python. This web application development book provides you with all the Django tools you need to simplify your developments with real-time results.

Buy the book

Help me keep writing

Every coffee gives me a push toward the next article.

Comments

There are no comments yet.