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

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.

Related

How can I make the background image to be responsive without losing the proportions

I am doing an animation with HTML and CSS3 and I need adapt along with the background image. The problem is that the content stay within that div. I put the height and width fixed for this but don’t work. When I try using dynamic proportions (% or auto) and background-size: contain; the animation does not follow the original path.
With fixed size following the path:
and mobile works fine too
but, not is responsive:
With dynamic size is responsive, but not follow path:
Changed code:
#main{
position:relative;
- left: 0;
- height: 1366px;
- width: 980px;
+ // left: 0;
+ height: 100%;
+ width: 100%;
overflow:hidden;
background: url('../images/bg.png');
background-repeat: no-repeat;
-
+ background-size: contain;
}
DEMO
This is my index.html
<div id="main">
<div class="participant" style="z-index: 4;">
<div class="car">
<img class="photo" src="https://scontent-atl3-1.xx.fbcdn.net/v/t1.0-1/c21.21.259.259/s50x50/529297_568082979888645_1727470385_n.jpg?oh=c75505b8b23ff9abd26be1fd5771f81d&oe=582BAD0F" alt="">
<img class="sprite" src="http://i.imgur.com/OwYhg9T.png" alt="">
</div>
</div>
</div>
And my animation.css
#charset "utf-8";
/* CSS Document */
body, html {
height: 100%;
}
body {
padding:0;
margin:0;
}
#main{
position:relative;
left: 0;
height: 1366px;
width: 980px;
overflow:hidden;
background: url("http://i.imgur.com/G4gs6EG.png");
background-repeat: no-repeat;
}
#-moz-keyframes move
{
from {
right: -30%;
top: 8%;
}
to {
right: 140%;
top: 80%;
}
}
#-webkit-keyframes move
{
from {
right: -30%;
top: 8%;
}
to {
right: 140%;
top: 80%;
}
}
.participant {
position: absolute;
display: inline-block;
height:200px;
width: 200px;
right: 140%;
top: 80%;
-moz-animation:move 10s linear infinite;
-webkit-animation:move 10s linear infinite;
}
.sprite{
width: 100%;
height: 100%;
}
.photo{
position: relative;
top: 128px;
left: 99px;
width: 50px;
height: 50px;
}
This is a little tricky and requires fixed aspect ratio of the background image.
1. Make everything responsive.
First of all, it won't work if everything is %-based but the car is px-based (because if you resize your window everything will get smaller but the car will stay the same), so for starters you are going to have to change the size of your car to percentages.
2. Fix aspect ratio.
Then you need to fix the aspect ratio using mix of absolute and relative positions and paddings.
In your case your wrapper's CSS will look something like:
width: 100%;
padding-bottom: 71.74%; /* 980/1366 = ~ 0.7174 */
(your background image is 980x1366px)
DEMO
3. FUTURE PROOF: Fill the screen on every screen.
Unfortunately you can't do much about white space around your image because of the aspect ratio itself, I'd personally look for a 16:9 image for the background and it will fit most of the desktop/laptop screens perfectly, if you need to cover wide range of screens then you should use media queries with different-sized backgrounds.
Remember to adjust the padding-bottom of your container along with the image itself.
Hope it helps!
Try replacing height and width in your #main css class to:
#main{
height: 100%;
width: 100%;
background: url("http://i.imgur.com/G4gs6EG.png") no-repeat fixed center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I've got this working on codepen.io
http://codepen.io/NosNits/pen/RRqzPy

CSS - Changing position from fixed to allow scrolling

I am currently using some code to display an image in a 'scale to fill' type of way. The code I am using was used on a project I did a while ago to crate a background image for a site and now I want to change the positioning from fixed to allow scrolling. However I can't work out the margins and sizing.
Here is the code:
<div id="Main Page">
<div id="Background">
<img src="http://googledrive.com/host/0By-qb7dZ_m5feE94MkcwSWxLckU" />
<style>
#Background{
position: fixed;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
}
#Background img{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
min-width: 50%;
min-height: 50%;
}
</style>
</div>
If you want to use the background-image for the entire site, you can use this css-code:
body {
background-image: url("http://googledrive.com/host/0By-qb7dZ_m5feE94MkcwSWxLckU");
background-attachment: fixed;
background-size: 100% 100%;
}
Depending on your background-image you can try background-size: cover; instead of background-size: 100% 100%; for a better result too.
Here is a fiddle-example: https://jsfiddle.net/hxefwhpL/

Image doesn't fill width even though width is set to 100% on larger monitors

I have this in my CSS:
.cover .cover-image {
z-index: -1;
position: absolute;
top: 0px;
width: 100%;
height: 100%;
background-size:100%;
background-size: cover;
background-position: center;
}
I also have this in my HTML:
<div class="cover-image" style="background-image : url('./bkg.jpg');">
bkg.jpg is a 1939x1131 image - bigger than any of my monitors. On my smallest (1280x1024) monitor, it displays fine:
On my medium (1440x900) and large (1920x1080) monitor - both of which are smaller than the image - it shows a bit of white between the image and the scrollbar:
Why does this show, and how do I fix it?
Check your image source
https://chipperyman.com/dota/bkg.jpg
You have a white bar on your image.
double on background-size your code, please fix background-size:100%, otherwise you will get issue.. And i mean you not need using background-size because your image already bigger than your screen..
you need normalize html, body
html, body {
padding: 0;
margin: 0;
}
there.
Basically you are having an absolute positioning bug here. Just add 'left: 0px;' to your .cover .cover-image selector as per following:
.cover .cover-image {
z-index: -1;
position: absolute;
left: 0;
top: 0px;
width: 100%;
height: 100%;
background-size:100%; /* old browser fallback, but I'd delete it */
background-size: cover; /* duplicate of background-size */
background-position: center;
}
Here is also a jsfiddle example of fixing it: http://jsfiddle.net/webyourway/868L6hhg/

Can't resize image using CSS

I've tried every answer I could find on all the sites I could find, but still haven't been able to properly resize an image using CSS. I've got it inside a div, and tried resizing either one and resizing both. I'm trying to fit the image (underneath my navigation bar) to the page (meaning: as wide as the page, and relative height).
<div id="banner"><img src="resources/img/banner.png" class="myImage"></div>
First attempt:
.myImage{
max-width: 100%;
height: auto;
left: 0px;
right: 0px;
}
Second attempt:
#banner{
max-width: 100%;
height: auto;
left: 0px;
right: 0px;
}
Third attempt:
<div id="banner"><img src="resources/img/banner.png" id="myImage"></div>
#banner{
max-width: 100%;
height: auto;
left: 0px;
right: 0px;
}
#myImage{
max-width: 100%;
height: auto;
left: 0px;
right: 0px;
}
If your image is smaller than the screen, it will use the image width. If it is bigger, it uses max-width. So assuming your image is smaller than the display, you want to change your "max-width" to "width" to increase the image size.
<div id="banner"><img src="resources/img/banner.png" id="myImage"></div>
#banner{
max-width: 100%;
height: auto;
left: 0px;
right: 0px;
}
#myImage{
width: 100%;
height: auto;
left: 0px;
right: 0px;
}
The CSS property background:cover will help you!
html {
background: url(images/bg.jpg) no-repeat center center fixed;
background-size: cover;
}
Cover will extend your background to full screen.
CSS-Tricks has the best solution for this that I could find
html {
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Link to CSS-Tricks for the source and original code: https://css-tricks.com/perfect-full-page-background-image/
Did you remember to link to your stylesheet? I was just having this issue, then I realized that I forgot to link to the CSS and face-palm.
You have to give the id banner a specific width that is less than 100%. You don't need a height, it already is automatic. You have to target the image inside the banner and not the class added to the image. So it should look like this:
<div id="banner"><img src="resources/img/banner.png" class="myImage"></div>
#banner{
width: 60%;
left: 0px;
right: 0px;
}
#banner img{
width: 100%;
left: 0px;
right: 0px;
}

How to make background image go in the "background" using CSS/HTML

I want to fill my page with a background image and have the text aligned in place with that background. With the below code, the background image loads at the top of the page, and the text goes under it. I know I can use the "background: " function, but the way it is done in my below code allows for automatic resizing, regardless of browser size (i.e., mobile devices have small browser sizes). So, I just want the background image to go behind the text.
<html>
<head>
<title>Title</title>
<style>
img.bg
{
min-height: 100%;
min-width; 781;
width: 100%;
height: auto;
top: 0;
left: 0;
z-index: -1;
}
#media screen and (max-width: 781)
{
img.bg
{
left: 50%;
margin-left: -390.5;
}
}
#container
{
position: relative;
width: 781;
margin: 50 px auto;
height: 758;
border: 1px solid black
}
#left
{
position: relative;
left: 1.280409731113956%;
top: 14.51187335092348%;
padding-top: 10px;
padding-left: 10px;
padding-right: 10px;
width: 50%;
height: 50%;
color: #FFFFFF;
position: relative;
}
p
{
font: 14px Georgia;
}
</style>
</head>
HTML
<img class="bg" src="background.jpg">
<div id="container">
<div id="left">
<p>
Text
</p>
</div>
</div>
</body>
</html>
Make your BG image have a z-index of 1, and your #container div to have a z-index of 2. Does that work?
img {
position: relative;
z-index: 1;
}
#container {
position: relative;
z-index: 2;
top: 0px;
left: 0px; /*or whatever top/left values you need*/
}
Just use position: fixed for your background image http://dabblet.com/gist/3136606
img.bg {
min-height: 100%;
min-width: 781px;
width: 100%;
top: 0;
left: 0;
position: fixed;
z-index: -1;
}
EDIT (I wish there was a way to make it more visible than this)
OK, after reading the comments for the original question, I understand that the purpose is to have a background that scales nicely for any display sizes.
Unfortunately, quite a lot of mobile devices have a problem with position: fixed - you can read more about this here.
So the best solution in this case is to use a background image, not an img tag, having the background-size set to 100% (which will stretch the image - example), or to cover (which will scale the image such that it completely covers the screen - example)
Well, maybe you can also try that css:
body{
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
it's should cover all youre page even when page size is changed