Position div under floated div - html

A simple problem today but can't seem to find the solution. I have four divs - I would like #down to be placed directly below #up, but for the life of my can't figure it out. Here is my CSS and HTML.
#up {
width: 33.3%;
height: 100px;
background: #CCCCCC;
float: left;
}
#down {
width: 33.3%;
height: 100px;
background: #999999;
float: left;
}
#mid {
width: 33.3%;
height: 200px;
background: #999999;
float: left;
}
#right {
width: 33.3%;
height: 200px;
background: #CCCCCC;
float: left;
}
<div id="up"></div>
<div id="mid"></div>
<div id="right"></div>
<div id="down"></div>
Elements must remain floated left.

maybe try with an additional div to get both up and down together?
<div id="up_down">
<div id="up"></div>
<div id="down"></div></div>
<div id="mid"></div>
<div id="right"></div>
And you will changed your css to :
up_down{
width: 33.3%;
height: 200px;
}
#up {
width: 100%;
height: 50%;
background: #CCCCCC;
float: left;
}
#down {
width: 100%;
height: 50%;
background: #999999;
float: left;
}

Rather than working with floats, you might consider simply setting the display attribute of the middle divs to "inline-block". Remember that be default, div elements have a block display, which means that they take up the entire width of its parent element, even if its width is less than the parent width. inline blocks on the other hand fit together like puzzle pieces and flow horizontally rather than vertically.

As #isherwoord says in the first comment, wrap div#up and div#down together in another div, forexample div#column
(I hope you don't mind the HAML way of writing HTML.)
div#column
div#up
div#down
div#middle
div#right

If you know the heights/widths you can just set the top position of #down with position:relative;top:-100px; to pull it back up.
Example:
#up {
width: 33.3%;
height: 100px;
background: #CCCCCC;
float: left;
}
#down {
width: 33.3%;
height: 100px;
background: #999999;
float: left;
position:relative;
top:-100px;
}
#mid {
width: 33.3%;
height: 200px;
background: #999999;
float: left;
}
#right {
width: 33.3%;
height: 200px;
background: #CCCCCC;
float: left;
}
<div id="up"></div>
<div id="mid"></div>
<div id="right"></div>
<div id="down"></div>

Related

How can I put nav higher than it is using float and clear?

The structure I need to recreate
I need to put the bottom right rectangle below the blue square just like on the left side and I have trouble with it. I have to use float and clear. Currently it is too low. Only the shape matters.
.blok1_1 {
background-color: cornflowerblue;
width: 450px;
height: 330px;
float: left;
}
.blok1_2 {
background-color: cornflowerblue;
width: 362px;
height: 330px;
float: right;
clear: right;
}
.blok1_3 {
background-color: yellow;
width: 1075px;
height: 855px;
float: right;
}
.blok1_4 {
background-color: mediumspringgreen;
width: 450px;
height: 520px;
float: left;
}
.blok1_5 {
background-color: mediumspringgreen;
width: 360px;
height: 525px;
float: right;
clear: both;
}
<nav class="blok1_1">
</nav>
<nav class="blok1_2">
</nav>
<section class="blok1_3">
</section>
<nav class="blok1_4">
</nav>
<nav class="blok1_5">
</nav>
easiest way is to use flex
#container{
display:flex;
margin:0 auto;
justify-content:center;
}
.end{
display:flex;
flex-direction:column;
}
.top{
width:150px;
height:200px;
background-color:lightblue;
}
.bot{
width:150px;
height:400px;
background-color:green;
}
#middle{
width:150px;
height:600px;
background-color:yellow;
}
<div id='container'>
<div class='end'>
<div class='top'></div>
<div class='bot'></div>
</div>
<div id='middle'>
</div>
<div class='end'>
<div class='top'></div>
<div class='bot'></div>
</div>
</div>
So I assume this is an exercise with pretty specific requirements. In that case I don't think DCR's answer will suffice, altough I have to say that it would probably be the real-world solution these days. Especially the part where floats are replaced with flex and the html structure is changed in some grid like construction of a left, middle and right section. I fully believe that's the way to go.
BUT since it's an exercise and using float and clear are your only options. Have a look at the code below!
Use a container for the 4 square elements. (the yellow part in the middle is just the background from the container)
The left elements float left and the right elements float right.
Since you want the bottom squares to be below the top squares instead of next to them you also add the clear left or right rule to these elements.
.container {
background-color: yellow;
width: 350px;
height: 300px;
}
.left-top {
background-color: cornflowerblue;
width: 100px;
height: 100px;
float: left;
}
.right-top{
background-color: cornflowerblue;
width: 100px;
height: 100px;
float: right;
}
.left-bottom {
background-color: mediumspringgreen;
width: 100px;
height: 200px;
float: left;
clear: left;
}
.right-bottom {
background-color: mediumspringgreen;
width: 100px;
height: 200px;
float: right;
clear: right;
}
<div class="container">
<div class="left-top"></div>
<div class="right-top"></div>
<div class="left-bottom"></div>
<div class="right-bottom"></div>
</div>

Blank space next to Divs with clear: left element

So I am creating a responsive website using HTML and CSS, and I have created four div elements in a container width of 50%, each of these elements are 25% width and 25% height. Making a square. Next to these four div's is another div, 50% in width. When I try to get the two containers to go side side, they appear incorrect. The image below will demonstrate this clearly.
What I am trying to achieve.
Any help would be greatly appreciated!
HTML Code
<div id="tile-1-small-left-1">
</div>
<div id="tile-1-small-left-2">
</div>
<div id="tile-1-small-left-3">
</div>
<div id="tile-1-small-left-4">
</div>
<div id="tile-1-small-right">
</div>
The CSS code:
#tile-1-small-left-1 {
width: 25%;
height: 25%;
background-color: red;
display: block;
float: left;
}
#tile-1-small-left-2 {
width: 25%;
height: 25%;
background-color: green;
display: block;
float: left;
}
#tile-1-small-left-3 {
width: 25%;
height: 25%;
background-color: yellow;
display: block;
float: left;
clear: left;
}
#tile-1-small-left-4 {
width: 25%;
height: 25%;
background-color: blue;
display: block;
float: left;
}
#tile-1-small-right{
/*background-image: url(../img/hero_rotation.jpg);*/
background-color: purple;
background-size: 100% 100%;
background-repeat: no-repeat;
width: 50%;
float: right;
height: 45%;
}
Fiddle demo
As I mentioned, this usually calls for another level of structure. This would do, based on your original CSS:
html,
body {
height: 100%;
}
#tile-1-small-left-1 {
width: 50%;
height: 50%;
background-color: red;
display: block;
float: left;
}
#tile-1-small-left-2 {
width: 50%;
height: 50%;
background-color: green;
display: block;
float: left;
}
#tile-1-small-left-3 {
width: 50%;
height: 50%;
background-color: yellow;
display: block;
float: left;
clear: left;
}
#tile-1-small-left-4 {
width: 50%;
height: 50%;
background-color: blue;
display: block;
float: left;
}
.tile-1-small-right {
/*background-image: url(../img/hero_rotation.jpg);*/
background-color: purple;
background-size: 100% 100%;
background-repeat: no-repeat;
width: 50%;
float: right;
height: 45%;
}
<div class="tile-1-small-right">
</div>
<div class="tile-1-small-right">
<div id="tile-1-small-left-1">
</div>
<div id="tile-1-small-left-2">
</div>
<div id="tile-1-small-left-3">
</div>
<div id="tile-1-small-left-4">
</div>
</div>
What I've done is wrapped the four smaller elements in another instance of the larger element (which required changing your selector to a class rather than an ID).
This case also demonstrates why ID-based selectors are a bad idea, and why semantic class names should be used. Calling something -left becomes confusing as soon as that type of element gets used elsewhere in the page.
For the first image example, just add <div style="clear:both"></div>
<div id="tile-1-small-left-4"></div>
<div style="clear:both"></div>
<div id="tile-1-small-right"></div>

How to align divs next to each other?

I'm trying to set these divs to align like this:
but they end up either overlapping eachother (.title takes full width of container) or underneath eachother. Ideas?
.wrapper{
display: table;
float: left;
width: 1000px;
height: 200px;
}
.pic{
float: left;
width: 100%;
height: 20%;
}
.title{
width: 100%;
height: 20%;
}
.content{
width: 100%;
height: 20%;
}
.footer{
width: 100%;
height: 20%;
}
HTML:
<div class="wrapper">
<div class="pic"><img src="..."></div>
<div class="title"><p>title</p></div>
<div class="content"><p>lorem ipsum</p></div>
<div class="footer"></div>
</div>
JS FIDDLE: http://jsfiddle.net/mmb84836/
As per the Best Practice:
Put Pic in one Box and the other three Boxes on right in one Box and use "float:left or **display:inline-block**for those.
Here is the code for the same:
HTML
<div class="wrapper">
<div class="leftBox">
<div class="pic">pic</div>
</div>
<div class="rightBox">
<div class="title">title</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
</div>
CSS
div {
border:1px solid #000;
}
.wrapper {
display: block; /*Default Property - You Can Remove Also*/
width: 1000px;
height: 200px;
}
.leftBox {
float:left;
width :20%;
height:100%
}
.rightBox {
width :79.5%;
float:left;
height:100%
}
.pic {
width: 100%;
height: 100%;
}
.title {
width: 100%;
height: 20%;
}
.content {
width: 100%;
height: 20%;
}
.footer {
width: 100%;
height: 20%;
}
Here is the Working Fiddle: http://jsfiddle.net/7xLyc3q1/
You've got a lot of answers here, but none of them explain what is actually happening here. When using float, there's something important you need to understand: floated elements are lifted out of the box model and have effectively zero width and height as far as other elements are concerned. There is a workaround for this: by specifying overflow:hidden in the parent element, floated elements will no longer "collapse".
Here's an example that demonstrates this. Notice that the title, content, and footer have a width:100%, and they're only filling the space that is remaining for them -- this is probably what you'd expect to happen. Notice also that there was no need to float them to the right... they take the space that's left.
Try adding float: right to .title, .content, and .footer.
Also it may be worth considering using Foundation or Twitter Bootstrap. Both have grid systems so this would guarantee the divs would resize to fit any size screen.
<div class="wrap">
<div class="pic">pic</div>
<div class="other">oth1</div>
<div class="other">oth2</div>
<div class="other">oth3</div>
</div>
.wrap { width:100; height:200px; }
.pic { float:left; width:29%; height:100%; margin-right:1%; background-color:red; }
.other { float:left; width:70%; height:32%; margin-bottom:0.5%; background-color:green; }
and jsfiddle http://jsfiddle.net/t85kz39a/
Here is one way of doing it if you can specify a width for the image. I assumed that the image would be 200px wide in this demo.
Try the following CSS:
.wrapper{
width: 600px;
height: 200px;
padding-left: 200px;
border: 1px dashed gray;
}
.pic{
float: left;
width: 190px;
margin-left: -200px;
border: 1px dashed blue;
}
.pic img {
display: block;
}
.title{
width: auto;
height: 20%;
border: 1px dotted blue;
}
.content{
width: auto;
height: 20%;
border: 1px dotted blue;
}
.footer{
width: auto;
height: 20%;
border: 1px dotted blue;
}
The trick is to open up a space to place the image. Add a 200px wide left padding to
the .wrapper.
The padding will force .title, .content and .footer to align 200px from the edge
of the wrapper.
For .pic, set the width to 200px (or smaller) and set the left margin to -200px to move
it into the padding area.
Finally, set the correct width for .wrapper, 600px. The overall width of .wrapper
will compute to 800px (600px width + 200px left padding - -200px left margin from the
float).
See demo: http://jsfiddle.net/audetwebdesign/mgg1stmc/
The main benefit of this approach is that you don't need to add any other wrapping
elements. (If you use floats, the extra wrappers are necessary.)
There's a much simpler css-only way without changing your HTML structure:
Demo http://jsfiddle.net/bfhng3a9/
All you need:
.wrapper {
overflow:auto;
text-align:center;
}
.pic {
float: left;
width:20%;
}
.title, .content, .footer {
width:80%;
float:right;
clear: right;
}
You can use this code and it is working according to your design.
Live Working Demo
HTML Code:
<div class="wrapper">
<div class="pic"><img src="..."/></div>
<div class="title"><p>Title</p></div>
<div class="content"><p>Content</p></div>
<div class="footer"><p>Footer</p></div>
</div>
CSS Code:
.wrapper{
position: relative;
float: left;
width: 1000px;
height: 200px;
border: 1px solid #000000;
}
.pic{
float: left;
width: 300px;
height: 200px;
background-color: red;
position: relative;
}
.title{
width: 650px;
height: 60px;
background-color: green;
position: relative;
left: 350px;
top:-16px;
}
.content{
width: 650px;
height: 60px;
background-color: blue;
position: relative;
left: 350px;
top: -22px;
}
.footer{
width: 650px;
height: 60px;
background-color: gold;
position: relative;
left: 350px;
top: -28px;
}
Result:

Floating div covers the next div

I want to position a <div> on a line, and then another <div> afterward.
I set the first <div> to float left, and then put in the next one.
However, the first div is covering the second one. It's not positioning the second one after the first (as I expected) - it's starting it from the same place.
Here's the code (on JSFiddle: http://jsfiddle.net/LcrA9/)
<div id="container">
<div id="one">
</div>
<div id="two">
</div>
</div>
#container {
width: 300px;
height: 500px;
}
#one {
width: 100px;
height: 500px;
float: left;
background-color: green;
}
#two {
width: 200px;
height: 500px;
background-color: blue;
}
Float the second div, or put a margin-left equal to the first div's width:
Floating JSFiddle:
#two {
width: 200px;
height: 500px;
background-color: blue;
float: left;
}
Margin-left JSFiddle
#two {
width: 200px;
height: 500px;
background-color: blue;
margin-left: 100px;
}
#PlantTheIdea's suggestion to use display: inline-block is sound. This question does have a purpose though: when you want the second div to consume the remaining space (and don't want to use display: table e.g.)
Here's an example of that too:
#container {
height: 500px;
clear: both;
}
#one {
width: 100px;
height: 500px;
float: left;
background-color: green;
}
#two {
height: 500px;
background-color: blue;
margin-left: 100px;
}
You need to ditch the floats, and discover display:inline-block; my friend!
Your HTML:
<div id="container">
<div id="one">
</div><div id="two">
</div>
</div>
And your CSS:
#container {
width: 300px;
height: 500px;
}
#one,#two {
display:inline-block;
vertical-align:top;
/* Old IE */
*display:inline;
*zoom:1;
}
#one {
width: 100px;
height: 500px;
background-color: green;
}
#two {
width: 200px;
height: 500px;
background-color: blue;
}
Here is an updated jsFiddle.
The reason I eliminated the space between the two divs is because if there were any space, inline-block would put a 4px margin between them. The Old IE pieces are for IE7 and below.
You need float:left on both divs
#one {
width: 100px;
height: 500px;
float: left;
background-color: green;
}
#two {
width: 200px;
float:left;
height: 500px;
background-color: blue;
}
Fiddle
You could solve this by making the second div float as well:
#two {
float: left;
width: 200px;
height: 500px;
background-color: blue;
}
Float left the next one too!!
<div id="container">
<div id="one">
</div>
<div id="two">
</div>
</div>
#container {
width: 300px;
height: 500px;
}
#one {
width: 100px;
height: 500px;
float: left;
background-color: green;
}
#two {
width: 200px;
height: 500px;
background-color: blue;
float:left;
}
Just add another "float:left" value on second div.
#one {
width: 100px;
height: 500px;
float: left;
background-color: green;
}
#two {
width: 200px;
height: 500px;
background-color: blue;
float:left;
margin-left:20px
}
DEMO
Set The #two Div to float:left as well.

Why isn't my DIV children the same height as parent

I have been struggling with this for awhile now, and I can't seem to find any solution.
I have a frame, a top box, a left box and a right box and a middle box containing the last two.
I want these to be the height of the frame minus the height of the top box. This would result in the frame being filled, nothing more and nothing left.
What is wrong with my current code, and what would be a proper way to achieve this?
Here's the code:
<html>
<head>
<style type="text/css">
#frame {
width: 800px;
min-height: 500px;
border: 1px solid black;
}
#top {
width: 800px;
height: 80px;
float: left;
background-color: #666;
}
#middle {
width: 800px;
height: 100%;
float: left;
}
#left {
width: 200px;
height: 100%;
float: left;
background-color: #B3B4BD;
}
#right {
width: 600px;
height: 100%;
float: left;
background-color: #99BC99;
}
</style>
</head>
<body>
<div id="frame">
<div id="top">Top</div>
<div id="middle">
<div id="left">Left</div>
<div id="right">Right</div>
</div>
</div>
</body>
</html>
You can't specify a 100% height unless you explicitly set the parent's height. The reason is that the parent normally expands in height to fit its children, and you need to specify an exact height so that the parent knows what its height is in time for its children to need it.
That said, there are a number of ways of achieving a similar effect. For instance if one div is normally taller than the other then you can use absolute positioning to stretch the second div to the same height. Or if you're really desperate then you can use a table.
Try using proportions instead of exact pixels.
#frame {
width: 80%;
min-height: 500px;
border: 1px solid black;
margin-right:auto;
margin-left:auto;
}
#top {
width: 100%;
height: 80px;
float: left;
background-color: #666;
}
#middle {
width: 100%;
height: 100%;
float: left;
}
#left {
width: 33%;
height: 100%;
float: left;
background-color: #B3B4BD;
}
#right {
width: 66%;
height: 100%;
float: left;
background-color: #99BC99;
}
jsFiddle
Here's a screenshot of your demo with the updated CSS: