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.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.