search
HomeWeb Front-endHTML TutorialCSS realizes that the background image size does not change with browser scaling_html/css_WEB-ITnose

<!-- Author:博客园小dee -->

The size of the background image on the homepage of some websites does not change as the browser zooms. 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 of all, 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 ); The size of the petal background image is 1600*1600px (image address: http://hbfile.b0.upaiyun.com/img/unauth_page/food_bg.jpg);

Then there are two ways to achieve the background The effect of the image not scaling:

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 change the background image Expand to be large enough so that the background image completely covers 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. , use both -webkit-background-size: cover and -o-background-size: cover to be compatible with webkit kernel browser and Opera browser; the background-attachment attribute sets whether the background image is fixed or scrolls with the rest of the page, when set When fixed, the background image does not move when the rest of the page is scrolled.

Code (using Baidu’s starry sky map, the effect is the same as the Baidu screenshot above):

HTML:

<div id="con"></div>

CSS:

 1 body{ margin:0; padding:0;} 2 #con{  3  4     position:absolute; 5     top:0; 6     left:0; 7     height:100%; 8     width:100%; 9     background-image:url("maskimg/star.jpg"); 10     background-position: center 0;11     background-repeat: no-repeat;12     background-attachment:fixed;13     background-size: cover;14     -webkit-background-size: cover;/* 兼容Webkit内核浏览器如Chrome和Safari */15     -o-background-size: cover;/* 兼容Opera */16     zoom: 1;    17 }

Method 2. Instead of using the image as the background, use the CSS realizes that the background image size does not change with browser scaling_html/css_WEB-ITnose tag. The effect is that the image size will not change Changes as the browser zooms, but if there is a vertical scroll bar, the image will not be fixed but will move with the scroll bar. 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:

<div id="con"><img  src="/static/imghwm/default1.png"  data-src="maskimg/star.jpg"  class="lazy"  id="pic" alt="CSS realizes that the background image size does not change with browser scaling_html/css_WEB-ITnose" ></div>

CSS:

1 body{ margin:0; padding:0;}2 #pic{ width:100%;}

Statement
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
火狐浏览器是哪个国家的火狐浏览器是哪个国家的Sep 15, 2022 pm 02:55 PM

火狐浏览器是“美国”的。Firefox火狐浏览器是开源基金组织Mozilla研发的一个自由及开放源代码的网页浏览器;而Mozilla基金会成立于2003年7月,是一家美国公司,现位于美国加利福尼亚州的芒廷维尤。

电脑浏览器打不开网页但能上网怎么解决电脑浏览器打不开网页但能上网怎么解决Jun 28, 2023 am 11:26 AM

电脑浏览器打不开网页但能上网解决方法:1、网络设置问题,将路由器断电并等待几分钟,然后再重新插上电源;2、浏览器设置问题,清除浏览器缓存和浏览历史记录,确保浏览器没有设置代理服务器或虚拟专用网络;3、DNS设置问题,将DNS设置更改为公共DNS服务器地址;4、杀毒软件或防火墙问题,禁用杀毒软件或防火墙,再尝试打开网页;5、网页本身的问题,等待一段时间或联系网站管理员了解情况。

教你如何强制卸载edge浏览器教你如何强制卸载edge浏览器Jul 15, 2023 pm 06:17 PM

Windows10自带的Edge浏览器在程序面板上是不能被卸载的,但是有些网友不喜欢使用edge浏览器,想要卸载掉它。那么我们可以尝试如何卸载edge浏览器呢?下面小编就教下大家强制卸载edge浏览器的方法。具体的方法如下:1、右击左下角开始,点击“windowspowershell(管理员)”打开。2、进入命令界面,输入代码get-appxpackage*edge*,查找edge包。3、在edge包中找到packagefullname,选中并复制。4、接着输入命令Remove-appxpack

如何修复 Microsoft Edge 浏览器中的黑屏问题如何修复 Microsoft Edge 浏览器中的黑屏问题May 16, 2023 am 10:04 AM

微软于2020年初发布了基于Chromium(谷歌的开源引擎)的NewEdge版本。新Edge的感觉与谷歌Chrome相似,并且具有Chrome中可用的功能。但是,许多用户报告说他们在启动MicrosoftNewEdge后立即看到黑屏。用户可以访问设置菜单,但是当他们单击菜单中的任何选项时,它不起作用,只有黑屏可见。当计算机鼠标悬停在选项上并且用户可以关闭浏览器时,它会突出显示选项。在PC上打开新的Edge浏览器时是否遇到黑屏?那么这篇文章将对你有用。在这篇文章中,

a4纸尺寸是多少厘米a4纸尺寸是多少厘米Feb 11, 2024 pm 11:51 PM

a4纸现在用于的地方有很多,比如说办公、学习等等,现在复印所用到的纸张基本上都是a4纸,有很多的小伙伴不知道a4纸尺寸是多少厘米的现在就来看一下吧。一、尺寸A4纸规格为21cm×29.7cm(210mm×297mm),世界上多数国家所使用的纸张尺寸都是采用这一国际标准。二、由来A4纸是210mm×297mm的由来。原因一:一张长为2,宽为X的矩形,对折之后长宽分别是X和1,那么为了保证相似,就要求1:X=X:2。得到的比例为。然而297除以210,得到的是1.414,接近于,而这个比例正是一张纸

电脑尺寸怎么算电脑尺寸怎么算Mar 20, 2023 pm 01:43 PM

电脑尺寸的计算方法:1、找一个平面且稳定的桌子,将显示器放置在桌子上面,保持显示器的稳定;2、用尺子量对角线的长度;3、通过厘米和英尺之间的单位转换,计算显示器尺寸即可。

edge是什么浏览器edge是什么浏览器Jul 19, 2022 pm 12:41 PM

edge是由微软开发的基于Chromium开源项目及其他开源软件的网页浏览器。Edge浏览器主要特点是能够支持目前主流的Web技术,作为Windows10自带浏览器,给微软用户带来更好的功能体验。

苹果自带的浏览器叫什么苹果自带的浏览器叫什么Jul 18, 2022 am 10:42 AM

苹果自带的浏览器叫“Safari”;Safari是一款由苹果公司开发的网页浏览器,是各类苹果设备的默认浏览器,该浏览器使用的是WebKit浏览器引擎,包含WebCore排版引擎及JavaScriptCore解析引擎,在GPL条约下授权,同时支持BSD系统的开发。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

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.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!