1. 程式人生 > >【問題】父套子時,子元素的外邊距傳遞給父元素解決辦法

【問題】父套子時,子元素的外邊距傳遞給父元素解決辦法

css charset wid 阻止 nbsp tab gree back margin

  • 使用空的<table>標簽可以隔離父子元素的外邊距,阻止外邊距的重疊(直接添加會產生 無用標簽,要在CSS中添加屬性)
 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <style type="text/css">
 7             .box1{
 8                 height
: 300px; 9 width: 200px; 10 background-color: coral; 11 } 12 .box2{ 13 height: 200px; 14 width: 200px; 15 background-color: greenyellow; 16 margin-top: 30px; 17 } 18 .box1:before
{ 19 content: ""; 20 display: table; 21 } 22 </style> 23 </head> 24 <body> 25 <div class="box1"> 26 <div class="box2"> 27 28 </div> 29 </div
> 30 31 </body> 32 </html>

【問題】父套子時,子元素的外邊距傳遞給父元素解決辦法