


Two methods to use CSS to implement code that does not change the size of the background image as the browser zooms
This article mainly introduces the code about two methods to use CSS to realize that the size of the background image does not change with the browser zooming. It has a certain reference value. Now I share it with you. Friends in need can refer to it
The size of the background image on the homepage of some websites does not change with the zoom of the browser. This example uses CSS to realize that the size of the background image does not change with the zoom of the browser. Method 1. Use the image as the background. Method 2 uses the img tag. Friends who like it can take a look at
. The size of the background image on the homepage of some websites does not change with the browser zooming. For example, on the homepage of Baidu Personal Edition, the size of the background image does not change after scaling:
Another example is Huaban.com:
Now use CSS to achieve this effect.
First you need a picture of large enough size. The size of the Baidu background image above is 1600*1000px (picture address: http://4.su.bdimg.com/skin/12.jpg?2); petals The size of the background image is 1600*1600px (image address: http://hbfile.b0.upaiyun.com/img/unauth_page/food_bg.jpg);
Then there are two methods to achieve the effect of non-scaling of the background image:
Method 1. Use the image as background
There are several CSS properties to mention: background-size: cover. The function of this CSS3 property is to expand the background image to a large enough size. Make the background image completely cover the background area. Some parts of the background image may not be displayed in the background positioning area. If you do not use this attribute, the background image will shrink when the browser is zoomed in IE11 and FireFox, and use -webkit- background-size: cover and -o-background-size: cover are compatible with webkit kernel browsers and Opera browsers; the background-attachment attribute sets whether the background image is fixed or scrolls with the rest of the page. When set to fixed, the rest of the page Background image does not move when partially scrolled.
Code (using Baidu’s starry sky map, the effect is the same as the Baidu screenshot above):
HTML:
<p id="con"></p>
CSS:
body{ margin:0; padding:0;} #con{ position:absolute; top:0; left:0; height:100%; width:100%; background-image:url("maskimg/star.jpg"); background-position: center 0; background-repeat: no-repeat; background-attachment:fixed; background-size: cover; -webkit-background-size: cover;/* 兼容Webkit内核浏览器如Chrome和Safari */ -o-background-size: cover;/* 兼容Opera */ zoom: 1; }
Method 2 .Do not use the image as the background, but use the tag . The effect is that the image size will not change with the browser zooming, but if there is a vertical scroll bar, the image will not be fixed but will follow the scroll bar. move. Just set the width of the image to 100%.
The code is very simple, only a few lines, using Baidu’s starry sky map:
HTML:
<p id="con"><img src="/static/imghwm/default1.png" data-src="maskimg/star.jpg" class="lazy" id="pic" alt="Two methods to use CSS to implement code that does not change the size of the background image as the browser zooms" ></p> _fcksavedurl=""maskimg/star.jpg"></p>"
CSS:
body{ margin:0; padding:0;} #pic{ width:100%;}
The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to set CSS
Text font color
How to draw a loading circle animation with css3
About CSS background background and background -Analysis of position
The above is the detailed content of Two methods to use CSS to implement code that does not change the size of the background image as the browser zooms. For more information, please follow other related articles on the PHP Chinese website!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
