03-CSS初步介紹

村里唯一的运维發表於2024-06-11

01 CSS編寫規則

1.1 內聯樣式

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div style="color: red; font-size: 30px;">div元素</div>
</body>
</html>

1.2 內部樣式

把樣式單獨抽離出來

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <!-- 選擇器 -->
<style>
   div {
      color: red;
      font-size: 30px;
      background-color: orange;
   }
</style>
</head>
<body>
   <div>div元素</div>
</body>
</html>

如果存在多個同樣的標籤

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<!-- 選擇器 -->
<style>
  .div-2 {
    color: red;
    font-size: 30px;
    background-color: orange;
  }
</style>
<body>
  <div>div元素</div>
  <div class="div-2">第2div元素</div>
</body>
</html>

1.3 外部樣式

定義一個公共的css檔案,其它的html檔案都引用這個檔案
image
編寫公共的樣式

.title {
  color: red;
  font-size: 30px;
  background-color: orange;
}

test1.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<link rel="stylesheet" href="./css/styel.css">
<body>
  <div class="title">test1中的title</div>
</body>
</html>

test2.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
</head>
<link rel="stylesheet" href="./css/style.css">
<body>
  <div class="title">test2中的title</div>
</body>
</html>

1.4 CSS樣式很多的情況

實際工作中存在很多的css檔案,如果一個個匯入程式碼顯得臃腫

可以寫一個統一的入口

img

入口檔案

@import url(./style1.css);
@import url(./style2.css);

style1.css

.div {
  color: red;
  font-size: 20px;
}

style2.css

.title {
  color: red;
  font-size: 30px;
  background-color: orange;
}

html檔案引入

test1.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<link rel="stylesheet" href="./css/index.css">
<body>
  <div>test1中的title</div>
</body>
</html>

test2.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<link rel="stylesheet" href="./css/index.css">
<body>
  <div class="title">test2中的title</div>
</body>
</html>

02-CSS常用的5個屬性

2.1 font-size(字型大小)

谷歌預設的字型大小為16px

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  .title {
    font-size: 24px;
  }
</style>
<body>
  <div class="title">test1中的title</div>
</body>
</html>

2.2 color(字型顏色)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  .title {
    color: red;
  }
</style>
<body>
  <div class="title">test1中的title</div>
</body>
</html>

2.3 background-color(背景色)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  .title {
    background-color: aqua;
  }
</style>
<body>
  <div class="title">test1中的title</div>
</body>
</html>

2.4 width(寬度)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  .title {
    background-color: aqua;
    width: 120px;
  }
</style>
<body>
  <div class="title">test1中的title</div>
</body>
</html>
image

2.5 height

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
</head>
<style>
  .title {
    background-color: aqua;
    width: 120px;
    height: 50px;
  }
</style>
<body>
  <div class="title">test1中的title</div>
</body>
</html>
image