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.
- There is a slight gap around each cell.
- The sidebar is the same height as the content.
- The table is only as high as its content (possibly less than the 200px specified).
- The content of the TDs has no upper or lower margin (default CSS for a P in a DIV).
These can be fixed as follows:
- Add
border-spacing:0 to .table_container and (for older browsers) cellspacing="0" as an attribute of the TABLE. - Add
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. - Enclose the table in a DIV of the required height.
- Add P elements with specifically defined upper and lower margins in the TDs (default CSS for P in TD apparently has no margins).
<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.