⊗mkPmPsRl 178 of 250 menu

Relative Positioning of Elements in CSS

Relative positioning is specified using the relative value for the position property. This type of positioning allows you to shift elements relative to their current position by a given amount. At the same time, all other elements on the page will think that the element is located where it originally was. That is, with this type of positioning, the element does not fall out of the normal flow.

Offsets from the current position are specified by the top, right, bottom and left properties.

Example

To start, let's just make two blocks without positioning:

<div id="elem1"></div> <div id="elem2"></div> #elem1 { width: 100px; height: 100px; border: 1px solid green; } #elem2 { width: 200px; height: 200px; border: 1px solid red; }

:

Example

Now let's add relative to the first block. This won't change anything for now, since we haven't specified the block offset:

<div id="elem1"></div> <div id="elem2"></div> #elem1 { position: relative; width: 100px; height: 100px; border: 1px solid green; } #elem2 { width: 200px; height: 200px; border: 1px solid red; }

:

Example

Let's now move our block 30px from the top, giving it the top property. At the same time, all other elements will behave as if our block remained in the place where it was originally:

<div id="elem1"></div> <div id="elem2"></div> #elem1 { position: relative; top: 30px; width: 100px; height: 100px; border: 1px solid green; } #elem2 { width: 200px; height: 200px; border: 1px solid red; }

:

Example

Let's also move our block 40px to the left by giving it the left property:

<div id="elem1"></div> <div id="elem2"></div> #elem1 { position: relative; top: 30px; left: 40px; width: 100px; height: 100px; border: 1px solid green; } #elem2 { width: 200px; height: 200px; border: 1px solid red; }

:

Example

Negative values ​​of the top, right, bottom and left properties shift in the opposite direction. For example, a positive value of top shifts down, and a negative value shifts up.

Let's move our second block 40px up by giving it a negative top property value:

<div id="elem1"></div> <div id="elem2"></div> #elem1 { width: 100px; height: 100px; border: 1px solid green; } #elem2 { position: relative; top: -40px; left: 20px; width: 200px; height: 200px; border: 1px solid red; }

:

Practical tasks

The following blocks are given:

<div id="elem1"></div> <div id="elem2"></div> <div id="elem3"></div> text text text #elem1 { width: 100px; height: 100px; margin-bottom: 20px; background-color: #FF8888; } #elem2 { width: 100px; height: 100px; margin-bottom: 20px; background-color: #7E89EB; } #elem3 { width: 100px; height: 100px; margin-bottom: 20px; background-color: #4DEE99; }

:

Move these blocks using relative positioning as follows:

byenru