How to make background images load faster - wordpress

Most of my WordPress websites have a background image in the top fold. These images are the Largest Contentful Paint Element on the page and usually they get loaded last. Somewhere I read that 'Background images are last in line to be grabbed when a page is loaded'. Is it true?
Is it a good idea to use a place holder or image in the place of the background image and then change it later so that the LCP gets loaded quickly like below.
<div class="header-img"><img style="display: none;" src="images/header-img.jpg" alt=""></div>
.header-img {
width: 100%;
height: 500px;
background-size: cover;
background-image: url(images/header-img.jpg);
}
Source

You don't want to use a placeholder image to prioritize your background images in situations like this, you want to use <link rel="preload">. That will tell the browser to start downloading the image as soon as possible.
Try adding the following code to the <head> of your page, and then use your background image as normal. It should load much faster:
<link rel="preload" as="image" href="images/header-img.jpg">
You can read more about this technique here:
Preload critical assets to improve loading speed
Preloading responsive images

Related

Print css, only load images at print time

I'm currently developing a complex print style sheet which is in some ways different from the HTML currently displayed on screen. I've hit a hurdle with a balance between performance and achieving the desired layout.
Basically there is a gallery of images which are loaded via javascript on the fly. Images are built dynamically via JS and output to the DOM on each request for the next image.
My problem lies in that I need to build this for printing purposes. I think I'm left with a scenario where I will have to build additional html on the page just for the print page to look correct; that isn't so much of a problem, except the images are rather big, and even using "display:none" and media print { display:block; } won't prevent the images from being downloaded on desktop devices behind the scenes by the browser. In essence I need them to stay dormont on screens, and come to life using print styles.
I had considered using the css background-image property - which I believe doesn't cause the image to load in the browser, however background image doesn't seem to reliably print across different browsers.
I've also tried using onbeforeprint javascript, but again, this is mess of browser inconsistency.
Can anyone suggest any sort of solution for this? at the moment it seems like I'm going to have to suck up the additional overhead of all the images to achieve reliable results.
If background images are an option, you could prevent the download of those when setting the parent element of the image container to display: none
Example HTML:
<div class="invisible">
<div class="img-container">
</div>
</div>
Related CSS:
.invisible {
display: none;
}
.img-container {
background: url(img.xyz);
}
#media print {
.invisible {
display: block;
}
}
Apart from that a similar question had been asked: Prevent images from loading
May be that will help you, if background images are definitely NOT an option.

How to disable images from being viewed in the browser?

I've noticed that most of the websites now "somehow" disable viewing some of the images used in their template, so I'd like to obtain this same result:
I thought instead of using the tag <a>with <img>, I put a div and set the "background" property as an image yet it's still viewable in the browser!!
Any ideas?
This is not disabling the images, this is done by using images as backgrounds in CSS and not as a normal img tag like:<img src="your-image.jpg" />. Here's an example how this is done:
HTML
<div class="randomClass"></div>
And the CSS goes like this:
.randomClass {
background-image: url('http://i.ytimg.com/vi/1WmaBpkGjXk/mqdefault.jpg');
background-color: #cccccc;
width:350px;
height: 180px;
}
On the Jsfiddle link I provided above if you right click on the 1st image you have the option to open the image on a new page or the option to download it. On the second one you don't have this options by right clicking on it, but still these images can be downloaded in other ways.

ExtJs 4.2.1 tree - icon "loading" is not shown

on my page i have a tree view which is loading nodes asynchronously before expanding. I can't figure out how to display standard "loading" gif while waiting for node to load. I understand that it's standard behavior, but it simply wont work. Probably it's some css issue..
My page references css like this:
<link href="Scripts/app/ext-4/resources/ext-theme-classic/ext-theme-classic-all-debug.css" rel="stylesheet" />
I know it's there because everything else is rendered correctly. In that css file is this class:
.x-grid-tree-loading .x-tree-icon {
background-image: url('images/tree/loading.gif');
}
I'm certain that this class is used because if i put background-color: #ff0000; instead of background-image: url('images/tree/loading.gif');, the place where icon should appear gets red background. I understand that icon url should be set relative to css location, and it is. This is part of my file structure:
Scripts
app
ext-4
resources
ext-theme-classic
images
tree
loading.gif
ext-theme-classic-all-debug.css
Also, when i call that icon directly using img tag like this:
<img src="Scripts/app/ext-4/resources/ext-theme-classic/images/tree/loading.gif" />
loading icon is shown as expected.
Any ideas are much appreciated! Thanks!
EDIT: RESOLVED
I was able to figure out what was the problem. Since i was using custom icons for tree nodes (sorry for not mentioning that, i didn't think that was relevant), loading.gif was somehow showing underneath those icons. To resolve that, i changed
.x-grid-tree-loading .x-tree-icon {
background-image: url('images/tree/loading.gif');
}
to
.x-grid-tree-loading .x-tree-icon {
background-image: url('images/tree/loading.gif') !important;
}

change image when hover over, using CSS

<!DOCTYPE html>
<html>
<head>
<style>
.main {
background-image: url('a.jpg');
}
.main:hover {
background-image: url('b.jpg');
}
</style>
</head>
<body>
<div class="main">
</div>
</body>
</html>
When I load the page, my a.jpg didnt appear at all, hence no hover effect
Is it something wrong with my code?
http://jsfiddle.net/ZH9EL/6/
Nothing is wrong with your code. The first image is being redirected to the main website so you don't have an image being loaded. You will have to host it locally. I used a different image and it works.
No there is nothing wrong with your code, it could work, but I believe it is risky to use 2 images, if 1 is not loading it will not work...
You'd like to use two images but if hover image is taking to long to load or just not loading at all it will not work very well..
You might want to try Alois Mahdal's answer: https://stackoverflow.com/a/19967062/3217130 please read their reply..
Your css should be something like:
<style>
#main {
width: **Yoor-width-in-pixels**.px; height: **Yoor-heigth-in-pixels**px;
background: url('a-b.jpg') no-repeat left top;
}
#main:hover { background-position: -Yoor-negative-width-in-pixelspx 0px }
</style>
This will work and will load normal and hover at the same time so you wont face problems with dns or server delay and other problems that could make pages look ugly if images are not loaded...
Please try Alois Mahdal's answer, this will work for you, if you don't understand how to create this for your images (a-b.jpg and new smaller but better code) then I would like to tell you how to do this with your images and classes and the result you need.. I create sprites with Paint .NET or I use spriteme.org to optimize CSS on running websites you can copy new css and images if you're using spriteme.org they will be created on the fly. There are lot's of ways to create or edit images...
I hope my answer was helpful to you,
Happy coding

Background loads slowly when page switches

Today when I wrote css I found that there are some problems appearing. I used bootstrap and darkstrap to design. In darkstrap the body's style is
body {
color: #c6c6c6;
background-color: #2f2f2f;
}
And in my own css:
body {
background: url(../img/11.jpg) no-repeat fixed;
background-size: 100% 100%
}
It looks no problem but the only question is when I switch the page, the page seems to have an asynchronous load (but I didn't refresh the page), first completing the style in darkstrap, then loading my style after 1 second. But I put my css before the bootstrap and darkstrap. And I just not refresh the page.
At last, I quote the body style in darkstrap, when I switch the page again, the body's background-color also complete after 1 seconds, it looks awful, I know the image load may send a http request and its loading may last. But I just switch the page... so where is the problem?
Where are you loading the scripts and css? Is it and the end of the page body?
One way to fix this might be to move the script loading into the page <head> section. When you do this, all of it will be loaded before any body markup. This will ensure that your CSS and the bootstrap css is ready before you see anything on the page. The downside of this is that it might make the page appear to take longer to load.
There could be other reasons, but this is the first thing that sprang to mind for me.

Resources