赞
踩
目录
5,将父元素转变为行内块元素,display:inline-block
6,给父元素或者子元素定位都可以,原理同样是使其脱离标准流,不过只能用绝对定位和固定定位
例:
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- }
- .box {
- width: 200px;
- height: 200px;
- background-color: teal;
- /* 给子元素设置margin-top 100px 想让子元素离父元素顶端距离100px,可是高度塌陷导致了父元素离body移动了100px,没达到想要的效果 */
- margin-top: 100px;
- }
- </style>
- </head>
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
- </html>
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- /* 给父元素设置边框 */
- /* border: 1px solid red; */
- /* 或者 以下这种方式给父元素加边框但是不显示线*/
- border: 1px solid transparent;
- }
- .box {
- width: 200px;
- height: 200px;
- background-color: teal;
- /* 给子元素设置margin-top 100px 想让子元素离父元素顶端距离100px,可是高度塌陷导致了父元素离body移动了100px,没达到想要的效果 */
- margin-top: 100px;
- }
- </style>
- </head>
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
- </html>
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- /* 设置padding挤下去 */
- /* 设置padding值后要加边框盒子不然会撑开盒子 */
- box-sizing: border-box;
- padding-top: 100px;
- }
-
- .box {
- width: 200px;
- height: 200px;
- background-color: teal;
- /* 给子元素设置margin-top 100px 想让子元素离父元素顶端距离100px,可是高度塌陷导致了父元素离body移动了100px,没达到想要的效果 */
- /* margin-top: 100px; */
- }
- </style>
- </head>
-
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
-
- </html>
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- /* 给父元素设置溢出隐藏触发bfc机制 */
- overflow: hidden;
- }
-
- .box {
- width: 200px;
- height: 200px;
- background-color: teal;
- /* 给子元素设置margin-top 100px 想让子元素离父元素顶端距离100px,可是高度塌陷导致了父元素离body移动了100px,没达到想要的效果 */
- margin-top: 100px;
- }
- </style>
- </head>
-
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
-
- </html>
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- /* 给父元素或者子元素设置浮动*/
- /* float: left; */
- }
-
- .box {
- width: 200px;
- height: 200px;
- background-color: teal;
- /* 给子元素设置margin-top 100px 想让子元素离父元素顶端距离100px,可是高度塌陷导致了父元素离body移动了100px,没达到想要的效果 */
- margin-top: 100px;
- float: left;
- }
- </style>
- </head>
-
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
-
- </html>
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- /* 转换为行内块 */
- display: inline-block;
- }
-
- .box {
- width: 200px;
- height: 200px;
- background-color: teal;
- margin-top: 100px;
- }
- </style>
- </head>
-
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
-
- </html>
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- /* 给父元素或者子元素绝对定位或者固定定位 */
- /* position: absolute; */
- }
-
- .box {
- /* 给父元素或者子元素绝对定位或者固定定位 */
- position: fixed;
- width: 200px;
- height: 200px;
- background-color: teal;
- margin-top: 100px;
- }
- </style>
- </head>
-
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
-
- </html>
- <!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>Document</title>
- <style>
- .father {
- width: 500px;
- height: 500px;
- background-color: red;
- margin: 0 auto;
- /* 给父元素增加flex或者inline-flex */
- display: flex;
- /* display: inline-flex; */
- }
-
- .box {
- width: 200px;
- height: 200px;
- background-color: teal;
- margin-top: 100px;
- }
- </style>
- </head>
-
- <body>
- <div class="father">
- <div class="box">
-
- </div>
- </div>
- </body>
-
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。