How to change a DIV padding without affecting the width/height ?
Solution is to wrap your padded div, with fixed width outer div
HTML
<div class="outer">
<div class="inner">
<!-- your content -->
</div><!-- end .inner -->
</div><!-- end .outer -->
CSS
.outer, .inner {
display: block;
}
.outer {
/* specify fixed width */
width: 300px;
padding: 0;
}
.inner {
/* specify padding, can be changed while remaining fixed width of .outer */
padding: 5px;
}
How apply padding in HTML without increasing the div size?
Adding an inner div is, more or less, a good solution. There's the box-sizing
property, but it lacks support.
Here's an article on the subject:
http://www.quirksmode.org/css/box.html
And here's a polyfill (patch) for older IE versions -- although I'm not sure how it affects performance; I'd recommend using it carefully.
https://github.com/Schepp/box-sizing-polyfill
padding increases width and height of div
You can use CSS3 box-sizing
property to do this:
box-sizing: border-box;
http://css-tricks.com/box-sizing/
Supported in IE8+ but some browsers may require a prefix:
http://caniuse.com/css3-boxsizing
How do I add padding without affecting container?
You should use the CSS property box-sizing
.
Setting this to border-box
. It will count the padding and the border as part of the width.
So, in the example below. The width and height of the element will continue being 100px
even with the 25px
padding and 1px
border
.my-input-element {
box-sizing: border-box;
width: 100px;
height: 100px;
padding: 25px;
border: 1px solid #000;
}
DIV padding interfering with width
Simply keep the element a block element and you won't need width:100%
or box-sizing:border-box
(by the way it's good to keep it to avoid any future issue related to padding/border combined with width)
.entryrow { position: relative; height: 100px; background: linear-gradient(#2c647b, #191654); padding: 5px; margin: 5px; border: 1px solid #ccc; cursor: pointer;}
<div style="background:black;overflow:hidden"> <div class="entryrow"> Blubb </div></div>
How to make an element width: 100% minus padding?
box-sizing: border-box
is a quick, easy way to fix it:
This will work in all modern browsers, and IE8+.
Here's a demo: http://jsfiddle.net/thirtydot/QkmSk/301/
.content {
width: 100%;
box-sizing: border-box;
}
The browser prefixed versions (-webkit-box-sizing
, etc.) are not needed in modern browsers.
padding is getting added to width of div
First of all you need to learn CSS box-model
This states that whatever padding, border, margin
you add to you element does count outside it, so for example the element is of 200px width
and 100px height
, if you add padding
say 5px
than the width
and height
will be 205px
and 105px
respectively, so inorder to workaround with this you need to use CSS3 box-sizing
property, but as still it is CSS3
property and if IE is the main thing you want to supprt, I suggest you to resize the elements according to your needs
So for example a div with these styles
div {
height: 100px;
width: 200px;
padding: 5px;
}
You can re-size the above as
div {
height: 95px;
width: 195px;
padding: 5px;
}
CSS3 box-sizing
Reference
Related Topics
What Is The Maximum Allowed Negative Value for CSS Left Property
Pagespeed-Penalty for Using Srcset
How to Add Custom Breakpoints in Bootstrap4 and How to Use Responsive Breakpoint Mixins in SCSS
How to Show Image in Right Side of Text Box Instead of Left Side
Rotate Child Element Around The Parent Element with Transition
Position Fixed Not Working in Safari Version 8.0.2
Container Div Ignores Height of Floated Elements
CSS3 Transform: Translate3D Doesn't Affect The Z-Axis
Adding Background Image to Div Using CSS
Auto-Generate Div Ids for Bootstrap Accordion Menu Binding with Knockout Js
How to Specify The System's Default Serif and Sans-Serif Font-Family
Use CSS Variable with SCSS Darken and Ligthen Filter
How to Remove Upload Cancel Button from <P:Fileupload>
CSS Text Replace with Image, Need Hyperlink
Why Is The Top Border of This Inline Element Not Displaying and Why Does Using Float Correct This