I got four divs.
Div two is half the height of div 1, div 3 & 4 is half the height of div 2.
On a large display the placement of the divs are fine:
---------------- ---------- -------- --------
| | | Div 2 | | Div 3| | Div 4 |
| Div 1 | |________| ------- -------
| |
| |
----------------
On smaller screens I want the following layout:
---------------- ----------
| | | Div 2 |
| Div 1 | |________|
| | | Div 3 |
| | ----------
---------------- | Div 4 |
i.e. div 3 and 4 are placed under div 2.
But instead they are placed under div 1:
---------------- ----------
| | | Div 2 |
| Div 1 | |________|
| |
| |
----------------
-------- --------
| Div 3| | Div 4 |
------- -------
Is it possible to get the second alternative in some way? In my own attempts I have never got both the large screen and the smaller screen alternatives to work with the same code.
You could use the CSS property flexbox (MDN reference) to achieve this. See the example below (in full screen and stretch your screen).
While starting mobile first, you can work your way up and with setting media queries, you can easily modify the visual hierarchy of the boxes. First, put a display: flex; on the wrapper div and then on the desired breakpoint (here 480px), add another display: flex; on the .inner-wrapper.
Note: this is just a quick setup and there are many ways to do this.
.wrapper {
width: 100%;
height: 100vh;
display: flex;
}
.inner-wrapper {
width: 50%;
}
.box-large {
width: 50%;
height: 50%;
}
.box {
width: 100%;
height: 25%;
}
.box-small {
width: 100%;
height: 12.5%;
}
#media screen and (min-width: 480px) {
.inner-wrapper {
display: flex;
}
}
/* Visual stuff */
body {
margin: 0;
padding: 0;
}
.box-one {
background-color: red;
}
.box-two {
background-color: blue;
}
.box-three {
background-color: green;
}
.box-four {
background-color: yellow;
}
<div class="wrapper">
<div class="box-one box-large">red</div>
<div class="inner-wrapper">
<div class="box-two box">blue</div>
<div class="box-three box-small">green</div>
<div class="box-four box-small">yellow</div>
</div>
</div>
Related
I'm trying to make a calendar-like display where the dates' boxes are square-shaped but the whole calendar itself cannot have any scrolling..
it's supposed to look like this:
----------------------------------
| header element |
|--------------------------------|
| additional element |
|--------------------------------|
| ----------------- | --> start calendar (.box_fit-container)
| |sun|mon|tue|wed| |
| ----------------- |
| |d1 |d2 |d3 |d4 | |
| |d1 |d2 |d3 |d4 | |
| |d1 |d2 |d3 |d4 | | --> bottom of screen/viewport
| |d1 |d2 |d3 |d4 | | where it usually starts scrolling
| |d1 |d2 |d3 |d4 | |
| ----------------- |
----------------------------------
I managed to create the outer "frame" by using flex so that the outer container fills the remaining height of the original viewport, and I can also create an individual square shape, thanks to various SO contributors. but for whatever reason, I can't create a square shape where the object's width follows it's parent's height.
these are the code I have so far:
.box_fit-container {
display: flex;
flex-direction: column;
height: 80vh; /* this was supposed to be 100% according to the SO source I found, but since I'm working on a legacy code and there are other elements above this new one so I changed into 80vh to fit as close as possible */
}
.box_fit-header {
flex: 0 1 auto;
}
.box_fit-content {
flex: 1 1 auto;
}
.square-box {
position: relative;
width: 100%;
padding-top: 100%;
}
<div class="container">
<div class="box_fit-container">
<div class="box_fit-header">
<div>some header content
</div>
<div class="box_fit-content">
<!--
I need this .square-box elem to be square-shaped
but doesn't overflow outside the .box_fit-content
-->
<div class="square-box"></div>
</div>
</div>
</div>
If you can identify the height of the header or this one will always be fixed you can consider max-width to be 80vh - height of header
.box_fit-container {
display: flex;
flex-direction: column;
height: 80vh; /* this was supposed to be 100% according to the SO source I found, but since I'm working on a legacy code and there are other elements above this new one so I changed into 80vh to fit as close as possible */
border:1px solid;
}
.box_fit-header {
flex: 0 1 auto;
border:1px solid red;
}
.box_fit-content {
flex: 1 1 auto;
border:1px green;
}
.square-box {
position: relative;
width: 100%;
margin:auto;
max-width:calc(80vh - 25px);
border:2px solid;
}
.square-box:before {
content:"";
display:inline-block;
padding-top: 100%;
}
* {
box-sizing:border-box;
}
<div class="container">
<div class="box_fit-container">
<div class="box_fit-header">
<div>some header content
</div>
<div class="box_fit-content">
<!--
I need this .square-box elem to be square-shaped
but doesn't overflow outside the .box_fit-content
-->
<div class="square-box"></div>
</div>
</div>
</div>
I need the appropriate Html and CSS for the following
Here is the layout of the app
Detailed Figure General Figure
------------------------- -------------------------
| App Header | | App Header |
------------------------- -------------------------
| Module Header| Sub | | | |
|--------------| Module | | | |
| |--------| | | |
| Module | Sub | | Main | Sub |
| Content | Module | | Module | Module |
| |--------| | Space | Space |
| | Sub | | | |
|--------------| Module | | | |
| Module |--------| | | |
| Footer | | | | |
------------------------- -------------------------
App Header is a static 60px
Module Header is a static 35px
Module Content is dynamic and overflows
Module Footer is dynamic and needs to always display everything on screen (it would never be more than say 500px)
What is the appropriate way to setup the Main Module Space and children?
How do I get the Headers and Footers to understand they're in containers and should not cover the entire width of the screen?
(I've used absolute position for the footer but that causes Module Content to hide behind the Module Footer)
I figured it out for the most part
https://jsfiddle.net/jackyFrosty/46mjtghv/47/
<div class='mainApp'>
<div class='appHeader'>
Header
</div>
<div class='contentContainer'>
<div class='leftContainer'>
<div class='moduleContainer'>
<header class='header'>
Header
</header>
<main class='content'>
<div>Content 1</div><div>Content 2</div><div>Content 3</div><div>Content 4</div>
<div>Content</div><div>Content</div><div>Content</div><div>Content</div><div>Content</div>
<div>Content</div><div>Content</div><div>Content</div><div>Content</div><div>Content</div>
<div>Content</div><div>Content</div><div>Content</div><div>Content</div><div>Content</div>
<div>Content 96</div><div>Content 97</div><div>Content 98</div><div>Content 99</div>
</main>
<footer class='footer'>
<div>Footer</div><div>Footer</div><div>Footer</div><div>Footer</div>
<!--div>Footer</div><div>Footer</div><div>Footer</div><div>Footer</div-->
</footer>
</div>
</div>
<div class='rightContainer'>
Right Content
</div>
</div>
</div>
* {
box-sizing: border-box;
border-style: solid;
border-color: white;
padding: 0px;
margin: 0px;
}
.mainApp {
height: 100%;
}
.appHeader {
height: 60px;
border-color: black;
}
.contentContainer {
height: calc(100vh - 60px);
}
.leftContainer {
float: left;
width: 75%;
height: 100%;
overflow: auto;
border-color: black;
}
.rightContainer {
float: left;
width: 25%;
height: 100%;
overflow: auto;
border-color: black;
}
/* /////////// Important part ////////// */
.moduleContainer {
border-color: orange;
display: -webkit-flex;
display: flex;
flex-direction: column;
height: calc(100vh - 60px);
}
.header {
border-color: red;
height: 35px;
-webkit-flex: 0 0 auto;
flex: 0 0 auto;
}
.content {
border-color: green;
overflow-y: scroll;
-webkit-flex: 1 1 auto;
flex: 1 1 auto;
}
.footer {
border-color: blue;
}
Can I get flexbox sidebars to share a new row, after a media query?
Before (on wide screens)
+---+-------+---+
| | | |
| L | Main | R |
| | | |
+---+-------+---+
After (on narrow screens)
+-------+
| |
| Main |
| |
+-------+
| | |
| L | R |
| | |
+-------+
HTML (not changeable after media query)
<div class="container">
<div class="sideLeft">L</div>
<div class="MainContent">Main</div>
<div class="sideRight">R</div>
</div>
You can change order: -1 on main div with media queries. You also need to set flex-wrap: wrap on container element. Also calc(% - 10px) is for margin of 5px on both sides but if you don't want margins you can just use % as you can se here
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-wrap: wrap;
height: 200px;
border: 1px solid gray;
padding: 5px
}
.container > div {
display: flex;
border: 1px solid black;
margin: 5px;
flex: 1;
}
div.main {
flex: 3;
}
#media(max-width: 480px) {
div.main {
order: -1;
flex: 0 0 calc(100% - 10px);
}
.container > div:not(.main) {
flex: 0 0 calc(50% - 10px);
}
}
<div class="container">
<div class="sideLeft">L</div>
<div class="main">Main</div>
<div class="sideRight">R</div>
</div>
Now, the title might be a bit vague, but I didn't know any other way to word it. What I basically want is this:
--------- --------- ---------
| DIV 1 | | DIV 2 | | DIV 3 |
--------- --------- ---------
--------- --------- ---------
| DIV 4 | | DIV 5 | | DIV 6 |
--------- --------- ---------
These div's are in a parent with a width of 1000px. So each div is 30% of width (with the proper gap inbetween basically).
The HTML:
<div id="matches">
<div class="match_box">
DIV 1
</div>
<div class="match_box">
DIV 2
</div>
<div class="match_box">
DIV 3
</div>
<div class="match_box">
DIV 4
</div>
<div class="match_box">
DIV 5
</div>
<div class="match_box">
DIV 6
</div>
</div>
The CSS:
#matches {
height: 100%;
width: 1000px;
}
How would I go about doing this? (.match_box being the div that need to flow nicely).
Thanks in advance!
If you don't have to target older browsers, you can use flexbox. This allows you to space the elements nicely. Here they go to the edges on both sides.
#matches {
height: 100%;
width: 500px;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.match_box {
width:30%;
background-color: #eee;
margin-bottom: 10px;
}
https://jsfiddle.net/ekhts0fs/5/
Changing justify-content to space-between or center (see the link to flexbox above) can position them differently.
Try this:
#matches {
height: 100%;
width: 1000px;
}
.match_box {
width:30%;
float: left;
background-color: #ccc;
margin: 5px;
padding: 5px;
}
Live:
https://jsfiddle.net/Lu8p6gr3/2/
This is a question based on this question:
Twitter bootstrap 3 two columns full height
I have the same layout. But, when I try to put a DIV in the content part, it pushes the content of the sidebar down. Here's a Fiddle
<header>Header</header>
<div class="container">
<div class="row">
<div class="col-md-3 no-float">Navigation</div>
<div class="col-md-9 no-float"><div><div style="width=100%;height:55px;background:red"></div>Content</div></div>
</div>
CSS:
html,body,.container
{
height:100%;
}
.container
{
display:table;
width: 100%;
margin-top: -50px;
padding: 50px 0 0 0; /*set left/right padding according to needs*/
-moz-box-sizing: border-box;
box-sizing: border-box;
}
header
{
background: green;
height: 50px;
}
.row
{
height: 100%;
display: table-row;
}
.col-md-3.no-float, .col-md-9.no-float {
float: none; /* thanks baxabbit */
}
.col-md-3
{
display: table-cell;
background: pink;
}
.col-md-9
{
display: table-cell;
background: yellow;
float: none;
}
If you have other suggestions, I need to do the following layout using Bootstrap and Angular, keeping the idea of having full-height columns from the upper post:
+-------------------------------------------------+
| Header |
+------------+------------------------------------+
| Nav.Header | ContentHeader | < |
|------------|------------------------------------|
|Navigation | Content | T |
| | | E |
| | | X |
| | | T |
| | | |
| | | |
| | | |
+------------+------------------------------------+
Thank you