底下是一組CSS規則,由幾個部分所組成:
每個宣告最後都要加上分號 ;
才不會造成錯誤!
<head>
<link rel="stylesheet" href="./style.css">
</head>
<head>
<style>
p {
color: red;
}
</style>
</head>
<p style="color:red;">我是紅色的</p>
透過元素名稱來選擇。
透過元素的Class來選擇。
透過元素的ID來選擇。
p {
color: red;
}
a {
color: orange;
}
img {
width: 200px;
}
/* <p class="text">、<span class="super text"> */
.text {
border: 1px solid black;
}
.logo {
width: 500px;
}
/* <p class="blue text">、<div class="super blue text"> */
.blue.text {
color: blue;
}
#menu {
color: white;
}
#my-avatar {
height: 200px;
width: 200px;
}
#enemy {
color: red;
border: 1px solid black;
}
/* Select every element */
* {
color: red;
}
/* <div id="menu">、<div id="nav"> */
#menu, #nav {
font-size: 16px;
}
Separate with comma ","
p {
color: red !important;
}
...
rgb(RR, GG, BB)
rgba(RR, GG, BB, opacity)
Filter effect: rgba(0, 0, 0, 0.5)
#RRGGBB
More: MDN
p {
color: red;
}
div {
color: blue;
}
p {
font-size: 14px;
}
h1 {
font-size: 1.85em;
}
p {
font-family: Arial;
}
div {
font-family: Consolas,微軟正黑體,"Arial",sans-serif;
}
.italic {
font-style: italic;
}
.bold {
font-weight: bold;
}
.underline { /* or overline */
text-decoration: underline;
}
.storke {
text-decoration: line-through;
}
.decorated {
text-decoration: line-through underline;
}
.underscore {
text-decoration: underline red wavy;
}
text-decoration-line
text-decoration-style
text-decoration-color
.center.text {
text-align: center;
}
.left.text {
text-align: left;
}
.right.text {
text-align: right;
}
More: Link
.center.text {
text-align: center;
}
.left.text {
text-align: left;
}
.right.text {
text-align: right;
}
More: Link
p {
border: 2px solid red;
}
其他的樣式可以參考這邊 >
Ex:
p {
border: 2px solid red;
border-radius: 10px;
}
Effect:
div {
width: 200px;
height: 200px;
border: 2px solid red;
border-radius: 50%;
}
Effect:
div {
border-top: 2px solid red;
border-right: 2px solid green;
border-bottom: 2px solid blue;
border-left: 2px solid yellow;
}
Disable the border.
div {
border-top: 2px solid red;
border-right: 2px solid red;
border-left: 2px solid red;
}
div {
border: 2px solid red;
border-bottom: none;
}
body, html {
margin: 0;
padding: 0;
}
- height
元素高度
- weight
元素寬度
- content-box
設定的 height 與 width 只會設定 content, padding 和
border 會另外加上去,造成元素實際大小超過設定值。
- border-box
設定長寬時會連 padding 與 border 一起考慮進去。
div {
background-color: yellow;
}
div {
background-image: url("https://example.com/image.jpg");
}
div {
background-image: url("https://example.com/image.jpg");
background-position: center center;
background-size: 100% 100%;
background-repeat: no-repeat;
}
div {
background-attachment: fixed;
}
div {
background-attachment: scroll;
}
div {
background-attachment: local;
}
block
inline、inline-block
table
none
More: MDN
完全不渲染元素,也不會在文件中佔有位置。
與 display: none 類似,但實際上仍會占有空間。