Home Web Front-end HTML Tutorial CSS positioning and cascading_html/css_WEB-ITnose

CSS positioning and cascading_html/css_WEB-ITnose

Jun 24, 2016 pm 12:03 PM
css position cascade

position: static (static positioning)

When the position attribute is defined as static, the element can be defined as a static position. The so-called static position is where each element should be in the HTML document flow. Some locations

podisition positioning issues. Therefore, when the position attribute is not defined, it does not mean that the element does not have its own position. It will follow the default display as a static position. In the static positioning state, its position cannot be changed through the coordinate values ​​​​(top, left, right, bottom). .

position: absolute (absolute positioning)

When the position attribute is defined as absolute, the element will be separated from the document flow and will not be affected by the document flow at all. It will be positioned according to the coordinates of a certain reference object. position. When the absolutely positioned element does not explicitly specify the top, right, bottom, and left positioning attributes, it is still not out of the document flow and is affected by the document flow. It has the characteristics of relative positioning, but its position in the document flow The location no longer exists. If absolute positioning only displays the positioning x-axis or Y-axis, then it only has the positioning capability in this direction, and the other direction still displays the relative positioning feature.

Coordinate value:

top: Indicates the distance from the top outer wall of the positioning element to the top inner wall of the reference element

right: Indicates the distance from the right outer wall of the positioning element to the right side of the reference element The distance of the inner wall

left: represents the distance from the left outer wall of the positioning element to the left inner wall of the reference element

bottom: represents the distance from the bottom outer wall of the positioning element to the bottom inner wall of the reference element

<div id="box">box                                                            <div id="boxA">boxA</div>    <div id="boxB">boxB   <div id="boxC">boxC</div>   <div id="boxD">boxD</div>  </div> </div>
Copy after login

#box{ margin:40px auto; width:400px; height:400px; border:2px red solid;}#boxA{ position:absolute; top:100px; left:100px; width:50px; height:50px; background: #3E7DB0;}
Copy after login

The big box box is centered 40px from the top in the entire web page, but boxA is positioned absolute due to position Positioning, it is out of the big box, 100px from the left and 100px from the top of the entire web page. When the element is defined as absolute positioning, its position can be accurately positioned by combining its coordinate attributes (top, left, bottom, right)

position: relative (relative positioning)

Relative positioning is like a compromise method, which is to find a balance between static positioning and absolute positioning. The so-called relative positioning is to keep the applied elements from the document flow. , but can be offset using the original position as a reference through the coordinate value.

Coordinate value:

top: represents the distance from the top outer wall of the positioned element to the top outer wall of the original position

right: represents the distance from the right outer wall of the positioned element to the original position element The distance from the right outer wall

left: represents the distance from the left outer wall of the positioned element to the left outer wall of the original position element

bottom: represents the distance from the bottom outer wall of the positioned element to the bottom outer wall of the original position element

1 <div id="box">2      <div id="boxA">boxA</div>3      <div id="boxB">boxB4       <div id="boxC">boxC</div>5       <div id="boxD">boxD</div>6         </div>7     </div>
Copy after login

#box{ margin:40px auto; width:400px; height:400px; border:2px red solid;}#boxA{ position:relative; top:100px; left:100px; width:50px; height:50px; background: #3E7DB0;}#boxB{ width:50px; height:150px; background: #B9C8C5;}#boxC{ width:50px; height:50px; background: #1D92C8;}#boxD{ width:400px; height:50px; background: #086499;}
Copy after login

The big box box is centered 40px from the top in the entire web page. When the element boxA is positioned Defined as relative positioning, it is offset relative to its own position. According to the original position, it is 100px left and 100px top. When it encounters document flow boxD, it will cover boxD. Although relative positioning deviates from the original position, the space occupied by its original position is still retained and is not occupied by other elements.

position: fixed (fixed positioning)

Fixed positioning is a special form of absolute positioning. It uses the browser window as a reference to define web page elements. If you define an element to be fixed is displayed, the element is no longer affected by the document flow. He always uses the browser window to position the display position of his display. No matter how the browser window scrolls or the size of the browser window changes, the element will be displayed in the browser window. In layman's terms, the four sides of the browser window are used as the coordinate system to position the element.

1 <div id="box">box </div> 2 <div id="fixed">fixed</div>
Copy after login

 1 #box{ 2  margin:40px auto; 3  width:400px; 4  height:400px; 5  border:2px red solid; 6  position:fixed; 7 left:100px; 8 top:100px;  9 }10 #fixed{11  height:4000px;12 }
Copy after login

When the box is defined as fixed positioning, the box will always be within the browser window, fixed The properties can also be used to control the positioning of different borders from the wanderer through left, right, top, and bottom.

Relative and absolute positioning:

<div id="box">box  <div id="boxA">boxA</div>  <div id="boxB">boxB   <div id="boxC">boxC</div>   <div id="boxD">boxD</div>  </div> </div>
Copy after login

如果box把 position属性定义为relative,只有它的子元素boxA和boxB position属性定义为absolute才能相对box定位,而他的孙子元素boxC和boxD把position定义为absolute并不能相对box来定位。但是如果把boxB的position属性定义为relative,它底下的子元素boxC和boxD就能相对于父元素boxB来定位,所以,把position属性定义为relative的元素,只有它的子元素才能相对它定位,孙子元素并不能相对它定位。相对定位与绝对定位的结合使用可以更加精确的控制网页元素,设计出更强大的布局效果。

position属性定位小工具: www.linxz.de/css_tool/position_demo.html

层叠:

css可以通过 z-index 属性来排列不同定位元素之间的层叠顺序,该属性可以设置任何整数值,数值越大,所排列的顺序越靠前。

1  <div id="box">box2   <div id="boxA">boxA</div>3   <div id="boxB">boxB</div>4   <div id="boxC">boxC</div>5  </div>
Copy after login

 1 #boxA,#boxB,#boxC{ 2  width:100px; 3  height:200px; 4  position:absolute; 5 } 6 #boxA{ 7  background: #086499; 8  z-index:1; 9  left:100px;10 }11 #boxB{12  top:50px;13  left:50px;14  background: #B9C8C5;15  z-index:2;16 }17 #boxC{18  top:100px;19  background: #1D92C8;20  z-index:3;21 }
Copy after login

boxC排在最上面,boxB在最中间,boxA在最后。z-index的值越大越靠前。

第一次写博客,写的不好,请各位看官多多指正。  

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

See all articles