We have a lot of ideas for the development of your business, contact us!
The CSS property “background-size” will be set to “cover”, which instructs the browser to proportionally scale the background image’s width and height to be equal to or larger than the viewport’s dimensions. To improve page load speed on mobile devices, a media query can be used to serve a smaller version of the background image. While this is optional, it is recommended as serving a large image file (such as the 5500x3600px image used in the demo) can result in a 1.7MB payload, which is not ideal for mobile internet connections. Additionally, the image size is excessive for small screens.
Let’s stroll through the system.
HTML
Let’s take a walk through the system using this simple markup: <!Doctype html> <html> <body> …Insert your content here… </body> </html>
We will set the background image to cover the entire browser viewport by assigning it to the body element. This technique can also be applied to other block-level elements like divs or forms. If the dimensions of your block-level element are flexible, the background image will adjust to cover it completely.
CSS
We propose a fashion guideline for the frame detail as follows: the history-picture property specifies the location of the photograph, while the history-position property centers the background image both vertically and horizontally. The background image does not repeat with the background-repeat property set to no-repeat, and it remains fixed within the viewport with the background-attachment property. The background-size property is crucial, as it rescales the background image based on the box’s size, ensuring that it covers the entire element. Additionally, the history-color property sets a background color that displays while the background image is loading. The most important property/value pair to note is background-size: cover, which scales the background image proportionally to match or exceed the element’s width and height. This property is what makes the magic happen.
Although there is an issue with this property/fee pair, it can be resolved by taking into account the size of the historical past photograph in relation to the body detail’s dimensions. If the image is smaller, the browser will scale it up, which can result in pixelation and a decrease in image quality. Therefore, it is important to choose an appropriate image size. The demo uses a large image for big screens, but we need to ensure that the background image remains centered in the viewport and fixed in place even when the user scrolls down. This can be achieved by setting the background-position to center center and the background-attachment to fixed.
During the demonstration, I implemented a “load some content” function to showcase how scrolling behaves when the background-attachment is fixed. You can experiment with the positional property/values(such as background-attachment and background-position) by downloading the demo and adjusting them to observe how they affect page-scrolling and the background image. The other property values are self-explanatory. To simplify the CSS, I used shorthand notation for the background properties. By changing the URL value to your desired background image location, you can use the shorthand CSS notation. For smaller screens, I resized the original background image proportionally using Photoshop and reduced its file size using Smush.It. Although the resulting 114KB file size is still quite large for a purely aesthetic element, I would only recommend using it if it significantly improves the user experience. The media query I used is set at a max-width of 767px, which means that larger screens will display the larger background image file. However, resizing the browser window may cause a momentary flicker as the smaller or larger background image loads, and smaller devices may display a pixelated image due to their ability to render more pixels.
Start by creating your first responsive historical photo using CSS.
You can access the latest source code for this tutorial on GitHub. However, be cautious when using this technique as large files can negatively impact user experience, especially for those with slow or unreliable internet connections. To mitigate this, it’s important to set a good default background color so that users can still read the content while the background image loads. Additionally, optimizing your web images before publishing them is always a good idea. We have some helpful articles on this topic, including a guide to saving images for the web and tools for optimizing your images. The source code for this tutorial is in the public domain under CC0 1.0 Universal, meaning you can use, modify, sell, and distribute it without any restrictions or requirements for attribution.