盒子里面图片的位置怎么设置_重学前端基础:CSS背景颜色,图片,位置,原点,裁剪等设置...

58a8f24800488c1982d1b26fe7535efc.png

background-color

background-color: background-color: #f00;background-color: rgba(255, 0, 0, 0.5);background-color: transparent; /* 默认值 */

background-image

background-image: [, ]*/*  =  | none */background-image: url("../image/pic.png");background-image: url("../image/pic.png0"), url("../image/pic1.png");/* 多张背景图时,先引入的图片在上一层后引入则在下一层 */

当background-color 与 background-image 共存时,背景颜色永远在最底层(于背景图片之下)。

background-repeat

background-repeat 需与背景图片数量一致。

background-repeat: [,  = repeat-x | repeat-y | [repeat | space | round | no-repeat]{1,2}/* X 轴 Y 轴 */background-repeat: no-repeat repeat;
  • space 平铺并在水平和垂直留有空隙,空隙的大小为图片均匀分布后完整覆盖显示区域的宽高
  • round 不留空隙平铺且覆盖显示区域,图标会被缩放以达到覆盖效果(缩放不一定等比)

background-attachment

当页面内容超过显示区域时,使用 local 使背景图片同页面内容一同滚动。

background-attachment: [, ]* = scroll | fixed | local

background-position

background-position: [, ]* = [left|center|right|top|bottom||]|[left|center|right|top|bottom||] [left|center|right|top|bottom||] | [center |[left|right][|]?]&&[center |[left|right][|]?]/* 默认位置为 */background-position: 0 0;/* percentage 是容器与图片的百分比重合之处*/background-position: 20% 50%;/* 等同效果 */background-position: 50% 50%;background-position: center center;background-position: 0 0;background-position: left top;background-position: 100% 100%;background-position: right bottom;/* 四个值时方向只为参照物 */background-position: right 10px top 20px;
14bec6e1f394ceb5e5914c0dc34daafc.png

Sprite 的使用

background-image: url(sprite.png)background-repeat: no-repeat;background-positon: 0 -100px

使用位置为负值将图片偏移使需要的图片位置上移并显示正确的图案。

linear-gradient

linear-gradient()[[ | to [, ]+ = [left | right] || [top | bottom] =  [ | ]?background-image: linear-gradient(red, blue);background-image: linear-gradient(to top, red, blue);background-image: linear-gradient(to right bottom, red, blue);background-image: linear-gradient(0deg, red, blue);background-image: linear-gradient(45deg, red, blue);background-image: linear-gradient(red, green, blue);background-image: linear-gradient(red, green 20%, blue);
bf7e0ed5b14170477500030783a8f7e6.png

radial-gradient

radial-gradient( [ circle ||  ] [ at  ]? , | [ ellipse || [ |  ]{2}] [ at  ]? , | [ [ circle | ellipse ] ||  ] [ at  ]? , | at  ,  [ ,  ]+ ) = closest-corner | closest-side | farthest-corner | farthest-side =  [  |  ]?background-image: radial-gradient(cloest-side, red, blue);background-image: radial-gradient(circle, red, blue);background-image: radial-gradient(circle 100px, red, blue);background-image: radial-gradient(red, blue);background-image: radial-gradient(100px 50px, red, blue);background-image: radial-gradient(100px 50px at 0 0, red, blue);background-image: radial-gradient(red, green 20%, blue);
98a76e4729dad0549928011b6cd3f9b1.png

repeat-*-gradient

background-image: repeating-linear-gradient(red, blue 20px, red 40px);background-image: repeating-radial-gradient(red, blue 20px, red 40px);
c8fae61e72eb77770fce9f75e30fd73f.png

background-origin

案例模型

00c2861e603b911f4ae37a8391e22a8f.png

决定背景 (0,0) 坐标与 100% 坐标的区域。默认值为 padding-box。

[, ]* = border-box | padding-box | content-boxbackground-image: url(red.png);background-repeat: no-repeat;background-origin: padding-box;background-origin: border-box;background-origin: content-box;
d6032e6bd5e90b754972497113148c6e.png

background-clip

裁剪背景,默认值为border-box。

[, ]* = border-box | padding-box | content-boxbackground-image: url(red.png);background-repeat: no-repeat;background-clip: border-box;background-clip: padding-box;background-clip: content-box;
772a780adf838ec8903edd3c14fb847e.png

background-size

[, ]* = [ |  | auto] {1, 2} | cover | containbackground-image: url(red.png);background-repeat: no-repeat;background-position: 50% 50%;background-size: auto;background-size: 20px 20px;/* % 参照物为容器*/background-size: 50% 50%;/* 尽可能小,但宽度与高度不小过容器(充满容器) */background-size: cover;/* 尽可能大,但宽度与高度不超过容器(最大完全显示)*/background-size: contain;
59df0a38e371b7c26855d270e333901a.png

background shorthand

[ =  ||  [/ ]? ||  ||  ||  || /* 两个  第一个为 background-origin *//* 两个  第二个为 background-clip *//* 只出现一个  则即是 background-origin 也是 background-clip */ =  || background: url(red.png) 0 0/20px 20px no-repeat, url(blue.png) 50% 50%/contain no-repeat content-box green;
d7e1283a0e0f53dc8d5873f805234f24.png
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值