Home >Web Front-end >CSS Tutorial >In-depth analysis and understanding of the details of positioning and layout in CSS

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

WBOY
WBOYOriginal
2021-12-22 18:32:081770browse

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="https://dummyimage.com/600x400/00bcd4/fff" alt="">
    </p>
    <p>
        <img src="https://dummyimage.com/600x400/00bcd4/fff" alt="">
    </p>
    <p>
        <img src="https://dummyimage.com/600x400/00bcd4/fff" 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