search
HomeWeb Front-endCSS TutorialIn-depth analysis and understanding of the details of positioning and layout in CSS

This article brings you relevant knowledge about CSS positioning and layout. Let’s take a look at what relative positioning, absolute positioning and fixed positioning are, the different element properties and uses, and other knowledge. I hope it will be helpful to everyone.

In-depth analysis and understanding of the details of positioning and layout in CSS

1. Relative positioning

1.1) What is relative positioning

Relative positioning: The box can be adjusted according to its original position (Achieved through position descriptors).

Position descriptors:
left: move to the right; right moves to the left; top moves down; bottom moves up
(When the value inside is a negative number, Move in the opposite direction)

For example:
Original:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>相对定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        p {
            width: 500px;
            height: 500px;
            border: 1px solid #000;
            margin: 50px auto;
        }

        p {
            width: 100px;
            height: 100px;
            background-color: lightblue;
            position: relative;
            top: 50px;
            left: 50px;
        }
    </style></head><body>
    <p>
    	<p></p>
    </p></body></html>

In-depth analysis and understanding of the details of positioning and layout in CSS
Set p to relative positioning:

p {
    width: 100px;
    height: 100px;
    background-color: lightblue;
    position: relative;
    top: 50px;
    left: 50px;}

In-depth analysis and understanding of the details of positioning and layout in CSS

1.2) The nature and use of relative positioning

Properties

  • Relatively positioned elements are still essentially In the original position, it will just be rendered in a new place and will not affect other elements of the page.

Purpose

  • Used to fine-tune the position of elements
  • Relatively positioned boxes can be used as reference boxes for absolute positioning

For example:

nbsp;html>
    <meta>
    <meta>
    <meta>
    <title>相对定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        nav {
            width: 780px;
            height: 50px;
            margin: 40px auto;
        }

        nav ul {
            list-style: none;
        }

        nav ul li {
            float: left;
            width: 156px;
            height: 50px;
            line-height: 50px;
            text-align: center;
        }

        nav ul li a {
            display: block;
            width: 156px;
            height: 50px;
            background-color: lightcyan;
            color: #000;
            text-decoration: none;
        }

        nav ul li a:hover {
            border-top: 3px solid red;
        }
    </style>
    <nav>
        <ul>
            <li>
                <a>导航一</a>
            </li>
            <li>
                <a>导航二</a>
            </li>
            <li>
                <a>导航三</a>
            </li>
            <li>
                <a>导航四</a>
            </li>
            <li>
                <a>导航五</a>
            </li>
        </ul>
    </nav>

The effect at this time is like this:
In-depth analysis and understanding of the details of positioning and layout in CSS
You will find that when the mouse is hovering over it, the navigation area will sink.
After we set relative positioning for it and fine-tuned it:

nav ul li a:hover {
    border-top: 3px solid red;
    position: relative;
    top: -3px;}

In-depth analysis and understanding of the details of positioning and layout in CSS
This will solve the problem just now


2. Absolute positioning

2.1) What is absolute positioning

Absolute positioning: The box describes its position with coordinates and has its own absolute position.

Absolutely positioned reference box:
An absolutely positioned box will use the nearest box with positioning attributes among its ancestor elements as the reference point.

This box is usually positioned relatively, so it is also called "the son must be the father".

Position descriptors:
left: the distance to the left; right: the distance to the right; top: the distance to the top; bottom: the distance to the bottom

For example:

nbsp;html>
    <meta>
    <meta>
    <meta>
    <title>绝对定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            position: absolute;
            width: 500px;
            height: 300px;
            left: 200px;
            top: 100px;
            background-color: antiquewhite;
        }
    </style>
    <p>
    </p>

2.2) The nature and use of absolute positioning

The absolutely positioned box is vertically centered:

.box {
	position: absolute;
	top: 50%;
	margin-top: -自己高度一半;}

The absolutely positioned box is horizontally centered:

.box {
	position: absolute;
	left: 50%;
	margin-left: -自己宽度一半;}
  • Stacking order z-index attribute

Set absolute positioning The stacking order of elements.
is a positive integer without units. A larger value can suppress a smaller value (that is, the larger value is displayed on the upper layer)

For example:

nbsp;html>
    <meta>
    <meta>
    <meta>
    <title>绝对定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box1 {
            width: 300px;
            height: 300px;
            position: absolute;
            left: 100px;
            top: 100px;
            background-color: antiquewhite;
        }

        .box2 {
            width: 300px;
            height: 300px;
            position: absolute;
            left: 200px;
            top: 200px;
            background-color: lightblue;
        }
    </style>
    <p></p>
    <p></p>

The effect at this time is as follows:

<br/>

At this time we want box1 to be displayed on the upper layer, so we set a z-index attribute.

.box1 {
    width: 300px;
    height: 300px;
    position: absolute;
    left: 100px;
    top: 100px;
    background-color: antiquewhite;
    z-index: 100;}.box2 {
    width: 300px;
    height: 300px;
    position: absolute;
    left: 200px;
    top: 200px;
    background-color: lightblue;
    z-index: 1;}

Look at the effect:
In-depth analysis and understanding of the details of positioning and layout in CSS

  • Usage

Absolute positioning is used to "cover" and "cover" The effect of "hood"
can be used in conjunction with CSS sprites
can be combined with JS to achieve some animations

3. Fixed positioning

3.1) What is fixed positioning

Fixed Positioning: No matter how the page scrolls, it will always take the page as the reference point and be fixed there.

Position descriptors:
left: the distance to the left; right: the distance to the right; top: the distance to the top; bottom: the distance to the bottom

.box {
	position: fixed;
	top: 100px;
	left: 100px;}

3.2) The nature and use of fixed positioning

can be used to achieve that some elements will always appear in front of the current window. For example, the return to top button when browsing a page will always appear in a certain part of the current page. Position

For example:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>固定定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            position: fixed;
            bottom: 20px;
            right: 20px;
            width: 40px;
            height: 40px;
            
            text-align: center;
            line-height: 40px;
            border-radius: 50%;
            background-color: rgba(78, 209, 226, 0.5);
            
            cursor: pointer;
            font-size: 24px;
        }
    </style></head><body>
    <a class="box">^</a>
    <p>
        <img src="/static/imghwm/default1.png"  data-src="https://dummyimage.com/600x400/00bcd4/fff"  class="lazy"   alt="">
    </p>
    <p>
        <img src="/static/imghwm/default1.png"  data-src="https://dummyimage.com/600x400/00bcd4/fff"  class="lazy"   alt="">
    </p>
    <p>
        <img src="/static/imghwm/default1.png"  data-src="https://dummyimage.com/600x400/00bcd4/fff"  class="lazy"   alt="">
    </p></body></html>

The effect is as follows:
When the page goes to the bottom, the position of the return to top button in the lower right corner remains unchanged.
In-depth analysis and understanding of the details of positioning and layout in CSS

If you are interested, you can continue to visit: css video tutorial.

The above is the detailed content of In-depth analysis and understanding of the details of positioning and layout in CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

Feeling Like I Have No Release: A Journey Towards Sane DeploymentsFeeling Like I Have No Release: A Journey Towards Sane DeploymentsApr 23, 2025 am 09:19 AM

Deploying like an idiot comes down to a mismatch between the tools you use to deploy and the reward in complexity reduced versus complexity added.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor