Home Web Front-end CSS Tutorial What are the uses of position attribute in css

What are the uses of position attribute in css

Mar 16, 2020 am 10:42 AM
css position

What are the uses of position attribute in css

position attribute

The position attribute specifies the type of positioning method used for the element (static, relative, fixed, absolute, or sticky). There are five different values:

•static
•relative
•fixed
•absolute
•sticky

Then use top, bottom, The left and right attributes position the element. However, these properties will have no effect unless the position property is set first. They also work differently depending on the position value.

position:static;

By default, HTML elements are positioned static. Static positioned elements are not affected by the top, bottom, left and right attributes. The element position:static; is not positioned in any special way; it is always positioned according to the normal flow of the page:

(Recommended tutorial: CSS Getting Started Tutorial)

This< ;div> element's position:static;

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css</title>
    <style>
        div.static {
            position: static;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2>position: static;</h2>
<p>一个位置为position:static; 没有任何特殊的定位; 它是始终根据页面的正常流程定位:</p>
<div class="static">
    这个div元素的位置为:static;
</div>
</body>
</html>
Copy after login

position:relative;

An element with position:relative; positioned relative to its normal position. Setting the top, bottom, left, and right properties of a relatively positioned element will cause it to adjust away from its normal position. Other content will not be adjusted to fit any white space left by the element.

The position of this

element: relative;

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css</title>
    <style>
        div.relative {
            position: relative;
            left: 30px;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2>position: relative;</h2>
<p>position:relative的元素; 相对于其正常位置定位:</p>
<div class="relative">
    这个div元素有position: relative;
</div>
</body>
</html>
Copy after login

position:fixed;

The element position:fixed;is positioned relative to the viewport , which means it always stays in the same position even if the page scrolls. The top, bottom, left and right attributes are used to position elements. Fixed elements leave no gaps in the page where they would normally be. Notice the fixed element in the lower right corner of the page.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css教程(jc2182.com)</title>
    <style>
        div.fixed {
            position: fixed;
            bottom: 0;
            right: 0;
            width: 300px;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2>position:fixed;</h2>
<p>position:fixed; 相对于视口定位,这意味着即使页面滚动,它也始终保持在同一位置:</p>
<div class="fixed">
    这个div元素有position: fixed;
</div>
</body>
</html>
Copy after login

position:absolute;

Elements with position:absolute; are positioned relative to their most recently positioned ancestor (rather than positioned relative to the viewport, as in fixed). However; if an absolutely positioned element has no positioned ancestor, it will use the document body and move with the page scroll. Note: The position of a "positioned" element is any element except static.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css教程(jc2182.com)</title>
    <style>
        div.relative {
            position: relative;
            width: 400px;
            height: 200px;
            border: 3px solid #73AD21;
        }
        div.absolute {
            position: absolute;
            top: 80px;
            right: 0;
            width: 200px;
            height: 100px;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2>position: absolute;</h2>
<p>position:absolute;的元素; 相对于最近定位的祖先定位(而不是相对于视口定位,如fixed):</p>
<div class="relative">这个div元素有 position: relative;
    <div class="absolute">这个div元素有 position: absolute;</div>
</div>
</body>
</html>
Copy after login

position:sticky;

position:sticky;Positions elements based on the user's scroll position. Sticky elements toggle between relative and fixed depending on scroll position. It is positioned relatively until the given offset position is met in the viewport - then it "sticks" in place (as in position:fixed).

Note: Internet Explorer, Edge 15 and earlier versions do not support sticky positioning. Safari requires the -webkit- prefix (see example below). You must also specify at least one of top, right, bottom, or left for sticky positioning to work.

In this example, top: 0, the sticky element sticks to the top of the page when you reach its scroll position.

<!DOCTYPE html>
<html>
<head>
    <style>
        div.sticky {
            position: -webkit-sticky;
            position: sticky;
            top: 0;
            padding: 5px;
            background-color: #cae8ca;
            border: 2px solid #4CAF50;
        }
    </style>
</head>
<body>
<p>尝试在此框架内<b>滚动</b>以了解粘性定位的工作原理。</p>
<p>注意:IE/Edge15及更早版本不支position: sticky;。</p>
<div class="sticky">我很粘!</div>
<div style="padding-bottom:2000px">
    <p>在此示例中,当您到达其滚动位置时,粘性元素会粘到页面顶部(顶部:0)。</p>
    <p>向上滚动以消除粘性。</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>
</body>
</html>
Copy after login

Recommended related video tutorials: css video tutorial

The above is the detailed content of What are the uses of position attribute in css. 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)

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.

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 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 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 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 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