Keep image centered - html

I have this simple html:
<img src="http://i1.ytimg.com/vi/XWGszmviDpA/maxresdefault.jpg" class="bg">
So far i centered the image correctly as long as the window is not smaller than the image. Now i have problems to keep the face in the middle when the window is smaller! How can i fix this?
For me its important that the image height stays 100% of the window.
My css:
img.bg {
width: auto;
height: 100%;
position: fixed;
left:0;
right:0;
margin:0 auto;
}
Thanks!
http://jsfiddle.net/67jDp/1/

Try using it as a background-image.
CSS:
.bg {
background-image:url('http://i1.ytimg.com/vi/XWGszmviDpA/maxresdefault.jpg');
width: auto;
height: 100%;
position: fixed;
left:0;
right:0;
margin:0 auto;
width:70%; //Custom dimension
background-position:center;
background-size:cover;
}
Demo: http://jsfiddle.net/lotusgodkk/67jDp/2/

Try setting the image as the background of a div instead of an img tag
http://jsfiddle.net/CeVwN/1/
.bg {
background-image:url('http://i1.ytimg.com/vi/XWGszmviDpA/maxresdefault.jpg');
background-position:center;
background-size: auto 100%;
background-repeat: no-repeat;
width: auto;
height: 100%;
position: fixed;
left:0;
right:0;
margin:0 auto;
}

You can use this with little javascript
css :
img.bg {
height: 100%;
position: fixed;
left:50%;
}
JS :
var i = $('img');
var w = i.width();
i.css({marginLeft: -w/2});
$( window ).resize(function() {
i.css({marginLeft: -w/2});
});
DEMO : http://jsfiddle.net/67jDp/4/

Set width to 100% to get always the container's width. Then, set height to auto to allow the ratio:
img.bg {
width: 100%;
height: auto;
position: fixed;
left:0;
right:0;
margin:0 auto;
}
Remember to set a max-width to stop expanding when necessary.

Related

CSS: Element's relative size makes background-image disappear

I have a background-image that is 800x480 pixels. When my element has a fixed size I see the background-image, but not when the element has a relative size or a max-width.
Working CSS script
.audio-container, .settings-container {
max-width:800px;
height:480px;
position:absolute;
background-image:url("../../public/images/Audio/I_Audio_BGK.png");
background-repeat: no-repeat;
background-size: 100% 100%;
}
CSS script with no background image showing
.audio-container, .settings-container {
width:100%;
/* Same result with max-width */
height:100%;
position:absolute;
background-image:url("../../public/images/Audio/I_Audio_BGK.png");
background-repeat: no-repeat;
background-size: 100% 100%;
}
What can I do to show the background-image yet have the element sizes relative to the browser window?
By request, here are the parent DIVs
<div ng-controller="MainController" class="main-guy">
<div class="screen-inside">
<div class="audio-container" ng-controller="AudioController">
</div>
</div>
</div>
Here are the parent DIV CSS styles
.main-guy {
position:absolute;
/* Same result if width and height are set to a fixed number */
width: 100%;
height: 100%;
margin: auto;
}
.screen-inside {
margin:auto;
position:relative;
height:60%;
width:66.66%;
}
You have to change the position:absolute in .settings-container to position:relative as your image in this case act as a Child for .settings-container and the image should be according to its parent. So Position:absolute will not work.
Check the snippet
.main-guy {
position:absolute;
width: 100%;
height: 100%;
margin: auto;
background:#999;
}
.screen-inside {
margin:auto;
position:relative;
height:60%;
width:66.66%;
background-color:blue;
}
.audio-container, .settings-container {
width:100%;
/* Same result with max-width */
height:100%;
background-image:url(http://reservations.life/css/images/bg-01.jpg);
background-repeat: no-repeat;
background-size: cover;
position:absolute;
}
<div ng-controller="MainController" class="main-guy">
<div class="screen-inside">
<div class="audio-container" ng-controller="AudioController">
</div>
</div>
</div>
Using the following HTML:
<div class="settings-container"></div>
With the following CSS:
html, body { height: 100%; margin: 0; padding: 0; }
.settings-container {
width: 100%;
height: 100%;
text-align: center;
background-image: URL("your-image-here");
background-repeat: no-repeat;
background-size: cover;
}
Results in a background taking up 100% of the width and height of the viewport. It's difficult to solve your question properly without seeing the whole picture, but my guess is that you will need to apply height somewhere else in your document.
You may also run into issues with using position: absolute, but again that largely depends on the broader picture of how you're applying this to your site/application/whatever.

Single emcompassing Overflow DIV with an in image

I'm trying to make my image full screen while overflowing the div
pulled from here:
CSS - how to overflow from div to full width of screen
Except instead of using a color, I'm using an image..but I want it to be full screen also. Any ideas?
.main-header:after {
content: "";
position: absolute;
height: 100%;
top: 0;
left: 50%;
transform: translateX(-50%);
width: 99vw;
background-size: cover;
background: url(header_bg.jpg) no-repeat center center; /* help */
z-index: -1;
}
Like this?
body{
margin:auto;
}
#for_real,#top{
width:100px;
height:100px;
border:solid;
z-index:100;
}
#for_real img{
position:fixed;
top:0px;
left:0px;
opacity:0.5;
width:100vw ;
height:100vh;
}
<div id='top'>
TOP DIV WITH NO IMAGE
</div>
<div id='for_real'>
DIV WITH IMAGE
<img src='https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcT-gpy8G_VpoocZD5L5tuNO_hO_BC9zXl32WCjaHcE-ICiWhL5O'>
</div>

Centered full screen html image (not an image in css)

I'm trying to have a full screen image, easy enough with css using the code below.
width:100%;
height:100%;
background: url('photo2.jpg');
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
but the image is already placed in an html div, see here
<div class="fixed-background">
<img src="photo2.jpg"/>
</div>
It need's to be exactly how it would be using the css version, the only difference would be the image is called in html and not in the stylesheet.
try this
<style>
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.fixed-background {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
.myimg {
height: inherit;
}
</style>
<html>
<body>
<div class="fixed-background">
<img src="public/dbs/images/1.jpg" class="myimg" />
</div>
</body>
</html>
Use object-fit: cover; on the <img> tag:
<div>
<img src="photo2.jpg" style="object-fit: cover;"/>
</div>
that parameter is a rather new thing (not all browsers supported), but that's the way to go. See also http://caniuse.com/#search=object-fit
Without using a background, consider this:
#mydiv {
position: absolute;
top: 50%;
right: 50%;
bottom: 50%;
left: 50%;
margin-top: -100px; /* (calculate half the height of your image) */
margin-left: -100px; /* (calculate half the width of your image) */
}
Full screen Image? you could do something like this through HTML
<div class="fixed-background">
<img src="photo2.jpg" height="100%" width="100%">
</div>
http://jsfiddle.net/pj73m4po/
EDIT:
or are you looking for something like this?
http://jsfiddle.net/pj73m4po/1/
Try the following: http://jsfiddle.net/pj73m4po/4/
Put your image in a div 100% high and wide. If you don't want your image to be stretched you don't want to use width and height seperately.
body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.fixed-background {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
img {
height: auto;
width: auto;
min-width: 100%;
min-height: 100%;
}
Instead use min-width and min-height. if you have a predefined image you can adjust the position in css. If you don't unfortunately you need javascript to center it.
The points that I gather from your css are the following:
Center the image
Fix the position of the image (so it doesn't scroll with the page)
Cover the viewport, scale proportionally to fit
That said, I suggest the following given your html
.fixed-background{
position:fixed;
width:100vh;
height:100vh;
overflow:hidden;
}
.fixed-background > img{
position:absolute;
width:100%;
height:auto;
top: 50%;
transform: translateY(-50%);
}
Honestly, I haven't tested the above but I would suspect you might get some weird results using fixed and absolute positioning together. But since the code defines the width and height directly using viewport units, it should be good. You might need 100vh of margin applied to a sibling element to get things to line up because position:fixed; will break the element out of the document flow.

Stretching an image

All right so i have got an image inside a slider and it does not scale properly.
It is first small and then stretches.I want it to be stretched all the way through.How can this be achieved?
Have a look at the screenshots(ignore the red in second screenshot):
And the css:
.slider-wrapper {
width: 310px;
height: 580px;
background: url("images/S4.png") center center ;
background-size: contain;
background-repeat: no-repeat;
}
.nivoSlider {
position:relative;
width:268px;
height:474px;
top:51px;
bottom:0px;
left:21px;
right:23px;
overflow: hidden;
}
.nivoSlider img {
position:absolute;
top:0px;
left:0px;
width:268px;
height:474px;
}
And a link to the page:
http://oneapptheme.github.io
You can try this :-
.nivoSlider3 img{height:100% !important;}
use in the css file of image...
background-size: 100%;

CSS browers width

My project depend on blocks- if you click href in menu for example "about" site getting you to block with backogrund image. Backgrounds of blocks must adjust to the resolution user. Everythnig work great but if we want to change size browers in width images just unatural distort. I thnink that it is impossible to solve this problem, if we want to stay with:
width:100%;
height:100%;
SO How to make divs with bacgrounds image which will adjust to the resolution of widscreen, but if we change size of browers to custom example:365x900 background-image will still in the same resolutions(the same resolution means that size is dependet on the screen resolution)
the best situation will be then when bacground-images will adjust to the standard resolutions, but if we will change browers size for example on 200x900 then they will don't change theirs sizes and still stay in maximum(maximum size is dependent on the screen resolution)
CSS
#raz {
background-size:100%;
background-repeat: no-repeat;
margin-top: -300px;
border: 0px;
padding: 0px;
left: 0px;
width: 1200px;
height: 100%;
overflow: hidden;
z-index: 0;
}
#bg_raz {
position:absolute;
text-align:center;
width:100%;
height:100%;
}
#dwa {
background-size:100%;
background-repeat: no-repeat;
margin: 0px;
border: 0px;
padding: 0px;
left: 0px;
width: 100%;
height: 100%;
overflow: hidden;
z-index: 0;
}
#bg_dwa {
position:absolute;
width:100%;
height:100%;
}
HTML
<div id="dwa">
<img id="bg_dwa" src="http://lorempixel.com/output/city-q-c-600-325-4.jpg">
</div>
<div id="raz">
<img id="bg_dwa" src="http://lorempixel.com/output/city-q-g-600-325-7.jpg">
</div>
JSBIN
If you use background-size: cover; and add background-image on #dwa, you can delete <img/>.