CSS筆記——padding,margin為百分比計算時的參照對象


div的padding為百分比的兩種情況


padding-top,padding-bottom,margin-top,margin-bottom是百分比時是按照當前元素的父級元素的寬度來計算的

 

1. 當子div的寬度是百分比時

html代碼

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta chaset="utf-8">
<title>無標題</title>
<style type="text/css">
html,body{
margin:0;
padding:0;
height:100%;
}

.container{
width:100%;
}
.row{
width:100%;
}
.row:before ,
.row:after{
  display: block;
  content:'';
  height: 0;
  clear:both;
}
.col-md-4{
  float: left;
  width:33.33%;
}
.blue-bg,
.red-bg,
.yellow-bg{
  padding-top:33.333%;
}
.blue-bg{
background-color: #428bca;
}

.red-bg{
background-color: red;
}

.yellow-bg{
background-color: yellow;
}


</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 blue-bg"></div>
<div class="col-md-4 red-bg"></div>
<div class="col-md-4 yellow-bg"></div>
</div>
</div>
</body>
</html>

效果圖

 

 

2. 當子div的寬度是具體像素時

html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width , initial-scale=1">
<meta chaset="utf-8">
<title>無標題</title>
<style type="text/css">
html,body{
margin:0;
padding:0;
height:100%;
}
          
.row{
width:100%;
}

.row:before , .row:after{
display: block;
content:'';
height: 0;
clear:both;
}

.col-md-4{
float: left;
width:100px;
}

.blue-bg{
padding-top: 100%;
background-color: #428bca;
}

.red-bg{
padding-top: 100%;
background-color: red;
}

.yellow-bg{
padding-top: 100%;
background-color: yellow;
}


</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4 blue-bg"></div>
<div class="col-md-4 red-bg"></div>
<div class="col-md-4 yellow-bg"></div>
</div>
</div>
</body>
</html>

效果圖

 

 


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com