Tailwind Background

背景滚动(background attachment)

CSS提供的background-attachment属性用于设置背景图像的固定方式,比如是否随着页面滚动。简单来说,background-attachment用于指明背景图片的位置是固定于视口,还是随着包含块移动的。可理解为定义背景图片随滚动轴的移动方式。

background-attachment: scroll | fixed | local | inherit;
属性值描述
scroll默认值,背景像随着页面其它部分滚动而滚动,背景相对于元素固定,背景和内容绑定。
fixed背景不会随着页面的滚动而滚动,背景相对于视口固定,随页面滚动背景不动,相当于背景被设置在body上。
local背景随着元素内容的滚动而滚动,背景相对于元素内容固定。
inherit从父元素继承
background-attachment:scroll;

设置background-attachment背景图片是相对于元素自身固定,内容滚动时背景也会跟着滚动,同时附加到元素的border上。当元素设置overflow-hidden:scroll属性后会成为滚动元素,此时设置background-attachment:scroll背景图片将不会随着元素内容的滚动而滚动。

background-attachment:fixed;

设置background-attachment:fixed的元素中背景图片是相对于视口(viewport)固定,即使元素拥有滚动条,背景图片也不会随着内容而滚动。

background-attachment:local;

对于滚动元素(设置overflow:scroll属性)设置background-attachment:local后背景图片会随着内容的滚动而滚动。因为背景图片是相对于元素自身内容定位,初始状态为固定,当滚动元素出现滚动条后背景图片将会随着内容而滚动。

工具类属性
bg-scrollbackground-attachment:scroll;
bg-fixedbackground-attachment:fixed;
bg-localbackground-attachment:local;

背景色(background color)

工具类属性
bg-transparentbackground-color:transparent;
bg-currentbackground-color:currentColor;
bg-blackbackground-color:#000;
bg-whitebackground-color:#fff;

背景透明度(background opacity)

工具类描述
bg-opacity-0--bg-opacity:0;
bg-opacity-25--bg-opacity:0.25;
bg-opacity-50--bg-opacity:0.5;
bg-opacity-75--bg-opacity:0.75;
bg-opacity-100--bg-opacity:1;

背景定位(background position)

Tailwind采用bg-{side}方式定义工具类用于设置背景的定位

工具类描述
bg-topbackground-position:top;
bg-left-topbackground-position:left top;
bg-right-topbackground-position:right top;
bg-leftbackground-position:left;
bg-centerbackground-position:center;
bg-rightbackground-position:right;
bg-bottombackground-position:bottom;
bg-left-bottombackground-position:left bottom;
bg-right-bottombackground-position:right bottom;

背景大小(background size)

background-size: auto | cover | contain;
属性值描述
auto默认值,保持背景图片原始宽度和高度。
cover将背景图片缩放,若图片本身大于容器则缩小到合适宽高以适应容器。
contain将背景图片缩小以在容器中显示完整图片,若容器宽度于背景图片不一致时会出现留白。

当图片大小超过容器大小时,cover属性值会对背景图片进行裁剪,所以一般会配置background-position属性来决定显示图片内容的位置。

contain属性会将图片缩小以在容器中显示完整图片,多数情况下由于容器宽高比例与图片比例不同,因此造成图片与容器上下或左右留白的情况。

covercontain的区域在于,cover会按照背景图片最长边来按比例放大或缩小,contain则会按照背景最短边按比例放大或缩小。

工具类属性
bg-autobackground-size:auto
bg-coverbackground-size:cover
bg-containbackground-size:contain
position-size
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet">

<div class="container mx-auto my-4 p-4 text-center flex flex-row jutify-between">
  <div class="bg-gray-100 rounded overflow-hidden border m-2 w-1/2">
    <div class="h-48 w-full bg-center bg-cover bg-no-repeat" style="background-image:url('http://source.unsplash.com/800x800')"></div>
    cover
  </div>
  <div class="bg-gray-100 rounded overflow-hidden border m-2 w-1/2">
    <div class="h-48 w-full bg-center bg-contain bg-no-repeat" style="background-image:url('http://source.unsplash.com/800x800')"></div>
    contain
  </div>
</div>

背景重复(background repeat)

background-repeat属性用于设置是否及如何重复背景图片,即定义背景图片的平铺模式。背景图片会从原图开始重复,原图由background-image定义,并根据background-position放置。

background-repeat: repeat | repeat-x | repeat-y | no-repeat;

CSS3为background-repeat新增了roundspace两个属性值

  • round 背景图片自动缩放自导适应填满整个容器
  • space 背景图片以相同间距平铺且填充整个容器或某个方向
工具类属性
bg-repeatbackground-repeat:repeat;
bg-no-repeatbackground-repeat:no-repeat;
bg-repeat-xbackground-repeat:repeat-x;
bg-repeat-ybackground-repeat:repeat-y;
bg-repeat-roundbackgound-repeat:round;
bg-repeat-spacebackground-repeat:space;
repeat
<div class="bg-gray-100 rounded overflow-hidden border m-2">
    <div class="h-48 w-full bg-center bg-repeat-space" style="background-image:url('http://source.unsplash.com/100x100')"></div>
    center repeat space
</div>

<div class="bg-gray-100 rounded overflow-hidden border m-2">
    <div class="h-48 w-full bg-center bg-repeat-round" style="background-image:url('http://source.unsplash.com/100x100')"></div>
    center repeat round
</div>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值