Div structure with percent height - html

I'm trying to build certain structure on page.
Main div (.row) with 400px height, two child divs (columns col-md-6), and five inner divs: two with 50% of parent height, three with 33% of parent height. And I don't get how to implement this. Could you recommend the best solution?
P.S. I'm using bootstrap3.
UPDATE: Added code.
<div class="row">
<div class="col-md-6">
<div id="ember828" class="ember-view bar-category-box">
</div>
<div id="ember829" class="ember-view bar-category-box">
</div>
</div>
<div class="col-md-6">
<div id="ember830" class="ember-view bar-category-box">
</div>
<div id="ember831" class="ember-view bar-category-box">
</div>
<div id="ember832" class="ember-view bar-category-box">
</div>
</div>
</div>

Since the .row is fixed at 400px height, you can give whats inside the same height:
.row > .col-md-6 {
height: 100%;
}
then for each div inside .col-md-6 you give the height you need:
.row > .col-md-6 .half {
height: 50%;
}
.row > .col-md-6 .third {
height: 33.33%;
}
This should work.
And even if the parent .row div height changed the inside should adapt.

You can do this with Flexbox, or with combination of bootstrap and flexbox DEMO
body, html {
margin: 0;
padding: 0;
}
.container {
height: 400px;
width: 100%;
box-sizing: border-box;
display: flex;
border: 1px solid #73FCD6;
padding: 10px;
}
.left, .right {
display: flex;
flex-direction: column;
flex: 1;
}
.box {
border: 1px solid #965504;
flex: 1;
margin: 5px;
display: flex;
align-items: center;
justify-content: center;
}
<div class="container">
<div class="left">
<div class="box">Content</div>
<div class="box">Content</div>
</div>
<div class="right">
<div class="box">Content</div>
<div class="box">Content</div>
<div class="box">Content</div>
</div>
</div>

Related

How to make Flex Grid width relative to the biggest child's width?

I have something like this :
.wrapper {
display: flex;
width: 300px;
}
.content {
flex: auto;
}
.row {
display: flex;
flex-flow: row wrap;
}
.col {
width: 25%;
padding: 5px;
border: 1px solid black;
box-sizing: border-box;
}
<div class='wrapper'>
<div class='content'>Content</div>
<div class='row'>
<div class='col'>aa</div>
<div class='col'>aaaa</div>
<div class='col'>aaaaaa</div>
<div class='col'>aaaaaaaa</div>
</div>
</div>
I can't understand how :
The width of the .row element is calculated
The width of the .col elements are calculated
Why some content overflows the box and some don't
What I want is a grid system that gets its size relative to the largest child, so that each content fits in its .col cell.
I saw that I could do that with display: grid and grid-template-columns: 1fr 1fr 1fr 1fr, but then how do you make it responsive and how well is it supported ?
To answer your 3 first questions, you simly need to remove the width:25% to have the following:
.wrapper {
display: flex;
width: 300px;
}
.content {
flex: auto;
}
.row {
display: flex;
flex-flow: row wrap;
}
.col {
padding: 5px;
border: 1px solid black;
box-sizing: border-box;
}
<div class='wrapper'>
<div class='content'>Content</div>
<div class='row'>
<div class='col'>aa</div>
<div class='col'>aaaa</div>
<div class='col'>aaaaaa</div>
<div class='col'>aaaaaaaa</div>
</div>
</div>
We didn't define any width, so each col will fit its content and the row will have the width equal to the sum of all the col.
Now since we have the width of the row defined based on the content, it won't change and it will get used as a reference for the percentage. Using 25% for the col means that each one will get 25% of the previously defined width and we will logically have some overflow since the content inside each col isn't the same.
.wrapper {
display: flex;
width: 300px;
}
.content {
flex: auto;
}
.row {
display: flex;
flex-flow: row wrap;
}
.col {
padding: 5px;
border: 1px solid black;
box-sizing: border-box;
}
.width .col {
width:25%;
}
<div class='wrapper'>
<div class='content'>before width</div>
<div class='row'>
<div class='col'>aa</div>
<div class='col'>aaaa</div>
<div class='col'>aaaaaa</div>
<div class='col'>aaaaaaaa</div>
</div>
</div>
<div class='wrapper'>
<div class='content'>after width</div>
<div class='row width'>
<div class='col'>aa</div>
<div class='col'>aaaa</div>
<div class='col'>aaaaaa</div>
<div class='col'>aaaaaaaa</div>
</div>
</div>
To obtain what you want, I think the 1fr of CSS grid is the way to go (like you already noticed). Actually CSS grid is well supported. You will simply have issues with IE and you can follow this link to see the known bugs: https://caniuse.com/#feat=css-grid
In order to make it responsive you may consider media query to switch to a column layout on small screens:
.wrapper {
display: flex;
width: 300px;
}
.row {
display: grid;
grid-auto-columns:1fr;
grid-auto-flow:column;
}
.col {
padding: 5px;
border: 1px solid black;
box-sizing: border-box;
}
#media all and (max-width:500px) {
.row {
grid-auto-flow:row;
}
}
<div class='wrapper'>
<div class='content'>Content</div>
<div class='row'>
<div class='col'>aa</div>
<div class='col'>aaaa</div>
<div class='col'>aaaaaa</div>
<div class='col'>aaaaaaaa</div>
</div>
</div>

Display 2 div in block and vertical div beside them

I am trying to design a section which 3 image. I can get the two images to display by block easily. I can float the third image to the right and adjust the height easily. However my issue is it does not align side by side.Below is an example of what I am trying to achieve
This is an example of what I have so far
.image-one,
.image-two {
width: 250px;
background-color: green;
display: block;
margin-top: 10px;
}
.image-three {
float: right;
background-color: lightblue;
width: 250px;
height: 200px;
}
<div class="container">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
<div class="image-three"> Image three </div>
</div>
You should be able to simple add flex to the container, and then add the content within a left and a right div.
Here's a working example:
.container {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.image-one,
.image-two {
width: 250px;
height: 95px;
background-color: green;
margin-bottom: 10px;
}
.image-three {
background-color: lightblue;
width: 240px;
height: 200px;
margin-left: 10px;
}
<div class="container">
<div class="left">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
</div>
<div class="right">
<div class="image-three"> Image three </div>
</div>
</div>
You can use flexbox for this:
.container {
display: flex;
flex-direction: column; /* align items in columns */
flex-wrap: wrap; /* wrap to a new column when height is reached */
justify-content: space-between; /* add spacing in between top and bottom image */
height: 210px; /* height of your 2 images plus and spacing you want */
width: 510px; /* width of 2 columns plus any spacing */
}
.image-one,
.image-two {
width: 250px;
height: 100px;
background-color: green;
display: block;
}
.image-three {
background-color: lightblue;
width: 250px;
height: 210px; /* I would make this image the height of the other 2 plus spacing */
align-self:flex-end; /* align this to the right of the container */
}
<div class="container">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
<div class="image-three"> Image three </div>
</div>
Maybe you can add some internal divs like this:
<div class="container">
<div class="container-left">
<div class="image-one">Hello</div>
<div class="image-two">Image two</div>
</div>
<div class="container-right">
<div class="image-three"> Image three </div>
</div>
</div>
Then, add css to container-left and container-right to properly set the width and the float. Like this:
.container-left, .container-right{
width:250px;
float:left;
}
Why don't you make use of bootstrap columns?
HTML
<div class="container">
<div class="row main-row">
<div class="col-6 left-col">
<div class="row left-col-top">
<!-- Top left image here -->
</div>
<div class="row left-col-bottom">
<!-- Bottom left image here -->
</div>
</div>
<div class="col-6 right-col">
</div>
</div>
</div>
CSS
.main-row {
height:300px;
}
.left-col-top {
background-color:blue;
height:50%;
}
.left-col-bottom {
background-color:red;
height:50%;
}
.right-col {
background-color:green;
height:100%;
}
Easy flexbox solution :)
#main, #left {
display:flex;
}
#left {
flex-direction: column;
flex: 1;
}
.section {
flex: 1;
margin: 2px;
background-color: green;
}
<div id="main">
<div id="left">
<div class="section">Hello</div>
<div class="section">Hello</div>
</div>
<div id="right" class="section">Hello</div>
</div>

Correct Way to Design a Responsive Column Focused Div Table

I want to add a wide table to my website, consisting of many columns, but few headings; all the subsequent cells are tightly linked to the heading they fall under. The table does not fit on the width of cellphone browsers. I'd like the table to split at the headings.
I attempted the following, the container div is used to simulate a page width change:
.container {
border: 2px solid;
resize: horizontal;
overflow: auto;
width: 150px;
}
.flex-container {
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
background-color: lightgrey;
width: 100%;
height: 100%;
}
.table {
display: table;
background-color: lightgrey;
}
.cell {
display: table-cell;
width: 100px;
}
.row {
display: table-row;
}
.cell {
background: red;
color: white;
border: 1px solid white;
}
<!-- The container is horizontally resizable, to simulate a page width change. -->
<div class='container'>
<div class='flex-container'>
<div class='table'>
<div class='row'>
<div class='cell'>Heading 1</div>
</div>
<div class='row'>
<div class='table'>
<div class='row'>
<div class='cell'>One</div>
<div class='cell'>Two</div>
</div>
</div>
</div>
</div>
<div class='table'>
<div class='row'>
<div class='cell'>Heading 2</div>
</div>
<div class='row'>
<div class='cell'>Three</div>
</div>
</div>
</div>
</div>
Is this the correct way to achieve this? Are there better methods or conventions that I can adhere to?

Put one div next to two vertical divs

I'm trying to have one div, with a height of 100% in it's container (which has a height of 50%) and two divs next to that, which each have a height of 50%.
Here's an example of what I mean:
I would also like to have a margin between all the divs, as shown in the picture above.
Here's my code so far:
<div style="height: 50%;">
<div style="height: 100%; float: left; margin-right: 15px;">
<p>Content</p>
</div>
<div style="float: right; height: 50%;">
<p>Content</p>
</div>
<div style="float: right; height: 50%;">
<p>Content</p>
</div>
</div>
JsFiddle: https://jsfiddle.net/ne4njtvr/
Like this maybe?
Note, if you need to support older browsers, this can be done using display: table as well
html, body {
margin: 0;
height: 100%;
}
.wrapper {
display: flex;
height: 100%;
}
.wrapper .left,
.wrapper .right {
flex: 1;
display: flex;
flex-direction: column;
}
.wrapper .right div {
flex: 1;
}
.wrapper .right div ~ div {
flex: 2;
}
div {
border: 1px solid;
box-sizing: border-box;
}
<div class="wrapper">
<div class="left">
Left
</div>
<div class="right">
<div>
Right - Top
</div>
<div>
Right - Bottom
</div>
</div>
</div>

Set flex item height to size of content

I have three divs in a column. Each div has content that should scroll if it overflows. I would like each div to have the same height, with the max height of each div to be the height of its content. Is this possible to implement using flexbox?
Jsfiddle: http://jsfiddle.net/x6puccbh/2/
As you can see in the fiddle, all three sections are the same height, but I would like the middle section to be only as tall as its content.
<div class="container">
<div class="panel">
<div class="section">
<div class="header">HEADER</div>
<div class="content">content<br>content<br>content<br>content
<br>content<br>content<br>content<br>content
<br>content<br>content<br>content<br>content</div>
</div>
<div class="section">
<div class="header">HEADER</div>
<div class="content">content</div>
</div>
<div class="section">
<div class="header">HEADER</div>
<div class="content">content<br>content<br>content<br>content
<br>content<br>content<br>content<br>content
<br>content<br>content<br>content<br>content
</div>
</div>
</div>
.container {
height: 300px;
}
.panel {
display:flex;
flex-direction: column;
height: 100%;
}
.header {
height: 15px;
text-align: center;
flex-shrink: 0;
}
.section {
flex-grow: 1;
flex-shrink: 1;
flex-basis: auto;
height: 100%;
border: 1px solid red;
display: flex;
flex-direction: column;
}
.content {
overflow-y: auto;
height: 100%;
}
does this work for you?
<div class="section">
<div class="header">HEADER</div>
<p>content sjkdkjasdn asjn dvas jkdb ajd avsd</p>
</div>
css
.section:nth-child(2) {
height:unset;
}
p {
padding-bottom: 5em;
}
here's a fork of the fiddle
Use this:
height: fit-content;
Can you use link:
https://caniuse.com/?search=fit-content