How to position div's like this with CSS? - html

Trying to maintain an space between them, I'd like to position some divs in a specific form for my website, and then add content to them. I have been styling the pages responsively, so I would like to know if position those divs this way with responsiveness is posible. The result I guess it could be something like this:
Being X and Y the two div's I've already created (for the header and the menu) and Z the footer. The div's I'd like to put in those positions are those DIV 1, DIV 2 and DIV 3.
For the moment the two main parts above (header and menu) are styled like this:
* {
margin:0;
padding:0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
header {
width:90%;
height:30%;
margin: 0 auto;
background-color:darkblue;
color:white;
z-index: 105;
position:relative;
}
nav {
width:90%;
height:22%;
margin: 0 auto;
background-color:skyblue;
}
And the HTML I have for the moment for those DIV 1, DIV 2 and DIV 3 is this:
<div id="content">
<div id="leftinfo">
<ul>
<li>INFO</li>
<li>ABOUT</li>
</ul>
</div>
<div id="hcontent">
<div class="tophcontent">
</div>
</div>
<div id="hcontent2">
<p></p>
</div>
</div>
I've been struggling on how to position it like this, maintaining the web flow with the other divs. Any help or tips about it would be very appreciated.

I guess that you want something like
https://jsfiddle.net/2dxzr1mv/3/
*{
box-sizing:border-box;
white-space: nowrap;
}
div{
padding:0;
margin:0;
}
.div1{
width:20%;
height:100%;
background-color:red;
min-height:100vh;
display:inline-block;
}
.wrapper{
width:80%;
min-height:100vh;
display:inline-block;
}
.div2,.div3{
width:100%;
min-height:50vh;
background-color:yellow;
word-wrap: break-word;
overflow:hidden;
white-space: normal;
}
.div3{
background-color:blue;
}

I have tried something like your picture.
CSS
* {
margin:0;
padding:0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
#content {
table-layout: fixed;
display: table;
width: 100%;
height: 100%;
}
#content > div {
display: table-cell;
}
.tophcontent {
height: 40px;
margin: 0 10px;
border: 2px solid orange;
}
.midhcontent {
margin: 10px;
margin-bottom: 0;
height: calc(100% - 46px);
border: 2px solid green;
}
#leftinfo {
border: 3px solid gray;
width:120px;
}
HTML
<div id="content">
<div id="leftinfo">
<ul>
<li>INFO</li>
<li>ABOUT</li>
</ul>
</div>
<div id="header">
<div class="tophcontent">
</div>
<div class="midhcontent">
</div>
</div>
</div>
Here is my JSfiddle

Here is a sample using display: flex. It has today about 94% browser support, which I think one can consider very good.
A great benefit with this, it is fully dynamic regarding the content in each of the elements compared to float and inline-block versions.
html, body {
margin: 0;
height: 100%;
min-height: 100%;
}
* {
box-sizing: border-box;
}
.container {
min-height: 100%;
display: flex;
flex-direction: column;
}
.info, .about, .content-left, .content-right-top,
.content-right-bottom, .footer {
border: 1px solid;
}
.wrapper {
flex: 1;
display: flex;
min-height: 100%;
}
.content-left, .wrapper-inner {
flex: 1;
}
.wrapper-inner {
display: flex;
flex-direction: column;
min-height: 100%;
}
.content-right-top, .content-right-bottom {
flex: 1;
}
<div class="container">
<div class="info">
Info<br>
2 lines
</div>
<div class="about">
About<br>
in<br>
3 lines
</div>
<div class="wrapper">
<div class="content-left">
Left take all the rest of the space
Left take all the rest of the space
Left take all the rest of the space
Left take all the rest of the space
Left take all the rest of the space
Left take all the rest of the space
Left take all the rest of the space
Left take all the rest of the space
Left take all the rest of the space
</div>
<div class="wrapper-inner">
<div class="content-right-top">
Right - Top
Right - Top
Right - Top
Right - Top
Right - Top
Right - Top
Right - Top
Right - Top
</div>
<div class="content-right-bottom">
Right - Bottom
Right - Bottom
Right - Bottom
Right - Bottom
Right - Bottom
Right - Bottom
Right - Bottom
</div>
</div>
</div>
<div class="footer">
Footer<br>
has 2 lines
</div>
</div>

Is it something like this you mean ?
Codepen example
body, html{
height:100%;
}
#wrapper{
position:relative;
width: 90%;
height:100%;
margin:0 auto;
}
.left{
float:left;
width:30%;
height:100%;
background:green;
}
.top-right{
position:absolute;
right:0;
left:31%;
height:30%;
float:left;
background:blue;
}
.bottom-right{
position:absolute;
right:0;
left:31%;
bottom:0;
top:32%;
float:left;
background:red;
}

Related

Centered div containing a fixed fix

Can a centered div contain a fixed div?
I would like to center a fixed nav box and a scrolling main box side-by-side together in any big window. I can fix the nav box in a non-centered view (first code below), or center the view with a scrolling nav box (second code), but have been unable to combine these two traits, after many tries including multiple nested wrappers. Maybe fixed and centering are incompatible? If this is possible I will appreciate seeing how it is done. Thank you.
(1) Fixed nav box, scrolling main box, not centered:
<style>
#nav {position:fixed; top:50px; left:80px; width:270px; height:400px; background:#ddd }
#main {position:absolute; top:50px; left:380px; width:800px; height:1200px; background:#eee}
</style>
<div id="nav">
</div>
<div id="main">
</div>
</div>
(2) Centered, nav box scrolls (#bigscreen here is just a temp to show the centering):
<style>
#bigscreen {width:2000px; height:1200px;}
#window {width:100%; height:100%; display:flex; justify-content:center; align-items:center;}
#wrap {position:relative; width:1125px;height:800px; background:bisque}
#nav {position:absolute; top:54px; left:20px; width:270px; height:400px; background:#ddd }
#main {position:absolute; top:54px; left:300px; width:800px; height:640px; background:#eee}
</style>
<div id="bigscreen">
<div id="window">
<div id="wrap">
<div id="nav">
</div>
<div id="main">
</div>
</div>
</div>
</div>
First, you need to create a wrapper(#content-wrapper in my answer) that includes #nav and #main divs. Then add display: flex for that wrapper to set child divs horizontally. To align the wrapper into the center, define a fixed width(1100px in my answer) and set left, right margin to auto.
Then add position: relative to the wrapper. That allows you to play with css position property in child divs within the wrapper.
Finally, add position: fixed for #nav div to set fixed position and add position: absolute & left: 300px(in my answer) for #main div to scroll in the wrapper.
See below.
<style>
#content-wrapper {
position: relative;
width: 1100px;
margin: 50px auto;
display: flex;
}
#nav {
position: fixed;
width: 270px;
height: 400px;
background: #ddd
}
#main {
position: absolute;
left: 300px;
width: 800px;
height: 1200px;
background: #eee
}
</style>
<div id="content-wrapper">
<div id="nav"></div>
<div id="main"></div>
</div>
You mean like this?
* {
box-sizing: border-box;
}
body {
margin: 0;
}
#overlay {
position: fixed;
width: 100vw;
height: 100vh;
background: rgba(0,0,0,.8);
padding: 10px;
display: flex;
}
#nav {
margin-right: 10px;
width:20%;
background: rgb(240,240,240);
}
#main {
width: 80%;
background: rgb(240,240,240);
padding: 10px;
overflow: auto;
}
#content {
height: 2000px;
background-color: rgb(220, 220, 220);
font-family: arial;
text-align: center;
line-height: 2000px;
margin-bottom: 10px;
}
<div id="overlay">
<div id="nav">
</div>
<div id="main">
<div id="content">Overflow content</div>
</div>
</div>
If this is the result you're looking for you should focus only on the following lines, they are the most important ones to achieve this.
#overlay {
position: fixed;
width: 100vw;
height: 100vh;
display: flex;
}
#nav {
width:20%;
}
#main {
width: 80%;
overflow: auto;
}
Edit: You can click on full page to see the result in expanded height.

Fixed header, fixed footer dynamic content

I'm struggling with my dynamic content. So let me explain in a picture:
So my html looks like:
<div id="header"> ... </div>
<div id="container">
<div class="block"> ... </div>
<div class="block"> ... </div>
<div class="block"> ... </div>
<div class="block"> ... </div>
</div>
<div id="footer"> ... </div>
My question: How can I get my container be fluid and the header and footer be fixed? The blocks in the container are set on 50% height and width, so only the container has to be the 100% height (- the fixed header and footer).
You can do this with box-sizing property.
Like so:
FIDDLE
(the example I use here assumes a header of 64px height and footer of 30px height)
Markup
<header>header</header>
<div class="container">
<div id="content">
<div class="block">block1</div><!--
--><div class="block">block2</div><!--
--><div class="block">block3</div><!--
--><div class="block">block4</div>
</div>
</div>
<footer>footer</footer>
CSS
html,body
{
height: 100%;
}
header,footer,div
{
width: 100%;
}
.container
{
height: 100%;
background: pink;
margin: -64px 0 -30px;
padding: 64px 0 30px;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#content {
overflow:auto;
height:100%;
}
.block
{
width: 50%;
height: 50%;
display: inline-block;
border: 1px solid yellow;
-moz-box-sizing: border-box;
box-sizing: border-box;
text-align: center;
}
header
{
height: 64px;
background: purple;
position: relative;
z-index:1;
}
footer
{
height: 30px;
background: gray;
position: relative;
z-index:1;
}
Like this
working demo
css
*{
margin:0;
padding:0;
}
#header{
height:100px;
background-color:red;
position:fixed;
top:0;
width:100%;
}
#footer{
height:100px;
background-color:green;
position:fixed;
bottom:0;
width:100%;
}
#container{
background-color:#F7F7F7;
width:100%;
top:100px;
position:relative;
}
.block{
width:50%;
background-color:gray;
float:left;
}
Use float to make it fluid desgins.Be Caution about width part when border or padding is added. Because those are calculated with width. As Danield mentioned you can use Box sizing
Obviously Measurements should be in percentage to make it responsive Or alternatively you can write media queries.
You tagged your question with twitter-bootstrap. Here is a more Bootstrap friendly approach:
Demo: http://bootply.com/render/88297
Code: http://bootply.com/88297
This uses a somewhat standard BS header/nav and fixed footer. Then the center container uses table,table-row and table-cell to size the center boxes accordingly at 50/50.
.center-container {
height:100%;
display: table;
width:100%;
margin:0;
}
.center-row {
height:50%;
width:100%;
display: table-row;
}
.center-row > div {
height:100%;
width:50%;
display: table-cell;
border:1px solid #eee
}

CSS: 4 row fluid with scrollable center?

We're building an application to run on both desktop and mobile, so we're making our site as dynamic as we can.
In the new design, we've got a full width, fixed top, fixed-height header, followed by a max-width header, fixed position header, max-width footer bottom-fixed, and a center scrollable area to fill up the rest of the vertical space.
I've got the headers and footers fixed and seemingly working right, but the center body isn't filling the center space.
Here's the code and fiddle I've been working in: http://jsfiddle.net/chazthetic/KE5cX/2/
<div id="top">
<div style="float:left;color:white;">a<br /></div>
<div style="float:right;color:white;">a<br /></div>
</div>
<div id="sub">
<div id="mid">
<div id="inside">
<div id="insideHeader">
<div style="float:left;color:white;">a<br /></div>
<div style="float:right;color:white;">a<br /></div>
</div>
<div id="insideInner">
<div id="div" style="float:left;color:white;">a
<div id="div" style="float:right;color:white;">1
</div>
</div>
<div id="bot">
<div style="float:left;color:white;">a<br /></div>
<div style="float:right;color:white;">a<br /></div>
</div>
</div>
</div>
And the accompanying CSS:
* {
margin: 0;
}
html, body {
margin: 0;
}
#top {
height: 100px;
width: 100%;
background: #f00;
z-index: 5;
margin:0px;
padding:0px;
position:fixed;
top:0px;
}
#sub {
position:relative;
width: 100%;
margin:100px 0 0;
height:100%;
}
#mid {
background: #222;
width:100%;
max-width:400px;
height:100%;
margin:0 auto;
}
#push {
height: 150px;
}
#inside {
width:100%;
height:100%;
padding:0 0 0 0;
margin:50px 0 50px;
overflow:auto;
}
#insideHeader {
height:50px;
background: #0ff;
width:100%;
margin:100px auto 0;
top:0px;
position:fixed;
max-width:400px;
}
#insideInner {
height:100%;
width:100%;
display:table;
overflow:auto;
}
#inside #div {
height: 50px;
width: 50px;
background: #888;
border: 1px solid #fff;
}
#bot {
position: fixed;
bottom: 0;
height: 50px;
width: 100%;
max-width: 400px;
background: #0ff;
z-index: 2;
margin:0px auto;
left:auto;
right:auto;
}
Am I missing something to make the inside div work right? Is this solution even possible?
I figured it out!
Turns out it is possible and I was missing height:100%; on the body tag.
Once I added that, the center area filled up the space and voila, it scrolled! I also needed to add a bit of padding to the insideInner div (thanks #alireza) so the header didn't overlay any of it.

Fitting html div to screen with a scrollbar

I'm creating a site where the basic design consists of a few blocks on top of each other, something like this:
The first three divs are of set height and width, and the main area is also of a fixed width, with the whole thing centered horizontally on the screen. I want the main area to extend to the bottom of the screen, whatever the screen size and proportions, and to use a scroll bar within it if the contents extends beyond the bottom of the screen.
The problem I have found is that to use a scroll bar it seems you need an absolute height, so I haven't been able to find any method for fitting it and being able to scroll through the contents at the same time.
Any ideas?
Here's one way of doing this. I know there might be too many divs that are just for the look of the page, making it not 100% semantic. Anyway, here you go:
http://jsfiddle.net/vSt3Z/
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="content">
<div class="inner">
<div class="scroller">
Content
</div>
</div>
</div>
And the CSS:
.one, .two, .three {
height: 40px;
margin: 0;
padding: 0;
}
.content {
background: yellowgreen;
position: absolute;
top: 0;
left: 0;
z-index: -1;
padding-top: 120px;
width: 100%;
height: 100%;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box;
}
.content .inner {
height: 100%;
overflow-y: scroll;
}
.content .inner .scroller {
height: 1200px;
}
Please ignore this:
* {
margin: 0;
padding: 0;
}
it's there just to remove an annoying default padding from jsfiddle
Use calc with min-height:
HTML:
<div class="first block"></div>
<div class="second block"></div>
<div class="third block"></div>
<div class="main"></div>
CSS:
html,body{
height:100%;
width:100%;
padding:0;
margin:0;
}
.block{
width:100%;
height:100px;
}
.first{
background:red;
}
.second{
background:blue;
}
.third{
background:yellow;
}
.main{
min-height: calc(100% - 300px);
width:100%;
background:green;
}
JSFiddle
caniuse calc
In my opinion this is the simplest method:
DEMO: http://jsfiddle.net/ZPU5Z/
This does not put a scroll bar on the main #content section only but on the whole document. Unless you have a really compelling reason to do otherwise I suggest keeping things simple (and therefore highly compatible too!).
HTML
<div id="fixed-header">
<div id="header">Header</div>
<div id="bar1">Bar 1</div>
<div id="bar2">Bar 2</div>
</div>
<div id="content">
Main area
</div>
CSS
* {
margin: 0;
padding: 0;
border: 0;
color: white;
}
body {
background-color: blue;
}
#fixed-header {
position: fixed;
top: 0;
width: 100%;
}
#header {
background-color: black;
text-transform: uppercase;
height: 50px;
}
#bar1 {
height: 25px;
background-color: red;
}
#bar2 {
height: 25px;
background-color: green;
}
#content {
padding-top: 100px; /* header + bar1 + bar2 */
}

how to make the img at the left side and the texts at the right side vertically line up

css
.container{
height: 250px;
padding 10px 0;
}
.col-left{
display: inline-block;
background-image:url("support.png");
height:235px;
width:300px;
}
.col-right{
display: inline-block;
width:600px;
}
html
<div class="container">
<div class="col-left"></div>
<div class="col-right">
<h1>this is my title</h1>
<p>to reach their Potential</p>
</div>
</div>
Question:
I want the img at the left and the texts at the right
to show on the same line.
vertically line up(texts appear in the middle position of img)
how could I do this?
I guess this is what you want. Live Demo
.container {
height: 250px;
padding 10px 0;
}
.col-left {
display: inline-block;
background-image:url("http://www.lois-systems.co.uk/wp-content/uploads/2012/08/support.png");
height:235px;
width:300px;
vertical-align:middle;
}
.col-right {
display: inline-block;
width:600px;
vertical-align:middle;
}
.col-right h1, .col-right p {
margin:0;
}
As far as I understand your question I found that Float will solve your problem. Float works same as inline-block. You can learn more about this from this url http://www.vanseodesign.com/css/inline-blocks/
So, as far as my knowledge concert I will go for following solution, Hope this will help you.
CSS :
.container {width:100%;height: 250px;padding 10px 0;}
.col-left {float:left;background-image:url("support.png");height:235px; width:30%;}
.co-right {float:left;width:70%}
JSFiddle : http://jsfiddle.net/ugQCU/
Here is the code you want to achieve try this i've just added a div for displaying vertical align use this and let me know
Html code is
<div class="container">
<div class="col-left"></div>
<div class="col-right">
<div class="col-right-wrap">
<h1>this is my title</h1>
<p>to reach their Potential</p>
</div>
</div>
</div>
and css
.container{
height: 250px;
padding 10px 0;
}
.col-left {
float:left; /*give a direction where you want */
display: inline-block;
background-image:url("images/support.png");
height:235px;
width:300px;
}
.col-right {
float:left; /*give a direction where you want */
display: inline-block;
width:600px;
}
/* align however you want adjust this below code according to you */
.col-right-wrap{
display:table-cell;
vertical-align:middle;
}
I think something like this should be what you want:
HTML:
<div class="container">
<div class="col-left">
<img src="http://placehold.it/300x235" />
</div>
<div class="col-right">
<h1>this is my title</h1>
<p>to reach their Potential</p>
</div>
</div>
CSS:
.container {
width:100%;
height: 250px;
padding 10px 0;
}
.col-left {
float:left;
background-image:url("support.png");
height:235px;
width:300px;
}
.col-right {
float:left;
width: 600px;
padding-top: 50px;
}
LIVE DEMO
Do note that for the placement of the text I used padding, which may not be an ideal solution.
HTML code the same and CSS:
.container {
position: relative;
height: 235px;
padding 10px 0;
}
.col-left {
position: absolute;
background: #AAA;
height:235px;
width:300px;
}
.col-right {
display: table-cell;
vertical-align: middle;
height: 235px;
padding-left: 310px;
}
Demo:
http://jsfiddle.net/zWNCP/2/