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.
Related
This is my html code.
I want header separated with content and content should be separated with header and footer with each having position equal to absolute and stacked one below the other
<body>
<div id="header" class="div">
</div>
<div id="content" class="div">
</div>
<div id="footer" class="div">
</div>
</body>
css code
.div {position: absolute}
#header{top:0;left:0;right:0}
#footer{bottom:0;left:0;right:0}
You will need to specify top in the css of the content div, whose value should be equal to the header's height when you are using absolute positioning. One more thing, you may even achieve your purpose with more simple ways by using relative positioning. But as per your approach right now, this could be done.
Here is a working example.
These are the changes in the css code:
.div {
position: absolute;
border:solid 1px #000; /*added this just to visualize things clearly.*/
}
#header{
top:0;
left:0;
right:0;
height:50px; /*specified some random height to the header.*/
}
/* specified css rules for the content div */
#content{
top:50px; /*this should be equal to or more than header's height.*/
left:0;
right:0;
}
#footer{
bottom:0;
left:0;
right:0;
height:50px;
}
hm. I dunno if I really understand your question.
why are you positioning everything absolutely? it takes elements out of normal flow.
for instance if you just use your html:
<body>
<div id="header" class="div">
</div>
<div id="content" class="div">
</div>
<div id="footer" class="div">
</div>
</body>
And some simple CSS without absolute positioning:
#header {width: 100%;
height: 200px;
background-color: red;}
#content {width: 100%;
height: 200px;
background-color: blue;}
#footer {width: 100%;
height: 200px;
background-color: yellow;}
You get three divs stacked on each other. if you want to remove the margins / padding :
* {margin: 0px;
padding: 0px;}
I'm not sure what you mean by "stacked one below the other".
Perhaps you are referring to z-index (http://www.w3schools.com/cssref/pr_pos_z-index.asp)? In that case, use something like this:
.div {
position: absolute;
}
#header{
z-index: 3; /* uppermost */
}
#content{
z-index: 2; /* in between */
}
#footer{
z-index: 1; /* lowest */
}
Or are you referring to vertical position? – in which case https://stackoverflow.com/a/33030721/5412860 answer would work.
Though https://stackoverflow.com/a/33030747/5412860 makes more sense...
I want to create responsive popup banner with close button here is my simple scenario:
<div class="banner">
<img src="...">
X
</div>
And my CSS:
.banner img{
max-width:100%;
max-height:100%;
position:absolute;
}
.close-btn{
position:absolute;
right:0;
z-index:2;
color:red;
background:#000;
padding:4px;
}
As you can see I stretch image depending on width and height.
Problem: I want close-btn to stick to the right side of the image and overlap it. To solve this the banner must be the same width as the image. If banner has position:absolute its width and height of course is 0.
Is it possible to achieve only with CSS?
Here is fiddle: http://jsfiddle.net/fjckls/qq590xz5/
I need image to be responsive to width and height
To make your image fully width AND height responsive, first off, you need to alter your units. You're currently using %'s which is all well and good, but for the 'fully height responsive' concept, the % units aren't much help.
Instead, you should look into using vh (view-height) and vw (view-width) units, since these are for the actual viewport that the user can see currently.
In order to position your 'x' over the top right of your image, you're going to have to alter your css slightly.
You could possibly include a css rule for your banner, first off. Something like:
.banner {
position:relative;
display:inline-block;
}
Whilst removing the 'position:absolute' rule from your image, since now your banner div will be the size of your image (not the default '100% of screen' that divs are set to originally).
This leaves us one problem, you haven't actually set where abouts you want the 'x' to appear vertically, so it will default to 'where it would position normally', which, in this case, would be below the image. To tackle this, you would need to add a top: or bottom: declaration to your 'x' class, and in my case, i've chosen to set it to the top (top:0;).
The overall fiddle can be shown here
or here:
.banner img {
max-width: 100vw;
max-height: 100vh;
}
.close-btn {
position: absolute;
right: 0;
top: 0;
z-index: 2;
color: red;
background: #000;
padding: 4px;
}
.banner {
position: relative;
display: inline-block;
}
<div class="banner">
<img src="http://sockets.hogwartsishere.com/media/book_covers/l-bunny.jpg" /> X
</div>
I have updated the link
http://jsfiddle.net/qq590xz5/3/
<div class="banner">
<div style="position:abolute;">
<img src="http://sockets.hogwartsishere.com/media/book_covers/l-bunny.jpg">
X
</div>
</div>
.banner img{
max-width:50%;
max-height:100%;
}
.close-btn{
position:absolute;
z-index:2;
color:red;
top:1%;
background:#000;
padding:4px;
}
Have a look
Thanks
try this..
Html
<div class="banner">
<img src="http://sockets.hogwartsishere.com/media/book_covers/l-bunny.jpg">
X
</div>
CSS
.banner{
position:relative;
width:200px;
}
img{
max-width:100%;
}
.close-btn{
position:absolute;
right:0;
top:0;
z-index:1;
color:red;
background:#000;
padding:4px;
}
Fiddle Demo
I found a solution that keeps the image centered horizontally and the x button on the top right of the image. It involves:
1) Making the .banner absolutely positioned, with margins from each window edge. This centers the entire .banner, however you might want to use fixed position if you need it to scroll along with the user's viewport.
It'll work as long as there aren't any other positioned elements as its parents.
.banner {
position: absolute;
top: 5%;
left: 25%;
right: 25%;
bottom: 5%;
}
2) Making a thing that sticks around the image, which will serve as a positioning guide for the little X.
<div class="shrinkwrap">
<img src="...">
X
</div>
.shrinkwrap {
/* shrink-wraps this div around its content;
as a side-effect, lets this div be centered with text-align: center; */
display: inline-block;
/* new positioning context! */
position: relative;
/* keeps the responsiveness */
max-width: 100%;
height: 100%;
}
3) Positioning the shrinkwrapper to always be in the center of the .banner.
.banner {
/* ... */
text-align: center;
}
.close-btn {
/* ... */
top: 0;
}
The finished version of this is here: http://jsfiddle.net/boxmein/qq590xz5/5/
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/
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>
In all the posts I've been able to find a deal with either centering a div inside another div or putting a div at the bottom of another div, and the advice has been great but I haven't been able to find anything to do both.
My code is:
<body style="text-align:center; margin:0; padding:0;">
<div style="width:100%; height:100px; background-image:url(header.png);position:relative;">
<div>
<div style="height:75px; width:950px; background-image:url(formtop.png); bottom:0; position: absolute; margin-left:auto; margin-right:auto;">
<div style="float:left; position:relative; left:30px; top:15px">
<img src="logo.png" width="88" height="38">
</div>
<div style="margin-top:15px">
<h1>Product Form</h1>
</div>
</div>
</div>
</div>
</body>
All I want to do is to put the formtop.png div at the bottom and center of the containing div. I can do one or the other but I can't do both. If I change position:absolute to position:relative then the image centers itself but its too high. When I change it back to absolute then it sits nicely at the bottom of its containing div but in IE it's way off the right and in firefox it's at the left side of the page.
Any advice?
You can do it by setting the formtop.png <div> to 100% width and centering the background image using CSS:
<!-- div with the formtop.png background -->
<div style="
height:75px;
width:100%;
background:url(formtop.png) no-repeat 50% 0;
bottom:0;
position: absolute;">
As an aside, if you move all your inline styles into a .css file, your code will be a lot easier to work with and maintain:
<div class="formTop">
.formTop {
position: absolute;
bottom: 0;
left: 0;
height: 75px;
width: 100%;
/* Set the background image to centered in this element */
background: url(formtop.png) no-repeat 50% 0;
}
Have you tried left:0; right:0; trick for absolutely positioned elements? It does not work for IE7 nor IE6, but it does for the rest of the browsers and later versions.
This is an example http://jsfiddle.net/6w6VR/
Try to avoid html elements that are only used for style, because you might wish to change your style later.
See this example, which uses the :after pseudoclass:
div {
width:100%;
position: relative;
background-image: url(header.png);
}
div:after {
display: block;
position: absolute;
z-index: -1;
bottom: 0;
left: 0;
content: '';
width: 100%;
height: 20px;
background: url(http://upload.wikimedia.org/wikipedia/commons/3/38/Wiktionary-ico-de.png);
background-repeat: no-repeat;
background-position: center 0px;
}
You can set your header.png as background for div.