How to float elements independently? - html

I would like to position some elements at different distances from the left border of a parent div. Each element holds its own distance (in percentage of the width of the parent div).
Since position does not seem to be the right approach, I tried to do that by floating the element to the right
div {
display: flex;
flex-direction: row;
}
#root {
width: 50%;
border: solid;
}
#one {
float: right;
margin-left: 20%;
}
#two {
float: right;
margin-left: 80%;
}
<div id="root">
<div id="one">1</div>
<div id="two">2</div>
</div>
My expectation was that the first element would be at 10% of the div width (so 5% of the page width, since the parent div is 50% width relative to the page) and the second one at 80%. This is not the case, the docs mention that
Elements after a floating element will flow around it.
They also mention that clear can be used to avoid this but I did not manage to make it happen (that is to make it so that each float is recalculated from the left border of the parent div).
Is this something which is possible?
As a workaround I thought about calculating the float of the second element reltive to the first one, but that would horrendously(*) complicate my code so maybe there is a cleaner solution.
(*) For the case above that would be 80% - 20% (the ones which are already floated) = 60%. But even here the positionning is not correct (the 2 is too much to the right; there should be 20% blank, 1, 40% blank, 2, 20% blank - but the widths of the numbers themselves should be taken into account as well)
div {
display: flex;
flex-direction: row;
}
#root {
width: 50%;
border: solid;
}
#one {
float: right;
margin-left: 20%;
}
#two {
float: right;
margin-left: 60%;
}
<div id="root">
<div id="one">1</div>
<div id="two">2</div>
</div>

You can position element the way you want with position: absolute on the child elements. But keep in mind that the child elements don't control the height of the outer element.
#root {
position: relative;
border: solid;
width: 50%;
overflow:hidden;
height: 20px;
}
#one {
border: solid red;
position: absolute;
top: 0px;
left: 20%;
}
#two {
border: solid blue;
position: absolute;
top: 0px;
left: 60%;
}
<div id="root">
<div id="one">1</div>
<div id="two">2</div>
</div>
Demo: https://jsfiddle.net/uu0oftyr/

Use
clear: left;
float: left;
margin-left: [the percentage value for your distance];
Erase the flex settings from the container and add overflow: hidden to make sure the floats are considered part of the containers height.
Here is an example:
#root {
width: 50%;
border: solid;
overflow: hidden;
}
#one {
clear: left;
float: left;
margin-left: 20%;
border: 1px solid red;
}
#two {
clear: left;
float: left;
margin-left: 60%;
border: 1px solid green;
}
<div id="root">
<div id="one">1</div>
<div id="two">2</div>
</div>
P.S.: You could do something similar with all right settings:
clear: right;
float: right;
margin-right: [...];

Related

How do I set a floating element automatically fill the remaining height of its parent element (height not fixed and displayed as a table)?

I have the following code:
.parent {width: 960px; display: table}
.1 {
width: 45%;
margin: 20px;
float: left;
height: 1000px; /* it can be smaller or bigger than this value to fit its content */
}
.2 {
width: 45%;
margin: 20px;
float: right;
height: 200px;
}
.3 {
width: 45%;
margin: 20px;
float: right;
}
<div class="parent">
<div class="1">1</div>
<div class="2">2</div>
<div class="3">3</div>
</div>
How do I write the CSS for class "3" so that its height automatically fill the remaining height of the table (in the case above, 720px, as the parent element will have, I assume, height of 1000px too)? Note that the height of class "1" can change according to its contents.
Off-topic: Is there a better way to make it look like the picture below other than the codes I'm using now (only using CSS and HTML)?
The Image of the Table
Try this one. the third element(green) will adjust based on the height of .one. But it is implemented based on the assumption that .two is having fixed dimensions.
.parent {
width: 960px;
border: solid 2px #999;
float: left;
position: relative;
}
.one {
width: 50%;
float: left;
height: 1000px;
background: #ccc;
}
.two {
width: 50%;
float: right;
height: 200px;
background: #aaa;
}
.three {
position: absolute;
top: 200px;
left: 50%;
right: 0;
bottom: 0;
background: green;
}
<div class='parent'>
<div class='one'>one</div>
<div class='two'>two</div>
<div class='three'>three</div>
</div>

CSS margin-top makes div go out of parent's height

I have problem setting layout because one of my child divs makes goes out of it's parent div.
I have: header with 10% height, container with height 90%, and inside one 'div1' with height set to 90% and margin-top set to 10%. If I remove margin-top everything is ok, if not it goes out of parent size creating scrolls etc. (I want div1 height set, I dont need height set to auto etc.)
JsFiddle: https://jsfiddle.net/5q9vh93n/
HTML:
<body>
<div id="header">Header</div>
<div id="container">
<div id="div1">1</div>
<div id="div2">2</div>
</div>
</body>
CSS:
body, html
{
color: white;
margin: 0px;
padding: 0px;
height: 100%;
}
#header
{
background-color: blue;
height: 10%;
}
#container
{
width: 100%;
height: 90%;
background-color: gray;
}
#div1 {
background-color: red;
float: left;
width: 15.67%;
margin-top: 10%;
margin-left: 1.5%;
height: 90%;
}
#div2 {
background-color: green;
float: right;
width: 100px;
width: 43.17%;
margin-right: 3.6%;
}
Use Transform here is a demo
#div1 {
background-color: red;
float: left;
width: 15.67%;
transform: translateY(10%);
margin-left: 1.5%;
height: 90%;
}
you should use position: absolute; and change margin-top to top , margin-left to left. So css of #div as follow:
#div1 {
position: absolute;
background-color: red;
float: left;
width: 15.67%;
top: 10%;
left: 1.5%;
height: 90%;
}
I was really surprised when I've discovered this, but vertical padding and margin are relative to the parent's width, not height.
To solve your problem, you can use the top property instead, which is relative to the parent's height, as you expect.
So, just change your code to this, and it'll work.
#div1 {
top: 10%;
position: relative;
}
JSFiddle: https://jsfiddle.net/5q9vh93n/2/
Hope it helps!

Vertically center 2 floating divs

I have a problem. I want to achieve something like this:
I have a div with fixed height, and 2 other divs inside, with variable / unknown height, which I want to have
a) vertically centered
b) floating left /right
Right now I am trying something like this.
<div class="wrapper">
<div class="left">This is left</div>
<div class="right">This should be right</div>
</div>
.wrapper:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.left {
display: inline-block;
vertical-align: middle;
}
.right {
display: inline-block;
vertical-align: middle;
}
Everything is perfectly centered, but the right div is next to the left one, and not on the right side. As soon as I start to put in
float: right;
into my right class, it is on the right side, but not centered anymore. And I have no clue how to achieve this.
Thank you in advance!
There is a really cleaver answer to this at http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/ It suggests this code:
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
There are other solutions to this problem also, but this is the most simple. You can then just float each box left or right.
EDIT: another link with a lot of ways of doing this http://css-tricks.com/centering-css-complete-guide/
Try using Flexbox, e.g.
.wrapper {
display: flex;
justify-content: space-between;
align-items: center;
align-content: center;
}
.left {
display: inline-block;
vertical-align: middle;
background: red;
}
.right {
vertical-align: middle;
background: green;
}
http://jsfiddle.net/hafpuvtq/
More info: http://css-tricks.com/snippets/css/a-guide-to-flexbox/
You have to set the html, body elements of height: 100% and margin and padding of 0 outside the container class first before declaring any of the following classes:
HTML
<body>
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
</body>
CSS
html, body {
height: 100%;
padding: 0;
margin: 0;
}
.container {
position: relative;
top: 50%;
height: 100px;
}
.box1 {
height: 100px;
width: 100px;
background-color: red;
float: left;
}
.box2 {
height: 100px;
width: 100px;
background-color: green;
float: right;
}
The left and right both have to contain floats; left box for float: left; and right box for float: right;
That's right - floating an element removes it from the document flow, so it can't align itself to its parent element's line-height. Instead, put a wrapper div around each of the two child elements, and float the wrappers, left and right respectively. Make sure their height is 100%, and then vertically align the children inside them, as you currently are.
See http://jsfiddle.net/conLs2fd/6/.
this answer is just css
.wrapper {
position: relative;
height: 200px;
border: 1px solid gray;
}
.left {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100px;
height: 100px;
margin: auto;
background-color: lightgray;
display:inline-block;
}
.right {
position: absolute;
top: 0;
bottom: 0;
right: 0;
width: 100px;
height: 100px;
margin: auto;
background-color: gray;
display:inline-block;
}
<div class="wrapper">
<div class="left child">This is left</div>
<div class="right child">This should be right</div>
</div>
Here is one way of doing it that involves using text-align: justify on the .wrapper parent block. If you can specify the height of .wrapper, you
can set line-height to the same value of the height.
Add a :after pseudo-element of height: 0 to force a second line for the line box containing the elements, which will allow the justification to work.
.wrapper {
border: 1px dotted gray;
height: 100px; /* for demo only */
line-height: 100px;
text-align: justify;
}
.wrapper:after {
content: '';
display: inline-block;
height: 0;
width: 100%;
}
.left, .right {
border: 1px dotted blue;
line-height: 1.2;
}
.left {
display: inline-block;
vertical-align: middle;
}
.right {
display: inline-block;
vertical-align: middle;
}
<div class="wrapper">
<div class="left">This is left</div>
<div class="right">This should be right</div>
</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:

Unable to vertically align side-by-side child divs within the parent

I have three child divs sitting side-by-side inside the parent. The left and right are of a fixed width, whilst the middle is a variable width and needs to resize with the browser. As they are of different heights, I need to vertically align them inside the parent, but I cannot get them to, and they stick to the top. Is there any way I can do this? The height of the child divs are fixed, but the height of the parent should be variable.
CSS:
#divMain { width: 100%; min-width:320px; height:400px}
#div1 { width: 100px; height: 200px; float: left; red;vertical-align:middle;display:inline-block; }
#div2 { margin-left: 110px; height: 400px; margin-right: 110px; vertical-align:middle;}
#div3 { width: 100px; height:300px; float: right; vertical-align:middle;display:inline-block;}
HTML
<div id="divMain">
<div id="div1"></div>
<div id="div3"></div>
<div id="div2"></div>
</div>
Here's a JS-free way of achieving this using position: absolute on the side divs:
Fiddle: http://jsfiddle.net/Xa8TW/2/
CSS
#divMain {
width: 100%;
min-width:320px;
position: relative;
}
#div1 {
width: 100px;
height: 200px;
background-color: red;
position: absolute;
top: 50%;
margin-top: -100px;
}
#div2 {
height: 600px;
margin: 0 110px;
background-color: green;
}
#div3 {
width: 100px;
height:300px;
background-color: blue;
position: absolute;
top: 50%;
right: 0;
margin-top: -150px;
}
HTML can be left unchanged, but it is now also possible to swap the places of #div2 and #div3, since there are no floated elements that require a certain order.
Is the height of DIVs fixed? As it seems from your code the heights are fixed to 400px. If that's true then the solution is very simple - just provide a margin-top to div1 and div3.
I've created the fiddle for this and removed some of useless CSS snippets (which do not make nay difference) as well. Have a look
http://jsfiddle.net/8kv2K/