Float two divs left and one right - html

My code structure looks like this
<div style="height: 100px;
Width: 200px;"> <!-- Container -->
<div style="float: left;
height: 50px;
Width: 100px;
background-color: red;">
</div>
<div style="float: left;
height: 50px;
Width: 100px;
background-color: blue;">
</div>
<div style="float: right;
height: 50px;
Width: 100px;
background-color: green;">
</div>
</div>
But the right position of elements should look like this:
┌──────┬──────┐
│ red │green │
├──────┼──────┘
│ blue │
└──────┘
I cannot change or add any additional code, the only way is with CSS.
How should I float the divs to be in the right order as I mentioned above?
Edit: My code doesn't and can't contain div with clear.

you dont need floating for that. disable all floating using !important to override the inline styles, and then use :nth-of-type() to select the green div and position it absolutely with right and top equal 0;
div {
position: relative;
}
div > div{
float: none !important;
}
div > div:nth-of-type(3) {
position: absolute;
right: 0;
top:0;
}
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;">
</div>
<div style="float:left; height: 50px; Width:100px; background-color:blue;">
</div>
<div style="float:right; height: 50px; Width:100px; background-color:green;">
</div>
</div>

You can use clear: left on the blue box to push it down and then use negative margin on the green box to push it up.
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left;height: 50px;
width:100px; background-color:red;">
</div>
<div style="float:left;clear:left;
height: 50px; Width:100px; background-color:blue;">
</div>
<div style="float:left; height:50px;
width:100px; background-color:green;margin-top:-50px;">
</div>
</div>

Well this is more like a puzzle instead of a legit question but here goes.
With the proper use of margins and positions in addition to assigning null to clear property one can accomplish your scenario.
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;"></div>
<div style="float: right; height: 50px; margin-top: 50px;Width:100px; background-color:blue;position: absolute;"></div>
<div style="clear: none;"></div>
<div style=" height: 50px; margin-left: 100px;margin-bottom: 50px;Width:100px; background-color:green;"></div>
</div>
</div>

Keeping the same HTML structure, you could select the divs in CSS using :nth-child(N). In this case you'd just need to update the blue (2) and green (4) boxes, and the one with the clear:both style (3):
div > div:nth-child(2) {
margin-top: 50px;
}
div > div:nth-child(3) {
display: none;
}
div > div:nth-child(4) {
margin-top: -100px;
}
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;">
</div>
<div style="float:left; height: 50px; Width:100px; background-color:blue;">
</div>
<div style="clear:both;"></div>
<div style="float:right; height: 50px; Width:100px; background-color:green;">
</div>
</div>
Notice that this will work for this particular example. It would be ideal if the container div had an id and use that instead of div >.
For a more generic solution that would work independently of the height of the boxes, you could use transform:translate() like this:
div > div:nth-child(2) {
transform:translate(0%, 100%);
}
div > div:nth-child(3) {
display:none;
}
div > div:nth-child(4) {
transform:translate(0%, -100%);
}
As you can see on this JSFiddle: http://jsfiddle.net/eekhjv3n/1/

Related

how to make div in bottom inside the main div? [duplicate]

This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 3 years ago.
how to make div with class boxLinks = bottom in the main box = class = main-box?
my idea
this class main width 1200px;
this class main-box are 5 div, every div width 25%
.main-box {
width= "25%";
float= left;
}
<div class= "main">
<div class="main-box">
<div class="boxImg"></div>
<div class="boxTital"></div>
<div class="boxLinks">
<button> PRESS </button>
</div>
</div>
</div>
i want this div boxLinks in the last div,
You can use flexbox to do the work.
You have to set your main-box as flex.
then the boxLinks become a flex element, so with the properti align-self you can put it at the bottom.
You can fin a good tutorial on css-tricks
.main-box {
width: "25%";
float: left;
border: 1px solid #000;
width: 500px;
height: 400px;
display: flex;
}
.boxLinks {
flex: 0 1 100%;
height: 100px;
background: #000;
align-self: flex-end;/* this do the work */
}
<div class="main">
<div class="main-box">
<div class="boxImg"></div>
<div class="boxTital"></div>
<div class="boxLinks">
<button> PRESS </button>
</div>
</div>
</div>
Try this:
.main-box {
width= 25%;
float= left;
position:relative;
min-height:150px;
background:#000;
}
.boxLinks{
position:absolute;
bottom:0;
left:50%;
transform:translateX(-50%);
}
<div class= "main">
<div class="main-box">
<div class="boxImg"></div>
<div class="boxTital"></div>
<div class="boxLinks">
<button> PRESS </button>
</div>
</div>
</div>
It is pretty simple. You can use position: relative on 'main-box' and position: absolute, bottom:0 on 'boxLinks'.
Working example on jsFiddle.
.main {
display: flex;
}
.main-box {
width: 25%;
height: 100px;
float: left;
position: relative;
background-color: #f00;
}
.boxLinks {
position: absolute;
bottom: 0;
left: 0;
padding: 10px;
background-color: #0f0;
}
<div class="main">
<div class="main-box">
<div class="boxImg"></div>
<div class="boxTital"></div>
<div class="boxLinks">
<button> PRESS </button>
</div>
</div>
</div>

Alignment issues with html divs stacked on top of each other

I am having some difficulty with some html code (I have never done html programming of any significance) where I am trying to do something fairly simple. Actually I broke it down to the simplest form for now. So I have three blocks on top of each other. The first block at the very top has three sub boxes within it horizontally. I fixed the height of this block at 250px since my text fits into it. However my second block (Center) overlaps with the Top div. How do I specify that the center div start after the Top div? I want it to display a few pixels below the Top div.
<div id="Report" style="height: auto">
<div id="Top" style="width:inherit; height:250px">
<div id="First" class="TopMostLeft" >
<span style="font-family:Calibri; font-size:small">Info</span>
<table style="width:100%" > ... </table>
<div id="Second" class="TopMostCenter">
<span style="font-family:Calibri; font-size:small">Info2</span>
<div id="Third" class="TopMostRight">
<div id="About" class="TopRightDiv">
<table style="width:100%">
</div>
<div id="Center" style="border:solid; border-width:2px; border-color:lightgray; padding:4px; margin:10px">
<div id="Bottom" style="border-width:2px;border-width:2px; border-color:lightgray; padding:4px; margin:10px">
</div>
Here's what you need. Create a stylesheet so we can style it much better.
Working Fiddle: https://jsfiddle.net/v9jgj7n3/
I created your layout. This is how I understand what you need.
HTML
<div id="Report">
<div id="Top">
<div id="First" class="TopMostLeft" ></div>
<div id="Second" class="TopMostCenter"></div>
<div id="Third" class="TopMostRight"></div>
</div>
<div id="Center"></div>
<div id="Bottom"></div>
</div>
CSS
#Top {
width: 100%;
display: flex;
height: 250px;
}
#Top #First {
width: 10%;
background: red;
}
#Top #Second {
width: 40%;
background: blue;
}
#Top #Third {
width: 50%;
background: yellow;
}
#Center {
height: 200px;
width: 100%;
background: gray;
}
#Bottom {
height: 80px;
width: 100%;
background: black;
}
Edit the width and height to your desired value. This is how it will work
Also, I notice that you didn't close the child element of Top. You must always close DIV, so the HTML code will run well. It breaks your code.
Hope it helps. Cheers! Good Morning from Philippines.

How to set <div> s inline: text, img, text, text

At first I have to that I read a lot of tutorials and still I don't know what am I doing wrong...
I would like to use 4 divs inline. In those divs I would like to put: text, image, text, text. And I would like the middle text to set automatically to the maximum width.
I wrote a simple code, just to show my problem:
<div>
<div style="float: right; width: 100px; background-color: red">right</div>
<div style="margin-right: 100px; background-color: blue">
<div style="width: 100px; background-color: green">left</div>
<div style="width: 100px; margin-left: 100px; background-color: pink">
<img src="../zdjecia_przedmiotow/1_small.jpg" />
</div>
<div style="margin-left: 200px; background-color: green">center</div>
</div>
</div>
And it looks like this:
I would like to do it using divs!
what am I missing?
First you need to float those divs inside so they align next to each other. Then you can use calc() to make the last container take the rest of the width;
FLOAT EXAMPLE
OR
You can use display: table on the parent and set the children to display: table-cell like so:
TABLE EXAMPLE
Also I restructured this a bit since there was some unnecessary elements/styles in there:
HTML
<div class="wrapper">
<div class="box box1">left</div>
<div class="box box2">
<img src="../zdjecia_przedmiotow/1_small.jpg" />
</div>
<div class="box box3">center</div>
<div class="box box4">right</div>
</div>
CSS
.wrapper{
overflow:hidden;
width:100%;
}
.box{
float: left;
}
.box1{
width: 100px;
background-color: green;
}
.box2{
width: 100px;
background-color: pink;
}
.box3{
background-color: green;
width:calc(100% - 300px);
}
.box4{
width:100px;
background-color: blue;
}
CLEANER FIDDLE
I simplified the HTML structure a bit and used floats. With the CSS left inline:
<div style="background-color:blue;">
<div style="width: 100px; background-color: green; float:left;">left</div>
<img src="../zdjecia_przedmiotow/1_small.jpg" style="width: 100px; background-color: pink; float:left;" />
<div style="background-color: green; width:calc(100% - 300px); float:left;">center</div>
<div style="width: 100px; background-color: red; float:right;">right</div>
</div>
After CSS is out:
.box{background-color:blue}
.left{width: 100px; background-color: green; float:left;}
.fill{background-color: pink; width:calc(100% - 300px);}
.right{width: 100px; background-color: red; float:right;}
<div class="box">
<div class="left">left</div>
<img class="left" src="../zdjecia_przedmiotow/1_small.jpg"/>
<div class="left fill">center</div>
<div style="right">right</div>
</div>
Fiddle

Position div over other content

I am using bootstrap and the page width is NOT fixed.
I would like to display a contact form div (grey box below) like this:
So the grey contact form is sort of floating over the blue and white divs.
Thanks!
Here's what I have been trying to do: http://jsfiddle.net/w69j4xam/
<div class="header">
Header
</div>
<div class="content">
<div class="bluediv">
Some text here
</div>
<div class="whitediv">
Some more text here
</div>
<div class="contactform">
Contact Form<br/><br/><br/><br/>
</div>
</div>
body{
padding: 20px;
}
.header{
height: 50px;
background-color: green;
}
.content{}
.bluediv{
height: 150px;
background-color: #AFEEEE;
}
.whitediv{
height: 180px;
background-color: #FFF;
}
.contactform{
background-color: grey;
width: 100px;
position: absolute;
}
In terms of your jfiddle example, all you need to add is a right and a top.
.contactform{
right:50px;
top:100px;
background-color: grey;
width: 100px;
position: absolute;
}
http://jsfiddle.net/w69j4xam/2/
Position the outer div however you want, then position the inner divs using absolute. They'll all stack up.
<style type="text/css">
.inner {
position: absolute;
}
</style>
<div class="outer">
<div class="inner">1</div>
<div class="inner">2</div>
<div class="inner">3</div>
<div class="inner">4</div>
</div>

HTML: div floats are not horizontally aligned

I have two divs that I've placed inside another div:
<div id="outer_div">
<div id="left_div">
<!-- Buttons and text field -->
</div>
<div id="right_div">
<!-- Buttons and drop list -->
</div>
</div>
My style sheet includes the following:
#left_div {
float:left;
}
#right_div {
float:right;
}
Now, when I do this, I expect that the left and right div will be on the left and right of the screen, respectively, and that they will be aligned horizontally as well. The first holds true, however, the second hope is not true. If I put left_div above right_div, the right_div buttons and drop list are on a line below the left_div. If I put right_div above left_div, the divs are almost in line, but the right div is slightly elevated, so that it overlaps a div that is above it.
Try setting a width to your outer_div
#outer_div {
width: 100%; //or whatever width you would like it to be
}
#left_div, #right_div {
width: 50%;
}
here is a sample that works fine for me.
<div id="outer_div">
<div id="left_div">
<div class="button"></div>
<div class="button"></div>
</div>
<div id="right_div">
<div class="button"></div>
<div class="button"></div>
</div>
</div>
and css
#outer_div
{
overflow: hidden;
width: 450px;
background: yellow;
}
#left_div {
float:left;
width:200px;
height: 200px;
border: thin red solid;
}
#right_div {
float:right;
width:200px;
height: 200px;
border: thin blue solid;
}
.button {
width: 75px;
height: 25px;
margin: 5px;
border: thin green solid;
}
and the fiddle: http://jsfiddle.net/cMhpK/