-1

When addind a css of something like

#mydiv {
    width: 100%;
    padding: 20px;
    background-color: red; /* only for visualization*/
}
<div id="mydiv" >my div<div/>

It will overflow the page.

How can I make this "100 percentage width" not overflow because of the padding?

I don't want to hide the overflow, but I want to make the width a little less than 100% but without having to hardcode some width.

Something like width: 100% - padding

Vencovsky
  • 23,092
  • 9
  • 82
  • 131

4 Answers4

2

Add box-sizing: border-box to the div's css

Temani Afif
  • 211,628
  • 17
  • 234
  • 311
m_sultan
  • 403
  • 4
  • 14
0

By using calc, you can remove the padding from 100%

#mydiv {
    width: calc(100% - 40px)
    padding: 20px;
    background-color: red; /* only for visualization*/
}
<div id="mydiv" >my div<div/>
danielarend
  • 1,216
  • 11
  • 23
0

use just

 width: auto;

no need to provide 100% it is by default uses full.

enter image description here

Manjuboyz
  • 6,655
  • 3
  • 18
  • 39
0

If div contain display property as Block no need to give width 100%, else add display property as block or flex

width:auto;
display:block;

else

display:flex;
Palani samy
  • 105
  • 3