I want to make 2 divs beside each other to be aligned on the same horizontal line WITHOUT FLOATs
I've tried Position:relative ,, but no luck
See the example below :
http://jsfiddle.net/XVzLK
<div style="width:200px;height:100px;background:#ccc;">
<div style="background:Blue; float:left; width:100px; height:100px;"></div>
<div style="background:red; float:left; margin-left:100px; width:100px; height:100px;"></div>
</div>
From the link above, I need the red box to be on the same line of blue box with no space below ..
EDIT : I want the red box to stay outside the container gray box (just as it is) thanks
Relative with inline-block display
#one {
width: 200px;
background: #ccc;
}
#two {
display: inline-block;
background: blue;
position: relative;
left: 0;
width: 100px; height: 100px;
}
#three {
display: inline-block;
background: red;
position: relative;
left: 0;
width: 100px; height: 100px;
}
<div id="one"><div id="two"></div><div id="three"></div></div>
EDIT
The code below also works fine. Here, because of comments, the line feed is commented out and ignored.
#one {
width: 200px;
background: #ccc;
}
#two {
display: inline-block;
background: blue;
position: relative;
left: 0;
width: 100px; height: 100px;
}
#three {
display: inline-block;
background: red;
position: relative;
left: 0;
width: 100px; height: 100px;
}
<div id="one">
<div id="two"></div><!--
--><div id="three"></div>
</div>
Why it works block displays take the whole width of their container, even if you set a very small width, rest of the space
will be taken as margin (even if you remove margin). That's just how
they behave. inline-block displays work much like inline displays
except that they do respect the padding etc you give them. But they
still ignore margins (someone correct me if I am wrong). Same as
inline displays, if you give a line-feed between them in your HTML,
it's converted to a small space. So to remove that, Here I have the
HTML in a single line. If you indent the code then the div#three
will be pushed down (as you have fixed width of div#one so height is
only option.)
Use Position properties when your height and width are fixed
<div style="width:200px;height:100px;position:relative;background:#ccc;">
<div style="background:Blue; position:absolute; left:0%; width:50%; height:100%;">
</div>
<div style="background:red; position:absolute; left:50%; width:50%; height:100%;">
</div>
</div>
If you want to avoid float, position and inline-block, here's a margin-only solution:
<div style="width:200px; background:#ccc;">
<div style="background:blue; width:100px; height:100px;"></div>
<div style="background:red; width:100px; height:100px; margin:-100px 0 0 100px;"></div>
</div>
Updated fiddle
If you want your divs on same line without floats you can use display: inline-block; and give some negative margin value to your div because inline-block contains some margin between them.
See this fiddle
As your Edited question I have submitted another fiddle here
Or you could simply add margin-top: -100px; to your fiddle.
http://jsfiddle.net/XVzLK/22/
<div style="width:200px;position: relative; background:#ccc;">
<div style="background:Blue; position:absolute; top:0; left: 0; width:100px;height:100px;"></div>
<div style="background:red; position:absolute; top:0; right: 0; width:100px;height:100px;"></div>
</div>
Setting position relative on the coloured divs makes their position relative to where they would have been in the document. I think what you wanted to do is make the containing div position relative, and the children divs positioned absolutely within it. I'm assuming that "with now space below" meant "with no space below"
There is a tutorial here that may be of use: http://www.barelyfitz.com/screencast/html-training/css/positioning/
Related
Quick and easy question. I'd like to have a floating box that stays in the bottom right of a div (in HTML). How would I do this with css?
Thanks! (attached is what I want it to look like)
Hope this will be what you are looking for.
.navBar {
height: 100px;
background-color: blue;
}
.div1 {
position: relative;
height: 200px;
}
.div1 .box {
position: absolute;
bottom: 40px;;
right: 40px;;
width: 200px;
height: 40px;
background-color: red;
}
.div2 {
height: 100px;
background: green;
}
<div class="main-container">
<div class="navBar"></div>
<div class="div1"><div class="box"></div></div>
<div class="div2"></div>
</div>
what you're looking for is:
position:absolute;
bottom:0;
right:0; which will position things relative to the positioned parent.Note that the parent element (div) needs to have its position set as well. Most people do position:relative;
The values bottom:0 and right:0 means to move it 0px away from the bottom of the parent and 0 px away from the right side of the parent.
See the following w3schools for further information:
https://www.w3schools.com/css/css_positioning.asp
https://www.w3schools.com/css/tryit.asp?filename=trycss_position_absolute
Can someone tell me why this code doesn't work
<div id="dojam_text_container" style="width: 300px; height: 300px;
margin:auto; background-color:Blue;">
<div style="display:table-cell; vertical-align:middle;">
<span id="text_dojmovi1" style="">"bla bla bla</span></div>
</div>
and this does..
<div style="position:absolute; top:10vw; left:10vh;">
<div style=" border: dotted red 1px; background-color:white; width:20vw; height:
20vh; display:table-cell; text-align:center; vertical-align:middle; position:relative;">
<span> I am some centered shrink-to-fit content!
<br />
</span>
</div>
</div>
Give display:table to your parent div
<div style="position: absolute;display:table;width:300px; height:300px; top:100px; left:100px; background-color:Black;">
.............
</div>
Fiddle
Docs to understand tabular structure using divs
Vertical & Horizontal Centering
Common issues:
Parent do not have a position set
Ancestors do not have a height or width set
Solution for child with known dimensions
This is a solution if you need it to be IE6 compatible. If you know the dimensions of the child, then
define the height/width of the parent.
set the parent's positioning
position the child absolutely with top, left being 50%, 50%.
add a negative margin to offset the child's height & width
JsFiddle
html, body, div.parent {
position: relative;
width: 100%;
height: 100%;
}
div.child {
position: absolute;
width: 10em;
height: 10em;
top: 50%;
margin-top: -5em;
left: 50%;
margin-left: -5em;
}
Solution for child with unknown dimensions
This solution will work for IE8+. If you don't know the dimensions:
define the height/width of the parent.
set the parent's display to table
set the parent to vertical align: middle
JsFiddle
div.fluid.parent {
display: table;
vertical-align: middle;
}
div.fluid.child {
width: auto;
height: auto;
}
More Resources
For more resources, see w3.org
I have a very specific problem that although can easily be solved by changing the HTML or using negative margins/absolute positioning, in this occasion neither are possible.
In a nutshell, I need the "2nd-child" element which is larger than the "1st-child" element to be positioned against the coordinate space of the "wrapper". This may not be possible but being able to do so would greatly decrease the complexity required in some javascript calculations we need to perform.
Under normal circumstances we would set the "1st-child" to position relative or absolute in order to position the "2nd-child" against the coordinate space of its parent. I need to reverse this behaviour. Therefore I have tried static positioning on the "1st-child" naively assuming that the "2nd child" would be positioned against the "wrapper". (left:0; top:0; would result in the "2nd child" being positioned into the top left corner of the wrapper). While this positions the elements correctly it removes the required clipping set on the "1st-child" and displays the whole of "2nd-child".
So to summarise. How can I position the "2nd-child" element against the coordinate space of the "wrapper" element so that top:0; left:0; result in the "2nd child" being pushed into the top left corner despite it being a child the "1st-child" element while still maintaining the 'clipping' behaviour of overflow:hidden; which is set on "1st-child".
DISCLAIMER:
for the sake of readibility in the code below I have used ID's which start with numbers. This cannot be done in real code but makes referring to the elements far easier in this question.
HTML:
<div id="wrapper">
<div id="1st-child">
<div id="2nd-child">
</div>
</div>
<div>
CSS :
#wrapper {
position:fixed;
width:1000px;
height:1000px;
top:0;
left:0;
}
#1st-child {
margin-left : 200px;
margin-top : 200px;
width: 300px;
height: 200px;
position:static;
overflow: hidden;
}
#2nd-child {
position:absolute;
top:0;
left:0;
width:750px;
height:600px;
}
id attributes can't start with a digit. Renaming your id attributes and changing the selectors that reference them results in a page that has the layout you want.
Example on CodePen
Does this answer your question?
<div id="wrapper">
<div id="child1">
<div id="child2">
Hello
</div>
</div>
<div>
<style>
#wrapper {
width:600px;
height:300px;
background-color: pink;
overflow: hidden;
}
div#child1 {
margin-left: 100px;
margin-top: 50px;
width: 200px;
height: 150px;
border: 1px solid green;
}
div#child2 {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 200px;
border: 1px solid red;
}
</style>
I changed id's (they can't start with a number), and sizes (for debugging purposes)
See it in action: http://jsfiddle.net/5aL9s/
#2nd-child {
position:absolute;
top:-200px;
left:-200px;
width:750px;
height:600px;
}
I have this css:
#manipulate
{
position:absolute;
width:300px;
height:300px;
background:#063;
bottom:0px;
right:25%;
}
I have this html:
<div id="manipulate" align="center">
</div>
How do we position that div at the bottom center of the screen?!?
If you aren't comfortable with using negative margins, check this out.
HTML -
<div>
Your Text
</div>
CSS -
div {
position: fixed;
left: 50%;
bottom: 20px;
transform: translate(-50%, -50%);
margin: 0 auto;
}
Especially useful when you don't know the width of the div.
align="center" has no effect.
Since you have position:absolute, I would recommend positioning it 50% from the left and then subtracting half of its width from its left margin.
#manipulate {
position:absolute;
width:300px;
height:300px;
background:#063;
bottom:0px;
right:25%;
left:50%;
margin-left:-150px;
}
Use negative margins:
#manipulate
{
position:absolute;
width:300px;
height:300px;
margin-left:-150px;
background:#063;
bottom:0px;
left:50%;
}
The key here is the width, left and margin-left properties.
Here is a solution with two divs:
HTML:
<div id="footer">
<div id="center">
Text here
</div>
</div>
CSS:
#footer {
position: fixed;
bottom: 0;
width: 100%;
}
#center {
width: 500px;
margin: 0 auto;
}
Using a Flexbox worked for me:
#manipulate {
position: absolute;
width: 100%;
display: flex;
justify-content: center; // Centers the item
bottom: 10px; // Moves it up a little from the bottom
}
You can center it using negative margins BUT please note that it'll center exactly on the center of the screen IF any containing div is NOT SET to position:relative;
For example. http://jsfiddle.net/aWNCm/
So, best way to exactly center this div is to set correct properties position properties for its containing divs too otherwise it will be lost in some random ways.
100% working single line (Inline CSS Solve)
<div style="position: fixed; bottom: 10px; width: 100%; text-align: center;">Your Content Here</div>
100% working single line (Inline CSS Solve)
<div style="padding: 20px; width: 100%; text-align: center;">Your Content Here</div>
I'm finally trying to do away with tables and use CSS.
I have 3 DIVs that make up a three layered layout: header, body and footer. I'm now trying to overlay a 900px wide DIV on top of these layers, center aligned, which will hold some of my content and navigational buttons.
These are the 3 layers:
And this (done in Photoshop), is what I am trying to achieve but transparent to the eye:
My 3 base layers are coded like this:
<div id="main" style="width:100%; z-index:1; position:relative;">
<div id="header" style="width:100%; height:175px; text-align:center; background:#151515; z-index:1;"></div>
<div id="contents" style="width:100%; height:400px; position:relative; background:#FFF; z-index:1;"></div>
<div id="footer" style="width:100%; height:200px; position:relative; background:#151515; z-index:1;"></div>
</div>
I did manage to get a new layer to sit on top but it wasn't center aligned. Could somebody please point me in the right direction?
Somehting like this could help:
JSFiddle: http://jsfiddle.net/DSH5J/
Add:
<div id="square"></div>
#square {
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:0 auto;
margin-top:50px;
width:80%;
height:100%;
background-color:#333;
z-index:10;
}
Set the width and set margin-left and margin-right to auto. That's for horizontal only, though. If you want both ways, you'd just do it both ways.
margin-left:auto;
margin-right:auto;
Easiest way that I know of to centre a div of known width is to give it the following styles:
position: absolute;
left: 50%;
width: 900px;
margin-left: -450px;
"Putting my money where my mouth is": http://jsfiddle.net/YVmBU/2/
HTML:
<div id="main">
<div id="header"></div>
<div id="contents-box">
<div id="contents">
<p>Some text</p>
<p>etc</p>
<p>etc</p>
<p>etc</p>
<p>etc</p>
<p>etc</p>
</div>
</div>
<div id="footer"></div>
</div>
CSS:
#main {
}
#header {
position: relative;
height:100px;
background:#151515;
z-index: -1;
}
#contents-box {
border: dashed grey 1px; /* for understanding only, remove it in the end */
z-index: 1;
margin-top: -30px;
margin-bottom: -30px;
/* TODO: address min-height; try only one line of text. */
/* fixed height would work too, but would not let the box stretch dynamically */
}
#contents {
width: 75%;
height: 100%;
margin: 0 auto;
background: grey;
z-index: 1;
}
#footer {
position: relative;
height:75px;
background:#151515;
z-index: -1;
}
The only problem is with few text content: if min-height is used on #content, then the grey background does not stretch when there is few text; if a static height of N px is used, then the box does not stretch dinamically.
But if the two black bars merging when there is few content is not important, then ignore it.
Remove the grey dashed border and grey background; those are helpers - to know where each box is and understand what is happening.
By the way, the position: relative needs to be there on the z-index: -1; layers, otherwise the background does not go under. Read on position: this is because things in html have position: static by default, and z-index relies on position for its behaviour.
You can read about this in this page: http://tjkdesign.com/articles/z-index/teach_yourself_how_elements_stack.asp
The only problem is with few text content: if min-height is used on #content, then the grey background does not stretch when there is few text; if a static height of N px is used, then the box does not stretch dinamically.
But if the two black bars merging when there is few content is not important, then ignore it.