【CSS 22】圆角 椭圆角 边框图像 多重背景 背景尺寸 全尺寸背景图像 Hero Image background-origin属性 background-clip属性

说在前面

转眼间我们已经学习了CSS的基础教程以及中级教程,掌握了基本的语法以及各种拥有功能的属性样式,同时了解了定位布局特异性等知识,而现在我们来到了高级教程的学习,继续加油吧!

圆角

通过 CSS border-radius 属性,可以实现任何元素的圆角样式

<!--
border-radius 属性实际上是以下属性的简写属性
border-top-left-radius
border-top-right-radius
border-bottom-right-radius
border-bottom-left-radius
-->
<!DOCTYPE html>
<html>
<head>
<style>
#rcorners1 {
	border-radius: 25px;
	background: #73AD21;
	padding: 20px;
	width: 200px;
	height: 150px;
}

#rcorners2 {
	border-radius: 25px;
	border: 2px solid #73AD21;
	padding: 20px;
	width: 200px;
	height: 150px;
}

#rcorners3 {
	border-radius: 25px;
	background: url(/i/paper.jpg);
	background-position: left top;
	background-repeat: repeat;
	padding: 20px;
	width: 200px;
	height: 150px;
}

</style>
</head>
<body>

<h1>border-radius 属性</h1>

<p>拥有指定背景颜色的元素圆角</p>
<p id="rcorners1">Rounded corners!</p>
<p>拥有边框元素的圆角</p>
<p id="rcorners2">Rounded corners!</p>
<p>拥有背景图片元素的圆角<p>
<p id="rcorners3">Rounded corners!</p>

</body>
</html>

指定四角
border-radius 属性可以接受一到四个值
规则如下:
四个值 - border-radius: 15px 50px 30px 5px;(依次分别用于:左上角、右上角、右下角、左下角)
三个值 - border-radius: 15px 50px 30px;(第一个值用于左上角,第二个值用于右上角和左下角,第三个用于右下角)
两个值 - border-radius: 15px 50px;(第一个值用于左上角和右下角,第二个值用于右上角和左下角)
一个值 - border-radius: 15px;(该值用于所有四个角,圆角都是一样的)

椭圆角

#rcorners1 {
  border-radius: 50px / 15px;
  background: #73AD21;
  padding: 20px; 
  width: 200px;
  height: 150px; 
}

#rcorners2 {
  border-radius: 15px / 50px;
  background: #73AD21;
  padding: 20px; 
  width: 200px;
  height: 150px; 
}

#rcorners3 {
  border-radius: 50%;
  background: #73AD21;
  padding: 20px; 
  width: 200px;
  height: 150px;
} 

边框图像

通过使用 CSS border-image 属性,可以设置图像用作围绕元素的边框
属性有三部分:

  1. 用作边框的图像
  2. 裁切图像的位置
  3. 定义中间的部分需要重复还是拉伸

border-image 属性接受图像,并将其切成九部分,就像井字游戏板
然后,将拐角放置在拐角处,并根据您的设置重复或拉伸中间部分

注意:为了使 border-image 起作用,该元素还需要设置 border 属性

/*重复图像的中间部分以创建边框*/
#borderimg {
	border: 10px solid transparent;
	padding: 15px;
	border-image: url(border.png) 30 round;
}

/*图像的中间部分被拉伸以创建边框*/
#borderimg {
	border: 10px solid transparent;
	padding: 15px;
	border-image: url(border.png) 30 stretch;
}

/*
border-image 属性实际上是以下属性的简写属性
- border-image-source
- border-image-slice
- border-image-width
- border-image-outset
- border-image-repeat
*/

多重背景

CSS 允许您通过 background-image 属性为一个元素添加多幅背景图像
不同的背景图像用逗号隔开,并且图像会彼此堆叠,其中的第一幅图像最靠近观看者
下面的例子有两幅背景图像,第一幅图像是花朵(与底部和右侧对齐),第二幅图像是纸张背景(与左上角对齐)

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
	background-image: url(/i/photo/flower.gif), url(/i/paper.jpg);
	background-position: right bottom, left top;
	background-repeat: no-repeat, repeat;
	padding: 15px;
}
</style>
</head>
<body>

<h1>多重背景</h1>
<p>下面的 div 元素有两幅背景图像</p>

<div id="example1">
	<h1>Welcome to Shanghai</h1>
	<p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
	<p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>

</body>
</html>

多重背景图像可以使用单独的背景属性(如上所述)或 background 简写属性来指定
下面的例子使用 background 简写属性(结果与上例相同)

#example1 {
	background: url(flower.gif) right	bottom no-repeat, url(paper.gif) left top repeat;
}

背景尺寸
CSS background-size 属性允许您指定背景图像的大小
可以通过长度、百分比或使用以下两个关键字之一来指定背景图像的大小:contain 或 cover
下面的例子将背景图像的大小调整为比原始图像小得多(使用像素)

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
	border: 2px solid hotpink;
	background: url(/i/photo/flower.gif);
	background-size: 100px 80px;
	background-repeat: no-repeat;
	padding: 15px;
}

#example2 {
	border: 3px solid hotpink;
	background: url(/i/photo/flower.gif);
	background-repeat: no-repeat;
	padding: 15px;
}
</style>
</head>
<body>
<h1>background-size属性</h1>
<p>调整大小之后的background-image:</p>
<div id="example1">
	<h2>Welcome to Shanghai</h2>
	<p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
	<p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>

<p>background-image原始尺寸</p>
<div id="example2">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>

</body>
</html>

background-size 的其他两个可能的值是 contain 和 cover
contain 关键字将背景图像缩放为尽可能大的尺寸但其宽度和高度都必须适合内容区域
这样,取决于背景图像和背景定位区域的比例,可能存在一些未被背景图像覆盖的背景区域

cover 关键字会缩放背景图像,以使内容区域完全被背景图像覆盖其宽度和高度均等于或超过内容区域
这样,背景图像的某些部分可能在背景定位区域中不可见

<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
	border: 2px solid hotpink;
	height: 120px;
	width: 150px;
	background: url(/i/photo/flower.gif);
	background-repeat: no-repeat;
	background-size: contain;
}

.div2 {
	border: 2px solid hotpink;
	height: 120px;
	width: 150px;
	background: url(/i/photo/flower.gif);
	background-repeat: no-repeat;
	background-size: cover;
}

</style>
</head>
<body>

<h1>background-size 属性</h1>

<h2>background-size: contain</h2>
<div class="div1">
<p>Lorem ipsum dolor sit amet.</p>
</div>

<h2>background-size: cover:</h2>
<div class="div2">
<p>Lorem ipsum dolor sit amet.</p>
</div>

<h2>background-size 未定义:</h2>
<div class="div3">
<p>Lorem ipsum dolor sit amet.</p>
</div>

<h2>原始图像</h2>
<img src="/i/photo/flower.gif" alt="Flowers" width="224" height="162">
</body>
</html>

定义多个背景图像的尺寸
在处理多重背景时,background-size 属性还可以接受多个设置背景尺寸的值(使用逗号分隔的列表)
下面的例子指定了三幅背景图像,每幅图像有不同的 background-size 值

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
	background: url(/i/photo/tree.png) left top no-repeat, url(/i/photo/flower.gif) right bottom no-repeat, url(/i/paper.jpg) left top repeat;
	padding: 15px;
	background-size: 50px, 130px, auto;
}
</style>
</head>
<body>

<div id="example1">
	<h1>Welcome to Shanghia</h1>
	<p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
	<p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>

</body>
</html>

全尺寸背景图像
现在,我们希望网站上的背景图像始终覆盖整个浏览器窗口
具体要求如下

  1. 用图像填充整个页面(无空白)
  2. 根据需要缩放图像
  3. 在页面上居中图像
  4. 不引发滚动条
/*
下面的例子显示了如何实现它:使用 <html> 元素(<html> 元素始终至少是浏览器窗口的高度)
然后在其上设置固定且居中的背景
最后使用 background-size 属性调整其大小
*/
html {
	background: url(img_man.jpg) no-repeat center fixed;
	background-size: cover;
}

body {
	color: white;
}

Hero Image

/*
还可以在 <div> 上使用不同的背景属性来创建 Hero Image(带有文本的大图像),并将其放置在您希望的位置上
请注意,这项技术会将使图像响应:请调整浏览器窗口的大小来查看效果
*/
body {
	margin: 0;
	font-family: Arial, Helvetica, sans-serif;
}

.hero-image {
	background: url(img_man.jpg) no-repeat center;
	background-size: cover;
	height: 500px;
	position: relative;
}

.hero-text {
	text-align: center;
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	color: white;
}

background-origin 属性
CSS background-origin 属性指定背景图像的位置
该属性接受三个不同的值:

  1. border-box - 背景图像从边框左上角开始
  2. padding-box - 背景图像从内边距边缘左上角开始(默认)
  3. content-box - 背景图像从内容左上角开始

background-clip 属性
CSS background-clip 属性指定背景的绘制区域
该属性接受三个不同的值:

  1. border-box - 背景绘制到边框的外部边缘(默认)
  2. padding-box - 背景绘制到内边距的外边缘
  3. content-box - 在内容框中绘制背景
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zanebla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值