Home Web Front-end JS Tutorial CSS positioning and application scenario analysis

CSS positioning and application scenario analysis

Dec 13, 2017 pm 01:27 PM
css position Case Analysis

We all know that when developing web pages, sometimes we can achieve good results by flexibly using the CSS positioning postion attribute for layout. In this article, we summarized the position knowledge points and commonly used places, hoping to help Friends who learn css can refer to it.

<script>ec(2);</script>
Copy after login

First we explain the postion attribute in detail.

In CSS3, postion is described like this


The summary is as follows:
static is the default layout, set top\left. . Properties will have no effect.
relative is a relative layout, set top\left. . Will be relative to the control in the file.
Absolute is absolute positioning, set top\left. . Will depend on the entire page.
fixed is positioned relative to the browser window, and the set top/left attributes are relative to the position of the browser window.

In addition, after testing my code:
1. If top\left. . The position of one of the default, absolute, and fixed layout properties will change relative to the position of the parent control.
2.relative relative positioning, if there is a parent control, the relative position is the nearest parent control, not the nearest parent control on the same layer. Followed by brother controls.
3.static has no effect on other covering layers.

Then let’s prove the above conclusion through code:

Case 1

HTML:

<p id="zero">
            <p id="one">one</p>
            <p id="two">two</p>
            <p id="tree">tree</p>
</p>
Copy after login

CSS:

        #zero{
                width:200px;
                height: 200px;
                margin: 100px 500px;
                background: black;
    
                z-index: 0;
            }
            #one{
                width: 100px;
                height: 100px;
                position: relative;
                top: 50px;
                left:20px;
                background: red;
                z-index: 1;
            }
            #two{
                width: 100px;
                height: 100px;
                position: absolute;
                top: 190px;
                
                background: yellow;
                z-index: 2;
            }
            #tree{
                width: 100px;
                height: 100px;
                position: fixed;
                top: 250px;
                left: 600px;
                background: deepskyblue;
                z-index: 3;
            }
Copy after login

Here you can see that p with id one is the layout relative to the parent control.

Case 2:

CSS:

                                                                                                             #                                                                         

#It can be seen from here that when relative positioning is relative to the nearest parent control, not the same layer parent control.



Case 3: If there is no parent p:

HTML

       
               #first{
               width: 200px;
                height: 200px;
                background: black;
                margin-top: 100px;
                z-index: 1;
            }
            #second{
                margin-top: 10px;
                margin-left:10px;
                width: 150px;
                height: 150px;
                background: yellow;
                z-index:2;
            }
            #thrid{
                width: 100px;
                height: 100px;
                position:relative;
                background: red;
                top: 30px;
                left: 30px;
                z-index: 1;
            }
Copy after login

CSS




<p id="out"></p>
<p id="out1"></p>
Copy after login



Through this situation, it can be seen that if there is no parent control, the relative positioning is relative to the sibling control.


Description of z-index in CSS3

Common applications in position development

1. Floating windows on both sides of the web page (player, top button, floating advertisement, function button etc.)

2. The navigation bar floats to the top.
3. Hide p to realize the pop-up window function (control the position and appearance of p by setting the positioning and z-index of p)



Among them, 1 and 3 are relatively simple. This can be achieved by setting position=fixed, top left, and z-index, which will not be explained here

Case 2:

Judge the position of the scroll bar by calling the js function, exceeding the navigation When the height of the bar from the top is set, set position to fix to fix the position of the navigation bar. Otherwise, position is static, and maring and other attributes remain unchanged.

JS:


 

 #out{
                margin-top: 50px;
                width: 200px;
                height: 200px;
                background: black;
                z-index: 1;
            }
            
            #out1{
                width: 200px;
                height: 200px;
                background: yellow;
                position: relative;
                z-index: 3;
                top:10px;
            }
Copy after login

HTML:


 
 var mt = 0;
        window.onload = function () {
            var myp = document.getElementById("myp");
            var mt = myp.offsetTop;
            window.onscroll = function () {
                var t = document.documentElement.scrollTop || document.body.scrollTop;
                if (t > mt) {
                    myp.style.position = "fixed";
                    myp.style.margin = "0";
                    myp.style.top = "0";
                }
                else {
                    myp.style.margin = "30px 0";
                    myp.style.position = "static";
                }
            }        
        }
Copy after login



Set appropriately CSS to control the style you want.
Related recommendations:



About the position attribute in css


Comparison of css float attribute and position:absolute


css: The use and definition of list-style-position

The above is the detailed content of CSS positioning and application scenario analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

See all articles