CSS3新增背景属性(四)

CSS3新增背景属性

1 background-origin

设置背景图原点起始位置:

  • padding-box:默认值从padding区域开始显示背景图像;
  • border-box:从border区域开始显示背景图像;
  • content-box:从content区域开始显示背景图像。
html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>background-origin</title>
    <style>
        div {
            height: 400px;
            width: 400px;
            padding: 30px;
            border: 20px dashed red;
            background-color: darkkhaki;
            margin: 0 auto;
            /* 引入图片 */
            background-image: url("../../1、CSS选择器/imgs/fengjing.png");
            /* 图片是否重复 */
            background-repeat: no-repeat;
            /* 图片起始位置 */
            background-origin: content-box;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>
1 background-clip

裁剪背景图。

  • padding-box:从padding区域开始向外裁剪背景图像;
  • border-box:默认值,从border区域开始向外裁剪背景图像;
  • content-box:从content区域开始向外裁剪背景图像;
  • text:背景图只呈现在文字上,需要加上-webkit-,而且要将文字设置为透明色。
html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>background-clip</title>
    <style>
        div {
            height: 400px;
            width: 400px;
            padding: 30px;
            border: 20px dashed red;
            background-color: black;
            margin: 0 auto;
            background-image: url("../images/bg.jpg");
            background-repeat: no-repeat;
            background-origin: padding-box;

            font-size: 100px;
            text-align: center;
            line-height: 400px;
            color: transparent;

            /* -webkit-background-clip: text; */
            background-clip: content-box;
        }
    </style>
    </style>
</head>

<body>
    <div>文字</div>
</body>

</html>
3 background-size

设置背景图尺寸。

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>background-size</title>
    <style>
        div {
            height: 400px;
            width: 400px;
            border: 1px solid black;
            background-image: url("../images/bg.jpg");
            background-repeat: no-repeat;

            /* 用长度指定背景图片大小 */
            /* background-size: 400px 400px; */
            /* 用百分比指定图片大小  */
            /* background-size: 100% 100%; */
            /* 默认值,背景真实大小 */
            /* background-size: auto; */
            /* 等比缩放,使背景图片完全在容器内,可能导致一部分容器没有图片,以先满足的图片为准 */
            /* background-size: contain; */
            /* 等比缩放,使图片尽可能覆盖容器,有时图片可能显示不完整 */
            background-size: cover;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>
4 background复合属性
html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>background-origin</title>
    <style>
        div {
            height: 400px;
            width: 400px;
            padding: 30px;
            border: 20px dashed red;
            margin: 0 auto;

            /* background: color url repeat position / size orgin clip */
            background: rgb(146, 97, 97) url("../images/bg.jpg") 5px 5px / 100% 100% border-box content-box;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>
5 多背景图
html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>多背景图</title>
    <style>
        div {
            height: 400px;
            width: 400px;
            border: 1px solid black;

            background: url("../images/touxiang.png") no-repeat top left/ 20% 20%,
                url("../images/touxiang.png") no-repeat top right/ 20% 20%,
                url("../images/touxiang.png") no-repeat bottom left/ 20% 20%,
                url("../images/touxiang.png") no-repeat bottom right/ 20% 20%;
        }
    </style>
</head>

<body>
    <div></div>
</body>


</html>
相关推荐
不是吧这都有重名17 分钟前
利用systemd启动部署在服务器上的web应用
运维·服务器·前端
霸王蟹17 分钟前
React中巧妙使用异步组件Suspense优化页面性能。
前端·笔记·学习·react.js·前端框架
Maỿbe26 分钟前
利用html制作简历网页和求职信息网页
前端·html
森叶1 小时前
Electron 主进程中使用Worker来创建不同间隔的定时器实现过程
前端·javascript·electron
霸王蟹1 小时前
React 19 中的useRef得到了进一步加强。
前端·javascript·笔记·学习·react.js·ts
霸王蟹1 小时前
React 19版本refs也支持清理函数了。
前端·javascript·笔记·react.js·前端框架·ts
繁依Fanyi1 小时前
ColorAid —— 一个面向设计师的色盲模拟工具开发记
开发语言·前端·vue.js·编辑器·codebuddy首席试玩官
明似水2 小时前
Flutter 开发入门:从一个简单的计数器应用开始
前端·javascript·flutter
沐土Arvin2 小时前
前端图片上传组件实战:从动态销毁Input到全屏预览的全功能实现
开发语言·前端·javascript
爱编程的鱼3 小时前
C#接口(Interface)全方位讲解:定义、特性、应用与实践
java·前端·c#