了解CSS

151 阅读3分钟

1 CSS是什么

  • CSS 指层叠样式表 (Cascading Style Sheets)
  • 样式定义HTML如何显示 image.png
  • CSS的基础代码 选择器selector 选中页面元素
h1{
color:white;
font-size:14px;
}

CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明,选择器通常是您需要改变样式的 HTML 元素。

每条声明由一个属性和一个值组成。

属性(property)是您希望设置的样式属性(style attribute)。每个属性有一个值。属性和值被冒号分开。 属性+属性值组成声明,多条声明用逗号隔开,整体叫做规则。

2 在页面中如何使用CSS

有三种方法

1、<!--外链-->

<link rel="stylesheet" href="/assets/style.css>

2、<!--嵌入-->

<style>
  li{margin:0; list-style:none}
  p{margin: lem 0;}
</style>
复制代码

3、<!--内联-->

<p style="margin:lem 0">Example Content</p>

完整架构:html用来写内容,CSS设置元素形式。
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Blog</title>
  <style>
    h1 {
      color: orange;
      font-size: 24px;
    }
    p {
      color: gray;
      font-size: 14px;green
      line-height: 1.8;
    }
  </style>
</head>
<body>
  <h1>Web框架的架构模式探讨</h1>
  <p>在写干货之前,我想先探讨两个问题,
  模式的局限性?模式有什么用?</p>
</body>
</html>
复制代码

3 CSS是如何工作的

image.png

3-1 选择器Selector

  • 找出页面中的元素给他们设置样式

  • 有多种方式选择元素

    • 按照标签名、类名或者Id
    • 按照属性
    • 按照DOM树中的位置
  1. 通配选择器
<h1>This is heading</h1>
<p>this is some paragraph.</p>

<style>
* {
  color: red;
  font-size: 20px;
}
</style>
复制代码
  1. 标签选择器
<h1>This is heading</h1>
<p>this is some paragraph.</p>

<style>
h1 {
  color: orange;
}
p {
  color: gray;
  font-size: 20px;
}
</style>
复制代码
  1. id选择器
<h1 id="logo">
  <img src="https://assets.codepen.io/59477/h5-logo.svg" alt="HTML5 logo" width="48" />
  HTML5 文档
<h1>

<style>
  #logo {
    font-size: 60px;
    font-weight: 200;
  }
</style>
复制代码
  1. 类选择器
<h2>Todo List</h2>
<ul>
  <li class="done">Learn HTML</li>
  <li class="done">Learn CSS</li>
  <li>Learn JavaScript</li>
</ul>
<style>
.done {
  color: gray;
  text-decoration: line-through;
}
</style>
复制代码
  1. 属性选择器
<label>用户名:</label>
<input value="zhao" disabled />

<label>密码:</label>
<input value="123456" type="password" />

<style>
  [disabled] {
    background: #eee;
    color: #999;
  }
</style>
复制代码
<p>
  <label>密码:</label>
  <input type="password" value="123456" />
</p>

<style>
  input[type="password"] {
    border-color: red;
    color: red;
  }
</style>

复制代码
<p><a href="#top">回到顶部</a></p>
<p><a href="a.jpg">查看图片</a></p>

<style>
  a[href^="#"] {
    color: #f54767;
    background: 0 center/1em url(https://assets.codepen.io/59477/arrow-up.png) no-repeat;
    padding-left: 1.1em;
  }
 
  a[href$=".jpg"] {
    color: deepskyblue;
    background: 0 center/1em url(https://assets.codepen.io/59477/image3.png) no-repeat;
    padding-left: 1.2em;
  }
</style>
复制代码

3-2 基于伪类的选择器

  • 状态伪类
  • 结构性伪类

通过判断状态进行制定状态样式,伪类即通过判断符合条件的状态。

<a href="http://example.com">
  example.com
</a>

<label>
  用户名:
  <input type="text">
</label>

<style>
a:link {
  color: black;
}

a:visited {
  color: gray;
}

a:hover {
  color: orange;
}

a:active {
  color: red;
}

:focus {
  outline: 2px solid orange;
}
</style>
复制代码

根据DOM节点位置

<ol>
  <li>阿凡达</li>
  <li>泰坦尼克号</li>
  <li>星球大战:原力觉醒</li>
  <li>复仇者联盟 3</li>
  <li>侏罗纪世界</li>
</ol>

<style>
li {
  list-style-position: inside;
  border-bottom: 1px solid;
  padding: 0.5em
}

li:first-child {
  color: coral
}

li:last-child {
  border-bottom: none;   /最后一个没有横线
}
</style>
复制代码

对于选择器来说,可以把标签、类、属性放在一个组合里面

<label>
  用户名:
  <input class="error" value="aa">
</label>
<span class="error">
  最少3个字符
</span>

<style>
  .error {
    color: red;
  }
  
  input.error {
    border-color: red;
  }
</style>
复制代码

组合类型

image.png

<article>
  <h1>拉森火山国家公园</h1>
  <p>拉森火山国家公园是位于...</p>
  <section>
    <h2>气候</h2>
    <p>因为拉森火山国家公园...</p>
    <p>高于这个高度,气候非常寒冷...</p>
  </section>
</article>

<style>
  article p {     /选中后面所有的
    color: black;
  }

  article > p {    /亲子关系,后面一个 
    color: blue;
  }

  h2 + p {        /h2相邻选择器 
    color: red; 
  }
</style>
复制代码

3-3 选择器组

标签类与属性类举例image.png

4 颜色

  • rgb调色
  • hsl:色相,饱和度,亮度
  • 不透明度alpha,可以组合成rgba,hsla

5 字体

5-1 font-famliy

通用字体族,设置多个字体形式,从前到后去匹配字体,选择浏览器上有的字体。image.png

  • 字体列表最后写上通用字体族
  • 英文字体放在中文字体前面

5-2 Web fonts

基本形式,采用标签

<h1>Web fonts are awesome!</h1>

<style>
  @font-face {
    font-family: "Megrim";
    src: url(https://fonts.gstatic.com/s/megrim/v11/46kulbz5WjvLqJZVam_hVUdI1w.woff2) format('woff2');
  }

  h1 {
    font-family: Megrim, Cursive;
  }
</style>
复制代码

直接定义

<style>
  @font-face {
    font-family: f1;
    src: url("//s2.ssl.qhimg.com/static/ff00cb8151eeecd2.woff2") format("woff2");
  }

  @font-face {
    font-family: f2;
    src: url("//s3.ssl.qhimg.com/static/a59a90c9e8f21898.woff2") format("woff2");
  }

  @font-face {
    font-family: f3;
    src: url("//s2.ssl.qhimg.com/static/58324737c4cb53a5.woff2") format("woff2");
  }

  h1 {
    font-size: 50px;
  }
</style>

<h1 style="font-family: f1, serif">落霞与孤鹜齐飞,秋水共长天一色。</h1>
<h1 style="font-family: f2, serif">落霞与孤鹜齐飞,秋水共长天一色。</h1>
<h1 style="font-family: f3, serif">落霞与孤鹜齐飞,秋水共长天一色。</h1>
复制代码

5-3 font-size

image.png

  • 关键字使用section去定义
<section>
  <h2>A web font example</h2>
  <p class="note">Notes: Web fonts ...</p>
  <p>With this in mind, let's build...</p>
</section>

<style>
  section {
    font-size: 20px;
  }

  section h1 {
    font-size: 2em;
  }

  section .note {
    font-size: 80%;
    color: orange;
  }
</style>
复制代码
  • 长度及字体定义
<p class="normal">Normal Text</p>
<p class="italic">Italic Text</p>    //倾斜

<style>
  p {
    font-size: 36px;
    font-family: "Helvetica Neue", sans-serif;
  }

  .normal {
    font-style: normal;
  }

  .italic {
    font-style: italic
  }
</style>
复制代码
  • 字体宽度
<ul>
  <li class="w1">锦瑟无端五十弦(100)</li>
  <li class="w2">锦瑟无端五十弦(200)</li>
  <li class="w3">锦瑟无端五十弦(300)</li>
  <li class="w4">锦瑟无端五十弦(400-normal)</li>
  <li class="w5">锦瑟无端五十弦(500)</li>
  <li class="w6">锦瑟无端五十弦(600)</li>
  <li class="w7">锦瑟无端五十弦(700-bold)</li>
  <li class="w8">锦瑟无端五十弦(800)</li>
  <li class="w9">锦瑟无端五十弦(900)</li>
</ul>

<style>
  .w1 { font-weight: 100 }
  .w2 { font-weight: 200 }
  .w3 { font-weight: 300 }
  .w4 { font-weight: 400 }
  .w5 { font-weight: 500 }
  .w6 { font-weight: 600 }
  .w7 { font-weight: 700 }
  .w8 { font-weight: 800 }
  .w9 { font-weight: 900 }
</style>
复制代码
  • 行间距定义
  • 文章对齐方式
  • 字符距离,单词距离
  • 首行缩进距离
  • 下划线位置
  • 空格