Home Web Front-end CSS Tutorial CSS implements responsive full-screen background image

CSS implements responsive full-screen background image

May 27, 2017 pm 05:24 PM

CSS implements responsive full-screen background image

One of the most popular forms of web pages currently is full-screen large images. This article will use the simplest way to achieve this effect. The CSS property background-size is used ,no javascript required.

Core Concept

  • ## Use the background-size attribute to fill the entire viewport

When the css attribute background-size value is cover, the browser will automatically scale the width and height of the background image proportionally until it is greater than or equal to the width and height of the viewport

  • Use media queries to provide smaller-sized background images for mobile devices

  • ## Why should we provide small-sized background images for mobile devices? In the demo , the actual size of the background image we see is 5498px * 3615px, the purpose of using such a large size image is to satisfy most widescreen monitors without blurry display, at the expense of a 1.7MB image size.

But there is no need to use such large images on mobile devices. At the same time, large images will cause slower loading, especially on mobile networks.

It should be noted that providing a small background image for mobile devices is optional for this technical solution.

Practice

    ## HTML
  • ##

    <!doctype html>
    <html>
    <body>
        ...Your content goes here...
    </body>
    </html>
    Copy after login
    Later we will specify a background image for the body tag, so that the background image can fill the entire browser viewport.

  • In fact, this solution can be effective for all block-level containers. If the width and height of your block-level container are dynamic, the background image will automatically expand and contract to fill the entire container.

The style of the CSS body tag is as follows:
  • body {
    /* 加载背景图 */
    background-image: url(images/background-photo.jpg);
    
    /* 背景图垂直、水平均居中 */
    background-position: center center;
    
    /* 背景图不平铺 */
    background-repeat: no-repeat;
    
    /* 当内容高度大于图片高度时,背景图像的位置相对于viewport固定 */
    background-attachment: fixed;
    
    /* 让背景图基于容器大小伸缩 */
    background-size: cover;
    
    /* 设置背景颜色,背景图加载过程中会显示背景色 */
    background-color: #**46;
    }
    Copy after login
    The most important one above is:

    background-size: cover;
    Copy after login
In this way, the browser will scale the background image proportionally until the width and height of the background image are no less than the width and height of the container (in the above example, the body tag).

One thing to note here is that if the background image is smaller than the size of the body tag (for example, on a high-resolution monitor, or when the page content is particularly large), the browser will stretch the image. We all know that when an image is stretched, it becomes blurry. Therefore, when choosing a background image, pay special attention to the size. In order to take care of the large screen size, the image size used in the demo is 5498px * 3615px.

At the same time, in order to keep the background image always centered relative to the viewport, we declare:

background-position: center center;
Copy after login

The above rules will position the origin of the background image scaling to the center of the viewport.

The next problem we need to solve is: when the height of the content is greater than the height of the viewport, a scroll bar will appear. We want the background image to remain fixed relative to the viewport, even as the user scrolls.

The solution is:

background-attachment: fixed;
Copy after login

##  (Optional) Use media queries to deal with small screens

In order to cope with the small screen, I used photoshop to scale the background image to 768px * 505px, and then used smush.it Compress image size. This reduces the image size from 1741KB to 114KB, saving 93%. The following is how the media query is written:

@media only screen and (max-width: 767px) {
  body {
    background-image: url(images/background-photo-mobile-devices.jpg);
  }
}
Copy after login

The above media query will max-width: 767px Set as a breakpoint, which means that when the browser viewport is larger than 767px, a large background image will be used, otherwise a small background image will be used.

The downside of using the above media query is that if you shrink the browser window from 1200px to 640px (or vice versa), you will see a brief flicker as the small or large background image is being load.

The above is the detailed content of CSS implements responsive full-screen background image. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles