Scaling a background image with css and html - html

I have a hard time scaling my image correctly with css and html.
The css looks like this:
#media (max-width: 979px) {
.header-wrapper {
height: 388px;
background: url("../images/taikuri.jpg") no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
Then the html looks like this:
<header id="header" class="header">
<div class="header-wrapper"> <img src="./imag/taikuri.jpg"
alt="taikureita" width="2050" height="812"/>
I would like to scale the image somehow to get faster loading speed.
GTmetrix page test gives me this advice:
The following images are resized in HTML or CSS. Serving scaled images could save 49.1KiB (69% reduction).
I haven't been able to figure out the right way to do this so I have to ask for help.

What you're attempting to do is reduce the file size of the image so that it loads faster.
What your solution posted in your question is attempting to do is reduce the display width and height of the image, so it shows up on the screen as a smaller width and height. However, the same image, with all its massive file size, is still being loaded in. The browser is just shrinking the look of it for you.
And looking at your code, you're setting a background image to the same thing as the image that you've written into your html. This doesn't matter, as the image will cache, but just so you know you're basically embedding your image twice.
Think of it this way: I'm 200lbs. If I take a picture of myself and shrink it, I'm still 200lbs in real life. That's what you're trying to do. BUT... if I lose some weight, then I'm smaller in real life and won't hurt so much if I sit on you. That's what you're asking how to do. Make sense?
In this case, open the image up in photoshop, make a copy, resize it to a smaller width (you can do this from Image > Image size) with the contain aspect ratio control turned on. Upload that image and use it instead.
Another, better, option is to
Delete your <img> out of your html, as it's duplicated and huge. You can recreate that scaling effect in css.
Set up more media queries to use different images at different media widths.
I see you're already using this big image at a max-width of 979px. If that's the case, set up an image that is width 979px and use it in this media query. If you need to get bigger, create a larger image that has higher jpg compression and create a larger media query to handle it, something like the following code (note I renamed your image as examples of setting up different images):
#media (max-width: 979px) {
.header-wrapper {
height: 388px;
background: url("../images/taikuri_979w.jpg") no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
}
Here's a second media query, with a larger image. Note I added a padding-top as a percentage with a min-height so that the top area would scale respectively to the width of the browser):
#media (min-width: 1000px) {
.header-wrapper {
height: auto;
min-height:388px;
background: url("../images/taikuri_really_big.jpg") no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
padding-top:35%;
}
}
If you don't have photoshop, you can resize your image using a service like this: http://www.picresize.com/
I've never used this but it looks like it does what you want. Also, if you're on a Mac, the Preview app has an image resize. Not sure what might be on Windows or Linux, though, sorry!

Scaling the image with CSS will not give better loading speeds.
The reason for this is because your browser don't download the image at the size you specify in your CSS. The browser downloads the image at full size, and then show you the image at whatever size that you specified with your code.
You should have a look at tinypng.com or compressor.io for compressing of your image. This would help you with loading speeds. Compressing an image, makes it smaller in file size. It will take less space on the hard drive, and would also be less for the server to transfer to the user.

You could use javascript to load the image after the page loads, and put something like this before the < / body > tag.
<script>
var width = document.getElementById('header').clientWidth;
if(width>=1000)
{
document.getElementById("imageid").src="./images/BigImage.jpg";
}
if(width>500 && width<1000)
{
document.getElementById("imageid").src="./images/MediumImage.jpg";
}
if(width<=500)
{
document.getElementById("imageid").src="../images/SmallImage.jpg";
}
</script>
Of course this would assume that the browser has javascript turned on.

Related

White Space on Larger Monitors When Background Image ends

I am using Bootstrap, HTML and CSS for my website. I built it on my Macbook Pro, and now when I move to my desktop monitor (which is much larger), there seems to be a large white space on the right side of the web page due to the monitor being much larger in width.
The height is working perfectly fine and it scales well, but it seems like right when the image ends (width) the site wants to keep going, but is not stretching the image to fit the larger monitors horizontally (width).
How can I fix this?
Image of Website Problem:
This is what I came up with so far.
body {
background-image: url("http://i.imgur.com/vDF3k2s.jpg");
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
background-repeat: no-repeat;
width: 100%;
overflow: auto;
font-family: Lato;
}
html {
height: 100%;
}
I appreciate all suggestions, thanks!
Without your HTML code, I can't exactly replicate what you are doing, so I will have to assume some things... always dangerous! LOL!
Anyway, I will first tell you what I believe is going on, then I will explain some things to do to combat that, and then I will provide the actual code, based off of the code you have already supplied.
Ok, I believe what is happening is that since you haven't specified where within the body you want the background image, by default the browser is giving you the image at 100% of it's original size and floated to the left.
In order to get a fullscreen image, which is what I believe you are shooting for, I would first place the background image within the "html" tag instead of the "body" tag. Second, I would let the browser know that you want the image to be centered and not to repeat. Then I would tell the browser that you want the image to "cover the screen".
So, with that said, here is the code for what I just explained:
html {
background: url("http://i.imgur.com/vDF3k2s.jpg")no-repeat center;
background-size: cover;
overflow: auto;
}
I should explain that I have deleted the -webkit, -moz, etc. portions of the code. That is because "overflow" is compatible with so many older versions of the most commonly used browsers, that to write that in your code is unnecessary.
Also, when I'm creating a fullscreen background image on a page, I usually use "background" instead of "background-image" and I have the "overflow" as "hidden". I can't honestly tell you if that is a "best practice" or if your writing "overflow" as "auto" is, but I also don't know what other content you plan to put in place, etc.
Anyway, I hope my solution helps and please hit me back to let me know!

Why does the background image not cover the whole page sometimes?

I have an HTML page where I have an image as a background. Whenever I view the page on my iPhone 6, the background image sometimes doesn't cover the whole page. This happens only about 20% of the time and doesn't happen after reloading the page. It always happens when I clear cache and website data on my phone. This is the CSS to set it to the background(I got it from the web by searching "how to make a good CSS background image")
html { /*Copied from the web*/
background: url(img/sea.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
body{
background: none;
}
The url of the page is msolonko.net/signup.php.
As you can see on the image, there is white space below the login button. Why does the image work correctly sometimes, but rarely not? How can I fix this?
EDIT:
Changing resolutions of the image has no effect on the issue.
I do not want the image to repeat. It looks good with just one image if the issue doesn't occur.
Also, if I cannot fix this issue, I will make the image only appear if it is a computer. If the device width is less than some threshold, I will just use a background color instead of image. I am still hoping I can fix this though.
Two solutions: Make it so the image repeats (turn no-repeat to repeat) or..
Increase the height of sky.jpg since sky.jpg is not tall enough to fit the phone.
So if I'm not mistaking (which I probably am), you should make your sky.jpg image 960 x 660 pix. Some average phone pixel resolutions at http://www.binvisions.com/articles/tablet-smartphone-resolutions-screen-size-list/
This should help! Sorry if it didn't.
EDIT: Did you at least try the techniques? (I'm new to Stack, I can't comment)
edit the image before you load the page, make the correct size for it and there you got.
be recursive.
(i have the same problem and do it this way becouse i found no way to find a solution in the code.)
i use programs like photoshop and corel.
I got it! I was learning some CSS... CSS Code:
body {
background-image:sky.jpg; /*or whatever*/
}
Should work...maybe.

Get 100% of image background showing on website, no matter the screen size

I'm sure I poorly structered that question title, but this is what I'm trying to do:
Key: image = moving picture behind submit form
Problem 1: Get the background image to stay at that perfect size no matter the width of the page.
Get the entire frame of the moving image showing.
When the image is at the perfect width (http://gyazo.com/702e443a6b814b1abc26a801836f4d6f) it shows the entire image.
When the width is increased even more, the image starts to 'zoom in'. (Which I don't want.)
Problem 2: On mobile devices with much smaller screens, most of the image is cut off (and you can't scroll to see the rest).
Get the entire frame of the moving image showing.
Have the entire image centered and the user should be able to see the entire frame of the image.
This is what my code looks like for the image:
background-image: url("https://s-media-cache-ak0.pinimg.com/originals/a4/79/4c/a4794cbfe048505d9645339738a8ddc7.jpg");
height: 60%;
background-size: 100%;
background-repeat: no-repeat;
background-size: cover;
jsfiddle
You need to do the following:
#top{
background-image: url("https://s-media-cache-ak0.pinimg.com/originals/a4/79/4c/a4794cbfe048505d9645339738a8ddc7.jpg");
height: 60%;
background-position: center -150px;
background-repeat: no-repeat;
}
Because you were using the background-size: 100%; you're telling the browser to scale the image for you. By removing it and positioning the background you'll achieve what you want with the image never resizing.
Fiddle
Note: You'll need to make sure that your image is large enough that when the page is stretched really large that you won't see the background. It will be hard to do, given that screen resolutions are getting so large now.
#your_image_container_id {
width: 100%;
}
What you are trying to say is called "Responsive Web Design". In order to achieve RWD, you never use absolute units such as pixels, points... but rather use relative units such as %, em which scales according to size of the screen. but you have do some calculation though such as %, make sure all elements on same row do not have the total of with percentage of over 100%, or something like that and also have to compensate for paddings and margins.

full screen background, single page website

I'm trying to build a very simple one page website. Here's a wireframe model:
I have a very large background graphic (1920 x 1080) and I would like it to cover the whole page and when the browser window is made smaller, I would like scrollbars to appear (or something similar; the point is that the bg resizes). The page will also have some header text and a logo in the middle and a button below it and some text under a line after that.
I have tried:
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;
}
But if I make the browser window too small, it shows up wrong. I experimented having the top most and bottom most text IN the background graphic but after resizing to a very small size the sides of the text get cut off.
Another thing that almost works is this:
body {
padding: 0;
margin: 0;
background: #f8f7e5 url('../images/background.jpg') no-repeat center top;
background-attachment:scroll;
width: 100%;
}
But the text won't play nice with that.
So my question would be, what would be the best way to achieve what I'm trying to do?
I would use background-size: cover for my background-image. It is well supported except in IE8. I would also set my background-image in media-queries so that I could have one or more smaller images for smaller size resolutions and use the larger image only on the largest devices. This way a small mobile device doesn't need to download a giant 1920px x 1080px image.
Your media queries can also be used to set your text for different devices. For example, you could have 3 divs for the text at the top of the screen that are each set to a width of 33.33333% and floated left so that they sit side-by-side on a desktop size screen. On the smallest devices, such as a mobile phone that is in portrait mode though you could set each of those columns to be 100% so that they are one on top of each other.
It requires a little thoughtful planning before hand, but if you're having trouble, sit down with a pencil and a paper and make 3-5 mockups for how you want things to look at different screen sizes. It could be as simply as one for a mobile device, one for a tablet device and another for a desktop device. Build your css for the smallest device size and make that look reasonably good, then create a media query for the next largest category of screens and start adapting your css to fit those size devices, and then do the next.
Here's an example of what I think you might be looking for. I used background-size:100% 100%; so it will distort your image when you resize your page if the image isn't large enough. But it does always cover the entire background. Hope this helps!
http://jsfiddle.net/JF9me/

How can I make my page load a different image based on resolution? [duplicate]

This question already has answers here:
Set background image according to screen resolution
(7 answers)
Closed 9 years ago.
I am working on this site. It is Joomla-based so I had to pick up some new things while trying to convert it. I finally was able to get it to look the way I want but I have some problems as the main background (an image module) is 2100px wide and looks great on wide resolutions, but it's huge on smaller ones.
Is there a way to code something in the HTML (or otherwise) that will say if the resolution is this then make the module display this image, etc.?
I don't know about loading in a different module, but you can use CSS media queries to load in a new background image for specific viewport dimensions:
#media only screen and (min-width:0px) and (max-width:2099px)
{
.my_image { background-image:MY_NEW_IMAGE; }
}
Try
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;
}
Demo
From: Perfect Full Page Background Image, css3 technique.