Make child divs inside a parent div equal width and fluid - html

How can I make the 3 child divwith class .box have the same width while fluidly occupying the entire parent container div while staying inline.
Here is a fiddle
#container {
width: 20em;
background: red;
text-align: center;
}
.box {
display: inline-block;
margin: 1em;
border: 1px solid #000;
}
<div id="container">
<div class="box">test</div>
<div class="box">test</div>
<div class="box">test</div>
</div>

use CSS flexbox for that
#container {
width: 20em;
display: flex;
background: red;
text-align: center;
}
.box {
flex: 1;
border: 1px solid #000;
}
<div id="container">
<div class="box">test</div>
<div class="box">test</div>
<div class="box">test</div>
</div>
OR
use inline-block as you already are using, with a few tweaks.
* {
box-sizing: border-box
}
#container {
width: 20em;
background: red;
text-align:center
}
.box {
font-size: 16px;
display: inline-block;
width: calc(100% / 3);
border: 1px solid #000;
}
<div id="container">
<div class="box">test</div><div class="box">test</div><div class="box">test</div>
</div>
OR
use CSS tables for old browsers support
#container {
width: 20em;
display: table;
background: red;
text-align: center;
}
.box {
display:table-cell;
border: 1px solid #000;
}
<div id="container">
<div class="box">test</div>
<div class="box">test</div>
<div class="box">test</div>
</div>

display:table version:
#container {
width: 20em;
background: red;
text-align: center;
display: table;
table-layout: fixed;
border-spacing: 1em;
/* instead the margin:1em; you applied to children */
}
.box {
display: table-cell;
;
border: 1px solid #000;
}
.middle{vertical-align:middle;}
<div id="container">
<div class="box">test</div>
<div class="box">test</div>
<div class="box">test</div>
</div>
<hr/>
<div id="container">
<div class="box">test
<br/>+ 1line ?</div>
<div class="box">test</div>
<div class="box middle">test</div>
</div>
table-layout:fixed will fix width value you set, for main container and children.
if children(table-cell) have no width set, they will spray evenly
#container {
width: 20em;
background: red;
text-align: center;
display: table;
table-layout: fixed;
border-spacing: 1em;
/* instead the margin:1em; you applied to children */
}
.box {
display: table-cell;
;
border: 1px solid #000;
}
.middle{vertical-align:middle;}
<div id="container">
<div class="box">test test test test </div>
<div class="box">test</div>
<div class="box">test</div>
</div>
<hr/>
<div id="container"class=" bis ">
<div class="box">testtesttesttest testtesttest</div>
<div class="box">test</div>
<div class="box">test</div>
</div>

If you want to do this with display: inline-block you can set equal width of 33.33% on each .box, remove white space from HTML and also add box-sizing: border-box.
* {
box-sizing: border-box;
}
#container {
width: 20em;
background: red;
text-align: center;
}
.box {
display: inline-block;
width: 33.33%;
border: 1px solid #000;
}
<div id="container">
<div class="box">test</div><div class="box">test</div><div class="box">test</div>
</div>

Related

Html CSS - div center, and left, right alignment [duplicate]

This question already has answers here:
Force flex item to span full row width
(2 answers)
Closed 2 years ago.
How do I make the top div of the full horizontal length of the main container, while keeping the next two div, .left and .right in flex to each other?
To look like this -
.main {
border: 1px solid red;
display: inline-flex;
}
.main div.top {
border: 1px solid orange;
width: auto;
display: inline-block;
}
.main div.left {
border: 1px solid blue;
}
.main div.right {
border: 1px solid green;
}
<html>
<body>
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
</body>
</html>
Another way to do this is with grid:
.main {
display: grid;
grid-template-columns: 1fr 1fr;
}
.main .top {
grid-column: 1/3;
}
.main {
border: 1px solid red;
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 4px;
padding: 4px;
}
.main .top {
border: 1px solid orange;
grid-column: 1/3;
}
.main .left {
border: 1px solid blue;
}
.main .right {
border: 1px solid green;
}
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
Use display: flex; and flex-wrap: wrap on the parent container, 100% width on the first child and flex-grow: 1 on the other children, or also use percentage widths on the second and third DIVs.
* {
box-sizing: border-box;
}
.main {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
}
.main div.top {
border: 1px solid orange;
width:100%;
}
.main div.left {
border: 1px solid blue;
width: 40%;
}
.main div.right {
border: 1px solid green;
width: 60%;
}
<html>
<body>
<div class="main">
<div class="top"> <h1>top</h1>
</div>
<div class="left"> <h1>left</h1>
</div>
<div class="right"> <h1>right</h1>
</div>
</div>
</body>
</html>
Any width of .left and .right
.main {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
}
.main div.top {
border: 1px solid orange;
width: auto;
display: inline-block;
flex: 1 1 100%;
}
.main div.left {
border: 1px solid blue;
flex: 1 1 auto;
}
.main div.right {
border: 1px solid green;
flex: 1 1 auto;
}
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div class="left">
<h1>left 11111111</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
You can try something like this:
I just added 2 extra divs if that is not an issue?
#MainDiv {
width: 200px;
border: 1px solid red;
}
.main {
width: auto;
display: flex;
flex-direction: column;
}
#lower {
display: flex;
flex-direction: row;
}
.left,
.right {
width: 100px;
border: 1px solid black;
}
<html>
<body>
<div id="MainDiv">
<div class="main">
<div class="top">
<h1>top</h1>
</div>
<div id="lower">
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
</div>
</div>
</body>
</html>
You can two div to create additional sections.
.main {
border: 1px solid red;
display: flex;
flex-direction:column;
}
.main div.top {
border: 1px solid orange;
width:auto;
}
.main div.left {
border: 1px solid blue;
flex:1
}
.main div.right {
border: 1px solid green;
flex:1
}
.main__section2 {
display:flex
}
<html>
<body>
<div class="main">
<div class="main__section1">
<div class="top">
<h1>top</h1>
</div>
</div>
<div class="main__section2">
<div class="left">
<h1>left</h1>
</div>
<div class="right">
<h1>right</h1>
</div>
</div>
</div>
</body>
</html>

CSS 2 fluid 1 fixed column

I am trying to achieve this layout.
left column fixed size
right column fluid, it may have x number of elements inside, for example up to 4 divs 50px wide (this is done dynamically) so it must be max 200px wide, or if it has 3 such elements, then it must be 150px wide...
center column fluid, takes all the rest space
The closest I have comes is this:
#container {
overflow:hidden;
width: 100%;
}
#leftcol {
border: 1px solid #0f0;
float: left;
width: 80px;
}
#rightcol {
border: 1px solid #0f0;
float: right;
}
#centercol {
border: 1px solid #000;
margin-left: 80px;
}
.box{
width:50px;
height:20px;
background:red;
float:left;
}
<div id="container">
<div id="leftcol">
fixed 80px
</div>
<div id="rightcol">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
<div id="centercol">
fluid center
</div>
</div>
but center fluid is not correct width.
I can change some html if it will be easier to achieve desired effect.
You can do it with the Flexbox:
body {margin: 0}
#container {
display: flex; /* displays flex-items (children) inline */
overflow: hidden;
}
#leftcol {
border: 1px solid #0f0;
width: 80px;
}
#centercol {
flex: 1; /* takes the remaining horizontal space */
border: 1px solid #000;
}
#rightcol {
display: flex;
border: 1px solid #0f0;
max-width: 200px; /* adjust to your needs */
}
.box {
width: 50px;
height: 20px;
background: red;
}
<div id="container">
<div id="leftcol">
fixed 80px
</div>
<div id="centercol">
fluid center
</div>
<div id="rightcol">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
</div>
</div>

HTML table-cell how to set width

How can I force width to elements with display: table-cell?
<div id="wraper">
<div id="left">
<p>Left</p>
</div>
<div id="right">
<p>Right and Hide</p>
</div>
</div>
My CSS doesn't do anything, width of #left and #righ elements isnt affected.
#wraper {
display: table-wrap;
}
#left {
border: 1px solid red;
display: table-cell;
width: 30%;
}
#right {
border: 1px solid blue;
display: table-cell;
width 70%;
}
it just works fine for me
JS Fiddle: https://jsfiddle.net/qq0t46pt/1/
HTML:
<div id="wraper">
<div id="left">
<p>Left</p>
</div>
<div id="right">
<p>Right and Hide</p>
</div>
</div>
CSS:
#wraper {
display: table-wrap;
}
#left {
border: 1px solid red;
display: table-cell;
width: 30%;
}
#right {
border: 1px solid blue;
display: table-cell;
width 70%;
}
table-wrap is not a valid display value. See MDN
Use display:table.
Then you need to give the table a width so the child elements can calculate their own width accordingly.
#wraper {
display: table;
width: 70%; /* or your chosen value */
margin: auto;
}
#left {
border: 1px solid red;
display: table-cell;
width: 30%;
}
#right {
border: 1px solid blue;
display: table-cell;
width 70%;
}
<div id="wraper">
<div id="left">
<p>Left</p>
</div>
<div id="right">
<p>Right and Hide</p>
</div>
</div>

Stretch an element of unknown width alongside elements of fixed width inside parent of unknown width

I have a parent div of unknown width (the width depend on some screen width calculations). The number of the child divs is 4 and are floated so that they are horizontally aligned. The 1st, 2nd and 4th are good candidates for fixed width value. However, the 3rd element can stretch to fit the remaining space in the parent div.
I don't know why the approach of display:table; for parent and display:table-cell for children didn't work for me. The three element's width is fixed except for the concerned div where I also tried width:auto to no avail.
<div class="parent">
<div class="first"></div>
<div class="second"></div>
<div class="third"></div>
<div class="fourth"></div>
</div>
A minimal CSS:
.parent
{
width: 100%;
display:table;
}
.first
{
width: 80px;
height: 80px;
float: left;
display: table-cell;
}
.second
{
width: 80px;
height: 80px;
float: left;
display: table-cell;
}
.third
{
height: 80px;
float: left;
display: table-cell;
}
.fourth
{
width: 80px;
height: 80px;
float: left;
display: table-cell;
}
Your usual help is much appreciated.
You can do this with Flexbox, so if you add flex: 1 to one child div it will take rest of free width
.parent {
display: flex;
border: 1px solid black;
}
.child {
border: 1px solid black;
margin: 5px;
padding: 5px;
width: 50px;
}
.long {
flex: 1;
}
<div class="parent">
<div class="child">Div 1</div>
<div class="child">Div 2</div>
<div class="child long">Div 3</div>
<div class="child">Div 4</div>
</div>
Or you can use CSS Table with table-layout: fixed here is Browser support
.parent {
display: table;
width: 100%;
border: 1px solid black;
table-layout: fixed;
}
.child {
border: 1px solid black;
display: table-cell;
margin: 5px;
padding: 5px;
width: 50px;
}
.long {
width: 100%;
}
<div class="parent">
<div class="child">Div 1</div>
<div class="child">Div 2</div>
<div class="child long">Div 3</div>
<div class="child">Div 4</div>
</div>
For IE9+
You can use inline-block and calc() for this.
Snippet
body {
margin:0
}
.parent {
border:solid black;
font-size:0; /*fix inline-block gap*/
}
.child {
border: 1px solid red;
margin: 5px;
width:100px;
display:inline-block;
font-size:16px /*restore font -size*/
}
.calc{
width: calc(100% - 348px)
}
<div class="parent">
<div class="child">one</div>
<div class="child">two</div>
<div class="child calc">three</div>
<div class="child">four</div>
</div>
For IE8+
you can use display:table/table-cell
Snippet
body {
margin: 0
}
.parent {
border: solid black;
display: table;
table-layout: fixed;
width: 100%;
/*optional*/
border-collapse:separate;
border-spacing:5px;
box-sizing:border-box;
}
.child {
border: 1px solid red;
width: 100px;
display:table-cell;
}
.big {
width:100%
}
<div class="parent">
<div class="child">one</div>
<div class="child">two</div>
<div class="child big">three</div>
<div class="child">four</div>
</div>

DIV Table with rows and columns

I'm making a DIV table but the last two rows which contain colums don't show a border.
Does somebody know a workarround?
Here is my code:
CSS
.contentwrap { width: 300px; display: table; }
.contentwrap .box-row {display: table-row;}
.contentwrap .box { padding: 15px; display: table-cell; border: 1px solid #e5e5e5; }
.contentwrap .box-column { display: table-row; }
.contentwrap .column { padding: 15px; display: table-cell; width: 10%; border-color: #e5e5e5; }
HTML
<div class="contentwrap">
<div class="box-row">
<div class="box">row 1</div>
</div>
<div class="box-row">
<div class="box">row 2</div>
</div>
<div class="box-row">
<div class="box">rpw 3</div>
</div>
<div class="box-row">
<div class="box">row 4</div>
</div>
<div class="box-row">
<div class="box-column">
<div class="col">column1</div>
<div class="col">column2</div>
<div class="col">column3</div>
<div class="col">column4</div>
</div>
<div class="box-column">
<div class="col">column1</div>
<div class="col">column2</div>
<div class="col">column3</div>
<div class="col">column4</div>
</div>
</div>
</div>
I'm not sure if this is the effect you want, but I just changed
border-color: #e5e5e5; in .contentwrap .column to border: 1px solid #e5e5e5;
Like this
.contentwrap .col
{
padding: 15px;
display: table-cell;
width: 10%;
border: 1px solid #e5e5e5
}
Also you might want to check your namings, in your html you are using col but your css is using column. I changed it to col for you here.
From what I can understand, the following modifications to your CSS should do the trick.
.box-column { display: table-row; }
.col{ padding: 15px; display: table-cell; width: 10%; border-color: #e5e5e5; border: 1px solid #e5e5e5; }