Split page into 4 equal sections with fixed header - css

I am trying to divide a page into 4 equal sections (2x2 grid) with a fixed header. So far I have a it split into 4 but because of the header, the page is going bigger than 100%.
I want the 4 parts to only take up the available space on the screen and not make the page scroll but be equal.
HTML:
<header></header>
<div class="container">
<div class="block blue"></div>
<div class="block green"></div>
<div class="block purple"></div>
<div class="block red"></div>
</div>
CSS:
html, body {
height: 100%;
}
header {
width: 100%;
height: 117px;
background-color: grey;
}
.container {
width: 100%;
height: 100%;
}
.block {
width: 50%;
height: 50%;
float: left;
}
.blue {
border-top: 4px solid blue;
}
.green {
border-top: 4px solid green;
}
.purple {
border-top: 4px solid purple;
}
.red {
border-top: 4px solid red;
}
See demo so far: http://jsfiddle.net/Kas78/1/
I also want to be able to make it responsive so that each block can be changed to 100% width and a fixed height when the page is smaller than 690px so that needs to be kept in mind.

I'd recomment to change your code to this:
http://jsfiddle.net/NicoO/Kas78/2/
html, body {
height: 100%;
}
header {
height: 117px;
background-color: grey;
}
.container {
height: calc(100% - 117px);
}
.block {
width: 50%;
height: 50%;
float: left;
}
I throw out some width:100% since block elements will fill the parent element by default.

Related

Change width of mosaic layout to be 50% / 50%

I would like the images in this layout to each take up 50% of the page.
Right now the split is 2/3 for the large image and 1/3 for the 2 smaller images.
Images are here:
https://www.joshungerdesign.com/packed
I can make the large image take up 50% by using this:
.tweak-index-gallery-layout-packed [data-index-gallery-images='3']
.Index-gallery-item:nth-child(3n+1) {
width: 50%;}
I don't know how to make the small images extend to the right side once the large image width is changed.
I just write basic code, I hope it'll help you out. Thanks
body {
margin: 0;
}
.Index-gallery-inner {
background-color: #fff;
display: inline-block;
width: 100%;
}
.Index-gallery-item {
box-sizing: border-box;
float: left;
height: 500px;
border: 5px solid #fff;
width: 50%;
}
.Index-gallery-item:nth-child(2),
.Index-gallery-item:nth-child(3){
height: 250px;
width: 50%;
}
.Index-gallery-item-inner {
background-color: #ccc;
height: 100%;
width: 100%;
}
.Index-gallery-item-image {
background-color: white !important;
padding: 0 !important;
}
<div class="Index-gallery-inner clear">
<div class="Index-gallery-item">
<div class="Index-gallery-item-inner"></div>
</div>
<div class="Index-gallery-item">
<div class="Index-gallery-item-inner"></div>
</div>
<div class="Index-gallery-item">
<div class="Index-gallery-item-inner"></div>
</div>
</div>

Make full-width without setting `width` [duplicate]

This question already has answers here:
Setting div width to 100% minus certain amount of px
(5 answers)
Expand a div to fill the remaining width
(21 answers)
Closed 6 years ago.
I will simulate what i need to achieve.
for example, i want that #2 took the whole space, remaining of 100% - 180px.. how to achieve that?
p.s. seems flexbox is more supported over devices than calc - http://css3clickchart.com/#flexbox
You can use flexbox model as shown below. Adding flex: auto; will allow the right content to use remaining width.
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
#parent {
display: flex;
height: 100%;
}
#left {
width: 180px;
background-color: hotpink;
}
#right {
flex: auto;
background-color: dodgerblue;
}
<div id="parent">
<div id="left"></div>
<div id="right"></div>
</div>
Use css calc
Here with a example.. This might help:
.class {
width: -moz-calc(100% - 100px);
width: -webkit-calc(100% - 100px);
width: calc(100% - 100px);
}​
You can use float: left and overflow: hidden.
aside {
float: left;
width: 30%;
background: beige;
}
article {
overflow: hidden;
background: brown;
color: white;
}
<aside>
sidebar
</aside>
<article>
content
</article>
There are many ways to do this. One simple way is below.
1st way: Simple inline-block
.container {
width: 100%;
height: 600px;
background-color: #f2f2f2;
}
.sidebar {
background-color: red;
width: 180px;
height: 600px;
float: left;
}
.main-content {
display: inline-block;
background-color: green;
}
<div class="container">
<div class="main-content">Main Content</div>
<div class="sidebar">Sidebar</div>
</div>
Fair warning though: In this case the .main-content will only take the space it needs, and will not actually be full width. So If you want to set background to it, you should actually set the backround to .container.
2nd way: Use calc for width
.container {
width: 100%;
height: 600px;
background-color: #f2f2f2;
position: relative;
}
.sidebar {
background-color: red;
width: 180px;
height: 600px;
float: left;
}
.main-content {
float: right;
background-color: green;
width: calc(100% - 180px);
height: 600px;
}
<div class="container">
<div class="main-content">Main Content</div>
<div class="sidebar">Sidebar</div>
</div>
3rd way: use Flex
.container {
width: 100%;
height: 600px;
background-color: #f2f2f2;
display: flex;
}
.sidebar {
background-color: red;
width: 180px;
height: 600px;
}
.main-content {
background-color: green;
flex: auto;
}
<div class="container">
<div class="sidebar">Sidebar</div>
<div class="main-content">Main Content</div>
</div>
Flexbox is probably the nicest solution, but saidly old browsers don't support it.
4th way of doing this is the oldfasioned way with faking tables:
.container {
width: 100%;
height: 600px;
background-color: #f2f2f2;
display: table;
}
.sidebar {
background-color: red;
width: 180px;
display: table-cell;
}
.main-content {
display: table-cell;
background-color: green;
}
<div class="container">
<div class="sidebar">Sidebar</div>
<div class="main-content">Main Content</div>
</div>

First div fixed, second full width. Cant change structure of html

Have problem. I have this code.
<div class="main">
<div class="sidebar"></div>
<div class="content"></div>
</div>
I need to make two colums.
"Sidebar" must have fixed width 200px;
And "content" all remaining width to fullscreen.
I cant change the structure of html code, just css.
if absolute position is ok, you can use it to say left:200px; right:0 and get all the space you need
fiddle : http://jsfiddle.net/h2udmqhn/
.main {
position: relative;
height: 300px;
width: 300px;
border: 1px solid black;
}
.sidebar {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 200px;
background: red;
}
.content {
position: absolute;
top: 0;
left: 200px;
right: 0;
height: 200px;
background: blue;
}
<div class="main">
<div class="sidebar"></div>
<div class="content"></div>
</div>
Use float: left for .sidebar and left margin for .content:
.sidebar {float: left; width: 200px; background: red;}
.content {background: green; margin: 0 0 0 200px;}
http://jsfiddle.net/orty5qtj/1/
Another option is to use calc, which is unsupported in IE8. The solution above works fine in all browsers.
Try this :
.sidebar {
float: left;
min-height: 50px;
background: red;
width: 200px;
}
.content {
background : yellow;
margin-left: 200px;
min-height: 50px;
}
https://jsfiddle.net/Saiyam/5krmkkkx/3/
There a couple of simple ways to do this without the need for calc, margins or absolute positioning. Both of the following ways have the added bonus of keeping the columns the same height as each other
Using display table (compatible to back ie8)
.main {
display: table;
width: 100%;
}
.main > div {
display: table-cell;
}
.sidebar {
width: 200px;
background: blue;
}
.content {
background: red;
}
<div class="main">
<div class="sidebar">200px</div>
<div class="content">the rest</div>
</div>
Using flex (for newer browsers only unless used with the browser prefix):
.main {
display: flex;
width:100%;
max-width:100%;
}
.sidebar {
width: 200px;
flex: 0 0 200px;
background-color:blue;
}
.content {
background-color:red;
flex: 1 0 auto;
}
<div class="main">
<div class="sidebar">200px</div>
<div class="content">the rest</div>
</div>

How to position header, sidebox and mainbox?

Demo: http://jsfiddle.net/zz1ou0bv/
HTML:
<div class="header">header</div>
<div class="sidebox">sidebox</div>
CSS:
.header {
background-color: red;
height:60px;
}
.sidebox {
width: 210px;
background-color: blue;
color: black;
position: absolute;
bottom: 0;
top: 0;
}
How do I position the blue box just under the red box? I could for example add top: 68px; to the .sidebox to fix the problem but is there any other way to position it automatically, I would like to change the header height without being forced to change the top tag in sidebox to make it fit.
How do I position a brand new div that takes up the WHOLE white area under the red box and besides blue box? This should be automatic in case header/sidebox changes height/width. The green content should be replaced with this new div: http://i.gyazo.com/a41107cb7c1844b439f045ad85d40aec.png
If you always want the sidebar to occupy 100% of the window you could try this approach:
html, body { height: 100%; }
.header {
background-color: red;
height:60px;
}
.sidebox {
width: 210px;
background-color: blue;
color: black;
bottom: 0;
top: 0;
min-height: 100%;
}
http://jsfiddle.net/zz1ou0bv/2/
Here's my attempt: http://jsfiddle.net/rL8z9bm0/ .
Regarding your second question you should always have some kind of relation between the blue and yellow boxes, either pixels or percentage (better)
.sidebox {
width: 30%;
...
}
.content {
left:30%;
...
}
Seems like a good use case for the flexbox here:
Fiddle with Flexbox
Here's the relevant source:
HTML
<div class="wrapper">
<div class="header">header</div>
<div class="sidebox">sidebox</div>
<div class="content">content</div>
</div>
CSS
body {
height: 100vh;
margin: 0;
}
.wrapper {
display: flex;
flex-flow: row wrap;
height: 100%;
}
.header {
background-color: red;
height:60px;
flex: 1 100%;
}
.sidebox {
flex: 1 auto;
background-color: blue;
color: white;
}
.content {
flex: 4 auto;
background-color: sienna;
color: white;
height: calc(100% - 60px);
}
Hope that helps.

How to get CSS table-cell to 100% for responsive design [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do I get the following setup with CSS to work?
jsfiddle
http://jsfiddle.net/16ex38mL/2/
Basically, I intend to put an input box to #header-nav-content-search and let the div and the one below it resize responsively to 100% of the remaining width.
I have two static width columns. One is the first one with 240px, and one is the last one with 200px.
code
#header-nav-content-search {
width: 100%;
}
didn't do the trick.
I have concentrated on reducing the HTML markup needed. The following example is mainly based on that excellent sketch of yours, so it will need some tweaking.
Basic Idea
Create a three "column" CSS table with the center cell remaining fluid:
<div class="table">
<div class="cell"></div>
<div class="cell center">I contain 4 fluid divs with the class ".inner"</div>
<div class="cell"></div>
</div>
The center cell contains your 4 inner boxes with the class .inner
Basic CSS Styles
box-sizing: border-box will allow us to calculate percentage width including padding and borders
The main container, .table, is given a fixed height (could be changed to percentage)
The .inner divs are display: inline-block and are given appropriate percentage widths and fixed heights equal to half the containers height
The left and right columns are given their fixed widths
.table is given an appropriate min-width to prevent the inner divs from overlapping
Note: In the HTML markup, the inner divs closing and opening tags have no space between them. This is important as it prevents a gap that is present with inline elements.
Refer to this article for more information.
CSS / HTML / Demo
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
}
body {
margin: 0;
}
.table {
display: table;
height: 300px;
width: 100%;
min-width: 600px;
table-layout: fixed;
}
.cell {
display: table-cell;
vertical-align: top;
}
.left {
width: 240px;
}
.right {
width: 200px;
border-left: solid 1px #000;
}
.inner {
display: inline-block;
height: 150px;
vertical-align: top;
}
.center-left {
width: 30%;
}
.center-right {
width: 70%;
}
/* Borders */
.table {
border: solid 1px #000;
}
.inner {
border-bottom: solid 1px #000;
border-left: solid 1px #000;
}
.center-right .inner {
border-right: solid 1px #000;
}
.inner:nth-child(3),
.inner:nth-child(4) {
border-bottom: none;
}
<div class="table">
<div class="cell left">
240px width
</div>
<div class="cell center">
<div class="inner center-left">
30% width 50% height
</div><div class="inner center-right">
70% width 50% height
</div><div class="inner center-left">
30% width 50% height
</div><div class="inner center-right">
70% width 50% height
</div>
</div>
<div class="cell right">
200px width
</div>
</div>
I wouldn't do it that way. Here's one way to get you started.
DEMO: http://jsbin.com/faveca/1/
http://jsbin.com/faveca/1/edit
HTML:
<header>
<div class="fixed-width-240 eq">
240px column fixed width what about is it equal to the others, yes it is.
</div>
<div class="fluid eq">
fluid column
</div>
<div class="fixed-width-200 eq">
200px column
</div>
</header>
CSS
body,
html {
margin: 0;
padding: 0;
}
header div,
header div:before,
header div:after {
box-sizing: border-box
}
header {
border: 2px solid #000
}
header:after {
content: "";
display: table;
clear: both;
}
.fixed-width-240 {
z-index: 1;
position: relative;
background: red;
}
.fixed-width-200 {
z-index: 2;
position: relative;
background: orange;
}
.fluid {
position: relative;
z-index: -1;
background: #ccc;
}
#media (min-width:700px) {
header {
overflow: hidden
}
header .eq {
padding-bottom: 99999px;
margin-bottom: -99999px;
}
.fixed-width-240,
.fixed-width-200 {
float: left
}
.fixed-width-240 {
width: 240px;
width: 240px;
margin-right: -240px;
border-right: 2px solid #000;
}
.fixed-width-200 {
float: right;
z-index: 2;
width: 200px;
margin-left: -200px;
border-left: 2px solid #000;
}
.fluid {
float: left;
padding: 0 220px 0 260px;
width: 100%;
}
}

Resources