css page layout - foreground "overlying" background - html

I recently started to get familiar with web application development and struggle with a basic css layout question.
The desired layout is a central area overlapping several background elements.
See here:
This image shows the desired layout:
I have also hacked it on codepen:
Codepen
Where I use html divs to build the background around the central area.
<div class="page-element">
<div class="element-side"></div>
<div class="element-middle"></div>
<div class="element-side"></div>
</div>
and css to place it like that:
.page-element {
display: flex;
}
.element-middle {
width: 90%;
}
.element-side {
flex: 1;
}
But the way I did it there doesn´t look like proper layout style.
What is the right way in modern css to do such a layout?

Since most of your structure is fine I kept it, and the major changes is how to create the left/right gutter and the overflow on the header/footer.
For the left/right gutter I simply removed the side elements, kept the width: 90% and used margin: 0 auto to horizontally get things centered.
For the header/footer overflow I removed the below-nav/footer1 elements, added pseudo elements on the main (done with CSS only) and an extra bottom/top padding on the header/footer so the pseudo won't overlap their content.
.nav1, .nav2, .nav1 > div {
display: flex;
background: #f05d23;
}
.nav-elem {
flex: 1;
}
.nav1 > div, .nav2 > div {
margin: 0 auto; /* added, center horiz. */
width: 90%;
text-align: center;
padding: 1em;
box-sizing: border-box;
}
.nav2 > div {
padding-bottom: 3em; /* added, avoid overlap */
}
.main {
position: relative;
display: flex;
background: #e4e6c3;
}
.main::before, .main::after { /* added, create overflow */
content: '';
position: absolute;
left: 5%;
width: 90%;
height: 3em;
background: #f7f7f2;
}
.main::before {
top: 100%;
}
.main::after {
bottom: 100%;
}
.main section {
margin: 0 auto; /* added, center horiz. */
width: 90%;
padding: 1em;
box-sizing: border-box;
background: #f7f7f2;
}
.footer {
display: flex;
background: #2d2a32;
}
.footer section {
margin: 0 auto; /* added, center horiz. */
width: 90%;
padding: 1em;
padding-top: 4em; /* added, avoid overlap */
box-sizing: border-box;
text-align: center;
color: #f7f7f2;
}
body {
padding: 0;
margin: 0;
}
h1, h2 {
font: bold 2em Sans-Serif;
margin: 0 0 1em 0;
}
h2 {
font-size: 1.5em;
}
p {
margin: 0 0 1em 0;
}
<div class="nav1">
<div>
<div class="nav-elem">Plan</div>
<div class="nav-elem">AdminPlan</div>
<div class="nav-elem">Help</div>
<div class="nav-elem">Login</div>
</div>
</div>
<div class="nav2">
<div>
<h1>Pageheader.</h1></div>
</div>
<div class="main">
<section>
<h1>Main Content</h1>
<p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by
arches into stiff sections.</p>
<p>The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me? " he thought. It wasn't a dream.</p>
<p>His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had
recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then
turned to look out the window at the dull weather. Drops</p>
</section>
</div>
<div class="footer">
<section>
<h2>Footer</h2>
<p>Whatever goes into a page </p>
</section>
</div>
Note, the .footer section's padding value in padding-top: 4em; can of course be added to the existing shorthand property like this: padding: 4em 1em 1em; (/* top | horizontal | bottom */)

Here's an example of how to achieve that - sorry but have been typing.on mobile.
https://jsfiddle.net/su5w1595/
.wrapper {
Position: absolute;
Height:100%;
Width:100%;
}
.top {
Position:relative;
Background-color:blue;
Width:100%;
Height:30%;
}
.middle {
Position:relative;
Background-color:red;
Width:100%;
Height:30%;
}
.footer {
Position:relative;
Background-color:yellow;
Width:100%;
Height:30%;
}
.main {
Position:absolute;
Top:20%;
Left:10%;
Height: 60%;
Width:80%;
Margin:auto;
Background-color: white;
}

Related

Why does this flex item not change according to its container's height?

What I'd Like to Happen
I have a .box whose height is constrained to stay within the viewport. Nested a couple levels within this element I have a .content-body that contains a header and a .content item. I'd like for just .content to shrink (with scrollbars displayed) when the height of .box is small enough.
What Happens
In Chrome it all works as intended (like the pic above), but in almost every other browser, it gets cutoff because the content doesn't want to shrink.
The Code
I made a CodePen to demonstrate the problem. Simply resize the height of the browser and observe how the content element responds. Here's the same code on StackOverflow for posterity.
/* the most relevant rules to the problem */
.container{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
}
.box {
display: flex;
flex-direction: column;
max-height: calc( 100vh - 50px );
position: relative;
overflow: hidden;
}
.box-body {
display: flex;
flex-flow: column;
}
.content-body{
display: flex;
flex-direction: column;
}
.content{
overflow: auto;
}
/* other stuff (appearance mostly) that probably doesn't matter */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.box{
/* appearance */
width: 600px;
border-radius: 20px;
border: 1px solid #bbb;
}
.box-body{
/* appearance */
padding: 20px;
font-family: sans-serif;
font-size: 18px;
}
.content-header{
/* appearance */
background: #ddd;
padding: 10px;
margin-bottom: 10px;
}
.box-label {
/* appearance */
padding: 30px 10px;
background: teal;
color: white;
font-family: sans-serif;
font-size: 20px;
}
.content{
padding: 10px;
}
p {
margin: 10px 0px;
}
<div class="container">
<div class="box">
<div class="box-label">
Header
</div>
<div class="box-body">
<div class="content-body">
<div class="content-header">
Content Header
</div>
<div class="content">
<p>Mr do raising article general norland my hastily. Its companions say uncommonly pianoforte favourable.</p>
<p>On projection apartments unsatiable so if he entreaties appearance. Rose you wife how set lady half wish. Hard sing an in true felt. Welcomed stronger if steepest ecstatic an suitable finished of oh. Entered at excited at forming between so
produce.</p>
<p>Answer misery adieus add wooded how nay men before though. Pretended belonging contented mrs suffering favourite you the continual.</p>
</div>
</div>
</div>
<div class="box-label">
Footer
</div>
</div>
</div>
An initial setting on flex items is min-height: auto.
This means that a flex item, in a column-direction container (like the ones in your code), cannot be shorter than its content.
As you have observed, flex items in Chrome do what you want right out of the box.
Firefox, Edge and possibly other browsers need you to override the default setting.
Add this to your code:
.box-body {
min-height: 0; /* new */
}
.content-body {
min-height: 0; /* new */
}
revised fiddle
Full explanation: Why doesn't flex item shrink past content size?

In IE11, why does this flex item not change according to its container's height?

This question is almost the same as one of my previous questions, but it's specifically focused on IE11, as the answer of the previous question worked on all other browsers. IE11 is a special case because of its many flexbox bugs, hence the new question.
What I'd Like to Happen
I have a .box whose height is constrained to stay within the viewport. Nested a couple levels within this element I have a .content-body that contains a header and a .content item. I'd like for just .content to shrink (with scrollbars displayed) when the height of .box is small enough.
What Happens
In IE11 only, it gets cut off because the content doesn't want to shrink.
The Code
I made a CodePen to demonstrate the problem. Simply resize the height of the browser and observe how the content element responds. Below is the same code on StackOverflow for posterity. The ideal solution will leave this working the same on all other browsers, but fix it for IE11.
/* the most relevant rules to the problem */
.container{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
}
.box {
display: flex;
flex-direction: column;
max-height: calc( 100vh - 50px );
position: relative;
overflow: hidden;
}
.box-body {
display: flex;
flex-flow: column;
min-height: 0;
}
.content-body{
display: flex;
flex-direction: column;
min-height: 0;
overflow: hidden;
}
.content{
overflow: auto;
}
/* other stuff (appearance mostly) that probably doesn't matter */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.box{
/* appearance */
width: 600px;
border-radius: 20px;
border: 1px solid #bbb;
}
.box-body{
/* appearance */
padding: 20px;
font-family: sans-serif;
font-size: 18px;
}
.content-header{
/* appearance */
background: #ddd;
padding: 10px;
margin-bottom: 10px;
}
.box-label {
/* appearance */
padding: 30px 10px;
background: teal;
color: white;
font-family: sans-serif;
font-size: 20px;
}
.content{
padding: 10px;
}
p {
margin: 10px 0px;
}
<div class="container">
<div class="box">
<div class="box-label">
Header
</div>
<div class="box-body">
<div class="content-body">
<div class="content-header">
Content Header
</div>
<div class="content">
<p>Mr do raising article general norland my hastily. Its companions say uncommonly pianoforte favourable.</p>
<p>On projection apartments unsatiable so if he entreaties appearance. Rose you wife how set lady half wish. Hard sing an in true felt. Welcomed stronger if steepest ecstatic an suitable finished of oh. Entered at excited at forming between so
produce.</p>
<p>Answer misery adieus add wooded how nay men before though. Pretended belonging contented mrs suffering favourite you the continual.</p>
</div>
</div>
</div>
<div class="box-label">
Footer
</div>
</div>
</div>
IE requires height attribute defined for scrolling. If you change your max-height to height for your .box element, it will work in IE as well.

Two DIVs 1 with static width, other fliud, but how to get right div to stack UNDER # breakpoint?

I have two divs next to each/side by side..
The LEFT div has a FLUID width.
The RIGHT div has a static wdth.
When I resize the screen/browser... it work great! (and as intended).
However because of the way it was set up:
(Fiddle example: http://jsfiddle.net/VHcPT/384/)
The RIGHT div in physically first in the mark-up..(and floated RIGHT).
However at say 768px breakpoint.. I need this RIGHT (static) DIV to stack UNDER the LEFT div.. how can I achieve this?
If I physically have the RIGHT div AFTER the LEFT div in the markup.. it would stack as expected.. but I need to have it FIRST so the fluid/static behavior in place works as it should.
So to re-cap, its NOT about getting the two divs next to each other one fluid, one static.. its how to handle that at a responsive/breakpoint.. and get the static (RIGHT) div to stack UNDER the fluid (LEFT) div
Using the fiddle example.. the RED DIV would go UNDER (stack) the GREEN lines/div.. (the green would then be full width).. at a certain breakpoint.
and because code is required now:
HTML:
<div id="contentcontainer">
<div class="rightcontainer">mm</div>
<div class="leftcontainer">
<div class="item_1">
some text
</div>
<div class="item_2">
some text
</div>
</div>
</div>
CSS:
#directorycontainer {
padding:10px 10px;
display:table;
box-sizing: border-box;
width: 100%;
font-size: 0.8em;
font-weight: normal;
}
.directory {
background: green;
margin-right: 10px;
overflow: hidden;
padding-right: 10px;
position: relative;
}
.mapcontainer {
background: red;
display:table;
width:240px;
height:480px;
float:right;
position:relative;
overflow: hidden;
}
.providercontainer{
background-color: #f7f9fb;
border: 1px solid #e1dacd;
display: table;
margin-bottom: 0.625em;
padding: 0;
width: 100%;
}
OK well looks like this works and should be an acceptable answer/solution:
Fiddle:
http://jsfiddle.net/VHcPT/389/
HTML/Markup:
<div id="contentcontainer">
<div class="leftcontainer">
<div class="item_1">
some text
</div>
<div class="item_1">
some text
</div>
</div>
<div class="rightcontainer">mm</div>
</div>
CSS:
* {box-sizing: border-box;}
#contentcontainer {
padding:10px 10px;
display:table;
box-sizing: border-box;
width: 100%;
font-size: 0.8em;
font-weight: normal;
}
.leftcontainer {
background: green;
overflow: hidden;
padding: 5px;
float:left;
width:calc(100% - 240px);
}
.rightcontainer {
background: red;
display:table;
width:240px;
height:480px;
float:left;
position:relative;
overflow: hidden;
}
.item_1{
background-color: #f7f9fb;
border: 1px solid #e1dacd;
display: table;
margin-bottom: 0.625em;
padding: 0;
width: 100%;
}
works with whatever breakpoints you set and the elements will stack correctly.
you may like my FLEXBOX alternative to you problem. It may take a bit of practice, but it will eventually give you much more control.
The FIDDLE
Below the basic CSS structure, no other 'display', 'position' or 'overflow' needed. With this structure you can mix-match any number of fixed and/or fluid columns.
.flex--box { display: flex; flex-flow: row wrap }
.flex--fluid { flex: 1 1 auto }
.flex--fixed { flex: 0 0 auto; min-width: 240px }
/* MOBILE MQ */
#media all and (max-width: 360px) {
.flex--fluid, .flex--fixed {
flex: 1 1 auto;
}
}
Let me know if you have problem with it.
And of course, do give credit if you think it is worth it.
( BTW: I changed the colors to something less retina intensive &D )

Sticky footer margin issue

I am playing around with CSS Sticky Footer and I have an issue where...
* { margin: 0; }
Although it is designed to reset all DIV margins, this is not what I want to do. The text on every page are squished and have no margins now.
I have tried defining it on every element I want but with no success i.e...
div.wrapper, div.push, div.footer { margin: 0; }
How can I bypass it so only the necessary elements by sticky footer have a margin of 0, and the rest remain untouched?
Delete the * CSS and change the HTML one to:
html, body {
height: 100%;
margin: 0;
}
.footer * {
margin: 0;
}
I don't get it.. why use sticky footer. Try creating a css table structure like this. People underestimate CSS tables! They can be awesome in creating fluid designs..
#wrapper { display: table; height: 100%; width: 1000px; }
#wrapper > header, #wrapper > footer { display: table-row; min-height: 100px; }
#main { height: 100%; }
<div id="wrapper">
<header>
<h1>I'm a header!</h1>
</header>
<div id="main">
</div>
<footer>
<p>I'm a footer!</p>
</footer>
</div>
In this CSS example none of the other elements will be affected and both the footer and header keep their height while the mid section is fluid filling the restspace..
Also take a look at table-cell.. it allowes for horizontal structurs like a solid left side with a fluid main section.
Also to correct the text in the fotter you can edit this CSS:
font: 0.8em helvetica,arial,sans-serif;
Line 50 of the style.css
.footer p {
position: absolute;
left: 0;
bottom: 4px;
width: 700px;
padding: 0;
color: white;
font: 0.8em helvetica,arial,sans-serif;
text-align: center;
}
Why would you use a premade sticky footer if it's been created that easily?
Try:
<footer><span>Footer Content</span></footer>
with the following CSS:
footer{
position:fixed;
bottom:0px;
height:30px;
width: 100%;
z-index: 9999;
color: #595959;
font-size: 9pt;
text-align: center;
}

Centering two transparent columns using CSS

So, right to the point, here's what I want (minus the poor quality)...
http://www.hbperspective.com/alt3/site.jpg
And here's what I've got...
http://www.hbperspective.com/alt3/
I'm trying to get those two transparent columns to be centered as they are in the pic. With this CSS layout I'm having a heck of a time figuring out how to do that without causing all kinds of other problems. Here is my styling...
* {
padding: 0;
margin: 0;
}
body {
background: #000000 url('background_div.png') repeat-y center top;
color: #ffffff;
font-family: Arial, Helvetica, sans-serif;
margin: 0 auto;
}
#wrapper {
background: url('background_header_transparent.png') no-repeat center top;
width: 100%;
display: table;
overflow: hidden;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
background: #000000;
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
.container {
float: left;
position: relative;
margin-top: 100px;
}
.content {
position: relative;
float: left;
}
#contentColumn{
width: 540px;
}
#sidebarColumn {
width: 190px;
margin-left: 20px;
float: left;
display: inline;
}
#contentColumn .content {
width: 500px;
padding: 10px;
}
#sidebarColumn .content {
width: 170px;
padding: 10px;
}
* html #contentColumn .overlay { height: expression(document.getElementById("contentColumn").offsetHeight); }
* html #sidebarColumn .overlay { height: expression(document.getElementById("sidebarColumn").offsetHeight); }
The markup is pretty simple, probably be just easier to look at it from the link provided. So, like I said I'm not really sure what to do at this point to get it working the way I want. Any ideas?
div#container {
width:500px; /* Same width as both columns */
margin:auto; /* Will center the container */
}
div#col1 {
float:left; /* allows side-by-side columns */
width:250px;
}
div#col2 {
float:left;
width:250px;
}
div.clear {
clear:both; /* Stops columns from extending past border of container */
}
<div id="container">
<div id="col1"></div>
<div id="col2"></div>
<div class="clear"></div>
</div>
And for extra credit, avoid using expressions :) Instead, perform any needed logic like that with javascript, via a framework like jQuery.
There are so many gotchas creating CSS columns I would suggest using a framework instead of rolling your own. There are lots of gotchas which are browser defendant and that you may not see unless you check in IE, FF, Safari, Opera, etc.
A few good frameworks are:
YUI Grids
Blueprint CSS
Blocks (new experimental)
Rules for centering things in CSS:
The thing you're centering must be assigned a width
The margins on that block must be assigned to auto
I have it working on your site in FF3 and IE7 using
div#wrapper {
width:800px;
margin: auto;
}
div#contentColumn
{
margin-left: 20px;
}
If you want to fix up the logo (see top right) then add an extra container div immediately inside the wrapper, and apply the above width/margin to the container (as suggested by Jonathan Sampson.)