search
HomeWeb Front-endCSS TutorialHow to use css position

How to use css position

Nov 21, 2017 pm 04:47 PM
cssposition

We often use position for hierarchical absolute positioning, so how can we master the use of CSS to manipulate the position attribute? Today we are here to study it with you.

position syntax and structure

position syntax:

position : static absolute relative

position parameters:

static : There is no special positioning, the object follows HTML positioning rules

absolute: Drag the object out of the document flow and use left, right, top, bottom and other attributes for absolute positioning. And its cascading is defined through the css z-index attribute. At this time, the object does not have margins, but still has fillers and borders

relative: The object cannot be stacked, but will be offset in the normal document flow based on left, right, top, bottom and other attributes

position description:

Setting the positioning method of the object can make the layout layer easy to position absolutely and control the box object more accurately.

The actual use of position

Absolute positioning position is used to position box objects. Sometimes there are several small objects in a layout, and it is not easy to use css padding and css margin for relative positioning. At this time we can use absolute positioning to easily do it. Especially for the irregular layout of several small boxes in a box, it is very convenient for us to use position absolute positioning to lay out objects at this time.

Absolute positioning position demonstration is suitable for images and irregular layouts. You can use position:absolute;position:relative for absolute positioning.

Absolute positioning and float cannot be used at the same time, such as a large box. Some of them are absolute positioning, and some use css float floating positioning. In this way, the IE6 browser will not display these absolute positioning and relative positioning in the changed object. This can also be regarded as IE6 CSS HACK. Just be careful not to mix them.

Absolute positioning usage conditions

position:absolute;position:relativeThe use of absolute positioning is usually the parent defines position:relative positioning, the child defines the position:absolute absolute positioning attribute, and the child uses Left or right and top or bottom perform absolute positioning.

.div{position:relative} definition, usually it is best to define CSS width and CSS height

.div-a{position:absolute;left:10px;top:10px} defined here The distance from the left side of the parent is 10px, and the distance from the top of the parent is 10px

or

.div-a{position:absolute;right:10px;bottom:10px} defined here The distance to the right of the parent is 10px, and the distance to the bottom of the parent is 10px

corresponds to the HTML structure

<div class="div">
    <div class="div-a"></div>
</div>

This way, "div-a" is absolutely positioned in the parent "div" box Inside.

Note that left (left) and right (right) can only choose one definition in an object, and bottom (bottom) and top (top) can also only choose one definition in an object.

position application case

Here is an example of applying position absolute positioning. We set the css border of an outermost box to red, css width to 400px, css height to 200px, and the interior contains 2 In order to use absolute positioning for these two boxes, the CSS name of the first box is "div-a", its width is 100px, the background color is black, the height is 100px, the positioning distance is 10px from the parent, and the distance from the left is 10px; the second box CSS class is named "div-b", its width and height are 50px respectively, css background color is blue, the distance from the bottom of the parent is 13px, and the distance from the right of the parent is 15px.

1. The css code is as follows

<style> 
.div{ position:relative;width:400px;height:200px; 
border:1px solid #000} 
/* 定义父级position:relative 为就认为是绝对定位声明吧 */ 
.divc-a{ position:absolute;width:100px;height:100px; 
left:10px;top:10px;background:#000} 
/* 使用绝对定位position:absolute样式 并且使用left top进行定位位置 */ 
.div-b{ position:absolute;width:50px;height:50px; 
right:15px;bottom:13px;background:#00F} 
/* 使用绝对定位position:absolute样式 并且使用right bottom进行定位位置 */ 
</style>

html code snippet

<div class="div"> 
    <div class="div-a"></div> 
    <div class="div-b"></div> 
</div>

There are only so many ways to use the position attribute of CSS. Friends who need it can save it. Please continue. Follow this site for other updates.


Related reading:

Several examples of small functions made with pure CSS


Commonly used link tag annotations in HTML


Why sometimes DIV CSS fails to load

The above is the detailed content of How to use css position. 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
Anchor Positioning Just Don't Care About Source OrderAnchor Positioning Just Don't Care About Source OrderApr 29, 2025 am 09:37 AM

The fact that anchor positioning eschews HTML source order is so CSS-y because it's another separation of concerns between content and presentation.

What does margin: 40px 100px 120px 80px signify?What does margin: 40px 100px 120px 80px signify?Apr 28, 2025 pm 05:31 PM

Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

What are the different CSS border properties?What are the different CSS border properties?Apr 28, 2025 pm 05:30 PM

The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

What are CSS backgrounds, list the properties?What are CSS backgrounds, list the properties?Apr 28, 2025 pm 05:29 PM

The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

What are CSS HSL Colors?What are CSS HSL Colors?Apr 28, 2025 pm 05:28 PM

Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

How can we add comments in CSS?How can we add comments in CSS?Apr 28, 2025 pm 05:27 PM

The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

What are CSS Selectors?What are CSS Selectors?Apr 28, 2025 pm 05:26 PM

The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

Which type of CSS holds the highest priority?Which type of CSS holds the highest priority?Apr 28, 2025 pm 05:25 PM

The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools