According to w3schools:
Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add padding, borders and margins.
What I'm finding in practice with an <img...>
tag, however, is that whether I specify width and height via CSS, or by using the width
and height
attributes of the <img...>
tag, is that it's not just the size of my content being affected. The entirety of the image content, the padding, and the border are being sized to match my specified width and height.
To put some specific numbers on this:
- I have flag icons measuring 32px * 22px. I want to scale them down 50% to 16px * 11px.
- I want to put 1 px of padding around the flag icons, then a 1 pixel solid light gray border.
- The total screen size of the result including padding and border should be 20px * 15px.
The CSS that works correctly is this:
.flag-image {
width: 20px;
height: 15px;
position: relative;
top: 3px;
padding: 1px;
border: lightgray 1px solid;
}
If it were true, however, that I'm only supposed to be specifying the content size, shouldn't that be 16px and 11px for width and height instead? Why, if I use 16px and 11px, is my flag icon squashed too small? I've taken a screen shot, magnified it, and counted pixels. It's 20px and 15px that give me correct results.
Is this something special about the img
tag? Am I missing or misinterpreting the HTML or CSS specs?