css background-size与背景图片填满div的具体研究包括cover contain属性测试

css background-size与背景图片填满div的具体研究包括cover contain属性测试

浏览次数:
作者: 墨鱼
信息来源: 本站原创
更新日期: 2019-10-11 18:01:39
文章简介

CSS的`background-size`属性可控制背景图片大小,其中`cover`可确保图片完全覆盖div并保持宽高比,而`contain`则保证图片完整显示在div内,但可能留有空白。根据实际需求选择合适的属性,实现背景图片完美填充div的效果。

  • 正文开始
  • 相关阅读
  • 推荐作品

background-size与背景图片填满div

在开发中,常有需要将一张图片作为一个div的背景图片充满div的需求

background-size的取值及解释

background-size共有三种属性,分别为

background-size: cover

MDN文档解释说明:缩放背景图片以完全覆盖背景区,可能背景图片部分看不见。A keyword that is the inverse of contain. Scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). The image "covers" the entire width or height of the container. When the image and container have different dimensions, the image is clipped either left/right or top/bottom.

这里的关键说明在于标红的两个区域,分别是它会保持图片的宽高比当图像和容器具有不同的尺寸时,图像被左/右或顶部/底部裁剪。之后会结合例子说明

background-size: contain

MDN文档解释说明:缩放背景图片以完全装入背景区,可能背景区部分空白。A keyword that scales the image as large as possible and maintains image aspect ratio (image doesn't get squished). Image is letterboxed within the container. When the image and container have different dimensions, the empty areas (either top/bottom of left/right) are filled with the background-color.

这里的关键说明在于标红的两个区域,分别是它会保持图片的宽高比当图像和容器具有不同的尺寸时,空区域(左/右/上/右)填充背景色。之后会结合例子说明

background-size: width-value,height-value;

分为固定大小百分比auto,固定大小就是写死;auto就是以背景图片的比例缩放背景图片。。

百分比的的MDN文档解释说明<percentage> 值,指定背景图片相对背景区(background positioning area)的百分比。背景区由background-origin设置,默认为盒模型的内容区与内边距,也可设置为只有内容区,或者还包括边框。如果attachment 为fixed,背景区为浏览器可视区(即视口),不包括滚动条。不能为负值。

实验及声明

这次选用鲁殿作为背景图,这张图的尺寸是260*234,宽高比为260/234 ≈ 1.11


假设一div的宽高为200*200,下面测试中上为表现图,下为原图

background-size: contain

background-size: cover

background-size: auto (auto)

background-size: 100% 100%

分析及解释:首先contain是不行的,原理在于contain要保持宽高比将图片完全放入div中,div为200×200。原图为260×234,所以为了放入div,宽260→200,那么高就得200/宽高比(1.11) = 180,所以会有下面的空白。
再其次,cover在这个时候也是不符合要求的,虽然看起来貌似符合要求,但是与原图是有差别的吗,原因在于cover与contain完全放入相反,它要求完全覆盖。所以要覆盖就要从更小的高计算。高234→200宽就等于200×1.11 = 222,图像就会被右部裁剪掉一部分。
再再其次,auto是原图大小也是不符合的。

所以,background-size: 100%, 100%;是符合需求的。按照div的大小进行计算

其他各种大小div与各种大小的图片搭配请参照上述说明自行分析。
个人建议这种需求都使用background-size: 100%, 100%;

禁止页面被复制和禁止右键,一段样式一段JS就行了,无需复杂设定!
« 上一篇 2019年08月08日
jquery/js当前URL对当前栏目高亮突出显示的两种方案
下一篇 » 2019年12月24日

    Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2021-04-18/5c87eae4cfea26956e87735ff61f5138.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS企业展示模板 - HTML5响应式,手机平板自适应,含产品案例与新闻图片
    1.7k阅读0条评论0个赞
    采用帝国CMS新版,集成产品案例、新闻、单页简介等展示栏目。适合企业团队全方位展示产品与形象。后台操作便捷,安全可靠,性能卓越。全站响应式设计,手机平板浏览同样高端,助力企业快速搭建专业展示平台!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2022-11-06/2931debbbe3431419fe1129fa58cd5b7.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 图片网站模板 - 帝国CMS自适应HTML5响应式手机模板源码整站
    3.6k阅读0条评论0个赞
    专为图片展示、壁纸等站点设计,自动兼容各类移动设备,手机访问无忧。帝国CMS后台管理,安全稳定无漏洞,图片添加便捷,到手即可快速建站!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2015-05-16/8244eac574ae6b2bd5c2e5aeae3c0583.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • PHP瀑布流帝国CMS整站模板,功能全面,HTML5自适应手机浏览
    6.3k阅读0条评论0个赞
    此源码为最终完美版,已修复所有不合理及错误,品质卓越,非盗版可比。支持资源下载、视频播放、图片展示、文章资讯等多类型站点,功能全面。包含用户权限、积分扣点等,智能判断内容类型并前端显示。后台可自由开关瀑布流及自定义侧栏。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2018-07-26/07f8dc998acd2df74a89cc0a2639b4d3.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 智能网址导航站 | 帝国CMS自适应HTML5源码,二维码便捷访问,支持全设备
    7.2k阅读3条评论0个赞
    帝国CMS新版驱动,网址导航站建设新选择。无需编程,分类自定义展示。图文HTML自由排版,SEO友好。智能识别LOGO,导航多样灵活。直连或内容页跳转,设置随心所欲。地址加密,保护权重,构建高效导航平台。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2017-03-26/ab38018b8766ed7dc53356656204db9e.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS新闻资讯模板:整洁大气,HTML5响应式,打造专业手机博客文章平台
    3.1k阅读0条评论5个赞
    本模板基于帝国CMS新版构建,专为个人博客、工作室及自媒体网站设计。采用HTML5响应式技术,完美适配手机与各类移动设备。模板性能稳定,安全可靠,同时功能强大,易于扩展,助您轻松打造专业级新闻资讯发布平台。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2015-05-14/d446d3421e67a045d5381f30f5073edc.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS个人博客模板,新闻资讯自适应HTML5手机浏览
    2.8k阅读0条评论0个赞
    专为个人博客及文章资讯站点打造,代码整洁优化,视觉效果出众!提供多种列表模板及独特音乐展示墙,后台管理便捷。默认SEO优化,支持手机及多种设备响应式浏览,是帝国CMS中最优秀的个人博客模板。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2015-09-24/a46a7c80e64d4da9f16419ebf67cdc93.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 图片展示个人博客 | 自适应响应式手机移动HTML5 | 帝国CMS整站模板
    2.6k阅读0条评论0个赞
    采用帝国CMS最新核心,安全高效,易于扩展!HTM5响应式布局,适配各类手机移动设备。内置广告位,支持图片展示、产品展示、个人博客、自媒体等。整站设计高端大气,到手即用!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2021-02-21/5eecc5405048006fe1b16fe6ed203e9e.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 个人博客&资讯帝国CMS新版自适应HTML5响应式手机模板整站
    1.1k阅读0条评论0个赞
    采用帝国CMS新版,本模板设计高端大气又不失简洁,完美适配个人博客、新闻资讯、文章分享及图片展示类网站。全面支持手机与平板设备,所有内容修改均通过后台轻松管理,操作简便快捷,助您快速搭建个性化网站。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2021-01-05/05b8b6938a291695d42840a9e4b5d3e1.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS企业官网模板:HTML5响应式,手机自适应,一站式产品展示与新闻发布平台
    1.6k阅读0条评论0个赞
    采用帝国CMS新版内核,打造企业专属展示平台。涵盖产品案例、新闻动态、单页简介等丰富栏目,全面展示企业形象与产品魅力。后台操作简便,安全可靠,性能卓越。整站响应式设计,手机平板浏览同样高端大气,助力企业快速构建专业、美观的在线展示窗口!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2015-05-19/c07d5cb286f326862e479fc8fcceea21.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS门户模板源码,资讯美女娱乐八卦两性健康,支持手机端
    6.2k阅读0条评论0个赞
    采用帝国CMS最新版,安全稳定高效。适合门户、资讯、美女图片、娱乐八卦、两性健康等网站。后台广告位管理丰富,支持快速发布广告。首页推荐和头条属性强大,信息调用方便快捷。自动缩略图尺寸,图片展示不变形。默认SEO优化,自带5000条信息,5G空间存放,到手即可开站!最近加入手机端适配。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2019-04-07/2896cd09014c2ce5aa0236612fb591b3.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS整站模版:视频会员播放、软件下载、资讯新闻,自适应HTML5手机门户
    2.9k阅读0条评论0个赞
    基于帝国CMS新版核心,打造安全高效、性能卓越的整站模版。后台操作简便,无需代码知识,轻松配置首页布局。默认SEO优化,栏目列表智能显示标题图片或多图模式,提升用户体验。内置MP4、M3U8多终端播放,支持权限控制与收费管理。下载功能同样集成扣点与权限。六种内容样式自动适配,直接部署即可运营,打造全方位内容门户!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2015-05-06/4355a48da373214a976b6266da1e6fa9.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 第四版移植模板 - 个人博客帝国CMS源码 - 自适应HTML5响应式手机整站
    8.3k阅读0条评论0个赞
    基于帝国CMS最新版,安全稳定高效。移植WP经典模板,功能完备,改名换标即运营,适合资讯/文章类网站,自带SEO优化,轻松开启站长之旅。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2021-05-16/0e8224ed2d365be09cee21d281059350.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • HTML5自适应帝国CMS模板 - 新闻博客工作室,视频收费播放下载一站式解决
    3.4k阅读1条评论0个赞
    基于帝国CMS新版核心,构建安全高效网站模板。后台操作简便,无需编程知识。支持MP4/M3U8等多终端播放,并配备权限控制与收费功能。下载亦设权限与扣点,即装即用,轻松运营!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2019-08-14/bef642a6771de1c38e0a7ae2739585e4.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS全能门户模板:视频收费、新闻资讯、商城购物,自适应HTML5整站解决方案
    7.2k阅读0条评论0个赞
    采用帝国CMS新版核心,构建安全高效、性能卓越的门户模板。后台操作简便,无需代码知识,轻松配置首页布局。默认SEO优化,助力搜索引擎友好。多样栏目样式,适配各类频道风格。内置MP4/M3U8多终端播放,支持权限控制与收费管理。下载功能同样集成扣点与权限。广告位丰富,PC/手机精准投放。适用于新闻资讯、图片展示、软件下载、商品发布等多场景,更多功能待您探索!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2019-01-01/b0e3748b430aad797f5ed05baaf6af01.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS新闻资讯SEO站群模板 | HTML5自适应响应式手机整站源码
    4.6k阅读0条评论0个赞
    本模板基于帝国CMS新版精心打造,整站运行快速、安全高效,代码结构清晰、整洁。特别针对SEO需求进行了细节优化,非常适合专业SEO团队或个人使用,轻松打造高效的新闻资讯站群网络。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2019-10-11/18af8ff8bd7d4763f22c14fbbe572a84.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 宽屏自适应SEO模板:帝国CMS新闻博客站,简洁响应式HTML5整站设计
    3.3k阅读0条评论0个赞
    采用帝国CMS最新核心,安全可靠,性能卓越。后台操作简便,无需编程知识。全站设计简洁大方,手机浏览效果极佳。特色宽屏自适应响应式布局,列表智能识别图文,SEO友好,轻松打造高效新闻博客站点!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2016-01-15/7a55b5b23e796e5198a56edc0e85305a.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 企业官网帝国CMS整站源码 - 自适应响应式HTML5后台模板,手机友好
    4.4k阅读0条评论0个赞
    帝国CMS新版驱动,安全高效稳如磐石。后台管理一键改文,代码清晰SEO友好。版面高端大气,移动设备无缝浏览。扩展性强,未来功能轻松加。高端企业站建设,此源码模板首选!

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2020-09-27/8109a4ad41e694a19c10d643c1c8bc2b.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS自适应手机HTML5整站模板:视频收费播放下载+新闻资讯门户
    1.6k阅读0条评论0个赞
    帝国CMS新版核心,构建安全高效视频收费平台。自动图文适配,3种列表模式任选。带图TAG推荐,海报分享增互动。默认SEO优化,多终端播放兼容。权限控制与扣点收费灵活设置,下载亦享同等功能。一键部署,轻松运营。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2022-02-14/b536f23e83b6788eeeaa0333f40efb0f.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 帝国CMS淘宝客导购模板 - 漂亮大气,快速响应HTML5,手机平板自适应
    1.8k阅读0条评论0个赞
    采用帝国CMS最新版核心,扁平化设计风格,打造高端大气、快速流畅的淘宝客导购类网站模板。完美适应手机、平板等多终端浏览,所有修改均可后台轻松管理。集成标签TAG拼音化自动URL,优化SEO,提升搜索引擎排名。导购栏目与内容模板分离设计,首页智能识别,展现多元前端效果,助力站长高效运营。

  • Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/moyubuluo.com/d/file/2015-12-25/cbc1cf87a31a78cdaad7dab2993c3f29.jpg) is not within the allowed path(s): (/www/wwwroot/moyubuluo.com/:/tmp/) in /www/wwwroot/moyubuluo.com/e/class/t_functions.php on line 2808
  • 个人博客资讯站 | 帝国CMS自适应HTML5 | 响应式手机模板整站
    3.9k阅读0条评论0个赞
    采用帝国CMS最新版,扁平化设计高端大气。适合个人博客、资讯文章类建站,完美自适应手机平板。后台管理简单快速!

如本文对您有帮助,就请墨鱼抽根烟吧!