问题
Why does the padding in this example not equal 300px?
#Test{
width:600px;
padding-right:50%;
box-sizing:border-box;
background:#ddd;
}
<div id="Test">
TEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ HTEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ HTEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ HTEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ H
</div>
Where is the browser deriving it's calculation, 50% of what?
In the example below the padding actually becomes smaller as the container of
#Test
becomes larger.I actually want the width of
#Test
to be dynamic however I've fixed it for the purpose of this example.And how do I get 50% padding of
#Test
?
Notes: I am not just looking for a solution but an understanding of the way padding is working here.
回答1:
TL;DR
The padding is calculated according to the parent's width
First, you should note that:
Percentage:
The percentage is calculated with respect to the width of the generated box's containing block [...] (source: w3c, emphasis mine)
This means the padding is calculated according to the parent element width (it's worth to note that in non flexbox layouts, padding top and bottom are also relative to the width of the containing block).
With box-sizing: border-box
When you change the default box-model to border-box, the padding (and border) is included in the width of the element like in your example. So with
width:600px;
padding-right:50%;
box-sizing:border-box;
The right padding must be 50% of parent's width but the overall width of element is 600px wide. The only moment the padding right is 50% of element's width is when parent width = element width
(Note that this can't happen in your example because the parent is body and body has a default margin).
Workaround:
If you want the padding to be 50% of the element's width, in this box model you can:
- set a fixed padding:
width:600px; padding-right: 300px;
- give the element a fluid width :
width:50%; padding-right:25%;
Without box-sizing: border-box
In the default box-model, the padding isn't included in the width of the element so element width = 600px + 50% of parent's width
as you can see in the following example:
#Test{
width:600px;
padding-right:50%;
background:#ddd;
}
<div id="Test">
TEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ HTEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ HTEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ HTEXT ETISI DHOASIHD I SAIDUHSILAUDH LISH ADBHSJADB JHSA DKH ASKDH KSAH DKLJS ADLK ASJKLD KLASH DLSJAH KLS ADKL S JS KSH KD KSJ HDKJSH DKH SDKH SKDH KSJH DKJSH DKJ H
</div>
Workaround:
If you want the padding to be 50% of the element's width, in this box model you can:
- set a fixed padding:
width:300px; padding-right: 300px;
- give the element a fluid width :
width:25%; padding-right:25%;
References:
- The box model MDN, W3C
- The padding property : MDN, W3C
- The box-sizing property : MDN W3C
回答2:
It's in relation to the parent element's width, unless the parent's width depends on this one.
From W3C's CSS 2.1 Specification:
The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'. If the containing block's width depends on this element, then the resulting layout is undefined in CSS 2.1.
来源:https://stackoverflow.com/questions/34748285/how-is-padding-calculated-when-using-percentage