目录
1. 颜色(Color)
1.1. 具名颜色
1.2. RGB
1.3. HSL
2. 背景(Background)
2.1. 背景色(background-color)
2.2. 背景裁剪(background-clip)
2.3. 背景图(background-image)
2.4. 背景定位(background-position)
2.5. 背景定位(background-origin)
2.6. 背景重复方式(background-repeat)
2.7. 背景图尺寸(background-size)
2.8. 背景粘附(background-attachment)
2.9. 写为一个属性(background)
3. 几道笔试题
1. 颜色(Color)
CSS 数据类型 <color> 表示一种标准RGB色彩空间(sRGB color space)的颜色。一种颜色可以用以下任意的方式来描述:
1.1. 具名颜色
如果只想使用基本的颜色,最简单的方法是使用颜色的名称。CSS 把这种颜色称为具名颜色(named color)。
示例:
div {
background-color: red;
}
1.2. RGB
1.2.1. CSS 规范
颜色可以使用红-绿-蓝(red-green-blue (RGB))模式的两种方式被定义:
例如:
div {
background-color: #f00;
}
1.2.2. RGB基本原理
图1-1:光学三原色(RGB)
图1-2:RGB颜色表
示例:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
div {
width: 100px;
height: 100px;
margin: 10px;
float: left;
}
div:nth-of-type(1){
background-color: #ff0000;
}
div:nth-of-type(2){
background-color: #00ff00;
}
div:nth-of-type(3){
background-color: #0000ff;
}
div:nth-of-type(4){
background-color: #ffff00;
}
div:nth-of-type(5){
background-color: #ff00ff;
}
div:nth-of-type(6){
background-color: #00ffff;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<p style="clear:both;"></p>
<div></div>
<div></div>
<div></div>
</body>
</html>
1.3. HSL
颜色也可以使用 hsl() 函数符被定义为色相-饱和度-明度(Hue-saturation-lightness)模式。
1.3.1. HSL 基本原理
HSL是一种将RGB色彩模型中的点在圆柱坐标系中的表示法。HSL即色相(Hue)、饱和度( Saturation)、亮度( Lightness)。
图1-3:HSL 色相环
图1-4:HSL 色相、饱和度、明度
示例:
就说到这
个人觉着颜色这东西吧.....
大体了解即可
2. 背景(Background)
2.1. 背景色(background-color)
CSS属性中的 background-color 会设置元素的背景色。
语法:
关键点:
示例:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
div {
padding: 1em;
}
div {
border: 10px dotted;
}
.exampleone {
background-color: teal;
color: white;
}
.exampletwo {
background-color: rgb(153,102,153);
color: rgb(255,255,204);
}
.examplethree {
background-color: #777799;
color: #FFFFFF;
}
</style>
</head>
<body>
<div class="exampleone">
Lorem ipsum dolor sit amet, consectetuer
</div>
<div class="exampletwo">
Lorem ipsum dolor sit amet, consectetuer
</div>
<div class="examplethree">
Lorem ipsum dolor sit amet, consectetuer
</div>
</body>
</html>
2.2. 背景裁剪(background-clip)
background-clip 属性控制背景能够延伸到何处。
(The background-clip CSS property sets whether an element's background extends underneath its border box, padding box, or content box.)
语法:
示例:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
p {
border: .8em darkviolet;
border-style: dotted double;
margin: 1em 0;
padding: 1.4em;
background: linear-gradient(60deg, red, yellow, red, yellow, red);
font: 900 1.2em sans-serif;
text-decoration: underline;
}
.border-box { background-clip: border-box; }
.padding-box { background-clip: padding-box; }
.content-box { background-clip: content-box; }
.text {
background-clip: text;
-webkit-background-clip: text;
color: rgba(0,0,0,.2);
}
</style>
</head>
<body>
<p class="border-box">The background extends behind the border.</p>
<p class="padding-box">The background extends to the inside edge of the border.</p>
<p class="content-box">The background extends only to the edge of the content box.</p>
<p class="text">The background is clipped to the foreground text.</p>
</body>
</html>
2.3. 背景图(background-image)
CSS background-image 属性用于为一个元素设置一个或者多个背景图像。
常用语法:
background-image: url("./media/examples/lizard.png");
关键点:
示例:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
p {
font-size: 1.5em;
color: #FE7F88;
background-image: none;
background-color: transparent;
}
div {
background-image:
url("https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png");
}
.catsandstars {
background-image:
url("https://mdn.mozillademos.org/files/11991/startransparent.gif"),
url("https://mdn.mozillademos.org/files/7693/catfront.png");
background-color: transparent;
}
</style>
</head>
<body>
<div>
<p class="catsandstars">
This paragraph is full of cats<br />and stars.
</p>
<p>This paragraph is not.</p>
<p class="catsandstars">
Here are more cats for you.<br />Look at them!
</p>
<p>And no more.</p>
</div>
</body>
</html>
2.4. 背景定位(background-position)
background-position 为每一个背景图片设置初始位置。这个位置是相对于由 background-origin 定义的位置图层的。
2.4.1. 语法(仔细品一品):
2.4.2. 综合示例:
/* Keyword values */
background-position: top;
background-position: bottom;
background-position: left;
background-position: right;
background-position: center;
/* <percentage> values */
background-position: 25% 75%;
/* <length> values */
background-position: 0 0;
background-position: 1cm 2cm;
background-position: 10ch 8em;
/* Multiple images */
background-position: 0 0, center;
/* Edge offsets values */
background-position: bottom 10px right 20px;
background-position: right 3em bottom 10px;
background-position: bottom 10px right;
background-position: top right 10px;
/* Global values */
background-position: inherit;
background-position: initial;
background-position: unset;
2.5. 背景定位(background-origin)
background-origin 属性确定计算源图像的位置时以什么的边界为基准,定义的是背景定位区域。
The background-origin CSS property sets the background's origin: from the border start, inside the border, or inside the padding.
语法:
示例:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
div{
width: 150px;
height: 150px;
padding: 50px;
margin: 10px;
border: 10px dashed #000000;
float: left;
}
div {
background-color: #ffff00;
background-image: url(pic.jpg);
background-repeat: no-repeat;
background-size: 100px 100px;
}
/* backgound-clip */
div:nth-of-type(1){
background-clip: border-box; /* default */
}
div:nth-of-type(2){
background-clip: padding-box;
}
div:nth-of-type(3){
background-clip: content-box;
}
/* backgound-origin */
div:nth-of-type(4){
background-origin: border-box;
}
div:nth-of-type(5){
background-origin: padding-box; /* default */
}
div:nth-of-type(6){
background-origin: content-box;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<p style="clear:both;"></p>
<div></div>
<div></div>
<div></div>
</body>
</html>
2.6. 背景重复方式(background-repeat)
CSS 属性 background-repeat 定义背景图像的重复方式。背景图像可以沿着水平轴,垂直轴,两个轴重复,或者根本不重复。
语法:
/* 单值语法 */
background-repeat: repeat-x;
background-repeat: repeat-y;
background-repeat: repeat;
background-repeat: space;
background-repeat: round;
background-repeat: no-repeat;
/* 双值语法: 水平horizontal | 垂直vertical */
background-repeat: repeat space;
background-repeat: repeat repeat;
background-repeat: round space;
background-repeat: no-repeat round;
background-repeat: inherit;
示例:
2.7. 背景图尺寸(background-size)
background-size 设置背景图片大小。图片可以保有其原有的尺寸,或者拉伸到新的尺寸,或者在保持其原有比例的同时缩放到元素的可用空间的尺寸。
2.8. 背景粘附(background-attachment)
background-attachment CSS 属性决定背景图像的位置是在视口内固定,还是随着包含它的区块滚动。
2.9. 写为一个属性(background)
background 属性是一个简写属性,可以在一次声明中定义一个或多个属性:background-clip、background-color、background-image、background-origin、background-position、background-repeat、background-size 和 background-attachment。
示例:
background: green;
background: url("test.jpg") repeat-y;
background: no-repeat url("../../media/examples/lizard.png");
background: left 5% / 15% 60% repeat-x url("../../media/examples/star.png");
background: border-box red;
3. 几道笔试题
题目01:
题目02:
题目03:
题目04:
题目05:
题目06:
题目07:
题目08:
参考:
《CSS 世界》 《CSS核心技术详解》 《CSS权威指南 第四版 上册》 color: https://developer.mozilla.org/zh-CN/docs/Web/CSS/color_value mdn, background: https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-color https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip https://developer.mozilla.org/en-US/docs/Web/CSS/background-image https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-position https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-origin https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-repeat https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-size https://developer.mozilla.org/zh-CN/docs/Web/CSS/background-attachment 【CSS3】background-origin 和 background-clip的区别: https://www.cnblogs.com/shytong/p/5077129.html
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有