This section describes five common positioning methods in the CSS

Published: 2024-04-22 09:32:53 Author: Warm Front I want to comment
This article mainly introduces the five common positioning methods of CSS and their specific applications in detail, the example code in the article explains in detail, has a certain reference value, interested partners can learn with the small series

There are five kinds of positioning

1. The position attribute of the CSS

  • static: Default, the element is in the normal document flow and is not specifically positioned.
  • relative: Positioned relative to the element's initial position in the document flow.
  • absolute: Positioning relative to the nearest located (non-static) ancestor element.
  • fixed: Relative to browser window positioning, the element stays in the specified position even if the window scrolls.
  • sticky: Based on the scroll positionrelativeandfixedSwitch between positioning.
Positioning mode Absolute positioning Relative positioning Fixed positioning Viscous positioning Static positioning
Location origin Relative to the nearest located ancestor element Position in the document flow relative to the element itself Relative to the browser window Relative to the nearest positioning context The position of the element in the normal document flow
Position reference Relative to the located ancestor element Relative to the original position of the element itself Browser window Relative to the nearest positioning context Location in the document flow
Rolling effect It moves as the page scrolls It moves as the page scrolls Fixed to a certain location in the browser window Moves as the page scrolls, does not scroll until a certain distance It moves as the page scrolls
Element position adjustment Through the top, right, bottom, and left attributes Through the top, right, bottom, and left attributes Through the top, right, bottom, and left attributes Through the top, right, bottom, and left attributes non-adjustable

2. Attributes used to locate elements

top, right, bottom, left: These attributes are used to specify the position of an element relative to its containing block (usually the parent element). They only work for elements that use relative positioning, absolute positioning, fixed positioning, or viscous positioning.

z-index: Specifies the stacking order of the elements. The higher the value, the closer the elements are. An element with a higher z-index value appears above an element with a lower z-index value.

3. Detailed description and examples of positioning types

1. Static positioning

Default location mode. Elements are arranged in a normal document flow and do not change position due to the positioning property. At this point, the top, right, bottom, left, and z-index properties will have no effect.

2. Relative positioning

Allows you to adjust the element to its original position. It doesn't leave the document flow, but it leaves the space in its original place.

.relative-box {
  position: relative;
}

3. Absolute positioning

The element is removed from the document stream and its location is determined relative to its nearest location ancestor. If no ancestor is located, it locates relative to the initial boundary of the document.

.absolute-box { 
  position: absolute;
}

4. Fix the position

The element is positioned relative to the window, so its position does not change as you scroll through the page. With fixed positioning, you can create elements that are fixed to a certain place on the page, such as navigation bars or suspended ads.

.fixed-box {
  position: fixed;
}

5. Sticky positioning

It combines relative and fixed positioning features. When the page scrolls to a specific location, sticky elements "stick" to a certain location in the window. Elements are positioned relative until they cross a certain threshold, and then become fixed. This positioning is often used to create elements that are fixed at the top or bottom of the page when scrolling, such as the navigation bar.

.sticky-box { 
  position: sticky;

4. Precautions

1. The document flow is affected

Absolute and fixed positioning can take elements out of the normal flow of the document, which can affect the layout of other elements.

2. Stack sequence

Be careful when using z-index to control the stacking order of elements to ensure that there are no unexpected overwrites.

3. Performance

Using a lot of positioning elements can affect the performance of your page, especially on mobile devices.

Step 4: Compatibility

Different browsers may interpret positioning properties slightly differently, and testing is required to ensure compatibility across browsers.

5. Scroll bar

When using fixed positioning, be aware that elements are fixed in the browser window, which can cause unnecessary scroll bars on pages with long content.

5. Practical application

1. The son abandons the father

The parent element.parent sets relative positioning, while the child element.child sets absolute positioning and is positioned in the center of the parent element through the top and left attributes. transform: translate(-50%, -50%); Center the child element in both vertical and horizontal directions. In this way, the child element is absolutely positioned relative to the parent element, that is, the child is not the parent phase.

HTML code:

<div class="parent"> <div class="child"> Child element </div> </div>

CSS code:

.parent { position: relative; /* Parent element set relative positioning */ width: 300px; height: 200px; } .child { position: absolute; /* Child element set absolute positioning */ top: 50%; /* Positioned vertically at 50% of the parent */ left: 50%; /* Horizontally positioned at 50% of the parent */ transform: translate(-50%, -50%); /* Center child element by translation */}

2. Ceiling effect

To achieve a cap effect in CSS, you can use the position: sticky property. This property allows the element to be fixed to the page when it scrolls to a specific position, as if it were attached to the top.

The.header element uses position: sticky; And set top: 0; To ensure that it is fixed at the top of the page. When scrolling the page, the.header will remain at the top of the viewport

<div class="header"> This is a top header </div> <div class="container"> <p> This is the content of the page. As you scroll through the page, the head is fixed at the top. </p> </div>
<style> .container { width: 100%; height: 1500px; /* To demonstrate the scrolling effect */ padding-top: 50px; /* Keep content unblocked by fixed headers */}.header {position: -webkit-sticky; /* Compatibility */ position: sticky; top: 0; /* Fixed at top */ background-color: #333; z-index: 1000; /* If there are other elements to cover at the top, use z-index */} </style>

To this article about the five common positioning methods in CSS detailed explanation of the article is introduced to this, more related CSS positioning methods please search the script home previous articles or continue to browse the following related articles, I hope you will support the script home in the future!

  • Tag: Locate the css

Related article

  • The CSS sets the spacing for automatic scroll positioning

    This article mainly introduces the method of CSS setting automatic rolling positioning spacing, the article has detailed code examples and graphic introduction, for everyone's study or work has a certain value, the need of friends can refer to
    2023-09-22
  • An implementation of locating HTML elements using CSS

    When locating content on the page, you can use some attributes to help you manipulate the position of elements, this article mainly introduces the implementation of CSS to locate HTML elements, there are five main types, interested can understand
    2022-07-05
  • Describes the differences between the four positioning methods in the CSS

    This article mainly introduces the difference between the four kinds of positioning in CSS. This article introduces you in great detail through the combination of examples and pictures. It has certain reference value for your study or work
    2020-07-23
  • Summary of positioning in CSS

    CSS positioning has four different roles in different scenarios, this article will introduce you to the relevant knowledge of CSS positioning, through the example code screenshot to introduce you in detail, for everyone's study or work has a certain reference value
    2020-03-20
  • Learn more about how to use positioning in CSS (Summary)

    This article mainly introduces how to use positioning in in-depth learning CSS (summary), the article introduces very detailed through the example code, which has certain reference learning value for everyone's study or work, and the friends who need to learn together with Xiaobian below
    2019-12-30
  • CSS positioning tutorial

    This article mainly introduces the tutorial of CSS positioning, Xiaobian feel very good, now share to you, also give you a reference. Let's take a look
    2018-01-30
  • Description of the positioning mode of the css position setting element

    The following Xiaobian will bring you a detailed explanation of the positioning method of css position setting elements. Xiaobian feel very good, now to share with you, but also to give you a reference. Let's take a look
    2016-08-05

Latest comments