Home >Web Front-end >CSS Tutorial >In-depth analysis and understanding of the details of positioning and layout in CSS
This article brings you relevant knowledge about CSS positioning and layout. Let’s take a look at what relative positioning, absolute positioning and fixed positioning are, the different element properties and uses, and other knowledge. I hope it will be helpful to everyone.
Relative positioning: The box can be adjusted according to its original position (Achieved through position descriptors).
Position descriptors:
left: move to the right; right moves to the left; top moves down; bottom moves up
(When the value inside is a negative number, Move in the opposite direction)
For example:
Original:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>相对定位</title> <style> * { margin: 0; padding: 0; } p { width: 500px; height: 500px; border: 1px solid #000; margin: 50px auto; } p { width: 100px; height: 100px; background-color: lightblue; position: relative; top: 50px; left: 50px; } </style></head><body> <p> <p></p> </p></body></html>
Set p to relative positioning:
p { width: 100px; height: 100px; background-color: lightblue; position: relative; top: 50px; left: 50px;}
Properties
Purpose
For example:
nbsp;html> <meta> <meta> <meta> <title>相对定位</title> <style> * { margin: 0; padding: 0; } nav { width: 780px; height: 50px; margin: 40px auto; } nav ul { list-style: none; } nav ul li { float: left; width: 156px; height: 50px; line-height: 50px; text-align: center; } nav ul li a { display: block; width: 156px; height: 50px; background-color: lightcyan; color: #000; text-decoration: none; } nav ul li a:hover { border-top: 3px solid red; } </style> <nav> <ul> <li> <a>导航一</a> </li> <li> <a>导航二</a> </li> <li> <a>导航三</a> </li> <li> <a>导航四</a> </li> <li> <a>导航五</a> </li> </ul> </nav>
The effect at this time is like this:
You will find that when the mouse is hovering over it, the navigation area will sink.
After we set relative positioning for it and fine-tuned it:
nav ul li a:hover { border-top: 3px solid red; position: relative; top: -3px;}
This will solve the problem just now
Absolute positioning: The box describes its position with coordinates and has its own absolute position.
Absolutely positioned reference box:
An absolutely positioned box will use the nearest box with positioning attributes among its ancestor elements as the reference point.
This box is usually positioned relatively, so it is also called "the son must be the father".
Position descriptors:
left: the distance to the left; right: the distance to the right; top: the distance to the top; bottom: the distance to the bottom
For example:
nbsp;html> <meta> <meta> <meta> <title>绝对定位</title> <style> * { margin: 0; padding: 0; } .box { position: absolute; width: 500px; height: 300px; left: 200px; top: 100px; background-color: antiquewhite; } </style> <p> </p>
The absolutely positioned box is vertically centered:
.box { position: absolute; top: 50%; margin-top: -自己高度一半;}
The absolutely positioned box is horizontally centered:
.box { position: absolute; left: 50%; margin-left: -自己宽度一半;}
Set absolute positioning The stacking order of elements.
is a positive integer without units. A larger value can suppress a smaller value (that is, the larger value is displayed on the upper layer)
For example:
nbsp;html> <meta> <meta> <meta> <title>绝对定位</title> <style> * { margin: 0; padding: 0; } .box1 { width: 300px; height: 300px; position: absolute; left: 100px; top: 100px; background-color: antiquewhite; } .box2 { width: 300px; height: 300px; position: absolute; left: 200px; top: 200px; background-color: lightblue; } </style> <p></p> <p></p>
The effect at this time is as follows:
<br/>
At this time we want box1 to be displayed on the upper layer, so we set a z-index attribute.
.box1 { width: 300px; height: 300px; position: absolute; left: 100px; top: 100px; background-color: antiquewhite; z-index: 100;}.box2 { width: 300px; height: 300px; position: absolute; left: 200px; top: 200px; background-color: lightblue; z-index: 1;}
Look at the effect:
Absolute positioning is used to "cover" and "cover" The effect of "hood"
can be used in conjunction with CSS sprites
can be combined with JS to achieve some animations
Fixed Positioning: No matter how the page scrolls, it will always take the page as the reference point and be fixed there.
Position descriptors:
left: the distance to the left; right: the distance to the right; top: the distance to the top; bottom: the distance to the bottom
.box { position: fixed; top: 100px; left: 100px;}
can be used to achieve that some elements will always appear in front of the current window. For example, the return to top button when browsing a page will always appear in a certain part of the current page. Position
For example:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>固定定位</title> <style> * { margin: 0; padding: 0; } .box { position: fixed; bottom: 20px; right: 20px; width: 40px; height: 40px; text-align: center; line-height: 40px; border-radius: 50%; background-color: rgba(78, 209, 226, 0.5); cursor: pointer; font-size: 24px; } </style></head><body> <a class="box">^</a> <p> <img src="https://dummyimage.com/600x400/00bcd4/fff" alt=""> </p> <p> <img src="https://dummyimage.com/600x400/00bcd4/fff" alt=""> </p> <p> <img src="https://dummyimage.com/600x400/00bcd4/fff" alt=""> </p></body></html>
The effect is as follows:
When the page goes to the bottom, the position of the return to top button in the lower right corner remains unchanged.
If you are interested, you can continue to visit: css video tutorial.
The above is the detailed content of In-depth analysis and understanding of the details of positioning and layout in CSS. For more information, please follow other related articles on the PHP Chinese website!