Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 13, 2022 03:44 pm GMT

CSS isn't magic. All tips about the position property to avoid common mistakes

Hey folks!

I'd like to talk about the position property! We'll consider all nuances that confuse developers. Also I created the Live Cheatsheat. Use it for deep learning!

But before embarking on reading I leave the link on my Substack newsletter about CSS. You know what to make

Let's go!

position: absolute

The block values

Note! I mean the block values are the block, flex, grid and table value

1. When we add position: absolute to all childs they fall out of the parent, i.e it's the height property will be set to 0px.

Image description

If position: absolute is set to the part of childs the parent height is computed depending on the height of childs without position: absolute.

Image description

2. Adding position: absolute doesn't change the display computed value. All values are saved.

Image description

3. The width and height properties of elements with position: absolute are computed depending on content. Therefore the width property of elements with display: block stops to fill up all available space by the text direction.

Image description

4. Since the width and height properties can be applied to elements with block values we don't get some changes when we add position: absolute to them.

Image description
Image description

5. The padding and border properties work without changes for elements after adding position: absolute.

Image description
Image description

Margin Collapsing of the elements with block values stop to work. Vertical margins don't extend beyond the parent. Margins between the elements don't collapse.

Image description

6. Elements with block values are positioned in a column by default. When adding position: absolute they will be shifted according to a defined algorithm.

The first element in HTML saves a start position. The remaining elements shift in such a way that they are one above the other. The first element in HTML will be the bottom and the last element the top.

Image description

Elements with position: absolute can't overlap nearby elements without position: absolute that are before them.

The first element with position: absolute will be after the last element without. Other nearby elements with position: absolute will overlap the first.

Image description

Elements with position: absolute will overlap following nearby elements because they will shift. The gap of shifting will be equal to the sum of the sizes of the elements with position: absolute.

Image description
Image description

The inline values

Note! I mean the inline values are the inline, inline-block, inline-flex, inline-grid and inline-table value

7. As with block values when adding position: absolute to all childs the parent height is set to 0px.

Image description

When adding position: absolute to a part of childs the parent height will be computed by depending on the height childs without position: absolute.

Image description

8. All inline values will be changed on block values. So the inline and inline-block will be changed to block, inline-flex -> flex, inline-grid -> grid and inline-table -> table.

Image description

9. By default, the width and height properties of elements with display: inline are computed depending on content. That behavior is saved but after adding position: absolute the auto value of these properties is changed to pixels.

Image description

10. As well as these properties can't be applied to elements with display: inline. But adding position: absolute changes that. They are applied.

Image description

But these properties are applied to elements with all other inline-* values. So we won't get any changes after adding position: absolute.

Image description

11. The vertical paddings, borders and margins of elements with display: inline stop to extend beyond the parent after adding position: absolute.

Image description

The same properties for the elements with display: inline-* don't extend beyond the parent. So there aren't changes after adding position: absolute.

Image description
Image description

12. Elements with inline values for the display property are positioned in a row by default. When adding position: absolute they will be shifted in such a way as for elements with block values.

They will be displayed each other. The first element will be the bottom and the last element the top.

Image description

They also can't overlap the elements before them. The first element with position: absolute will be after the last element without. Other nearby elements with position: absolute will overlap the first.

Image description

When adding position: absolute to some elements remaining elements will be shifted. They will be overlapped as well.

Image description

Flex items

13. Flex items stop to stretch by the additional axis when adding position: absolute to them.

Image description

14. In the case position: absolute is added to flex items they will be overlapped at each other.

The first element in HTML saves a start position. The remaining elements shift in such a way that they are one above the other. The first element in HTML will be the bottom and the last element the top.

Image description

Elements with position: absolute can overlap any nearby elements that might be before or after them. They move to the start position of the axis and line up in a column.

Image description

15. If we defined the width and height properties to flex items we won't get any changes after adding position: absolute to them.

Image description

16. The padding, border and margin properties continue working without changes when adding position: absolute to flex items.

Image description
Image description
Image description

position: fixed

Folks, I disabled default position relative to the viewport in examples using filter: opacity(1) because that helps me to show another behavior nuances. See the fixed value in the Values section for more details

The block values

Note! I mean the block values are the block, flex, grid and table value

1. When we add position: fixed to all childs they fall out of the parent, i.e it's the height property will be set to 0px.

Image description

If position: fixed is set to the part of childs the parent height is computed depending on the height of childs without position: fixed.

Image description

2. Adding position: fixed doesn't change the display computed value. All values are saved.

Image description

3. The width and height properties of elements with position: fixed are computed depending on content. Therefore the width property of elements with display: block stops to fill up all available space by the text direction.

Image description

4. Since the width and height properties can be applied to elements with block values we don't get some changes when we add position: fixed to them.

Image description

5. The padding and border properties work without changes for elements after adding position: fixed.

Image description
Image description

Margin Collapsing of the elements with block values stop to work. Vertical margins don't extend beyond the parent. Margins between the elements don't collapse.

Image description

6. Elements with block values are positioned in a column by default. When adding position: fixed they will be shifted according to a defined algorithm.

The first element in HTML saves a start position. The remaining elements shift in such a way that they are one above the other. The first element in HTML will be the bottom and the last element the top.

Image description

Elements with position: fixed can't overlap nearby elements without position: fixed that are before them.

The first element with position: fixed will be after the last element without. Other nearby elements with position: fixed will overlap the first.

Image description

Elements with position: fixed will overlap following nearby elements because they will shift. The gap of shifting will be equal to the sum of the sizes of the elements with position: fixed.

Image description

The inline values

Note! I mean the inline values are the inline, inline-block, inline-flex, inline-grid and inline-table value

7. As with block values when adding position: fixed to all childs the parent height is set to 0px.

Image description

When adding position: fixed to a part of childs the parent height will be computed by depending on the height childs without position: fixed.

Image description

8. All inline values will be changed on block values. So the inline and inline-block will be changed to block, inline-flex -> flex, inline-grid -> grid and inline-table -> table.

Image description

9. By default, the width and height properties of elements with display: inline are computed depending on content. That behavior is saved but after adding position: fixed the auto value of these properties is changed to pixels.

Image description

10. As well as these properties can't be applied to elements with display: inline. But adding position: fixed changes that. They are applied.

Image description

But these properties are applied to elements with all other inline-* values. So we won't get any changes after adding position: fixed.

Image description

11. The vertical paddings, borders and margins of elements with display: inline stop to extend beyond the parent after adding position: fixed.

Image description

The same properties for the elements with display: inline-* don't extend beyond the parent. So there aren't changes after adding position: fixed.

Image description
Image description

12. Elements with inline values for the display property are positioned in a row by default. When adding position: fixed they will be shifted in such a way as for elements with block values.

They will be displayed each other. The first element will be the bottom and the last element the top.

Image description

They also can't overlap the elements before them. The first element with position: fixed will be after the last element without. Other nearby elements with position: fixed will overlap the first.

Image description

When adding position: fixed to some elements remaining elements will be shifted. They will be overlapped as well.

Image description

Flex items

13. Flex items stop to stretch by the additional axis when adding position: fixed to them.

Image description

14. In the case position: fixed is added to flex items they will be overlapped at each other.

The first element in HTML saves a start position. The remaining elements shift in such a way that they are one above the other. The first element in HTML will be the bottom and the last element the top.

Image description

Elements with position: fixed can overlap any nearby elements that might be before or after them. They move to the start position of the axis and line up in a column.

Image description

15. If we defined the width and height properties to flex items we won't get any changes after adding position: fixed to them.

Image description

16. The padding, border and margin properties continue working without changes when adding position: fixed to flex items.

Image description
Image description
Image description

position: relative

The block values

Note! I mean the block values are the block, flex, grid and table value

1. When we add position: relative to all childs we don't get any changes. The parent height still is computed depending on the childs height.

Image description

That works if we add position: relative to a part of the childs.

Image description

2. Adding position: relative doesn't change the display computed value. All values are saved.

Image description

3. The default working principle of the width and height properties is saved.

Image description

The element with display: table differs from another values. The width property doesn't fill up all free space. Adding position: relative doesn't affect it.

Image description

4. Since the width and height properties can be applied to elements with block values we don't get some changes when we add position: relative to them.

Image description
Image description

5. The padding and border properties work without changes for elements after adding position: relative.

Image description
Image description

Margin Collapsing of the elements with block values works too. Vertical margins extend beyond the parent. Margins between the elements collapse.

Image description

6. When adding position: relative elements with block values are positioned one below the other.

Image descriptionImage description

Elements with position: relative can't overlap nearby elements without position: relative that are before or after them.

Image description
Image description

The inline values

Note! I mean the inline values are the inline, inline-block, inline-flex, inline-grid and inline-table value

7. As with block values when adding position: relative to all childs the parent height is computed depending on the childs height.

Image description

That behavior is saved if to add position: relative to a part of the childs.

Image description

8. All inline values won't be changed on block values like when adding position: absolute or fixed.

Image description

9. By default, the width and height properties of elements with display: inline are computed depending on content. That behavior is saved without changing the computed value.

Image description

10. As well as these properties can't be applied to elements with display: inline. Adding position: relative doesn't change that.

Image description

Also these properties are applied to elements with all other inline-* values. So we won't get any changes after adding position: relative.

Image description

11. The vertical paddings, borders and margins of elements with display: inline continue to extend beyond the parent after adding position: relative.

Image description

The same properties for the elements with display: inline-* don't extend beyond the parent. So there aren't changes after adding position: relative.

Image description

12. Elements with inline values for the display property are positioned in a row by default. When adding position: relative that behavior is saved.

Image description

They also can't overlap the elements before and after them.

Image description

Flex items

13. Flex items keep to stretch by the additional axis when adding position: relative to them.

Image description

14. After adding position: relative flex items are positioned by main axis.

Image description

Elements with position: relative can't overlap any nearby elements that might be before or after them.

Image description

15. If we defined the width and height properties to flex items we won't get any changes after adding position: relative to them.

Image description

16. The padding, border and margin properties continue working without changes when adding position: relative to flex items.

Image description
Image description
Image description

position: static

The block values

Note! I mean the block values are the block, flex, grid and table value

1. When we add position: static to all childs we don't get any changes. The parent height still is computed depending on the childs height.

Image description

That works if we add position: static to a part of the childs.

Image description

2. Adding position: static doesn't change the display computed value. All values are saved.

Image description

3. The default working principle of the width and height properties is saved.

Image description

The element with display: table differs from another values. The width property doesn't fill up all free space. Adding position: static doesn't affect it.

Image description

4. Since the width and height properties can be applied to elements with block values we don't get some changes when we add position: static to them.

Image description
Image description

5. The padding and border properties work without changes for elements after adding position: static.

Image description
Image description

Margin Collapsing of the elements with block values works too. Vertical margins extend beyond the parent. Margins between the elements collapse.

Image description

6. When adding position: static elements with block values are positioned one below the other.

Image description
Image description

Elements with position: static can't overlap nearby elements without position: static that are before or after them.

Image description
Image description

The inline values

Note! I mean the inline values are the inline, inline-block, inline-flex, inline-grid and inline-table value

7. As with block values when adding position: static to all childs the parent height is computed depending on the childs height.

Image description

That behavior is saved if to add position: static to a part of the childs.

Image description

8. All inline values won't be changed on block values like when adding position: absolute or fixed.

Image description

9. By default, the width and height properties of elements with display: inline are computed depending on content. That behavior is saved without changing the computed value.

Image description

10. As well as these properties can't be applied to elements with display: inline. Adding position: static doesn't change that.

Image description

Also these properties are applied to elements with all other inline-* values. So we won't get any changes after adding position: static.

Image description

11. The vertical paddings, borders and margins of elements with display: inline continue to extend beyond the parent after adding position: static.

Image description

The same properties for the elements with display: inline-* don't extend beyond the parent. So there aren't changes after adding position: static.

Image description

12. Elements with inline values for the display property are positioned in a row by default. When adding position: static that behavior is saved.

Image description

They also can't overlap the elements before and after them.

Image description

Flex items

13. Flex items keep to stretch by the additional axis when adding position: static to them.

Image description
14. After adding position: static flex items are positioned by main axis.

Image description

Elements with position: static can't overlap any nearby elements that might be before or after them.

Image description

15. If we defined the width and height properties to flex items we won't get any changes after adding position: static to them.

Image description

16. The padding, border and margin properties continue working without changes when adding position: static to flex items.

Image description
Image description
Image description

position: sticky

The block values

Note! I mean the block values are the block, flex, grid and table value

1. When we add position: sticky to all childs we don't get any changes. The parent height still is computed depending on the childs height.

Image description

That works if we add position: sticky to a part of the childs.

Image description

2. Adding position: sticky doesn't change the display computed value. All values are saved.

Image description

3. The default working principle of the width and height properties is saved.

Image description

The element with display: table differs from another values. The width property doesn't fill up all free space. Adding position: sticky doesn't affect it.

Image description

4. Since the width and height properties can be applied to elements with block values we don't get some changes when we add position: sticky to them.

Image description
Image description

5. The padding and border properties work without changes for elements after adding position: sticky.

Image description
Image description

Margin Collapsing of the elements with block values works too. Vertical margins extend beyond the parent. Margins between the elements collapse.

Image description

6. When adding position: sticky elements with block values are positioned one below the other.

Image description
Image description

Elements with position: sticky can't overlap nearby elements without position: sticky that are before or after them.

Image description
Image description

The inline values

Note! I mean the inline values are the inline, inline-block, inline-flex, inline-grid and inline-table value

7. As with block values when adding position: sticky to all childs the parent height is computed depending on the childs height.

Image description

That behavior is saved if to add position: sticky to a part of the childs.

Image description

8. All inline values won't be changed on block values like when adding position: absolute or fixed.

Image description

9. By default, the width and height properties of elements with display: inline are computed depending on content. That behavior is saved without changing the computed value.

Image description

10. As well as these properties can't be applied to elements with display: inline. Adding position: sticky doesn't change that.

Image description

Also these properties are applied to elements with all other inline-* values. So we won't get any changes after adding position: sticky.

Image description

11. The vertical paddings, borders and margins of elements with display: inline continue to extend beyond the parent after adding position: sticky.

Image description

The same properties for the elements with display: inline-* don't extend beyond the parent. So there aren't changes after adding position: sticky.

Image description

12. Elements with inline values for the display property are positioned in a row by default. When adding position: sticky that behavior is saved.

Image description

They also can't overlap the elements before and after them.

Image description

Flex items

13. Flex items keep to stretch by the additional axis when adding position: sticky to them.

Image description

14. After adding position: sticky flex items are positioned by main axis.

Image description

Elements with position: sticky can't overlap any nearby elements that might be before or after them.

Image description

15. If we defined the width and height properties to flex items we won't get any changes after adding position: sticky to them.

Image description

16. The padding, border and margin properties continue working without changes when adding position: sticky to flex items.

Image description
Image description
Image description

P.S.
I make unlimited Q&A sessions about HTML/CSS via email with expect responses in 2 days.

I make HTML/CSS reviews of your non-commercial projects and recommendations for improvement.

I help in searching high-quality content about HTML/CSS

Please, use my email for communication [email protected]

Discover more free things from me


Original Link: https://dev.to/melnik909/css-isnt-magic-all-tips-about-the-position-property-to-avoid-common-mistakes-398m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To