Home Web Front-end CSS Tutorial How to use css position

How to use css position

Nov 21, 2017 pm 04:47 PM
css position

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>
Copy after login

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>
Copy after login

html code snippet

<div class="div"> 
    <div class="div-a"></div> 
    <div class="div-b"></div> 
</div>
Copy after login

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

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

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.

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

See all articles