2017-06-09 17 views
2

CSS中的border-box和content-box有什麼區別? 我不清楚這兩個框之間。例如, box-sizing:border-box;box-sizing:content-box; 兩種樣式的輸出看起來相似。CSS中的border-box和content-box有什麼區別?

+0

它隻影響'width'和'height'屬性的結果,再有就是隻有當你有邊距或邊界的差異。 –

+0

https://www.w3schools.com/cssref/tryit.asp?filename=trycss3_box-sizing –

+0

https://developer.mozilla.org/en/docs/Web/CSS/box-sizing?v=example should answer你的問題。 –

回答

5

雖然盒子尺寸邊框;使用人們已經與Internet Explorer關聯的框模型,其中填充和邊框的尺寸包含在元素的尺寸中。 enter image description here (圖像source

實施例:

enter image description here (圖像source

演示添加。

$("#content").on("click", function() { 
 
    $("*").css("box-sizing", $(this).text()); 
 
}); 
 

 
$("#border").on("click", function() { 
 
    $("*").css("box-sizing", $(this).text()); 
 
});
.parent { 
 
    width: 50%; 
 
    border: 5px solid #E18728; 
 
    float: left; 
 
} 
 

 
.child { 
 
    width: 90%; 
 
    padding: 20%; 
 
    border: 4px solid black; 
 
    margin: .5em auto; 
 
} 
 

 
.twins { 
 
    width: 50%; 
 
    padding: 1em; 
 
    border: 4px solid black; 
 
    float: left; 
 
} 
 

 

 
/* styling for Pen, not related to box-sizing or layout */ 
 

 
body { 
 
    font-family: sans-serif; 
 
    font-size: 1.1em; 
 
} 
 

 
.buttons { 
 
    margin-bottom: .5em; 
 
} 
 

 
p:not(.intro) { 
 
    text-align: center; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="buttons"> 
 
    <p class="intro">Click the <code>border-box</code> button to fix the layout with the power of Box Sizing!</p> 
 
    <button id="content">content-box</button> 
 
    <button id="border">border-box</button> 
 
</div> 
 
<div class="parent"> 
 
    <p>Parent div with 50% width.</p> 
 
    <div class="child"> 
 
    <p>Child div with 90% width, 4px black border, and 20% padding </p> 
 
    </div> 
 
    <div class="twins"> 
 
    <p>Child div with 50% width, 4px black border, and 1em padding</p> 
 
    </div> 
 
    <div class="twins"> 
 
    <p>Child div with 50% width, 4px black border, and 1em padding</p> 
 
    </div> 
 
</div>

+0

謝謝先生!我明白了。 –

+0

很高興你明白。 – Hash

+0

你的圖片中有一個錯誤的值,它不應該是右邊的圖(邊框)上的'140px',它應該是'100px',因爲它在左邊 – LGSon