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
实现一个div垂直居中, 其距离屏幕左右两边各10px, 其高度始终是宽度的50%。同时div中有一个文字A,文字需要水平垂直居中。
高度始终是宽的的50%;那么要怎么实现呢? 这就要用到padding了,因为padding百分比是根据宽度来计算的;
父元素相对定位,那绝对定位下的子元素宽高若设为百分比,是相对谁而言的? 相对于父元素的(content + padding)值, 注意不含border 延伸:如果子元素不是绝对定位,那宽高设为百分比是相对于父元素的宽高,标准盒模型下是content, IE盒模型是content+padding+border。
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1"> <title>waiting</title> <style> ul,li { list-style: none; margin: 0; padding: 0; } html,body { height: 100%; padding: 0; margin: 0;} .wrap { background: #eee; width: 100%; padding: 0 10px; display: flex; height: 100%; box-sizing: border-box; } .test { background: red; width: 100%; padding-top: 50%; margin: auto; position: relative; display: flex; } .test>div{ color:#fff; position: absolute; top: 0; left: 0; height: 100%; width: 100%; display: flex; justify-content: center; align-items: center; } </style> </head> <body> <div class="wrap clearFloat"> <div class="test"> <div>A</div> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="IE=edge, chrome=1"> <title>waiting</title> <style> ul,li { list-style: none; margin: 0; padding: 0; } html,body { height: 100%; padding: 0; margin: 0;} .wrap { background: #eee; width: 100%; height: 100%; padding: 0 10px; position: relative; box-sizing: border-box; display: flex; } .test { background: red; width: 100%; height: calc(50% - 10px); margin: auto; color: white; display: flex; align-items: center; justify-content: center; } </style> </head> <body> <div class="wrap clearFloat"> <div class="test">A</div> </div> </body> </html>
The text was updated successfully, but these errors were encountered:
No branches or pull requests
要求:
实现一个div垂直居中, 其距离屏幕左右两边各10px, 其高度始终是宽度的50%。同时div中有一个文字A,文字需要水平垂直居中。
关键点:
高度始终是宽的的50%;那么要怎么实现呢?
这就要用到padding了,因为padding百分比是根据宽度来计算的;
用calc
The text was updated successfully, but these errors were encountered: