We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
该篇博文是根据慕课网教程——《css定位position》整理而来,内容虽然简单基础,但是整理记录一下,帮助新手的同时也务实一下自己的基础知识。
1、标准流:这是默认的布局方式。特点就是块级元素独占一行,意思就是两个块级或者多个块级元素不能在同一行显示,块级元素能设置宽高;内联元素能够在同一行显示,内联元素不能设置宽高。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> div, p { height: 50px; line-height: 50px; background-color: green; } span, a { height: 50px; line-height: 50px; background-color: green; } </style> </head> <body> <div>我是div</div> <p>我是p</p> <span>我是span</span> <a>我是a</a> </body> </html>
2、position定位属性
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> div { height: 50px; width: 200px; line-height: 50px; background-color: green; position: static; left: 20px; top: 20px; } </style> </head> <body> <div>我是div</div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> div { height: 50px; width: 200px; line-height: 50px; background-color: green; } p { position: relative; top: 0; left: 15px; height: 50px; width: 500px; line-height: 50px; background-color: brown; } </style> </head> <body> <div>我是div</div> <p>我是相对定位的p元素,根据自身所在的位置进行定位</p> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> div { height: 500px; width: 800px; line-height: 500px; background-color: green; margin-left: 55px; } p { position: absolute; top: 0px; left: 55px; height: 50px; width: 500px; line-height: 50px; background-color: brown; } </style> </head> <body> <div> <p>我是绝对定位的p元素,定位规则较复杂</p> </div> </body> </html>
当我们把父元素设置为相对定位或者绝对定位的时候,那么内部绝对定位的子元素就会根据父元素进行定位。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> div { height: 500px; width: 800px; line-height: 500px; background-color: green; margin-left: 55px; position: relative; } p { position: absolute; top: 0px; left: 55px; height: 50px; width: 500px; line-height: 50px; background-color: brown; } </style> </head> <body> <div> <p>我是绝对定位的p元素,定位规则较复杂</p> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> div { height: 500px; width: 800px; line-height: 500px; background-color: green; margin-left: 55px; position: relative; } p { position: inherit; top: 0px; left: 55px; height: 50px; width: 500px; line-height: 50px; background-color: brown; } </style> </head> <body> <div> <p>我是继承父元素相对定位的p元素</p> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> div { width: 100%; height: 3000px; border: 1px solid brown; } p { height: 50px; width: 500px; line-height: 50px; text-align: center; background-color: green; color: #fff; position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <div> <p>我是固定定位元素,相对于浏览器窗口进行定位</p> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> div { width: 300px; height: 300px; background-color: red; position: fixed; left: 33px; top: 55px; } p { width: 200px; height: 200px; background-color: green; position: fixed; left: 155px; top: 200px; } </style> </head> <body> <div>我是div</div> <p>我是p</p> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> div { width: 300px; height: 300px; background-color: red; position: fixed; left: 33px; top: 55px; z-index: 1; } p { width: 200px; height: 200px; background-color: green; position: fixed; left: 155px; top: 200px; } </style> </head> <body> <div>我是div</div> <p>我是p</p> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> div { width: 500px; height: 500px; background-color: red; } p { width: 200px; height: 200px; background-color: green; position: relative; z-index: -5; } h1 { width: 100px; height: 100px; background-color: black; position: absolute; top: 130px; left: 150px; z-index: -5; } </style> </head> <body> <div> <p>我是p</p> <h1></h1> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .parent1 { height: 500px; width: 500px; background-color: red; position: relative; z-index: 10; } .child1 { height: 300px; width: 300px; background-color: green; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } .parent2 { height: 400px; width: 400px; background-color: blueviolet; position: absolute; left: 50px; top: 50px; z-index: 5; } .child2 { height: 200px; width: 200px; background-color: teal; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <div class="parent1"> <div class="child1">child1</div> </div> <div class="parent2"> <div class="child2">child2</div> </div> </body> </html>
The text was updated successfully, but these errors were encountered:
No branches or pull requests
前言
该篇博文是根据慕课网教程——《css定位position》整理而来,内容虽然简单基础,但是整理记录一下,帮助新手的同时也务实一下自己的基础知识。
HTML中三种布局方式
1、标准流:这是默认的布局方式。特点就是块级元素独占一行,意思就是两个块级或者多个块级元素不能在同一行显示,块级元素能设置宽高;内联元素能够在同一行显示,内联元素不能设置宽高。
2、position定位属性
当我们把父元素设置为相对定位或者绝对定位的时候,那么内部绝对定位的子元素就会根据父元素进行定位。
z-index详解
The text was updated successfully, but these errors were encountered: