CSS基础 | 青训营

132 阅读3分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 10 天

本文为自己自学前端知识过程中的笔记

基础认知

CSS引入方式

引入方式书写位置作用范围使用场景
内嵌式CSS写在style标签中当前页面小案例
外联式CSS写在单独的css文件中,通过link标签引入多个页面项目中
行内式CSS写在标签的style属性中当前标签配合js使用

基础选择器

标签选择器

结构:标签名{css属性名:属性值;}

作用:通过标签名,找到页面中所有这类标签,设置样式

注意点:

  • 标签选择器选择的是一类标签,而不是单独某一个
  • 标签选择器无论嵌套关系有多深,都能找到对应的标签
 <head>
     <style>
         /* 选择器 {} */
         /* 标签选择器 就是 以标签名命名的选择器 */
         p {
             color: red;
         }
 
         /* 标签选择器 选中所有的这个标签都生效css */
     </style>
 </head>

类选择器

结构: .类名{css属性名:属性值;}

作用:通过类名,找到页面中所有带有这个类名标签,设置样式

注意点:

  • 所有标签上都有class属性,class属性的属性值称为类名
  • 类名可以由数字、字母、下划线、中划线组成,但不能以数字或中划线开头
  • 一个标签可以同时有多个类名,类名之间以空格隔开
  • 类名可以重复,一个类选择器可以同时选中多个标签
 <head>
     <style>
         .red {
             color: red;
         }
 ​
         .size {
             font-size: 66px;
         }
     </style>
 </head>
 <body>
     <!-- 类: 定义 和 使用才能生效 -->
     <p>111</p>
     <!-- 一个标签可以使用多个类名 , 需要空格隔开即可 -->
     <p class="red size">222</p>
     <div class="red">这个标签文字也要变红</div>
 </body>

id选择器

结构: #id属性值{css属性名:属性值;}

作用:通过id属性值,找到页面中带有这个id属性值的标签,设置样式

注意点:

  • 所有标签上都有id属性
  • id属性值类似于身份证号码,在一个页面中是唯一的,不可重复的
  • 一个标签上只能有一个id属性值
  • 一个id选择器只能选中一个标签
 <head>
     <style>
         /* 定义id选择器 */
         #blue {
             color: skyblue;
         }
     </style>
 </head>
 <body>
     <div id="blue">这个div文字是蓝色的</div>
     <p id="blue">111</p>
 </body>

通配符选择器

结构: ***** {css属性名:属性值;}

作用:找到页面中所有的标签,设置样式

 <head>
     <style>
         * {
             color: red;
         }
     </style>
 </head>
 <body>
     <div>div</div>
     <p>pppp</p>
     <h1>h1</h1>
     <span>span</span>
     <p>pppp</p>
     <h2>h2</h2>
 </body>

字体和文本样式

字体大小

属性名:font-size

取值:数字+px

 <head>
     <style>
         p {
             font-size: 30px;
         }
     </style>
 </head>
 <body>
     <!-- 默认字号是16 -->
     <p>段落文字</p>
 </body>

字体粗细

属性名:font-weight

取值:

  • 关键字

    正常normal
    加粗bold
  • 纯数字:100~900的整百数

 <head>
     <style>
         div {
             /* 加粗 */
             font-weight: 700;
         }
 ​
         h1 {
             /* 不加粗 */
             font-weight: 400;
         }
     </style>
 </head>
 <body>
     <div>这是div</div>
     <h1>一级标题</h1>
 </body>

字体样式(是否倾斜)

属性名:font-style

取值:

  • 正常(默认值):normal
  • 倾斜:italic
 <head>
     <style>
         div {
             /* 倾斜 */
             font-style: italic;
         }
 ​
         em {
             /* 正常的, 不倾斜 */
             font-style: normal;
         }
     </style>
 </head>
 <body>
     <div>div文字</div>
     <em>em</em>
 </body>

字体系列font-family

属性名:font-family

渲染规则:

  • 从左往右按顺序查找
 <head>
     <style>
         div {
             /* font-family: 宋体; */
             /* 如果用户电脑没有安装微软雅黑, 就按黑体显示文字 */
             /* 如果电脑没有安装黑体, 就按任意一种非衬线字体系列显示 */
             font-family: 微软雅黑, 黑体, sans-serif;
         }
     </style>
 </head>
 <body>
     <div>
         这是一个div标签
     </div>
 </body>
 <head>
     <style>
         p {
             /* font-size: ;
             font-style: ;
             font-weight: ;
             font-family: ; */
             /* font: style   weight   size    字体; */
             
             /* font: italic 700 66px 宋体;
             font-style: normal; */
             font: 100px 微软雅黑;
 ​
             /* 一个属性冒号后面书写多个值的写法 -- 复合属性 */
         }
     </style>
 </head>
 <body>
     <p>这是p标签</p>
 </body>

文本缩进

属性名:text-indent

取值:

  • 数字+px
  • 数字+em(推荐:1em=当前标签的font-size大小)
 <head>
     <style>
         p {
             /* text-indent: 50px; */
             /* 首行缩进2个字的大小 */
             /* 默认字号: 16px ; 32 */
             /* text-indent: 40px;
             font-size: 20px; */
 ​
             /* em: 一个字的大小 */
             text-indent: 2em;
             font-size: 40px;
         }
     </style>
 </head>
 <body>
     <p>2019年,事件视界望远镜团队让世界首次看到了黑洞的样子。</p>
 </body>

文本水平对齐方式

属性名:text-align

取值:

属性值效果
left左对齐
center居中对齐
right右对齐

注意点:

  • 如果需要让文本水平居中,text-align属性给文本所在标签(文本的父元素) 设置
 <head>
     <style>
         h1 {
             /* text-align: left; */
             /* text-align: right; */
             text-align: center;
         }
 ​
         body {
             text-align: right;
         }
     </style>
 </head>
 <body>
     <h1>新闻标题</h1>
 ​
     <img src="./images/1.jpg" alt="">
 </body>

文本修饰

属性名:text-decoration

取值:

属性值效果
underline下划线(常用)
line-through删除线(不常用)
overline上划线(几乎不用)
none无装饰线(常用)
 <head>
     <style>
         div {
             text-decoration: underline;
         }
 ​
         p {
             text-decoration: line-through;
         }
 ​
         h2 {
             text-decoration: overline;
         }
 ​
         a {
             text-decoration: none;
         }
     </style>
 </head>
 <body>
     <div>div</div>
     <p>ppp</p>
     <h2>h2</h2>
     <a href="#">我是超链接, 点呀</a>
 </body>

行高

作用:控制一行的上下行间距

属性名:line-height

取值:

  • 数字+px
  • 倍数(当前标签font-size的倍数)

应用:

  • 单行文本垂直居中可以设置line-height:文字父元素高度
  • 网页精准布局时,会设置line-height:1可以取消上下间距

行高与font连写的注意点:

  • 如果同时设置了行高和font连写,注意覆盖问题
  • font:style weight size/line-height family
 <head>
     <style>
         p {
             /* line-height: 50px; */
             /* 自己字号的1.5倍 */
             /* line-height: 1.5; */
 ​
             /* 66px 宋体 倾斜 加粗 行高是2倍 */
             font: italic 700 66px/2 宋体;
         }
     </style>
 </head>
 <body>
     <p>2019年,事件视界望远镜团队让世界首次看到了黑洞的样子。</p>
 </body>