0

In How is padding calculated when using percentage (%)? was stated, that padding is calculated according to parent's width.

I now have the following assignment CSS-Code

.img_tutorial_full_width
{
    background-color : #3E3E42;

    margin           : 0;
    border           : 0;
    padding          : 1%;

    width            : 99%;
}

which I would expect, when applied to an <img>-Tag like this:

<img class="img_tutorial_full_width" src="../img/example.jpg"></img>

to stretch the container over the whole width, since

margin=border := 0 + padding := 1% + content := 99% = 100%

However, the browser shows scrollbars, that are increasing, the wider the window is. Where is the conceptual mistake?

Benj
  • 889
  • 1
  • 14
  • 31

2 Answers2

0

By default images are inline element. Inline elements ignores height and width properties. Just add display: block; or display: inline-block to the .img_tutorial_full_width class and it should work just fine.

Also please note, the padding-top, padding-bottom, margin-top and margin-bottom can be applied, but these have no effects on the inline elements. For detail explanation, you can read this article

Mishel Tanvir Habib
  • 1,112
  • 11
  • 16
0

There is 100% Total Width. padding: 1%; takes 1% on the left and 1% on the right, so 2% in total

.img_tutorial_full_width
{
    background-color : #3E3E42;

    margin           : 0;
    border           : 0;
    padding          : 1%;

    width            : 98%;
}

works, because 98% + 2 * 1% = 100%

Benj
  • 889
  • 1
  • 14
  • 31