前端工程师养成记——三、CSS入门

本文深入讲解CSS的基础知识,包括格式、单位、基本样式、注释及三种样式应用方式:内联、内部和外部样式。此外,还详细介绍了颜色表示法、背景、边框、字体、文本样式以及复合样式的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CSS入门

格式

   选择器 { 属性1:值1; 属性2:值2; }
	div{
		width: 50%; height: 100px; background-color: red;
	}

单位

   px —> 像素(pixel)、百分比(%)

基本样式

   width(宽)、height(高)、background-color(背景色)
   长度单位:

         1、 px:像素
         2、 %:百分比 (外容器为100px,内容器设置为50%,则是50px

CSS注释 :

         /* CSS注释的内容 */

学习网站

         https://www.w3cschool.cn/css/https://www.w3cschool.cn/cssref/



一、css的三大样式

1、内联样式(也叫行间样式、行内样式)

     在html标签上添加style属性来实现
在这里插入图片描述
举例:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
	</head>
	<body>
		<div style="width: 200px; height: 200px; background-color: red;">这是一个div块</div>
	</body>
</html>

浏览器上的显示效果:
在这里插入图片描述

2、内部样式

     在<style>标签内添加的样式
     注: 内部样式的 优点 是可以 复用代码
在这里插入图片描述
举例:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="content-type" content="text/html; charset=utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
		<style>
			div{
				width: 200px;
				height: 200px;
				background-color: red;
			}
		</style>
	</head>
	<body>
		<!-- <div style="width: 200px; height: 200px; background-color: red;">这是一个div块</div> -->
		<div>这是一个div块</div>
	</body>
</html>

浏览器上的显示效果:
在这里插入图片描述

3、外部样式

     引入一个单独的CSS文件(common.css文件)
在这里插入图片描述

  引入方法:

     1)通过link标签引入外部资源,rel属性指定资源跟页面的关系,href属性表示资源的地址;

	<head>
		<link rel="stylesheet" type="text/css" href="xxx.css" />
	</head>

     2)通过 @import 来引入外部样式(注:这种方式有很多问题,不建议使用

	<style>
		@import url("./common.css");
	</style>

关于link与import的区别可见博客:https://www.cnblogs.com/my–sunshine/p/6872224.html


3)link标签知识点扩展:

   link属性对应的值如下:
在这里插入图片描述



二、css的颜色表示法

可参考:https://www.w3school.com.cn/cssref/css_colors.asp (了解颜色)
       https://www.w3school.com.cn/cssref/css_colors_legal.asp(颜色值)
       https://www.w3school.com.cn/cssref/css_colornames.asp(颜色名)
       https://www.w3school.com.cn/cssref/css_colorsfull.asp(颜色16j进制)

1、单词表示法

red、orange、yellow、green、lime、blue、purple、black、white(红橙黄绿青蓝紫黑白)
特殊的颜色:transparent(透明色)
在这里插入图片描述

2、16进制表示法

在这里插入图片描述
其他更为丰富的颜色请参考:https://www.w3school.com.cn/tags/html_ref_colornames.asp


3、RGB三原色表示法

   RGB 颜色值是这样规定的:rgb(red, green, blue)。每个参数 (red、green 以及 blue) 定义颜色的强度,可以是介于 0 与 255 之间的整数,或者是百分比值(从 0% 到 100%)。
   举例说,rgb(0,0,255) 值显示为蓝色,这是因为 blue 参数被设置为最高值(255),而其他被设置为 0。在这里插入图片描述

4、RGBA表示法

   RGBA 颜色值是 RGB 颜色值的扩展,带有一个 alpha 通道 - 它规定了对象的不透明度。

   RGBA 颜色值是这样规定的:rgba(red, green, blue, alpha)。alpha 参数是介于 0.0(完全透明)与 1.0(完全不透明)的数字。

background-color:rgba(255,0,0,0.5);



三、css背景样式

在这里插入图片描述

  • background-color :背景颜色

  • background-image :背景图片
         url(背景地址)
         默认:会水平垂直都铺满背景图

  • background-repeat : 背景图片的平铺方式
         repeat-x : x轴平铺,在水平方向重复
         repeat-y : y轴平铺,在垂直方向重复
         repeat : x轴、y轴都进行平铺( 默认值
         no-repeat : 都不平铺,背景图仅显示一次

  • background-postion : 背景图片的位置
         x | y: number(px、%) | 单词
          x : left、center、right
          y : top、center、bottom

  • background-attachment : 背景图随滚动条的移动方式
         scroll: 背景图按照当前元素的位置进行偏移( 默认值
         fixed : 背景图按照浏览器进行偏移

可参考:https://www.w3school.com.cn/cssref/index.asp#background



四、css边框样式

在这里插入图片描述

  • border-style :边框样式
         solid:实线
         dashed:虚线
         dotted:点线

  • border-width :边框大小(1px)
         thin:细
         medium:中等粗细
         thick:粗

  • border-color : 边框颜色

  • border : 一行设置多个(border:5px solid red;)

  • border-left-style: 设置单边的(border-left-style: dashed)

可参考:https://www.w3school.com.cn/cssref/index.asp#border



五、css字体样式

CSS 字体样式包括 字体类型、大小、粗细、风格(如斜体)和颜色。
在这里插入图片描述

1、font-family: 字体类型

     英文字体: Arial、Time New Roman
     中文字体: 微软宋体(Microsoft YaHei)、宋体(SimSun)

body{
	font-family: PingFangSC-Regular, Verdana, Arial, 微软雅黑, 宋体;
    font-size: 14px;
}

2、font-size: 字体大小

     像素: 12px、14px、16px ( 一般为偶数)
     百分比: 50%、20%、100%
     英文: xx-small(最小)、x-small(较小)、small、medium、large、x-large(较大)、xx-large(最大)
           相较于默认大小的对比

	h1 {font-size:250%;}
	h2 {font-size:large;;}
	p {font-size: 14px;}

3、font-weight: 字体粗细

     数字: 100、200、… 、900
     英文: normal(相当于100-500)、bold(相当于600-900)、lighter(更细)、bolder(更粗)

	p{font-weight:normal;}
	p{font-weight:bold;}
	p{font-weight:900;}

4、font-style: 字体风格
  • normal - 文本正常显示
  • italic - 文本斜体显示
  • oblique - 文本倾斜显示, 用的少
	p{font-style:normal;}
	p{font-style:italic;}
	p{font-style:oblique;}

italic 和 oblique 的区别

  1. italic 带有倾斜属性的字体的才可以设置倾斜操作(如果某些字体不带有倾斜属性,即使设置了也不起作用)
  2. oblique 没有倾斜属性的字体也可以设置倾斜操作

5、color: 字体颜色
	p { color: red; }

可参考:https://www.w3school.com.cn/cssref/index.asp#font



六、css文本样式

CSS 字体样式包括 文本装饰和文本大小写(针对英文)、文本缩进、文本对齐、文本行高、文本间距与英文折行等
在这里插入图片描述

1、文本装饰(text-decoration)

  1. underline - 下划线
  2. overline - 上划线
  3. line-through - 中划线
  4. blink - 闪烁
  5. none
p  {
    text-decoration: line-through;
    text-transform:  uppercase;
}
p.one {
    text-decoration: underline;
}

2、文本变换(针对英文)(text-transform)

  1. uppercase - 大写
  2. lowercase - 小写
  3. capitalize - 首字母大写
  4. none
span.lower {
    text-transform: lowercase;
}
span.upper {
    text-transform: uppercase;
}
span.capitalize {
    text-transform: capitalize;
}

3、文本缩进(text-indent)

  1. 数字: 10px
  2. 百分比: 20%
p {
	text-indent: 50px;
}

4、文本对齐(text-align)

  1. left - 左对齐
  2. right - 右对齐
  3. center - 居中
  4. justify - 两端对齐
h1 { text-align:center }
h2 { text-align:left }
h3 { text-align:right }

5、文本行高(line-height)

  1. 数字: px | scale()
  2. 百分比: 20%
p.small { line-height: 2; }   //  scale = 2 , 也可以设置为小数1.5等
p.big { line-height: 200%; }

6、文本间距

  • letter-spacing 字符间距 ( normal | 2px )
  • word-spacing 单词间距 ( normal | 2px )
h1 {letter-spacing:2px; }
h2 {letter-spacing:-3px; }

p { word-spacing:30px; }

7、英文折行

div {
	word-break:break-all;      // 强制换行
	word-wrap:break-word;   // 部分换行,对于末尾出现很长的一些单词时,可能存在一些空白区域
}

可参考:https://www.w3cschool.cn/css/html-css-text.html
        https://www.w3school.com.cn/cssref/index.asp#text



七、css复合样式

     复合样式有利于简化代码,写法是通过空格方式实现的。

     注意:background、border无需考虑写的顺序但是font需要考虑(至少需要两个值:size family )
     font的写法顺序:

		weight  style  size  family 
		style  weight  size  family 
		weight  style  size/line-height  family 

举例:

div {
	background: red url(./img/dog.png) no-repeat  20px center
	/*border: 2px red solid;*/
	border-right: red solid 20px;
	font: bold italic 30px/100px 宋体;
}

复合样式写法注意事项

  1. 一个CSS属性只控制一种样式,叫做单一样式
  2. 一个CSS属性控制多种样式,叫做复合样式
  3. 尽量不要两种样式混合写,如果非要混合写,那么一定要先写复合样式再写单一样式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值