关于背景图片的位置其background-position设置背景图片的位置有两种方式,一种是是根据像素设置,第二种根据百分比设置,第一种根据像素的位置是很简单的,只是关于百分比这个设置理解特别容易出现偏差的,先看看一段简单的代码:

   .bg-position{
background:url(../../public/images/404.jpg) no-repeat;
margin-top:100px;
margin-left:100px;
background-color:#ccc;
height: 500px;
width: 800px;
background-position: 100px 100px;
}

这种情况是可以预见的,如下所示:

网上看了一篇文章说是background-position是依赖于no-repeat的,在repeat的状态下和默认的状态下(默认即为repeat),background-position是不起作用的,因此自己尝试了一把,看了一下效果:

代码中去掉了no-repeat,同样是可以展示,以前repeat的时候没有注意和background-position结合一起,不过现在发现设置background-position之后,我们不断的调整垂直方向的距离,发现所有的位置变动都是根据正中间的全部显示的图片的位置进行调整,有兴趣的可以尝试一下~

我们将background-position设置为20% 20%,代码如下:

    .bg-position {
background: url(../../public/images/404.jpg) no-repeat;
margin-top: 100px;
margin-left: 100px;
background-color: #ccc;
height: 500px;
width: 500px;
background-position: 20% 20%;
}

最开始我以为是最开始的图片位置一样,500*0.2=100,但是事实上却不是这样:

这个百分比不是根据div的宽度来绝对的,而是根据div宽/高和图片的宽/高的差值来决定的:

position-x=(容器宽度-图片宽度)*百分比

position-y=(容器高度-图片高度)*百分比

这样就会理解上图出现的问题,如果想更好的理解一下可以理解图片的20%的点和容器在20%所在的点重合~

CSS-background-position百分比的更多相关文章

  1. CSS中position和header和overflow和background

    <!DOCTYPE html> <!--CSS中position属性--> <html lang="en"> <head> < ...

  2. PHP全栈开发(八):CSS Ⅲ background

    设置背景颜色: div { background-color:#b0c4de; } 不仅可以给body标签设置背景颜色,还能给p,div,h标签设置背景颜色 设置背景图片: body { backgr ...

  3. 深入理解css中position属性及z-index属性

    深入理解css中position属性及z-index属性 在网页设计中,position属性的使用是非常重要的.有时如果不能认识清楚这个属性,将会给我们带来很多意想不到的困难. position属性共 ...

  4. 关于CSS 的position定位问题

    对于初学者来说,css的position定位问题是比较常见的.之前搞不清楚postion定位是怎么回事,排版一直歪歪斜斜的,老是排不好 css的定位一般来说,分为四种: position:static ...

  5. css利用padding百分比实现图片自适应高度

    应用场景 宽高比率,实现图片自适应高度,防止图片加载过程高度为0,加载完图片高度撑起,它下面的div抖动问题 重点:CSS百分比padding都是相对宽度计算的 <div class=" ...

  6. CSS background 之设置图片为背景技巧

    首先先来看看background有那些值: 可以按顺序设置如下属性(可点击进入相应的css手册查看使用):background-color 背景颜色background-image 背景图片backg ...

  7. CSS background 属性详解

    CSS background Property 语法: background: bg-color bg-image position/bg-size bg-repeat bg-origin bg-cl ...

  8. CSS之position体验

    目录: 1. position介绍 2. relative 3. position 4. fixed与static 5. 总结 1. position介绍 position最简单的理解就是元素位置的定 ...

  9. [CSS3] CSS Background Images

    Body with background image and gradient html { background: linear-gradient(#000, white) no-repeat; h ...

  10. css background transparent All In One

    css background transparent All In One opacity ul { max-height: 100px; /* max-height: 187px; */ overf ...

随机推荐

  1. Web大前端面试题-Day3

    1. javascript的typeof返回哪些数据类型? 答案: undefined string boolean number symbol(ES6) Object Function 2. 列举3 ...

  2. xcode 拷贝新的ios image 进去以后 出现 the divices is locked

    苹果公司时不时的给你更新下ios系统.对于开发者来说.更新xcode是灾难性的. 一直在用xcode7.3.1,可是最新不小心把手机升级到 ios 10.1.1,这下好了,真机调试不行了.提示没有镜像 ...

  3. LDO current regulator for power LED

    LDO current regulator for power LED Challenge You've got a power LED? Great! Build a flash light! Wh ...

  4. DMA/TIM capture

    This is a more free standing example measuring the LSI (TIM5_CH4 internally) and demonstrating DMA/T ...

  5. oracle 删除字段中空格

    update  sales_report set region =  REGEXP_REPLACE(region,  '( ){1,}', '')

  6. 【Go命令教程】10. go fix 与 go tool fix

    命令 go fix 会把指定 代码包 的所有 Go 语言源码文件中的旧版本代码修正为新版本的代码.这里所说的版本即 Go 语言的版本.代码包的所有 Go 语言源码文件不包括其子代码包(如果有的话)中的 ...

  7. 依赖注入(DI)和控制反转(IOC)的理解,写的太好了。

    学习过spring框架的人一定都会听过Spring的IoC(控制反转) .DI(依赖注入)这两个概念,对于初学Spring的人来说,总觉得IoC .DI这两个概念是模糊不清的,是很难理解的,今天和大家 ...

  8. swift笔记(二) —— 运算符

    基本运算符 Swift支持大部分的标准C语言的操作符,而且做了一些改进,以帮助开发人员少犯低级错误,比方: 本该使用==的时候,少写了个=, if x == y {-} 写成了 if x = y {- ...

  9. 解决Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER

    今天在用java与mysql数据库时发现Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER ...

  10. 如何在Windows服务程序中添加U盘插拔的消息

    研究了下这个问题,主要要在一般的windows服务程序中修改两个地方: 一.调用RegisterServiceCtrlHandlerEx VOID WINAPI SvcMain( DWORD dwAr ...