示:文章尾部有全部源碼,大家可以CTRL + C直接拿走,不用謝我;當然,我還是建議大家都能通過學習自己手寫實現。
使用過CSS transition屬性的童鞋們應該都清楚,當元素在過渡開始或者結束時的高度為auto時,動畫是不生效的;那么如何才能實現元素高度的改變動畫效果呢?本篇文章將為大家提供一個基于Vue3的非常簡潔的解決方案。
要實現高度的改變動畫,我們需要在動畫進行之前為元素設置準確的高度。
當元素由可見變為隱藏狀態時,我們需要先獲取元素的計算高度,將其設置到style屬性上,然后執行一個觸發瀏覽器渲染引擎重繪的動作,然后再將高度設置為0,這樣高度的改變動畫就會正常進行。
當元素由隱藏變為可見狀態時,我們需要先將高度設置為auto,然后獲取元素的計算高度,再將高度設置為0,然后執行一個觸發瀏覽器渲染引擎重繪的動作,然后再將高度設置為計算高度,這樣高度的改變動畫就會正常進行。
現在,根據以上實現原理分析,我們創建一個高度的改變動畫通用組件CollapseTransition.vue。該組件非常簡單,僅需30多行代碼。我幾乎每行代碼都有注釋,大家應該能看懂吧?
<template>
<transition v-bind="listeners">
<!-- 當visible的值發生改變時,過渡組件的監聽器就會觸發 -->
<div v-show="visible" class="x-collapse-transition">
<slot />
</div>
</transition>
</template>
<script setup>
defineProps({ visible: Boolean })
const listeners={
// 元素由隱藏變為可見
onEnter (/** @type {HTMLElement} */ el) {
el.style.height='auto' // 將高度設為auto,是為了獲取該元素的計算高度
const endHeight=window.getComputedStyle(el).height // 計算高度
el.style.height='0px' // 將高度再設置為0
el.offsetHeight // 強制重繪,重繪后再改變高度才會產生動畫
el.style.height=endHeight // 設置為計算高度
},
onAfterEnter (/** @type {HTMLElement} */ el) {
el.style.height=null // 過渡進入之后,將高度恢復為null
},
// 元素由可見變為隱藏
onLeave (/** @type {HTMLElement} */ el) {
el.style.height=window.getComputedStyle(el).height // 計算高度
el.offsetHeight // 強制重繪,重繪后再改變高度才會產生動畫
el.style.height='0px' // 將高度設置為0
},
onAfterLeave (/** @type {HTMLElement} */ el) {
el.style.height=null // 過渡離開之后,將高度恢復為null
}
}
</script>
<style lang="scss">
.x-collapse-transition {
overflow: hidden;
transition: height .22s ease-in-out;
}
</style>
以上就是實現高度的改變動畫的通用組件源碼,童鞋們理解了嗎?是不是非常簡單?現在,我們實現折疊面板組件。使用過element-ui這樣的UI庫的童鞋們應該都知道,折疊面板是由兩個組件折疊面板Collapse和折疊面板項CollapseItem組合而成;
現在,我們先實現CollapseItem.vue組件。為了節省篇幅,我將源碼中的空行全部去掉了,縮進比較規范,自認為可讀性還行;源碼如下,一共30多行,我直接在源碼中添加了注釋,就不過多解釋了。
<template>
<div :class="[cls, { 'is-active': isActive }]">
<div :class="`${cls}_header`" @click="onToggle">
<div :class="`${cls}_title`">
<slot name="title">{{ title }}</slot>
</div>
<i :class="['x-icon-arrow-right', `${cls}_arrow`, { 'is-active': isActive }]"></i>
</div>
<x-collapse-transition :visible="isActive">
<div :class="`${cls}_content`">
<slot />
</div>
</x-collapse-transition>
</div>
</template>
<script setup>
import { computed, inject } from 'vue'
import { COLLAPSE_INJECT_KEY } from '../../constants' // 當它是個字符串常量就行
import { N, S, B } from '../../types' // N: Number, S: String, B: Boolean
import { genKey } from '../../utils' // 生成唯一性的數值key,之前的文章中有源碼
import XCollapseTransition from './CollapseTransition.vue' // 折疊動畫組件
const props=defineProps({
name: [S, N],
title: S,
disabled: B
})
const collapse=inject(COLLAPSE_INJECT_KEY, '') // 注入折疊面板組件提供的一些屬性和方法
const cls='x-collapse-item'
const idKey=computed(()=> props.name || genKey()) // 如果沒提供name,我們生成一個key
const isActive=computed(()=> collapse.includes(idKey.value)) // 是否展開狀態
function onToggle () { // 內容可見時隱藏,隱藏時可見
collapse.updateModel(idKey.value)
}
</script>
這是CollapseItem.vue組件的樣式。
@import './common/var.scss';
.x-collapse-item {
font-size: 13px;
background-color: #fff;
color: $--color-text-primary;
border-bottom: 1px solid $--border-color-lighter;
&:first-child {
border-top: 1px solid $--border-color-lighter;
}
&_header {
display: flex;
align-items: center;
justify-content: space-between;
height: 48px;
cursor: pointer;
}
&_content {
line-height: 1.8;
padding-bottom: 25px;
}
&_arrow {
margin-right: 8px;
transition: transform .2s;
&.is-active {
transform: rotate(90deg);
}
}
}
現在,我們實現Collapse.vue組件。該組件仍然只有30多行,大家理解起來應該很輕松,我就直接在源碼里添加注釋作為講解了;
<template>
<div class="x-collapse">
<slot />
</div>
</template>
<script setup>
import { provide, reactive, ref, watch } from 'vue'
import { COLLAPSE_INJECT_KEY } from '../../constants' // 一個字符串常量
import { S, B, A } from '../../types' // 參看CollapseItem組件
const props=defineProps({
modelValue: [S, A], // Vue3使用modelValue取代了Vue2中的value
accordion: B // 是否手風琴模式,手風琴模式只能有1個面板項是展開狀態
})
const emit=defineEmits(['update:modelValue', 'change'])
function emitValue (v) {
emit('update:modelValue', v) // 與props.modelValue結合實現雙向數據綁定
emit('change', v)
}
const model=ref(props.modelValue)
watch(()=> props.modelValue, v=> model.value=v)
provide(COLLAPSE_INJECT_KEY, { // 提供2個方法用于注入子組件,給子組件調用
includes (v) { // 根據面板的key,判斷是否包含該面板項
return props.accordion ? model.value===v : (model.value || []).includes(v)
},
updateModel (v) { // 更新面板項的內容折疊和展開狀態
const { value }=model
if (props.accordion) {
model.value=value===v ? null : v
emitValue(model.value)
} else {
if (!value) model.value=[]
const index=model.value.indexOf(v)
index > -1 ? model.value.splice(index, 1) : model.value.push(v)
emitValue(model.value)
}
}
})
</script>
以上就是折疊面板組件的實現。包括折疊動畫組件,一共僅需不到150行代碼,是不是非常簡單?童鞋們都理解了嗎?不管有什么疑問,童鞋們都可以問我。感謝閱讀!
CSS 是什么?
CSS是Cascading Style Sheets的簡稱,中文稱為層疊樣式表。
屬性和屬性值用冒號隔開,以分號結尾。
CSS 四種引入方式:
1.行內式
行內式是在標簽的style屬性中設定CSS樣式。
<div style="..."></div>
2.嵌入式
嵌入式是將CSS樣式集中寫在網頁的<head>標簽的<style></style>標簽對中。
<head>
...
<style type="text/css">
...此處寫CSS樣式
</style>
</head>
3.導入式
將一個獨立的.css文件引入HTML文件中,導入式使用@import 引入外部CSS文件,<style>標記也是寫在<head>標記中。
導入式會在整個網頁裝載完后再裝載CSS文件。
<head>
...
<style type="text/css">
@import "My.css"; 此處注意.css文件的路徑
</style>
</head>
4.鏈接式
將一個獨立的.css文件引入到HTML文件中,使用<link>標記寫在<head>標記中。
鏈接式會以網頁文件主體裝載前裝載CSS文件。
<head>
...
<link href="My.css" rel="stylesheet" type="text/css">
</head>
樣式應用順序:
.nick {
color: yellow !important;
}
基本選擇器:
1.通用元素選擇器
* 表示應用到所有的標簽。
* {color: yellow}
2.標簽選擇器
匹配所有使用 div 標簽的元素(可以匹配所有標簽)
div {color: yellow}
3.類選擇器
匹配所有class屬性中包含info的元素。
語法:.類名{樣式}(類名不能以數字開頭,類名要區分大小寫。)
.Mycolor {color: yellow}
<h3 class="Mycolor">nick</h3>
4.ID選擇器
使用id屬性來調用樣式,在一個網頁中id的值都是唯一的(是W3C規范而不是規則,所以不會報錯)。
語法:#ID名{樣式}(ID名不能以數字開頭)
#Mycolor {color: yellow}
<h3 id="Mycolor">Nick.</h3>
組合選擇器:
1.多元素選擇器
同時匹配h3,h4標簽,之間用逗號分隔。
h3,h4 {color: yellow;}
<h3>Nick</h3>
<h4>Jenny</h4>
2.后代元素選擇器
匹配所有div標簽里嵌套的P標簽,之間用空格分隔。
div p {color: yellow;}
<div>
<p>Nick</p>
<div>
<p>Nick</p>
</div>
</div>
3.子元素選擇器
匹配所有div標簽里嵌套的子P標簽,之間用>分隔。
div > p {color: yellow;}
<div>
<p>Nick</p>
<p>Nick</p>
</div>
4.毗鄰元素選擇器
匹配所有緊隨div標簽之后的同級標簽P,之間用+分隔(只能匹配一個)。
div + p {color: yellow;}
<div>Nick</div>
<p>Nick</p>
屬性選擇器:
1.[title] & P[title]
設置所有具有title屬性的標簽元素;
設置所有具有title屬性的P標簽元素。
[title]
{
color: yellow;
}
p[title]
{
color: yellow;
}
<div title>Nick</div>
<p title>Nick</p>
2.[title=Nick]
設置所有title屬性等于“Nick”的標簽元素。
[title="Nick"]
{
color: yellow;
}
<p title="Nick">Nick</p>
3.[title~=Nick]
設置所有title屬性具有多個空格分隔的值、其中一個值等于“Nick”的標簽元素。
[title~="Nick"]
{
color: yellow;
}
<p title="Nick Jenny">Nick</p>
<p title="Jenny Nick">Nick</p>
4.[title|=Nick]
設置所有title屬性具有多個連字號分隔(hyphen-separated)的值、其中一個值以"Nick"開頭的標簽元素。
例:lang屬性:"en"、"en-us"、"en-gb"等等
[title|="Nick"]
{
color: yellow;
}
<p title="Nick-Jenny">Nick</p>
5.[title^=Nick]
設置屬性值以指定值開頭的每個標簽元素。
[title^="Nick"]
{
color: yellow;
}
<p title="NickJenny">Nick</p>
6.[title$=Nick]
設置屬性值以指定值結尾的每個標簽元素。
[title$="Nick"]
{
color: yellow;
}
<p title="JennyNick">Nick</p>
7.[title*=Nick]
設置屬性值中包含指定值的每個元素
[title*="Nick"]
{
color: yellow;
}
<p title="SNickJenny">Nick</p>
偽類選擇器:
1. link、hover、active、visited
a:link{color: black}
a:hover{color: yellow}
a:active{color: blue}
a:visited{color: red}
<a href="#">Nick</a>
2. before、after
p {
color: yellow;
}
p:before{
content: "before...";
}
p:after{
content: "after...";
}
<p> Nick </p>
1. 顏色屬性:
color
transparent
opacity
2. 字體屬性:
font-style: 用于規定斜體文本
font-weight: 設置文本的粗細
font-size: 設置字體的大小
font-family:字體名稱
font:簡寫屬性
3. 文本屬性:
white-space: 設置元素中空白的處理方式
direction: 規定文本的方向
text-align: 文本的水平對齊方式
line-height: 文本行高
vertical-align: 文本所在行高的垂直對齊方式
text-indent: 文本縮進
letter-spacing: 添加字母之間的空白
word-spacing: 添加每個單詞之間的空白
text-transform: 屬性控制文本的大小寫
text-overflow: 文本溢出樣式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<link href="cc2.css" rel="stylesheet" type="text/css">-->
<style>
div {
width: 100px;
height: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<div>索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧 索寧</div>
</body>
</html>
text-decoration: 文本的裝飾
text-shadow:文本陰影
word-wrap:自動換行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
p {
width: 150px;
height: 160px;
background-color: #FFA500;
/*邊框陰影*/
box-shadow: 10px 10px 5px #888;
/*自動換行*/
word-wrap: break-word;
}
h1 {
text-shadow: 5px 5px 5px #888;
}
</style>
</head>
<body>
<p>
When you are old and grey and full of sleep,And nodding by the fire, take down this book,And slowly read, and dream of the soft look
</p>
<h1>索寧</h1>
</body>
</html>
a {
text-decoration: none;
/*去除a標簽下劃線*/
}
4. 背景屬性
background-color: 背景顏色
background-image 設置圖像為背景
background-position 設置背景圖像的位置坐標
background-repeat 設置背景圖像不重復平鋪
background-attachment 背景圖像是否固定或者隨著頁面的其余部分滾動
background 簡寫
5. 列表屬性
list-style-type: 列表項標志的類型
list-style-image:將圖象設置為列表項標志
list-style-position:列表項標志的位置
list-style:縮寫
1. 邊框
border-style:邊框樣式
border-color:邊框顏色
border-width:邊框寬度
border-radius:圓角
border: 簡寫
box-shadow:邊框陰影
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
border:2px solid;
border-radius:25px;
width: 140px;
}
</style>
</head>
<body>
<div>
點贊哦!dear.
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.radius1 {
display: inline-block;
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 20px;
}
.radius2 {
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
border-radius: 20px 35px;
}
.radius3 {
display: inline-block;
width: 100px;
height: 100px;
background-color: blue;
border-radius: 20px 35px 50px;
}
.radius4 {
display: inline-block;
width: 100px;
height: 100px;
background-color: green;
border-radius: 20px 35px 50px 60px;
}
</style>
</head>
<body>
<div>
<span class="radius1"></span>
<span class="radius2"></span>
<span class="radius3"></span>
<span class="radius4"></span>
</div>
</body>
</html>
邊框實現各種三角符號:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.triangle-one {
display: inline-block;
border-top: 50px red solid;
border-right: 50px green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-two {
display: inline-block;
border-top: 0 red solid;
border-right: 50px green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-stree {
display: inline-block;
border-top: 50px red solid;
border-right: 0 green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-four {
display: inline-block;
border-top: 50px red solid;
border-right: 0 green solid;
border-bottom: 0 yellow solid;
border-left: 50px blue solid;
}
.triangle-five {
display: inline-block;
border: 50px transparent solid;
border-top: 50px red solid;
}
.triangle-six {
display: inline-block;
border: 50px transparent solid;
border-bottom: 50px yellow solid;
}
.triangle-seven {
display: inline-block;
border: 50px transparent solid;
border-top: 60px red solid;
border-right: 0;
}
.triangle-eight {
display: inline-block;
border: 50px transparent solid;
border-left: 30px yellow solid;
border-bottom: 0;
}
</style>
</head>
<body>
<div class="triangle-one"></div>
<div class="triangle-two"></div>
<div class="triangle-stree"></div>
<div class="triangle-four"></div>
<div class="triangle-five"></div>
<div class="triangle-six"></div>
<div class="triangle-seven"></div>
<div class="triangle-eight"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.back {
width: 1000px;
height: 1000px;
margin: 0 auto;
background-color: #ddd;
position: relative;
}
.back-in {
position: absolute;
width: 1020px;
height: 45px;
left: -20px;
top: 50px;
background-color: #2F4F4F;
}
.back-img {
border: 20px solid transparent;
border-top: 10px solid dimgrey;
border-right: 0;
display: inline-block;
position: absolute;
top: 95px;
left: -20px;
}
.back-font {
line-height: 9px;
margin-left: 30px;
color: white;
}
</style>
</head>
<body>
<div class="back">
<div class="back-in"><h3 class="back-font">妹子求關注 ^.^</h3></div>
<div class="back-img"></div>
</div>
</body>
</html>
2.★ 盒子模型
一個標準的盒子模型:
padding:用于控制內容與邊框之間的距離;
margin: 用于控制元素與元素之間的距離;
一個參數,應用于四邊。
兩個參數,第一個用于上、下,第二個用于左、右。
三個參數,第一個用于上,第二個用于左、右,第三個用于下。
邊框在默認情況下會定位于瀏覽器窗口的左上角,但是并沒有緊貼著瀏覽器的窗口的邊框,這是因為body本身也是一個盒子,外層還有html,
在默認情況下,body距離html會有若干像素的margin,所以body中的盒子不會緊貼瀏覽器窗口的邊框了。
解決方法:
body {
margin: 0;
}
3.★ display
4. visibility
5.★ float 浮動
讓一行顯示兩個塊級標簽,會脫離文檔流
clear 清除浮動:
6. clip 剪裁圖像
rect 剪裁定位元素:
7. overflow 設置當對象的內容超過其指定高度及寬度時如何顯示內容
8.★ position 規定元素的定位類型
9. z-index 元素層疊順序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.z-index1 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
z-index: -1;
}
.z-index2 {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
top: 20px;
left: 20px;
z-index: 5;
}
</style>
</head>
<body>
<div class="z-index1"></div>
<div class="z-index2"></div>
</body>
</html>
10. outline 邊框輪廓
11. zoom 縮放比例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.zoom1 {
zoom: 100%;
}
.zoom2 {
zoom: 150%;
}
.zoom3 {
zoom: 200%;
}
</style>
</head>
<body>
<div class="zoom1">Nick 100%</div>
<div class="zoom2">Nick 200%</div>
<div class="zoom3">Nick 300%</div>
</body>
</html>
12. cursor 鼠標的類型形狀
鼠標放在以下單詞上,There will be a miracle:
url: 自定義光標
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<link href="cc2.css" rel="stylesheet" type="text/css">-->
<style>
body {
cursor: url("mouse.png"), auto;
/*圖片地址:http://images.cnblogs.com/cnblogs_com/suoning/845162/o_mouse.png*/
}
</style>
</head>
<body>
<div><img src="http://images.cnblogs.com/cnblogs_com/suoning/845162/o_ns.png" height="100%" width="100%"></div>
</body>
</html>
Auto: 默認
Default: 默認
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
Crosshair
Pointer
Move
text
wait
help
not-allowed
13. transform、transition 動畫效果
transform 轉換,變形
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nick</title>
<meta charset="utf-8" />
<style type="text/css">
div {
border: 1px solid black;
height: 30px;
width: 30px;
background-color: yellow;
/*transform-origin: 50px 50px;*/
transform-origin: left;
transform: rotate(50deg);
/*transform: skew(50deg,50deg);*/
/*transform: translate(50px,50px);*/
/*transform: scale(2);*/
}
</style>
</head>
<body>
<div></div>
</body>
</html>
Transition 平滑過渡
#property 指定屬性對應類型
1、color: 通過紅、綠、藍和透明度組件變換(每個數值單獨處理),如:background-color,border-color,color,outline-color等CSS屬性;
2、length:真實的數字,如:word-spacing,width,vertical- align,top,right,bottom,left,padding,outline-width,margin,min-width,min- height,max-width,max-height,line-height,height,border-width,border- spacing,background-position等屬性;
3、percentage:真實的數字,如:word-spacing,width,vertical- align,top,right,bottom,left,min-width,min- height,max-width,max-height,line-height,height,background-position等屬性;
4、integer 離散步驟(整個數字),在真實的數字空間,以及使用floor()轉換為整數時發生,如:outline-offset,z-index等屬性;
5、number真實的(浮點型)數值,如:zoom,opacity,font-weight等屬性;
6、transform list。
7、rectangle:通過x、 y、 width和height(轉為數值)變換,如:crop;
8、visibility:離散步驟,在0到1數字范圍之內,0表示“隱藏”,1表示完全“顯示”,如:visibility;
9、shadow:作用于color、x、y、和blur(模糊)屬性,如:text-shadow;
10、gradient:通過每次停止時的位置和顏色進行變化。它們必須有相同的類型(放射狀的或是線性的)和相同的停止數值以便執行動畫,如:background-image;
11、paint server (SVG):只支持下面的情況:從gradient到gradient以及color到color,然后工作與上面類似;
12、space-separated list of above:如果列表有相同的項目數值,則列表每一項按照上面的規則進行變化,否則無變化;
13、a shorthand property:如果縮寫的所有部分都可以實現動畫,則會像所有單個屬性變化一樣變化。
#支持執行transition效果的屬性
Property Name Type
background-color as color
background-position as repeatable list of simple list of length, percentage, or calc
border-bottom-color as color
border-bottom-width as length
border-left-color as color
border-left-width as length
border-right-color as color
border-right-width as length
border-spacing as simple list of length
border-top-color as color
border-top-width as length
bottom as length, percentage, or calc
clip as rectangle
color as color
font-size as length
font-weight as font weight
height as length, percentage, or calc
left as length, percentage, or calc
letter-spacing as length
line-height as either number or length
margin-bottom as length
margin-left as length
margin-right as length
margin-top as length
max-height as length, percentage, or calc
max-width as length, percentage, or calc
min-height as length, percentage, or calc
min-width as length, percentage, or calc
opacity as number
outline-color as color
outline-width as length
padding-bottom as length
padding-left as length
padding-right as length
padding-top as length
right as length, percentage, or calc
text-indent as length, percentage, or calc
text-shadow as shadow list
top as length, percentage, or calc
vertical-align as length
visibility as visibility
width as length, percentage, or calc
word-spacing as length
z-index as integer
鼠標放在以下圖片上,There will be a miracle:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nick</title>
<meta charset="utf-8" />
<style type="text/css">
.img-see-2016-7-2 {
background-image: url("http://images.cnblogs.com/cnblogs_com/suoning/845162/o_sea.jpg");
background-size: 660px;
background-repeat: no-repeat;
height: 300px;
width: 600px;
transition-duration: 30s;
transition-timing-function: ease;
transition-property: background-size;
}
.img-see-2016-7-2:hover {
background-size: 2000px;
}
</style>
</head>
<body>
<div class="img-see-2016-7-2"></div>
</body>
</html>
作者:suoning
原文鏈接:https://www.cnblogs.com/suoning/p/5625582.html
<h1 style=" width:300px;"> </h1>
內部樣式要寫在<head></head>標簽之間
<style type=" text/css"> 樣式</style>
<link href=" 樣式表位置" rel=" stylesheet" type=" text/css">
link這個也要放在<head></head>之間
第一種:
<style type=" text/css">
@import " 直接寫css文件路徑"
</style>
第二種:
<style type=" text/css">
@import url(寫樣式表文件路徑)
</style>
一、選擇器
1.派生選擇器
例如:(選擇div里面的span)
div span{給樣式}
<div>
<span>哈哈哈</span>
</div>
2.還有
列如:(p和a 都選擇)
p,a{給樣式}
<p>你好1</p>
<a>你好</a>
二、
背景顏色:
background-color: #ccc;
background意思是背景
color 意思是顏色
背景圖片:
background-image: url(寫圖片路徑);
background-repeat: repeat-y;
repeat 意思是重復平鋪
repeat-y 意思是按y軸平鋪
repeat-x 意思是按x軸平鋪
background-repeat:no-repeat 意思是不平鋪
background-position(設置背景圖像在網頁上的位置。)
background-position:center;(意思是定位圖片的位置)
1.center(居中)
2.top (上)
3.right (右)
4.left (左)
5.bottom (下)
background-position:50% 50%;(這種方法也可以使背景圖片居中)
background-attachment:fixed(使背景固定不隨滾動條走)
fixed(意思是固定的)
attachment(意思是附在那里的意思)
background-size (規定背景圖片的尺寸是css3的)
size 意思是尺寸
屬性: 1.contain
(把圖像圖像擴展至最大尺寸,以使其寬度和高度完全適應內容區域 )
意思就是按等比例縮放填滿區域
2.cover(鋪滿區域)
3.percentage(以父元素的百分比來設置背景圖像的寬度和高度)
注意:如果只設置一個值,則第二個值會被設置為 "auto"。
三、字體
這里要注意引號嵌套時外層要用雙引號內層要用單引號
font-family:"微軟雅黑","字體二","字體三"........;(定義字體)
定義多個字體的好處就是系統會默認從第一個字體開始找,如果系統沒有這個字體則默認使用瀏覽器設定的字體
font-style:normal;(定義字體如何顯示)
normal(是正常顯示)
italic(文本傾斜顯示)
font-weight:900; (設置文本的粗細)
weight(意思是重量的意思)
默認值:normal 等于400
還可以設置為 bold 等同于700
注意:設置文本粗細的值為: 100 ~ 900 為字體指定了 9 級加粗度
font-size:50px;(設置文字的大小)
單位有:px em
1em=16像素
2em 等于當前字體尺寸的兩倍。
四、文本
text-indent:;
text(文本)
indent(縮進)
單位:em
px
注意:
可以設置為負值。利用這種技術,可以實現很多有趣的效果,比如“懸掛縮進”,即第一行懸掛在元素中余下部分的左邊:
text-align (用于文本的對齊)
align(意思是排列)
后面的屬性有:1.left(左對齊)
2.right(右對齊)
3.center(居中)
4.justify(像word文檔里面的兩端對齊)
word-spacing(設置改變字(單詞)之間的標準間隔)
spacing(間隔)
單位: em
px
使用這個無論是詞語還是文字都必須有空格才能實現效果。
列如:
(你好 歡迎你來到 我的工作室。)
text-decoration(一般使用于去掉下劃線)
decoration(裝飾的意思)
屬性:1.none 去掉下劃線
2.underline (添加下劃線)
3.overline (添加上面劃線)
4.line-through (穿過一條線,一般多用于打折的價格劃一條線)
5.blink (閃爍的)
line-height(用于設置行高)
letter-spacing 字母間隔
五、a標簽的一個樣式
a:hover (鼠標指針位于鏈接的上方)
用法:<style>
a:hover{
font-size:20px;
}
</style>
當鼠標滑上去的時候字體變大20px。
六、列表
列表樣式:
如何去掉無序列表前面的小圓點:
list-style:none;
七、表格:
table, th, td
{
border: 1px solid blue;
}
border:邊界 邊框
1px 定義邊框的粗細
solid 定義實線
blue 定義顏色
border-collapse:collapse;(將邊框折疊為單一邊框)
border-spacing:50px;(將格與格之間的間距拉開,
但是是邊框線沒有合并的情況下才能生效)
八、行內元素轉換為塊元素
display:block;
block 使其轉化為塊元素
inline 使其轉化為行內元素
九、外邊距與內邊距
內邊距:
padding-top (上)
padding-right (右)
padding-bottom (下)
padding-left (左)
外邊距:
margin-top (上)
margin-right (右)
margin-bottom (下)
margin-left (左)
注意:與其到底是使用外邊距還是內邊距依據誰對誰而定!!
十、相對定位:(相對定位雖然元素離開原來的位置,但是不釋放原來的空間)
position: relative;
left: 30px;
top: 20px;
十一、絕對定位:(絕對定位元素離開,并且釋放原來的空間)
position: absolute;
left: 30px;
top: 20px;
為了瀏覽器適應,所以使用絕對居中時上面一定要寫一個相對定位作為參照。
十二、overflow:溢出部分的處理
scroll(卷簾) 出現滾動條
hidden 隱藏
auto 自動出現滾動條
偽類選擇器:
#a ul li:first-child{
margin-left:0px;
}
first-child(第一個孩子的意思)
以上這段代碼便是找到類a里面的ul 里面的Li 將第一個li標簽的外左間距去掉。
last-child(最后一個)
#a ul li:nth-child(1){
括號里面是幾選擇的就是第幾個
如果說三的倍數就是寫 3N
}
十三、CSS樣式的權重問題:
ID>Class>標簽本身(屬性選擇器)
十四、相對定位絕對定位的理解:
絕對定位:將被賦予此定位方法的對象從文檔流中拖出,使用left,right,top,bottom等屬性相對于其最接近的一個最有定位設置的父級對象進行絕對定位,如果對象的父級沒有設置定位屬性,即還是遵循HTML定位規則的,則依據body對象左上角作為參考進行定位。
相對定位(relative):對象不可層疊,依據left,right,top,bottom等屬性在正常文檔流中偏移自身位置。
且不釋放原來的位置。依據當前位置進行定位。
十五、邊邊陰影:
box-shadow(給邊框陰影)
div
{
box-shadow: 10px 10px 5px #888888;
陰影水平偏移值(可取正負值);陰影垂直偏移值(可取正負值);陰影模糊值;陰影顏色
}
給邊框切圓角:
border-radius:4px;
注意這里面的值取偶不取奇
相關屬性: border-top-right-radius ,
border-bottom-right-radius ,
border-bottom-left-radius ,
border-top-left-radius
文本溢出時顯示為省略號(..........)
text-overflow :ellipsis
resize:none;
十六、input點擊以后藍色邊框的去除:
outline:none; 去除input的藍色的那個線
十七、 ul ol dl 默認都有一個外邊距:
給他們所有的margin:0;padding:0;
*請認真填寫需求信息,我們會在24小時內與您取得聯系。