Center image vertically between header and bottom of window - html

I am trying to center an image of a phone vertically. The code I have to far works but if I decrease the window height the phone image will overlap the header. What I want to do is center the phone image vertically between the bottom of the header and the bottom of the window and stay there no matter how tall the window is (but not overlap the header).
Link to jsfiddle: jsfiddle.net/#&togetherjs=zAMDokl6RG.
Having lots of issues with this. Could someone give me some pointers on how to do this please? Thanks :
css:
* {
margin: 0;
padding: 0;
/* To keep our header correct */
}
#header {
background: #e9e6e6;
/* Here set your colour */
height: 55px;
width: 100%;
top: 0;
left: 0;
position: absolute;
/* box-shadow: 0px 2px 2px #888888;*/
}
.innerdiv {
position: absolute;
bottom: 0;
right: 0;
padding: 0px 0px;
z-index: -2;
}
.dllogodiv {
position: absolute;
top: 0;
right: 0;
padding: 5px 5px;
}
.centeredImage {
text-align: center;
display: block;
}
.centeredImage img {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
<div id="header">My header</div>
</div>
<div class="innerdiv">
<img class="imageCorner" src="http://s4.postimg.org/tyfx93u8p/logo.png">
</div>
<p class="centeredImage">
<img src="http://s4.postimg.org/p12cnzs9l/slide1.png">
</p>

heres a fiddle I put together
the idea is to have a top/middle/bottom. There is a css calc property you can use to calculate something, like height. Assuming you know what the height of your image is (lets say 200px), you can do:
top: calc(50% - 100px);
this will make the top of your image 50% from the top, minus half the size of the image, so that the middle of the image is 50% from the top.
of course, you have to set the middle section to position relative or absolute, and make the image position absolute inside.
This is just one quick way, there are other ways. Then again, usually you want to center something within a div, not the whole page.

Related

Responsive layout, how to keep text at bottom of dynamically resizing image

I am trying to overlay some text on an image.
This is easy if the image location and size stays the same, but here I am allowing the image to dynamically resize based on screen size.
http://jsfiddle.net/xcs9L7u6/1/
When I set the position of the text to absolute, the text box is the right size, and I can place it at the bottom of the image just fine, but that doesn't work when the image bottom keeps changing due to window size.
So..
how do I keep the text at bottom of the resizing image height?
keep the text box to the width of the resizing width of the image?
HTML :
<div>
<div class="gallery-background">
<div class="gallery-text">Setting up some text to look at boats and fill space so that things move and wrap but need more text as it didn't quite give the right feel</div>
<img src="http://static.giantbomb.com/uploads/original/0/4530/396796-boat.jpg" class="galleryLrg" />
</div>
</div>
CSS :
.gallery-background {
margin: 1.5rem 1rem 1rem 1rem;
/*needed for firefox and ie*/
height: 100%;
}
.gallery-text {
color: white;
padding: .5rem;
max-width: 100%;
display: inline-block;
text-align: left;
background-color: rgba(0, 255, 0, .65);
position: absolute;
}
.galleryLrg {
display: inline-block;
height: 90%;
width: 100%;
}
Any thoughts would be great,
Thank you.
All you've go to do is to set .gallery-background to position: relative, and gallery-textto position: absolute. From there onwards, maintaining .gallery-text at the bottom of the image is just a matter of setting the bottom, left and right CSS attributes to 0.
Fiddle here
Code to be changed:
.gallery-background {
position: relative;
}
.gallery-text {
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
just set .gallery-text to bottom: 0 and it will always sit at the bottom:
.gallery-text {
color: white;
padding: .5rem;
max-width: 100%;
display: inline-block;
text-align: left;
background-color: rgba(0, 255, 0, .65);
position: absolute;
bottom: 0; //add
}
and add position:relative to it's parent to keep it contained:
.gallery-background {
margin: 1.5rem 1rem 1rem 1rem;
/*needed for firefox and ie*/
height: 100%;
overflow: hidden; //add to hide excess
position: relative; //add to contain absolute element
}
FIDDLE
JSFiddle
There two points you should modify,add these attributes.
.gallery-background {
position:relative;
}
. gallery-text {
bottom:0;
}

Centered lightbox with dynamic height

I'm working on a lightbox. I need it to be dynamically sized based on its content. But I also need it to be centered in the screen. I'm trying something like this:
HTML:
<div class="lightbox-background">
<div class="lightbox">
LIGHTBOX CONTENT
</div>
</div>
CSS:
.lightbox-background {
background-color: rgba(0,0,0,0.9);
height: 100%;
left: 0;
position: fixed;
top: 0;
width: 100%;
z-index: 50;
}
.lightbox {
background-color: white;
width: 780px;
z-index: 100;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
height: auto !important;
max-height: 90%;
}
I couldn't make it work. I'd like to avoid using JS, if possible. How can I do it?
You could work with vertical-align: middle as well as the :before selector on the parent container. Check out my fiddle:
http://jsfiddle.net/GA5K3/2/
The best way that I know to center vertically with CSS is to absolute position top 50% then set a top margin negitave half height of element.
Since you don't know the height you'll have to use JS.
Maybe someone has a better technique.

grey transparent unclickable back screen

I am trying to create a gray transparent background screen, on top of my original html page.
What I have done so far is to append a div (with jquery) to the body tag with this css style:
.spesificPropertiesDiv {
position: absolute;
display: block;
top: 0px;
width: 100%;
height: 100%;
background-color: black;
opacity: 0.5;
z-index: 6000;
text-align: center;
}
as I mentioned before I am appending a div with this class to the body.
Every works fine when I append it on large screen (24 inch) but when I am appending it on 16 inch display the gray screen div's height is 100 px less than the body's height.
One more thing that I need to mention is that on large screen the page is fit on the screen where on the smaller screen a scroll-bar appears to make the page lower side of the page visible.
Why dose this happen? How can I fix it?
Thanks!
I have changed it to:
.spesificPropertiesDiv{
display: block;
position: fixed;
top: 0; bottom: 0; left: 0; right: 0;
width: 100%;
height: 100%;
background-color: black;
opacity: 0.5;
z-index: 6000;
text-align: center;
}
and it works!!!!
Thank you all for the help
Could you try:
.spesificPropertiesDiv {
position: fixed; *position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
}
Additionally, is there any padding on this div? Is it a direct child of the <body> tag?:
<body>
<div class="spesificPropertiesDiv"></div>
</body>

Centering an absolute positioned div within IE7

I have a relatively simple layout as seen in the simplified version of the code below:
<div id="protocol_index_body_wrapper">
<div id="protocol_index_body">
</div>
</div>
The CSS
#protocol_index_body_wrapper {
background: url("/images/stripe.png") repeat scroll 0 0 transparent;
position: absolute;
top: 120px;
left: 0px;
right: 0px;
bottom: 10px;
}
#protocol_index_body {
width: 960px;
margin: 0 auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
The absolute positioning is needed to make sure that both div's expand to the full height of the window. The expected behavior is seen in the image above and is present in IE8, Firefox, and Chrome. However, in IE7 the div which should be centered is flush against the left side. Any ideas how to fix this?
Have you tried specifically assigning an auto value to the margin-left and margin-right? This will force both margins to be equal and center the div provided your div is not at 100% width of the page.
{
margin-left: auto;
margin-right: auto;
}

How do I get a <div> to always be in the top right corner of the browser window regardless of browser size?

I want the following code to always be in top right, regardless of the size (i.e. when the user resizes the browser window, it is still in the same position):
<div id="navbar">Our Blog</div>
The CSS that accompanies this as follows:
#navbar {
position: absolute;
left: 850px;
width: 100px;
padding: 15px 0 0 0;
}
I would like to do it in CSS and HTML only.
change it from being positioned form the left to be positioned from the right...
#navbar {
position: absolute;
right: 0;
width: 100px;
padding: 15px 0 0 0;
}