Reputation: 1093
I’m trying to use padding in a div and which is wrapped by another div (‘container’), apologize for this simple question, but I couldn’t find an answer.
As long as I don’t use padding:10px;
the structure of the page is stable.
i.e. the right div is floating to the left (‘content’ is aside ‘sidebar’).
<html>
<head>
<style type="text/css">
.container {
max-width: 960px;
background-color: RED;
height: 200px;}
.sidebar {
background-color: GREEN;
width: 30%;
float: left;}
.content {
background-color: YELLOW;
float: left;
width: 70%;
padding: 10px;}
</style>
</head>
<body>
<div class="container">
<div class="sidebar">
<p>text in sidebar</p>
</div>
<div class="content">
<p>text in content</p>
</div>
</div>
</body>
</html>
When using padding:10px;
the right div (‘content’) goes down under ‘sidebar’ instead floating to the side of ‘sidebar’.
I’ve tried clear: left;
(right, both) but it doesn’t help me.
Any solution for the above and which is not particular style to <p>
,would be appreciated.
Upvotes: 5
Views: 38774
Reputation: 502
All the other answers (to date 10sep2020) concentrate on the fact that adding the padding to the .content DIV makes it larger than the required 70% causing it to be pushed under the .sidebar DIV and then trying to get the DIV size to be 70% including the padding. The simplest answer is to NOT pad the .content DIV (so it stays at 70% of the .container DIV) but to pad the P inside it. Without a width specification, the inner P is 100% of the DIV INCLUDING the padding thus the text of the P will compress accordingly (to 100% of the .content DIV - 10px on each side). So change
.content {
background-color: YELLOW;
float: left;
width: 70%;
padding: 10px;}
to
.content {
background-color: YELLOW;
float: left;
width: 70%;}
.content p {
padding: 10px;}
Alternatively, adding box-sizing: border-box
to the .content style changes the size calculation to include the padding but this is CSS3 while moving the padding into the P works in earlier versions. (Is anybody is still using it?)
Having tested this, I just have one (rhetorical) question. Why float both DIVs? If you only float the .sidebar DIV then extra text in the .content DIV will flow under it (and the width is now from the left edge of the .container DIV). By floating both, the extra text in the .content DIV stays to the right leaving a blank area under the .sidebar DIV (if it has less in it than the .content DIV). I assume that this is the intent but this is exactly the same effect that would result from using a 2 column table,
<html>
<head>
<style>
.table_container {
width: 100%;
max-width: 960px;
background-color: RED;}
.table_sidebar {
background-color: GREEN;
width: 30%;}
.table_content {
background-color: YELLOW;
width: 70%; /* unnecessary */
padding : 10px;}
</style>
</head>
<body>
<table class="table_container">
<tr>
<td class="table_sidebar">text in sidebar</td>
<td class="table_content">text in content</td>
</tr>
</table>
</body>
</html>
This does have a few slight differences from the original (floating DIV) solution.
border-spacing:0
to .table_container
and (for older browsers) cellspacing="0"
as an attribute of the TABLE.rowspan="2"
to the content TD, add an empty TD below the sidebar TD and force the sidebar TD to be as low as possible by adding height:1px
to .table_sidebar
.<html>
<head>
<style>
.container {
max-width: 960px;
background-color: RED;
height: 200px;}
.table_container {
width: 100%;
background-color: RED;
border-spacing: 0;}
.table_sidebar {
background-color: GREEN;
width: 30%;
height: 1px;}
.table_content {
background-color: YELLOW;
/* width: 70%; still unnecessary */
padding : 10px;}
td p {
margin: 1em 0;}
</style>
</head>
<body>
<div class="container">
<table class="table_container" cellspacing="0">
<tr>
<td class="table_sidebar"><p>text in sidebar</p></td>
<td rowspan="2" class="table_content"><p>text in content</p></td>
</tr>
<tr><td></td></tr>
</table>
</div>
</body>
</html>
While both methods produce the same output, the TABLE method is much less dependent upon getting sizes just right.
Upvotes: 0
Reputation: 2557
In order to avoid going over 100% of the page width and bumping .content
down, you may have to make some compromises. I can think of a few solutions:
.content
.content > * {
padding-left:10px;
}
.content
using percentage.content {
padding-left:2%;
width:68%;
}
.content {
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
All three of these solutions incur some bad things.
.content
could cause problems down the line when you want to give an element padding other than 10px..content
using percentage will look slightly different on different sized windows.Upvotes: 6
Reputation: 3186
This is a common problem, with typical web design not behaving the way our common logic would expect.
What is happening is the padding is actually making the .content
div become wider than its allocated slot, thus forcing it down underneath.
There's a few ways around this.
This rule: box-sizing:border-box;
makes it so that if you set width:100px
and padding:10px;
, the browser automatically makes the whole DIV 100px no matter what.
What in effect is happening is the DIV is becoming 80px wide, with 10px padding either side.
I've created a jsfiddle
The browser support for this rule is generally very good, although make sure to include all the rules I've added in the js (I.e, the one with, -webkit-, -moz-, and the one above which is just box-sizing:border-box;
)
For reference, all the rules here:
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
Upvotes: 0
Reputation: 747
That behaviour is totally correct. Please remember the so-called box model (in short):
margin, border and padding will all be added to the width value
In this case you have to calculate
30% + 10px + 70% + 10px = 100% + 20px
Browsers start supporting the css property box-sizing
(see https://developer.mozilla.org/en-US/docs/CSS/box-sizing for more information). You can set it to border-box
in order to change the default box model.
But remember to use those browser suffixes, too:
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
Upvotes: 1