CSS height of divs not correct - html

I am trying to produce the following, basically a page broken six pieces
__________
| | | |
| | | |
|__|__|__|
| | | |
| | | |
| | | |
----------
But what I'm getting looks like this:
__________
|__|__|__|
|__|__|__|
| | | |
| | | |
| | | |
| | | |
----------
Here is the css:
.paginationPage {
height: 110px;
width: 82px;
margin: 2px;
background: #F9F9F9;
border: 1px solid #CCCCCC;
}
#insertionGrid {
position: relative;
height: 100%;
width: 100%;
}
#odds, #evens {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
#odds {
z-index: 1;
}
#evens {
z-index: 2;
}
.insertionBlock {
float: left;
}
.oneSixth {
height: 50%;
width: 33%;
}
.oneEigth {
height: 25%;
width: 50%;
}
And here is the HTML:
<div class="paginationPage">
<div id="insertionGrid">
<div id="odds">
<div>
<div id="o1" class="oneSixth insertionBlock"> </div>
<div id="o2" class="oneSixth insertionBlock"> </div>
<div id="o3" class="oneSixth insertionBlock"> </div>
</div>
<div>
<div id="o4" class="oneSixth insertionBlock"> </div>
<div id="o5" class="oneSixth insertionBlock"> </div>
<div id="o6" class="oneSixth insertionBlock"> </div>
</div>
</div>
</div>
</div>
paginationPage is 110px and inside things are height 100%, except oneSixth class which is 50%. So why is the oneSixth div taking up something like 10% not 50%?

You have the right idea. You just need to get rid of the <div> tags around o1,o2,o3 and around o4,o5,o6. The o divs will automatically fit and fill into the second row, so you don't need them. If you have those div tags in there, you are fitting your o1,o2,o3 to it's parent div, and that's why they get smaller than you think they should.
See for a working example. I added a border so it's easier to see.
http://jsfiddle.net/WD79x/1/

Here's one method, using position:absolute.
Demo
HTML
<div class="paginationPage">
<div id="insertionGrid">
<div class="top">
<div id="o1" class="oneSixth insertionBlock"> </div>
<div id="o2" class="oneSixth insertionBlock"> </div>
<div id="o3" class="oneSixth insertionBlock"> </div>
</div>
<div class="bottom">
<div id="o4" class="oneSixth insertionBlock"> </div>
<div id="o5" class="oneSixth insertionBlock"> </div>
<div id="o6" class="oneSixth insertionBlock"> </div>
</div>
</div>
</div>
CSS
.paginationPage {
height: 110px;
width: 82px;
margin: 2px;
background: #F9F9F9;
border: 1px solid #CCCCCC;
position:relative;
}
.oneSixth {
width:33.33333%;
position:absolute;
}
.top .oneSixth:nth-child(even),
.bottom .oneSixth:nth-child(odd) {
background-color:red;
}
.top .oneSixth:nth-child(odd),
.bottom .oneSixth:nth-child(even) {
background-color:blue;
}
.oneSixth:nth-child(1) {
left:0;
}
.oneSixth:nth-child(2) {
left:33.33333%
}
.oneSixth:nth-child(3) {
right:0
}
.top .oneSixth {
top:0;
bottom:50%;
}
.bottom .oneSixth {
top:50%;
bottom:0;
}

#Brian Ray was right.
"However, I'm going to guess that it has to do with the container of the .oneSixth divs having no height set. Since percentage heights are based on the heights of their parents, the s with no class should have their heights set to 50% and the .oneSixth divs should have their heights set to 100%."

Related

How can I center an image horizontally with float property?

I am trying to center three images on my page. The page consists on a fixed menu on the left of the page and a div in which I want to display three photos (later it will be more photos) with float: left property on my CSS but centered horizontally, I mean that on the left and on the right of the photos have to be the same space. I want it to be responsive.
Here it is what I want:
┌───────────────────────────────────────────────────────────┐
| HEADER(The white space without anything by the moment) |
├──────────┬────────────────────────────────────────────────┤
| | DIV CONTAINER |
| | |
| | |
| | [SPACE] THE IMAGES [SPACE] |
| MENU | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
└──────────┴────────────────────────────────────────────────┘
Both spaces have to be equals.
I tried a lot of changes on my CSS to get that behaviour but I could not get it. I also take as reference the answers on this question: Align image in center and middle within div but still does not work.
Here is my actually code:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>Title</TITLE>
<style type = "text/css">
body{
margin: 0;
}
#menu{
background-color: orange;
position: fixed;
text-align: center;
left: 0px;
top: 0px;
width: 120px;
height: 100%;
margin-top: 150px;
}
#container{
position: absolute;
left: 120px;
top: 150px;
width: 100%;
height: 100%;
text-align: center;
}
img.centeredImages{
display: inline-block;
width: 350px;
height: 200px;
float: left;
margin: auto;
}
#image1{
margin: 20px 20px;
}
#image2{
margin: 20px 10px;
}
#image3{
margin: 20px 8px;
}
</style>
</HEAD>
<BODY>
<div id = "menu">
<span class = "spanMenu"> HOME </span>
<span class = "spanMenu"> LOGS </span>
<span class = "spanMenu"> QUESTIONS </span>
</div>
<div id = "container">
<img class = "centeredImages" id = "image1" src = "https://porelplanetaphoto.com/noticias/wp-content/uploads/2015/10/34059861vEE.jpg">
<img class = "centeredImages" id = "image2" src = "http://orig11.deviantart.net/9675/f/2008/333/3/b/3b5dd04be82af929dd3070cb089bcf03.jpg">
<img class = "centeredImages" id = "image3" src = "http://cdn.wonderfulengineering.com/wp-content/uploads/2014/07/HD-landscape-Photographs.png">
</div>
</BODY>
</HTML>
Thanks in advance!
1.Firstly change the width of the #container :
#container {
width: calc(100% - 120px);
}
2.And after that remove the float to the images. They are display: inline-block so they will be on one line:
img.centeredImages {
float: none;
}
I hope this will help you.
I recommend using bootstrap or foundation 5.
In bootstrap:
<div class="row">
<div class="col-md-3"><img src=""></div>
<div class="col-md-3"><img src=""></div>
<div class="col-md-3"><img src=""></div>
<div class="col-md-3"><img src=""></div>
</div>
you can add xs, lg, xl later if you want.
In foundation5:
<div class="row">
<div class="medium-3 columns offset-1"><img src=""></div>
.
.
.
</div>
you can also use block grid which is made for troubles like yours :).
By adding a display: inline-block to all the images and then text-align: center to the container element, the images will all take up the same space and be horizontally centered.
However, since you want each image to be centered horizontally but on a new line I would add display:block to the images, along with position:relative and left: 50%. Finally, I would add a negative margin on the images which would look like margin-left: -[insert half of your image width].
Hope this helps.
You can easily achieve that by wrapping each image in a container:
.image-wrapper{
width: 100%;
float: left;
}
.image-wrapper img{
width: 100%;
max-width: 350px;
max-height: 200px;
margin-bottom: 20px;
}
<div class="image-wrapper">
<image ... />
</div>
Have a look at this example:
body {
margin: 0;
}
#menu {
background-color: orange;
position: fixed;
text-align: center;
left: 0px;
top: 0px;
width: 120px;
height: 100%;
margin-top: 150px;
}
#container {
position: absolute;
top: 150px;
left: 120px;
text-align: center;
}
.image-wrapper img {
width: 100%;
max-width: 350px;
max-height: 200px;
margin-bottom: 20px;
}
.image-wrapper:last-child img {
margin-bottom: 8px;
}
.image-wrapper {
width: 100%;
float: left;
}
<div id="menu">
<span class="spanMenu"> HOME </span>
<span class="spanMenu"> LOGS </span>
<span class="spanMenu"> QUESTIONS </span>
</div>
<div id="container">
<div class="image-wrapper">
<img src="https://porelplanetaphoto.com/noticias/wp-content/uploads/2015/10/34059861vEE.jpg">
</div>
<div class="image-wrapper">
<img src="http://orig11.deviantart.net/9675/f/2008/333/3/b/3b5dd04be82af929dd3070cb089bcf03.jpg">
</div>
<div class="image-wrapper">
<img src="http://cdn.wonderfulengineering.com/wp-content/uploads/2014/07/HD-landscape-Photographs.png">
</div>
<div class="image-wrapper">
<img src="https://porelplanetaphoto.com/noticias/wp-content/uploads/2015/10/34059861vEE.jpg">
</div>
<div class="image-wrapper">
<img src="http://orig11.deviantart.net/9675/f/2008/333/3/b/3b5dd04be82af929dd3070cb089bcf03.jpg">
</div>
<div class="image-wrapper">
<img src="http://cdn.wonderfulengineering.com/wp-content/uploads/2014/07/HD-landscape-Photographs.png">
</div>
<div class="image-wrapper">
<img src="https://porelplanetaphoto.com/noticias/wp-content/uploads/2015/10/34059861vEE.jpg">
</div>
<div class="image-wrapper">
<img src="http://orig11.deviantart.net/9675/f/2008/333/3/b/3b5dd04be82af929dd3070cb089bcf03.jpg">
</div>
<div class="image-wrapper">
<img src="http://cdn.wonderfulengineering.com/wp-content/uploads/2014/07/HD-landscape-Photographs.png">
</div>
</div>
To have more images aligned on the same row change the width of .image-wrapper, and have a look at CSS media queries to obtain a better responsive result on different devices and screen sizes.

Two floating divs, one right and one left. How do I force them to stay on one line?

Alright, here's my problem: I've got a Div with a logo in it floated left, and a div with a menu in it floated right. When they touch, the menu drops to a new line, and I'd like them to be forced to stay on a single line, with the logo getting smaller instead.
| |
| +---------------+ |
|<---| logo |---> |
| | variable width| +----------------------------------------+ |
| | float left | | Menu | |
| | | | fixed width based on content | |<----->
| | | | float right | |
| | | +----------------------------------------+ |
| +---------------+ |
| |
The layout is basically:
<div class="Container">
<div class="Logo">
<a>
<img></img>
</a>
</div>
<div class="MenuContainer">
<div class="MenuInnards"></div>
</div>
</div>
Thanks for any help!
you can use "display:table" to solve the problem. By using this code you can also vertically center the content (as you show in your scheme).
HTML
<div class="Container">
<div class="Logo">
<a>
<img></img>
</a>
</div>
<div class="MenuContainer">
<div class="MenuInnards"></div>
</div>
</div>
CSS
.Container{max-width: 800px; width: 100%; display:table;}
.Logo{display:table-cell; vertical-align:middle}
.Logo a img{width: 100%; max-width: 200px}
.MenuContainer{vertical-align:middle; display:table-cell; width:400px;}
.MenuInnards{float:right;}
Try this
HTML
<div id="holder">
<div id="left">
<a><img/></a>
</div>
<div id="right"></div>
</div>
CSS
#holder {
width: 960px;
height: 300px;
background-color: green;
margin-left: auto;
margin-right: auto;
}
#left {
width: 248px;
height: 130px;
background-color: yellow;
float: left;
margin-top: 50px;
margin-left: 20px;
margin-right: 20px;
}
#right {
width: 640px;
height: 70px;
background-color: red;
float: right;
margin-top: 70px;
margin-left: 10px;
margin-right: 20px;
}

How can I lay out three divs in two columns without using tables?

I need the following layout of divs:
+-----------+-----------+
| | |
| Div A | |
| | |
| | |
+-----------+ Div C |
| | |
| Div B | |
| | |
| | |
+-----------+-----------+
I searched hard, but all I have found are lots of similar questions about placing divs in two columns (like this and this). None seem to deal with this exact problem.
I've tried using float:left and clear:both, but I just can't get anywhere.
Obviously it can be achieved using tables, but isn't it bad practice to use tables for layout. Can it be done without?
This should work
<div style="width:100%">
<div id="topLeft" style="float:left; width:50%; height:200px">
</div>
<div id=topRight style="float:right; width:50%; height:400px">
</div>
<div id="bottomLeft" style="float:left; width:50%; width:200px">
</div>
</div>
If they don't have to be in that order, this should work:
HTML
<div id="container">
<div id="div3"></div>
<div id="div1"></div>
<div id="div2"></div>
</div>
CSS
#container {
width:200px;
}
#div1, #div2 {
width: 100px;
height: 100px;
float: left;
clear: left;
}
#div3 {
height: 200px;
width: 100px;
float: right;
}
#div1 {
background-color: red;
}
#div2 {
background-color: blue;
}
#div3 {
background-color: green;
}
jsfiddle - http://jsfiddle.net/3UTcP/
<div>
<div id="AB-Wrapper" style="float:left;width:50%;">
<div id="DivA">some content</div>
<div id="DivB">some more content</div>
</div>
<div id="DivC" style="float:right;width:50%;">even more content</div>
<div style="clear:both;"><!-- clearer --></div>
</div>
http://jsfiddle.net/xREQK/

Anyway to get div with position:absolute outside of div with position:relative?

I have a structure like this:
<div class="a">
<div class="b">
<div>
<div class="c">
</div>
</div>
</div>
</div>
CSS:
.a { position:relative; }
.b { position:absolute; }
I understand that defining top and left/right properties positions absolute div to either its parent with position:relative or to the browsers window if such a parent doesn't exist. What I'm faced with, I cannot change the CSS for .a and .b. And I need .c to be on top of .a and slightly out of it. So that .a doesn't get a scroll bar.
Some ASCII art to illustrate, I guess :)
I have:
-------------------
| .a |^|
| | |<-- Scroll bar
| ------ | |
| | .c | |*|
-------------------
I need:
--------------------
| .a |
| |<-- No scroll bar
| ------ |
| | .c | |
----| | ---------
| |
------
This solution will stack all items and the <div class="c"> will reach out of its parent container:
.a {
position: relative;
width: 200px;
height: 200px;
border: 1px solid red;
background: #eee;
}
.b {
position: absolute;
top: 20px;
left: 20px;
width: 200px;
height: 200px;
border: 1px solid blue;
background: #ccc;
}
.c {
position: absolute;
bottom: -50px;
left: 20px;
width: 100px;
height: 100px;
border: 1px solid orange;
background: #aaa;
}
<div class="a">
<div class="b">
<div>
CONTENT
<div class="c"></div>
</div>
</div>
</div>
Note that this will only work, if the parent container has overflow:visible. When one of the parents has overflow:hidden|scroll you can't solve this, I guess.

CSS column layout

I'm going round in circles with a CSS layout. I basically want it like:
<-------><-------------->
<------><------>
400px 50% 50%
So its 3 colums, one fixed size, and the other two taking up 50% each of the remaining space. I cant seem to make the second and third take up 50% of the remaining space.
Any help would be greatly appreciated,
thanks very much :)
I tried a couple of variations. Below works in Chrome 2, Firefox 3.5 and IE8:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<head>
<title>NLR</title>
<style type="text/css">
html, body, div { margin: 0; border: 0 none; padding: 0; }
div { height: 500px; border-collapse: collapse; }
#wrapper { padding-left: 400px; }
#nav { width: 400px; margin-left: -400px; float: left; background: yellow; }
#main { overflow: hidden; background: blue; }
#left { float: left; width: 50%; background: red; height: 300px; }
#right { float: right; width: 50%; background: green; }
</style>
</head>
<body>
<div id="wrapper">
<div id="nav"></div>
<div id="main">
<div id="left"></div>
<div id="right"></div>
</div>
</div>
</body>
</html>
the markup:
<div id="left">some content</div>
<div id="main">
<div>more content</div>
<div>still more content</div>
</div>
the css:
#left {
float: left;
width: 400px;
margin-right: -405px; /* throwing in a little extra */
}
#main {
margin-left: 405px; /* matching the margin of #left */
}
#main > div {
width: 50%; /* may need to make it 49.9% for some browsers */
}
Your best bet would be to have it set up like so:
Wrappers are pretty important and make a lot of things much easier. To center things inside of wrappers, you can set margin left/right of the divs inside of the wrappers to "auto".
<div class="bigwrapper">
<div class="400pxdiv">
Content for 400pxdiv
</div>
<div class="rightwrapper">
<div class="50percent1">
50percent1's content
</div>
<div class="50percent2">
50percent2's content
</div>
</div>
</div>
First of all it's always a good practice to WRAP your layout. In the following example:
+---------------BODY-----------------+
|<---DIV#1---><--------DIV#2-------->|
| <---DIV3--> | <--DIV4--><--DIV5--> |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | |__________________| |
| |_________| | |____CLEAR DIV_____| |
+-----------------------------------+
DIV0 : main-wrapper
DIV1 : sidebar-wrapper
DIV2 : content-wrapper
DIV3 : sidebar-content
DIV4 : content-left
DIV5 : content-right
CLEAR DIV: to clear the floats.
This works for me in Firefox.
<!DOCTYPE html>
<title>Test</title>
<style>
#A { width: 400px; float:left; outline: thin solid pink; }
#B { margin-left: 400px; overflow: hidden; outline: thin solid pink; }
#B1, #B2 { width:50%; float:left; outline: thin solid pink; }
</style>
<div id=A>
A
</div>
<div id=B>
<div id=B1>
B1
</div>
<div id=B2>
B2
</div>
</div>