Image in the middle between header and footer - CSS - html

I want to built a simple landing page with a header,footer and an image - exactly between the header and the footer (horizontally/vertically centered).
The space between the header/footer and the image should be the same and should depend from the height of the browser-window.
The image has a fixed width (900px) and a fixed hight (600px).
Aw: it is a sticky footer
I have tried something like this:
{display:block; padding:0 40px; width:900px; margin:0 auto; vertical-align:middle;}
my html:
<div class="fbg">
<div class="fbg_resize">
<img src="images/image.png" width="900" alt="" />
</div>
<!--<div class="clr"></div>-->
to get it horizontally centered:
.fbg_resize { margin:0 auto; padding:0 40px; width:900px;}
here is the code that matters:
http://jsfiddle.net/SFWBL/

Have a look at this fiddle for the basic premise, it should be enough to get you started.
HTML
<div id='header'></div>
<div id='image'></div>
<div id='footer'></div>
CSS
html, body{
text-align:center;
height:100%;
width:100%;
margin:0;
padding:0;
}
#header, #footer{
height:50px;
width:100%;
}
#image{
height:50px;
width:50px;
margin:-25px auto 0 -25px;
background:grey;
position:absolute;
top:50%;
left:50%;
}
#header{
background:blue;
}
#footer{
position:absolute;
bottom:0px;
background:red;
}

Instead of using an img, you can try background-image for the div
html, body {
height: 100%;
}
.fbg {
background-image: url(http://lorempixel.com/900/600);
background-position: center;
background-repeat: no-repeat;
width: 100%;
height: 100%;
}
See modified JSFiddle

Relative (percentage) positions are the way to get your elements to recognize the size of the browser window. Since they work on the edges (top, left), you have to use a negative margin to move the item back up half the height of your item. Since you know the fixed height of your image is 600px, you need -300px. You want to give your image:
position: absolute;
top: 50%:
margin-top: -300px;

Related

Overlay image on the bottom of a responsive image slider

I'm trying to add an image to the bottom of a responsive slider and need help sticking it to the bottom.
When in full screen mode it works fine, but as I shrink the page it keeps floating up, higher and higher.
I know I can set media queries to fix the issue on certain breakpoints, but I'm looking for a more adaptive solution that will work without having to use media queries.
Here is an image of what the slider looks like at full width. (This is fine.)
Then as I start to scale the screen you can notice the grass image moving up.
This is my problem. I need that grass to stay glued to the bottom of the slider.
I don't want to post all the slider code so I made a simple version on JSFiddle.
Here is the code.
HTML:
<div class="pic" align="center"><img src="http://i.imgur.com/8uVGUkM.jpg" class="slider"/></div>
<div class="grassframe"><img src="http://i.imgur.com/vw8soIm.png" class="img-max"/></div>
CSS:
.slider{width:100%; max-width:960px; height:auto; position:relative; z-index:1;}
.grassframe {
position:relative;
max-width:1200px;
margin:-135px auto;
z-index:2;
}
.img-max { width:100%; height:auto; }
I created a JSFiddle if someone wouldn't mind taking a look.
You can use absolute position and margin-top with % value. DEMO
Vertical padding or margin using % value use the parent's width as reference. So once tuned it will worke with any width.
.slider {
width:100%;
max-width:960px;
height:auto;
position:relative;
z-index:1;
border:solid;
}
.grassframe {
position:absolute;
margin-top:-12.5%;
max-width:1200px;
left:0;
right:0;
z-index:2;
}
.img-max {
width:100%;
height:auto;
}
If you use a wrapper in relative position and set to same max-width, it even works better (grass in absolute or relative position doesn't matter much here as long as z-index is avalaible ): DEMO
.slider {
width:100%;
max-width:960px;
height:auto;
position:relative;
z-index:1;
border:solid;
}
.grassframe {
position:relative;
margin-top:-12%;
max-width:1200px;
left:0;
right:0;
z-index:2;
}
.img-max {
width:100%;
height:auto;
}
.container {
position:relative;
margin:auto;
max-width:1200px;
}
Try out the below code:
HTML
<div class="pic" align="center">
<img src="http://i.imgur.com/8uVGUkM.jpg" class="slider" />
<div class="grassframe"></div>
</div>
CSS
.pic {
position: relative;
}
.slider {
max-width:100%;
}
.grassframe {
background: url("http://i.imgur.com/vw8soIm.png") no-repeat scroll center top / 1920px auto rgba(0, 0, 0, 0);
bottom: -87px;
height: 310px;
position: absolute;
width: 100%;
}
Have a look at the JSFiddle

Why doesn't sticky footer work?

I have tried several things but my sticky header does not work. I've been trying several tutorials I found, but none worked. I have also looked at different post on stackoverflow but none described my problem.
Here's my HTML:
<div id='container'>
<div id='header>blabla</div>
<div id='actualpage'>bblablabal</div>
<div id='footer'>blablafooterblabla</div>
</div>
And here's the css:
html, body {
padding: 0;
margin: 0;
height: 100%;
}
#container{
background-color:white;
width:100%;
min-height: 100%;
height:100%;
}
#actualpage{
width:750px;
margin-left:auto;
margin-right:auto;
padding-bottom: 20px;
}
#footer{
margin-top:-20px;
clear:both;
height:20px;
background-color:#A6CE39;
padding-top:6px;
padding-bottom:6px;
min-width:100%;
bottom:0;
}
Thank you for your help!
You can add position: fixed or position:absolute (if you don't want the footer stick to bottom while scrolling) to your #footer:
#footer{
margin-top:-20px;
clear:both;
height:20px;
background-color:#A6CE39;
padding-top:6px;
padding-bottom:6px;
min-width:100%;
bottom:0;
position: fixed;
}
add position:absolute; to your #footer
and <div id='header> should be <div id='header'>
If you are referring to your footer, you may add position: absolute to your #footer
Fiddle
#footer {
margin-top:-20px;
clear:both;
height:20px;
background-color:#A6CE39;
padding-top:6px;
padding-bottom:6px;
min-width:100%;
bottom:0;
position: absolute;
}
#container{
background-color: yellow;
width:100%;
min-height: 100%;
}
#actualpage{
width:750px;
margin-left:auto;
margin-right:auto;
padding-bottom: 20px;
}
#footer{
margin-top:-32px;
clear:both;
height:20px;
background-color:#A6CE39;
padding-top:6px;
padding-bottom:6px;
min-width:100%;
}
<div id='container'>
<div id='header'>blabla</div>
<div id='actualpage'>bblablabal</div>
</div>
<div id='footer'>blablafooterblabla</div>
You need the footer outside of the container div.
Also, the bottom: 0; attribute was unnecessary and the negative margin for the footer needed to include the padding, which adds to the computed height
** also, add the closing "'" to id='header
tl;dr
See working CodePen: http://cdpn.io/KwzuA
or use Flexbox - see demo: http://codepen.io/astrotim/pen/PwYQOJ/right/?editors=010
Explanation
Using position for a sticky footer is typically not a good idea, as it removes the element from the document flow and can have undesired results of the footer overlapping the content when scrolling.
A tried and trusted method is to add a "push" div inside the wrapper div and then have the footer div below, outside the wrapper. Like this:
<div id="wrapper">
<header>
<h1>Header</h1>
</header>
<div id="body">
<p>Lorem ipsum...</p>
</div><!--#body-->
<div class="push"></div>
</div><!--#wrapper-->
<footer>
<p>Footer</p>
</footer>
For the CSS, you will need to set html, body and #wrapper with height: 100%. You then set a fixed height to your footer and apply the same height to #push. Then you offset the body with a negative margin-bottom. The #wrapper needs a few other properties, like so:
html, body {
height: 100%;
}
body {
overflow-y: scroll;
}
#wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -160px;
}
.push, footer {
height: 160px;
}
footer {
/* remember the box model: padding+height */
padding-top: 15px;
height: 145px;
}
The footer will now flow properly when the content of the page extends below the fold, and will be sticky when the content does not.
** Use Flexbox **
The modern approach to this is using Flexbox. See: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/
I used the Flexbox technique on a recent project

Absolute Position - Center - Max-width

I have som problems, regarding Max-width and Absolute Positioning.
In the top of my site, I have a '100%-width section' with a background-image.
I want a small image partly covering the bottom of the top image. To do so, I use Position:relative on the section (top image) and position:absolute on the small image. So far so good..
My problem is, that i want the small image to be centered and have a max-width, so it aligns to the content on the site. (Which have a max-width of 500px..)
I really can't figure out, how to do it...
So, is there a way to only affect vertically, when using position:absolute, or maybe something else i can do?
I hope it makes sense.. :)
screens:
http://cvdmark.com/images_help/screens.jpg
<body>
<section id="img_heading">
<img src="#">
</section>
<section id="content">
<ul>
<li>
</li>
</ul>
</section>
</body>
body {
width:100%;
min-width:100%;
background-color:#000;
float:left;
}
#img_heading{
width:100%;
min-width:100%;
height:150px;
margin-bottom:6em;
background:url(../img/heading_img_test.jpg);
background-position: center center;
float:left;
position:relative;
}
#img_heading > img{
width:90%;
height:100px;
position:absolute;
bottom:-75px;
margin: 0 auto 0 auto;
max-width:500px;
/*left:5%;*/
background-color:#F69;
}
#content{
width:90%;
margin: 0 auto 0 auto;
max-width:500px;
}
#content ul{
width:100%;
list-style-type: none;
}
#content ul li{
width:100%;
float:left;
margin-bottom:30px;
}
JS Fiddle : http://jsfiddle.net/rpatil/ukKgx/
I think what you'd want is to align an absolute position div to the bottom of the section and center it.. (this following assumes you have your content in the section centered too..) replace div with img if you want to and give it your required id.. I ve given "foo-id"
div#foo-id
{
position: absolute;
bottom: 0px; /*since you want it at the bottom of section*/
z-index: 2;
width:90%; /*since your asked for 90%*/
max-width: 500px;
/*the following to center your image*/
margin-left:-45%;
left:50%;
}
Believe that helps!
And here is an updated jsfiddle.. thanks #rubyist..
http://jsfiddle.net/ukKgx/2/
This is an other solution to make it centred --> jsFiddle
<section style="position: absolute; left: 50%;width:50%">
<div style="position: relative; left: -50%;background-color:#ccc; border: dotted red 1px;width: 90%;max-width:500px;height:150px">
</div>
</section>
If you want to make it more custom, you have to use Javascript onLoad page to calculate screen width % picture width

Css: How to make position fixed 100% resizable with bottom padding? - JS Fiddle provded

I'm trying to make fixed 100% but with a little frame around.
I just cant get it right, the frame would appear EITHER top/ bottom, or left/ right, but not from both sides...
Here's what I've got so far:
div.all_reviews{
background: url(../design/trans-gr.png);
position:fixed;
width:100%;
height:100%;
z-index:12500;
padding:15px;
}
div.wrap1{
width:100%;
height:100%;
background-color:#00AEEF;
}
EDIT:1 ALLRIGHT THEN, This is what I've gotten to so far:
http://jsfiddle.net/Hm7Mw/
div.all_reviews{
background: url(../design/trans-gr.png);
position:fixed;
width:100%;
height:100%;
z-index:12500;
overflow:auto;
}
div.wrap1{
display: block;
position:absolute;
height:auto;
width:100%;
min-width:962px;
bottom:6px;
top:6px;
left:0px;
right:0px;
}
div.wrap2{
margin:0px auto;
max-width:960px;
height:100%;
position:relative;
overflow:visible;
}
div.wrap3{
overflow:hidden;
height:auto;
min-height:100%;
width:100%;
position:absolute;
background-color: #FFF;
}
Again, it works perfectly with scrolling - ie,. I've made it scroll the whole thing, rather than what's inside the wraps.
However if I scroll it down, the padding at the bottom vanishes for some reason.
if I put overflow auto to the inner containers instead, then it would sort of 'fix' it, but they would have very ugly scrollbars in the middle of the screen- which I don't want.
HTML:
<div class="all_reviews">
<div class="wrap1">
<div class="wrap2">
<div class="wrap3">
(BLA)
</div>
</div>
</div>
</div>
</div>
You need something like this:
.onTopOfAll {
bottom: 0;
left: 0;
position: fixed;
right: 0;
top: 0;
z-index: 1;
/* Something else for style */
}
.onTopOfTop {
bottom: 15px;
left: 15px;
position: fixed;
right: 15px;
top: 15px;
z-index: 2;
/* Something else for style */
}
<div class="all_reviews">
<div class="wrap1">
<div class="wrap2">
<div class="wrap3">
/8Content/8
</div>
</div>
</div>
</div>
all_reviews{
background: url(../design/trans-gr.png);
position:fixed;
width:100%;
height:100%;
z-index:12500;
display:none;
}
div.wrap1{
display: block;
position:absolute;
height:auto;
width:100%;
bottom:25px;
top:5px;
left:-10px;
right:0px;
}
div.wrap2{
margin:0px auto;
width:100%;
max-width:940px;
min-height:100%;
position:relative;
}
div.wrap3{
overflow:auto;
height:100%;
width:100%;
position:absolute;
background-color: #FFF;
border:5px solid #CCC;
padding:5px;
}
Note to undo display : none with a script, and make the body fixed with overflow:hidden, so it doesn't scroll along the way on the background.
Try height and width of auto of div.all instead of 100%. You may need to add a height of 100% to your body.
your padding is adding some extra width in your div. You gave you div a width of 100% and as you applied the padding now the total width is 15px + 100% + 15px. If you can provide your html as well some idea about what you are going to do then it'll be helpful for rest of us to help you.
thanks.

Scrolling of whole page instead of inner div only

I'm fiddling with CSS again again...
http://www.kebax.dk/test.php
As you see, the container called map is scrolling independently of the rest of the container. How can I make the whole page scroll when more content is placed in the content?
I have tried using the overflow attribute, but without luck...
EDIT for future references:
body {
background:#000000;
margin:0;
padding:0;
overflow:scroll;
overflow-x:hidden;
}
#container{
position: relative;
height: 100%;
width: 950px;;
background: yellow;
margin-left:auto;
margin-right:auto;
overflow:auto;
}
#map {
position:absolute;
top:80px;
bottom:0;
left:0;
right:0;
background:#fff;
overflow:auto;
overflow-x:hidden;
}
#header {
height:80px;
width:900px;
background:#333;
margin:0;
padding:0;
}
#header h1 {
color:#fff;
margin:0;
padding:0;
}
#leftgradient {
width:50px;
height:80px;
float:left;
background:#096;
background: -webkit-gradient(linear, left top, right top, from(#000000), to(#333333));
}
#rightgradient {
width:50px;
height:80px;
float:right;
background:#096;
background: -webkit-gradient(linear, left top, right top, from(#333333), to(#000000));
}
#toppanel {
background:#179AE8;
width:950px;
height:50px;
}
#leftpanel {
background:#179AE8;
width:100px;
height:250px;
float:left;
}
#content {
background:#099;
width:850px;
margin-left:100px;
}
<div id="container">
<div id="leftgradient"></div>
<div id="rightgradient"></div>
<div id="header">
<header>
<h1>
Heading
</h1>
</header>
</div>
<div id="map">
<div id="toppanel">
top
</div>
<div id="leftpanel">
lefty
</div>
<div id="content">
Lots of text!!
</div>
</div>
</div>
If I understand correctly, you just need to remove a boatload of CSS declarations:
On body, remove: overflow: hidden.
On #container, remove: height: 100%, overflow: auto, position: relative.
On #map, remove:
position: absolute, left: 0, bottom: 0, right: 0, top: 80px
overflow-x: hidden, overflow-y: auto.
Now you can scroll the page (tested in Firefox only).
Removing all that stuff possibly broke some certain functionality on your page. Let me know if there is anything, and we can see about finding another way to add back in this missing functionality.
To fix the issue in the comments, add:
html, body { height: 100% }
On #container, add min-height: 100%.
Now, you can see the yellow background poking out on #container. A way to fix this would be to change that yellow to white, and then use a background image exactly like this:
(save and use it)
On #container:
background: #fff url(http://i.stack.imgur.com/q1Sp1.png) repeat-y
You also need to remove the white background-color from #map.
overflow: scroll; :) give that a whirl