I need to create a 2 columns layout side by side, one fixed width and one fluid.
The columns must fill 100% of the container width and with 2 versions: fixed on left and fixed on right.
I am using negative margin to accomplish that but I have two problems:
-
When using right column with fixed width there is a gap on the right;
-
The version using left column with fixed width is not working at all.
Could someone help me out fixing this?
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
div.container {
clear: both;
overflow: hidden;
}
div.container.right-fixed {
border: 1px solid red;
}
div.container.right-fixed div.fixed {
background-color: lightblue;
float: left;
margin-right: 80px;
padding: 10px;
width: 160px;
}
div.container.right-fixed div.fluid {
background-color: lightgreen;
float: left;
margin-left: -240px;
padding: 10px;
width: 100%;
}
div.container.right-fixed div.fluid div.content {
margin-left: 240px;
}
div.container.left-fixed {
border: 1px solid red;
}
div.container.left-fixed div.fixed {
background-color: lightblue;
float: left;
margin-left: 80px;
padding: 10px;
width: 160px;
}
div.container.left-fixed div.fluid {
background-color: lightgreen;
float: left;
margin-left: -240px;
padding: 10px;
width: 100%;
}
div.container.left-fixed div.fluid div.content {
margin-left: 240px;
}
<h1>Rigth column fixed width</h1>
<div class="container right-fixed">
<div class="fluid">
<div class="content">
Fluid
</div>
</div>
<div class="fixed">
Fixed
</div>
</div>
<h1>Left column fixed width</h1>
<div class="container left-fixed">
<div class="fixed">
Fixed
</div>
<div class="fluid">
<div class="content">
Fluid
</div>
</div>
</div>
2
Answers
Time for you to study up on flexbox. Here all I’ve done is remove your negative margins and floats, and add display: flex; to your container.
Please use flexbox for this kind of layout, every modern browser supports it