-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
  • 28,550
  • 17
  • 109
  • 176

4 Answers4

2

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

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
m_sultan
  • 433
  • 5
  • 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,379
  • 13
  • 26
0

use just

 width: auto;

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

enter image description here

Manjuboyz
  • 6,978
  • 3
  • 21
  • 43
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
  • 115
  • 3