CSS的一些常用样式,如背景、文字、文本、表格、列表等,以及一些常用的场景,如居中对齐、溢出、隐藏元素等。01、常用样式 1.1、background背景 设置元素背景的样式 background,更
顺晟科技
2021-06-30 17:04:25
140
p {
color: red;
}
/* 我是注释内容 */
<head>
<!--
引入外部样式文件(推荐):<link>标签在写在<head>标签内。外部样式表文件不能包含任何的HTML标签。样式表应该以.css扩展名进行保存。
href:定义所链接外部样式表文件的URL,可以是相对路径,也可以是路径。
type:定义所链接文档的类型,在这里需要指定为“text/CSS”,表示链接的外部文件为CSS样式表。
rel:定义当前文档与被链接文档之间的关系,在这里需要指定为“stylesheet”,表示被链接的文档是一个样式表文件。
-->
<link type="text/css" rel="styleSheet" href="./study.css" />
</head>
<body>
<!-- 行内样式:语法中style是标签的属性,实际上任何HTML标签都拥有style属性,用来设置行内式。其中属性和值的书写规范与CSS样式规则相同,行内式只对其所在的标签及嵌套在其中的子标签起作用。 -->
<div style="font-style:italic; color:blue;"> Hello there</div>
<div class="a"> Hello A</div>
<!-- 内部样式:语法中,style标签一般位于head标签中title标签之后,也可以把他放在HTML文档的任何地方。 -->
<style>
.a {
color: red;
}
</style>
</body>
<!-- 类可以在同个标签上有多个值 --> <div class="a b c"> Hello A</div> <!-- 类可以在多个标签上使用 --> <div class="a b"> Hello B</div>
/* 通配符选择器 */
* {
color: gray;
}
/* id选择器 */
#china {
color: red;
}
/* 类选择器 */
.america {
color: green;
}
/* 标签选择器 */
li {
color: yellow;
}
<body>
<ol class="haha">
<li>1</li>
<li>
<p>我是儿子</p>
</li>
<li>3</li>
<p>我是孙子</p>
</ol>
<div>熊大</div>
<p>熊二</p>
<span>光头强</span>
<a href="http://www.baidu.com">百度一下</a>
<input>
</body>
<style>
/* 后台选择器:两个元素用空格隔开,并且选中的是元素1所有层级的后代 */
.haha p {
color: green;
}
/* 子代选择器:只能选择子元素 */
.haha>p {
color: red;
}
/* 并集选择器:并集选择器中间用逗号隔开,支持任意其他选择器,尽量竖着写 */
div,
p,
.haha>a {
color: pink;
}
/* 伪类选择器:伪类选择器表示的是同一个标签,根据其不同的种状态,有不同的样式。例如<a>标签有点击前和点击后两种状态。注意:一定先写静态伪类,后写动态伪类。
1.静态伪类:只能用于超链接的样式。
:link。超链接点击之前(主要是能区分是不是带有href的<a>标签)
:visited。链接被访问过之后
2.动态伪类:针对所有标签都适用的样式。
:hover。悬停,鼠标放到标签上的时候
:active。激活,鼠标点击标签,但是不松手时。
:focus。是某个标签获得焦点时的样式(一般用于输入框获得焦点)
*/
/* 让超链接点击之前 */
a:link {
color: red;
}
/* 让超链接点击之后 */
a:visited {
color: orange;
}
/* 鼠标悬停,放到标签上的时候 */
a:hover {
color: green;
}
/* 鼠标点击链接,但是不松手的时候(激活) */
a:active {
color: black;
}
/* 输入框获取焦点时 */
input:focus {
background-color: palegoldenrod;
}
| 单位名称 | 说明 | 例子 | Piexels(像素) 一个像素的大小根据用户显示器尺寸和像素密度不同而设定 .classname { margin:5px; } Points(磅) 一英镑等于一英寸的1/72,来源于印刷背景设计。 .classname { margin:5pt; } Inches(英寸) 1英寸相当于72英镑 .classname { margin:5in; } Centimeters(厘米) 1厘米比28英镑稍大一些 .classname { margin:5cm; } Millimeters(毫米) 1毫米等于1/10厘米(约为3磅) .classname { margin:5mm; } Picas Picas是另一种印刷度量单位,等于12磅 .classname { margin:5pc; } Ems 一个em等于当前字体的大小,用于描述相对尺寸 .classname { margin:5em; } Exs 也与当前字体大小有关,等于小写字母x的高度,不常用 .classname { margin:5ex; } Percent(百分比) 这个单位与em相关,它是相对单位 .classname { margin:125%; }
|---|
<style>
div {
height: 50px;
width: 50px;
}
#cqz {
/* 设置文本的字体系列:楷体、宋体等(不推荐汉字,推荐使用英文)。可以设置多个,当前一个浏览器不支持的时候,用第二个,以此类推 */
font-family: "楷体";
/* 设置文本的字体大小:可以使用px、em、百分比。不同浏览器字体默认大小不同,所以必须设定文字默认值。h1等标题标签需要单独指定字体大小。 */
font-size: 16px;
/* 设置文本为斜体:默认normal是正常字体 */
font-style: italic;
/* 设置文本粗细:主要是normal和bold这两个值。blod为加粗。或者可以使用数字(注意不要有单位) */
font-weight: 700;
/* 复合写法:前两个可以省略,后两个不能省略。 */
font: font-style font-weight font-size font-family;
}
</style>
<body>
<div id="cqz">曹</div>
</body>
#cqz {
/* 设置文本颜色(具体设置方式见下文CSS中颜色定义) */
color: #00f0ff;
/* 设置文本水平对齐方式:左对齐(left)、右对齐(right)、居中(center)和两端对齐(justify)。 */
text-align: center;
/* 设置文本垂直对齐方式(设置行高,当行高与父容器的高度相同时且文字仅有一行,则文本垂直居中) */
line-height: 100px;
/* 设置文本装饰:下划线(underline)、删除线(line-through)、上划线(overline)、闪烁(blink)等。注意:<a>标签默认值为underline,想要去除<a>标签的下划线,就将text-decoration设置为none */
text-decoration: line-through;
/* 设置文本缩进:一般设置为em,代表当前文字大小的距离,是一个相对单位 */
text-indent: 2em;
}
a {
/* 去除<a>标签的默认下划线 */
text-decoration: none;
}
#cqz {
/* RGB函数 */
color: rgb(0, 255, 255);
/* 十六进制RGB */
color: #fff000;
/* W3C标准色 */
color: red;
}
<body>
<div class="cqz"></div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
.cqz {
margin-top: 50px;
margin-left: 50px;
height: 1000px;
width: 1000px;
border: 1px solid red;
/* 设置背景颜色 */
background-color: rgb(0, 0, 0);
/* 设置背景图片:默认重复铺满盒子 */
background-image: url("../images/123.webp");
/* 设置背景图片重复平铺方式:
1.repeat 垂直方向和水平方向重复。【默认】
2.repeat-x 水平方向重复。
3.repeat-y 垂直方向重复。
4.no-repeat 背景图像将仅显示一次(不重复)。
*/
background-repeat: no-repeat;
/* 设置背景图片定位:一般建议小图标和超大图片使用背景进行设置而不是<img>
1.需要有两个值。
2.如果仅规定了一个关键词,那么第二个值将是center。
3.方位名词: top left center right(方位名词不区分设置顺序)
4.像素/百分比: 设置像素值时,个值是x轴,第二个值是y轴。
*/
/* background-position: center top; */
background-position: 50% ;
/* 设置背景图片大小:如果设置了图片大小,图片会被自动拉伸或压缩到设置的大小。 */
background-size: 1000px 1000px;
/* 设置背景图片是否固定或者随着页面的其余部分滚动
1.scroll 默认值。背景图像会随着页面其余部分的滚动而移动。
2.fixed 当页面的其余部分滚动时,背景图像不会移动。
*/
background-attachment: fixed;
/* 复合写法:支持省略任意属性 */
/* background: #00FF00 url(bgimage.gif) no-repeat fixed top; */
}
</style>
<body>
<span>我是行内元素,被转换成了块级元素</span>
<div>我是块级元素,被转换成了行内元素</div>
<img src="../images/1.webp"><img src="../images/1.webp">
</body>
<style>
* {
margin: 0;
padding: 0;
}
/* 将span转换为块级元素 */
span {
display: block;
background-color: red;
width: 400px;
height: 200px;
}
/* 将div转换为行内块级元素 */
div {
display: inline-block;
background-color: green;
width: 300px;
height: 300px;
}
/* 将img转换为块级元素 */
img {
display: block;
width: 200px;
}
</style>
<style>
div {
height: 100px;
/* 层叠性:该属性会被同权重CSS覆盖 */
width: 50px;
margin-left: 50px;
border: black 1px solid;
/* 继承性:该属性会被子标签继承 */
font-size: 20px;
/* 继承性:该属性会被子标签覆盖 */
font-family: "楷体";
}
div {
/* 层叠性:该属性会覆盖上方的同权重CSS */
width: 60px;
}
p {
font-family: "微软雅黑";
}
</style>
<body>
<div>
<p>曹老三</p>
</div>
</body>
<body>
<div class="father" id="fatherid">
<div class="child1" id="child1Id">孩子1</div>
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
/* 权重为0 */
background-color: pink;
}
.child1 {
/* 权重为0010 */
background-color: red;
}
.father .child1 {
/* 权重为0020 */
background-color: black;
}
#fatherid .child1 {
/* 权重为0110 */
background-color: green;
}
#child1Id {
/* 权重为0100 */
background-color: wheat;
}
#fatherid #child1Id {
/* 权重为0200 */
background-color: greenyellow;
}
.child1 {
/* 权重为+∞ */
background-color: hotpink !important;
}
</style>
09
2022-11
24
2022-10
19
2022-10
07
2022-10
07
2022-10
27
2022-09