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

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

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.

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

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.

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

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.

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!
