Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
CSS 是什么?
CSS是Cascading Style Sheets的簡(jiǎn)稱,中文稱為層疊樣式表。
屬性和屬性值用冒號(hào)隔開,以分號(hào)結(jié)尾。
CSS 四種引入方式:
1.行內(nèi)式
行內(nèi)式是在標(biāo)簽的style屬性中設(shè)定CSS樣式。
<div style="..."></div>
2.嵌入式
嵌入式是將CSS樣式集中寫在網(wǎng)頁的<head>標(biāo)簽的<style></style>標(biāo)簽對(duì)中。
<head>
...
<style type="text/css">
...此處寫CSS樣式
</style>
</head>
3.導(dǎo)入式
將一個(gè)獨(dú)立的.css文件引入HTML文件中,導(dǎo)入式使用@import 引入外部CSS文件,<style>標(biāo)記也是寫在<head>標(biāo)記中。
導(dǎo)入式會(huì)在整個(gè)網(wǎng)頁裝載完后再裝載CSS文件。
<head>
...
<style type="text/css">
@import "My.css"; 此處注意.css文件的路徑
</style>
</head>
4.鏈接式
將一個(gè)獨(dú)立的.css文件引入到HTML文件中,使用<link>標(biāo)記寫在<head>標(biāo)記中。
鏈接式會(huì)以網(wǎng)頁文件主體裝載前裝載CSS文件。
<head>
...
<link href="My.css" rel="stylesheet" type="text/css">
</head>
樣式應(yīng)用順序:
.nick {
color: yellow !important;
}
基本選擇器:
1.通用元素選擇器
* 表示應(yīng)用到所有的標(biāo)簽。
* {color: yellow}
2.標(biāo)簽選擇器
匹配所有使用 div 標(biāo)簽的元素(可以匹配所有標(biāo)簽)
div {color: yellow}
3.類選擇器
匹配所有class屬性中包含info的元素。
語法:.類名{樣式}(類名不能以數(shù)字開頭,類名要區(qū)分大小寫。)
.Mycolor {color: yellow}
<h3 class="Mycolor">nick</h3>
4.ID選擇器
使用id屬性來調(diào)用樣式,在一個(gè)網(wǎng)頁中id的值都是唯一的(是W3C規(guī)范而不是規(guī)則,所以不會(huì)報(bào)錯(cuò))。
語法:#ID名{樣式}(ID名不能以數(shù)字開頭)
#Mycolor {color: yellow}
<h3 id="Mycolor">Nick.</h3>
組合選擇器:
1.多元素選擇器
同時(shí)匹配h3,h4標(biāo)簽,之間用逗號(hào)分隔。
h3,h4 {color: yellow;}
<h3>Nick</h3>
<h4>Jenny</h4>
2.后代元素選擇器
匹配所有div標(biāo)簽里嵌套的P標(biāo)簽,之間用空格分隔。
div p {color: yellow;}
<div>
<p>Nick</p>
<div>
<p>Nick</p>
</div>
</div>
3.子元素選擇器
匹配所有div標(biāo)簽里嵌套的子P標(biāo)簽,之間用>分隔。
div > p {color: yellow;}
<div>
<p>Nick</p>
<p>Nick</p>
</div>
4.毗鄰元素選擇器
匹配所有緊隨div標(biāo)簽之后的同級(jí)標(biāo)簽P,之間用+分隔(只能匹配一個(gè))。
div + p {color: yellow;}
<div>Nick</div>
<p>Nick</p>
屬性選擇器:
1.[title] & P[title]
設(shè)置所有具有title屬性的標(biāo)簽元素;
設(shè)置所有具有title屬性的P標(biāo)簽元素。
[title]
{
color: yellow;
}
p[title]
{
color: yellow;
}
<div title>Nick</div>
<p title>Nick</p>
2.[title=Nick]
設(shè)置所有title屬性等于“Nick”的標(biāo)簽元素。
[title="Nick"]
{
color: yellow;
}
<p title="Nick">Nick</p>
3.[title~=Nick]
設(shè)置所有title屬性具有多個(gè)空格分隔的值、其中一個(gè)值等于“Nick”的標(biāo)簽元素。
[title~="Nick"]
{
color: yellow;
}
<p title="Nick Jenny">Nick</p>
<p title="Jenny Nick">Nick</p>
4.[title|=Nick]
設(shè)置所有title屬性具有多個(gè)連字號(hào)分隔(hyphen-separated)的值、其中一個(gè)值以"Nick"開頭的標(biāo)簽元素。
例:lang屬性:"en"、"en-us"、"en-gb"等等
[title|="Nick"]
{
color: yellow;
}
<p title="Nick-Jenny">Nick</p>
5.[title^=Nick]
設(shè)置屬性值以指定值開頭的每個(gè)標(biāo)簽元素。
[title^="Nick"]
{
color: yellow;
}
<p title="NickJenny">Nick</p>
6.[title$=Nick]
設(shè)置屬性值以指定值結(jié)尾的每個(gè)標(biāo)簽元素。
[title$="Nick"]
{
color: yellow;
}
<p title="JennyNick">Nick</p>
7.[title*=Nick]
設(shè)置屬性值中包含指定值的每個(gè)元素
[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: 用于規(guī)定斜體文本
font-weight: 設(shè)置文本的粗細(xì)
font-size: 設(shè)置字體的大小
font-family:字體名稱
font:簡(jiǎn)寫屬性
3. 文本屬性:
white-space: 設(shè)置元素中空白的處理方式
direction: 規(guī)定文本的方向
text-align: 文本的水平對(duì)齊方式
line-height: 文本行高
vertical-align: 文本所在行高的垂直對(duì)齊方式
text-indent: 文本縮進(jìn)
letter-spacing: 添加字母之間的空白
word-spacing: 添加每個(gè)單詞之間的空白
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:自動(dòng)換行
<!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;
/*自動(dòng)換行*/
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標(biāo)簽下劃線*/
}
4. 背景屬性
background-color: 背景顏色
background-image 設(shè)置圖像為背景
background-position 設(shè)置背景圖像的位置坐標(biāo)
background-repeat 設(shè)置背景圖像不重復(fù)平鋪
background-attachment 背景圖像是否固定或者隨著頁面的其余部分滾動(dòng)
background 簡(jiǎn)寫
5. 列表屬性
list-style-type: 列表項(xiàng)標(biāo)志的類型
list-style-image:將圖象設(shè)置為列表項(xiàng)標(biāo)志
list-style-position:列表項(xiàng)標(biāo)志的位置
list-style:縮寫
1. 邊框
border-style:邊框樣式
border-color:邊框顏色
border-width:邊框?qū)挾?/strong>
border-radius:圓角
border: 簡(jiǎn)寫
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>
點(diǎn)贊哦!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>
邊框?qū)崿F(xiàn)各種三角符號(hào):
<!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">妹子求關(guān)注 ^.^</h3></div>
<div class="back-img"></div>
</div>
</body>
</html>
2.★ 盒子模型
一個(gè)標(biāo)準(zhǔn)的盒子模型:
padding:用于控制內(nèi)容與邊框之間的距離;
margin: 用于控制元素與元素之間的距離;
一個(gè)參數(shù),應(yīng)用于四邊。
兩個(gè)參數(shù),第一個(gè)用于上、下,第二個(gè)用于左、右。
三個(gè)參數(shù),第一個(gè)用于上,第二個(gè)用于左、右,第三個(gè)用于下。
邊框在默認(rèn)情況下會(huì)定位于瀏覽器窗口的左上角,但是并沒有緊貼著瀏覽器的窗口的邊框,這是因?yàn)閎ody本身也是一個(gè)盒子,外層還有html,
在默認(rèn)情況下,body距離html會(huì)有若干像素的margin,所以body中的盒子不會(huì)緊貼瀏覽器窗口的邊框了。
解決方法:
body {
margin: 0;
}
3.★ display
4. visibility
5.★ float 浮動(dòng)
讓一行顯示兩個(gè)塊級(jí)標(biāo)簽,會(huì)脫離文檔流
clear 清除浮動(dòng):
6. clip 剪裁圖像
rect 剪裁定位元素:
7. overflow 設(shè)置當(dāng)對(duì)象的內(nèi)容超過其指定高度及寬度時(shí)如何顯示內(nèi)容
8.★ position 規(guī)定元素的定位類型
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 鼠標(biāo)的類型形狀
鼠標(biāo)放在以下單詞上,There will be a miracle:
url: 自定義光標(biāo)
<!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: 默認(rèn)
Default: 默認(rèn)
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 動(dòng)畫效果
transform 轉(zhuǎn)換,變形
<!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 指定屬性對(duì)應(yīng)類型
1、color: 通過紅、綠、藍(lán)和透明度組件變換(每個(gè)數(shù)值單獨(dú)處理),如:background-color,border-color,color,outline-color等CSS屬性;
2、length:真實(shí)的數(shù)字,如: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:真實(shí)的數(shù)字,如:word-spacing,width,vertical- align,top,right,bottom,left,min-width,min- height,max-width,max-height,line-height,height,background-position等屬性;
4、integer 離散步驟(整個(gè)數(shù)字),在真實(shí)的數(shù)字空間,以及使用floor()轉(zhuǎn)換為整數(shù)時(shí)發(fā)生,如:outline-offset,z-index等屬性;
5、number真實(shí)的(浮點(diǎn)型)數(shù)值,如:zoom,opacity,font-weight等屬性;
6、transform list。
7、rectangle:通過x、 y、 width和height(轉(zhuǎn)為數(shù)值)變換,如:crop;
8、visibility:離散步驟,在0到1數(shù)字范圍之內(nèi),0表示“隱藏”,1表示完全“顯示”,如:visibility;
9、shadow:作用于color、x、y、和blur(模糊)屬性,如:text-shadow;
10、gradient:通過每次停止時(shí)的位置和顏色進(jìn)行變化。它們必須有相同的類型(放射狀的或是線性的)和相同的停止數(shù)值以便執(zhí)行動(dòng)畫,如:background-image;
11、paint server (SVG):只支持下面的情況:從gradient到gradient以及color到color,然后工作與上面類似;
12、space-separated list of above:如果列表有相同的項(xiàng)目數(shù)值,則列表每一項(xiàng)按照上面的規(guī)則進(jìn)行變化,否則無變化;
13、a shorthand property:如果縮寫的所有部分都可以實(shí)現(xiàn)動(dòng)畫,則會(huì)像所有單個(gè)屬性變化一樣變化。
#支持執(zhí)行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
鼠標(biāo)放在以下圖片上,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
頁CSS的垂直居中需求始終沒有停過,而其困難度也始終沒有讓人輕松過,經(jīng)過了每位開發(fā)先烈的研究后,據(jù)說CSS的垂直居中技巧已達(dá)到近十種之多,但始終鮮為人知,部分公司甚至將CSS的垂直居中技巧當(dāng)成面試題,其重要性可見一斑。
在不斷探索,發(fā)現(xiàn)中,CSS的垂直居中的方式竟然能達(dá)到23種,大家可以看看自己熟知的是哪幾種?
1、Line-height
適用情景:?jiǎn)涡形淖执怪本又屑记?/p>
這個(gè)方式應(yīng)該是最多人知道的了,常見于單行文字的應(yīng)用,像是按鈕這一類對(duì)象,或者是下拉框、導(dǎo)航此類元素最常見到的方式了。此方式的原理是在于將單行文字的行高設(shè)定后,文字會(huì)位于行高的垂直中間位置,利用此原理就能輕松達(dá)成垂直居中的需求了。
<div class="content">web前端開發(fā)</div> .content{ width: 400px; background: #ccc; line-height:100px; margin: auto; }
2、Line-height + inline-block
適用情景:多對(duì)象的垂直居中技巧
既然可以使用第一種方式對(duì)行元素達(dá)成垂直居中的話,當(dāng)然沒有理由不能做到多行啊~但是你需要將多個(gè)元素或多行元素當(dāng)成一個(gè)行元素來看待,所以我們必須要將這些數(shù)據(jù)多包一層,并將其設(shè)定為inline-block,并在該inline-block對(duì)象的外層對(duì)象使用inline-block來代替height的設(shè)置,如此便可以達(dá)到垂直居中的目的了,從使你的數(shù)據(jù)是包含了標(biāo)題跟內(nèi)容在內(nèi)也可以正常的垂直居中了。
<h2>line-height + inline-block</h2><div class="box box2"> <div class="content"><a href="#">web前端開發(fā)</a></div></div> .box{ width: 500px; border: 1px solid #f00; margin: auto; line-height: 200px; text-align: center; }.box2 .content{ display: inline-block; height: auto; line-height:1; width: 400px; background: #ccc; }
3、:before + inline-block
適用情景:多對(duì)象的CSS垂直居中技巧
:before 偽類元素搭配 inline-block 屬性的寫法應(yīng)該是很傳統(tǒng)的垂直居中的技巧了,此方式的好處在于子元素居中可以不需要特別設(shè)定高度,我們將利用:before偽類元素設(shè)定為100%高的inline-block,再搭配上將需要居中的子元素同樣設(shè)置成inline-block性質(zhì)后,就能使用vertical-align:middle來達(dá)到垂直居中的目的了,此方式在以往其實(shí)是個(gè)非常棒的垂直居中解決方案,唯獨(dú)需要特別處理掉inline-block元素之間的4-5px空間這個(gè)小缺陷,但也很實(shí)用了。
<h2>3.:before + inline-block</h2><div class="box box3"> <div class="content"></div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; text-align: center; }.box::before{ content:''; display: inline-block; height: 100%; width: 0; vertical-align: middle; }.box .content{ width: 400px; background: #ccc; display: inline-block; vertical-align: middle; }
4、absolute + margin 負(fù)值
適用情景:多行文字的垂直居中技巧
誰說絕對(duì)定位要少用?Amos認(rèn)為沒有少用多用的問題,重點(diǎn)在于你是否有妥善運(yùn)用才是重點(diǎn),絕對(duì)定位在這個(gè)例子中會(huì)設(shè)置top:50%來抓取空間高度的50%,接著在將居中元素的margin-top設(shè)定為負(fù)一半的高度,這樣就能讓元素居中了,此方法可是自古以來流傳多年的居中方式呢?
<h2>4.absolute + margin 負(fù)值</h2><div class="box box4"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; position: relative; }.box4 .content{ width: 400px; background: #ccc; height: 70px; position: absolute; top:50%; left: 50%; margin-left: -200px; margin-top: -35px; }
5、absolute + margin auto
適用情景:多行文字的垂直居中技巧
又一個(gè)絕對(duì)定位的垂直居中的方案,這個(gè)方式比較特別一點(diǎn),當(dāng)元素設(shè)置為絕對(duì)定位后,假設(shè)它是抓不到整體可運(yùn)用的空間范圍,所以margin:auto會(huì)失效,但當(dāng)你設(shè)置了top:0;bottom:0;時(shí),絕對(duì)定位元素就抓到了可運(yùn)用的空間了,這時(shí)你的margin:auto就生效了(神奇吧),如果你的絕對(duì)定位元素需要水平居中于父層,那你同樣可以設(shè)定left:0;right:0;來讓絕對(duì)定位元素取得空間可運(yùn)用范圍,再讓marign-left與margin-right設(shè)定為auto即可居中。但此方式的缺點(diǎn)是你的定位元素必須有固定的寬高(百分比也算)才能正常居中。
<h2>5.absolute + translate(-50%, -50%)</h2><div class="box box5"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; position: relative; }.content{ width: 400px; background: #ccc; height: 70px; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; }
6、absolute + translate
適用情景:多行文字的垂直居中技巧
在一個(gè)絕對(duì)定位居中的方式,此方式應(yīng)該算是最方便的了,因?yàn)榇司又械亩ㄎ辉夭恍枰潭ǖ膶捀撸覀兝媒^對(duì)定位時(shí)的top 與right設(shè)置元素的上方跟左方各為50%,再利用translate(-50%,-50%)位移居中元素自身寬與高的50%就能達(dá)成居中的目的了。(css3好棒)
<h2>6.absolute + margin: auto</h2><div class="box box6"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; position: relative; }.box5 .content{ width: 400px; background: #ccc; position: absolute; top:50%; left: 50%; transform: translate(-50%, -50%); }
7、Flex + align-items
適用情景:多行文字的垂直居中技巧
Flex!這東西自從面世之后就不斷的考驗(yàn)網(wǎng)頁開發(fā)者的良心,到底要不要拋棄float擁抱flex,我想這答案人人心中自由一把尺,但先碰Flex再碰Float可謂先甜后苦,這順序到底要倒吃甘蔗還是正吃甘蔗是實(shí)在難說,自從有了Flex之后,小孩考試一百分,設(shè)計(jì)網(wǎng)頁不跑版,客戶網(wǎng)頁都RWD,老板賺錢好開心,我也加薪(加班)好甘心,不由的說Flex真的是一個(gè)神物,我們只要設(shè)定父層display:flex以及設(shè)定次軸(cross axis)屬性align-items:center 就好了(說那么多結(jié)果重點(diǎn)就一行字是哪招啦),這個(gè)方式的優(yōu)點(diǎn)是此層不需要設(shè)定高度即可自動(dòng)居中,且原始代碼干凈無比,真的是用一次就讓你升天啦。
<h2>7.Flex + align-items</h2><div class="box box7"> <div class="content">web前端開發(fā) </div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; justify-content: center; align-items: center; }.content{ width: 400px; background: #ccc; }
8、Flex + :before + flex-grow
適用情景:多行文字的垂直居中技巧
Flex有多種方式可以讓你把數(shù)據(jù)居中,適用Flex-grow的延展特性來達(dá)成,這個(gè)例子中Amos適用了flex-direction:column直式排法,搭配:before偽元素適用flex-grow伸展值能夠取得剩下所有空間的特性,把它設(shè)定成一半的剩余空間就能做到把內(nèi)容數(shù)據(jù)準(zhǔn)確的推到垂直中間位置,算是個(gè)傳統(tǒng)技法的延伸方式。這樣的話上面第七個(gè)方式不是比較快?
<h2>8.Flex + before + flex-grow</h2><div class="box box8"> <div class="content">web前端開發(fā)</div></div>
h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; flex-direction: column; align-items: center; }.box:before{ content: ''; flex-grow: .5; }.content{ width: 400px; background: #ccc; }
9、Flex + margin
適用情景:多行文字的垂直居中技巧
繼續(xù)用Flex來居中,由于Flex元素對(duì)空間解讀的特殊性,我們只要在父層元素設(shè)定display:flex,接著在需要垂直居中的元素上設(shè)定margin:auto,即可自動(dòng)居中
<h2>9.Flex + margin</h2><div class="box box9"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; }.content{ width: 400px; background: #ccc; margin: auto; }
10、Flex + align-self
適用情景:多行文字的垂直居中技巧
align-self 應(yīng)該大家都不陌生,基本上就是對(duì)flex次軸cross axis 的個(gè)別對(duì)齊方式只要對(duì)單一子層元素設(shè)定align-self:center就能達(dá)成垂直居中的目的了。
<h2>10.Flex + align-self</h2><div class="box box10"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; justify-content: center; }.content{ width: 400px; background: #ccc; align-self: center }
11、Flex + align-content
適用情景:多行文字的垂直居中技巧
在正常的狀況下,align-content 僅能對(duì)次軸多行flex item做居中,但是當(dāng)我今天子層元素不確定有多少個(gè)時(shí),且有時(shí)可能會(huì)有單個(gè)的情況出現(xiàn)時(shí),此技巧就能用到了(當(dāng)然你也能有其他解法),既然是多行子元素才能用,那我們就為單個(gè)子組件多加兩個(gè)兄弟吧,使用:before以及:after 來讓子元素增加到多個(gè),這樣就能使用flex的align-content屬性來居中
<h2>11.Flex + align-content</h2><div class="box box11"> <div class="content">web前端開發(fā) </div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: flex; flex-wrap: wrap; justify-content: center; align-content: center; }.content{ width: 400px; background: #ccc; }.box11:before,.box11:after{ content: ''; display: block; width:100%; }
12、Grid + template
適用情景:多行文字的垂直居中技巧
CSS Grid最令人驚訝的就是這個(gè)template的功能了,簡(jiǎn)直就是把塊元素當(dāng)畫布在使用,我們僅需要把模板設(shè)置成三列,就能搞定垂直居中了
<h2>12.Grid + template</h2><div class="box box12"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; grid-template-rows: 1fr auto 1fr; grid-template-columns: 1fr auto 1fr; grid-template-areas: '. . .' '. amos .' '. . .'; }.content{ width: 400px; background: #ccc; grid-area: amos; }
13、Grid + align-items
適用情景:多行文字的垂直居中技巧
align-items不僅是Flex可用,連CSS Grid也擁有此屬性可使用,但在Flex中align-items是針對(duì)次軸cross axis作對(duì)齊,而在CSS Grid中則是針對(duì)Y軸做對(duì)齊,你可以把它想象成是表格中儲(chǔ)存單元格的vertical-align屬性看待,就可以很好理解了
<h2>13.Grid + align-items</h2><div class="box box13"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; justify-content: center; align-items: center; }.content{ width: 400px; background: #ccc; }
14、Grid + align-content
適用情景:杜航文字的垂直居中技巧
CSS Grid的align-content跟Flex的align-content有點(diǎn)差異,CSS Grid對(duì)于空間的解釋會(huì)跟Flex有一些些的落差,所以導(dǎo)致align-content在Flex中僅能針對(duì)多行元素起作用,但在Grid中就沒這個(gè)問題,所以我們可以很開心的使用align-content來對(duì)子元素做垂直居中,絲毫不費(fèi)力氣
<h2>14.Grid + align-content</h2><div class="box box14"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; justify-content: center; align-content: center; }.content{ width: 400px; background: #ccc; }
15、Grid + align-self
適用情景:多行文字的垂直居中技巧
align-self 應(yīng)該大家都不陌生,基本上就是對(duì)grid Y軸的個(gè)別對(duì)齊方式,只要對(duì)單一子層元素設(shè)置為align-self:center就能達(dá)成垂直居中的目的了
<h2>15.Grid + align-self</h2><div class="box box15"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; justify-content: center; }.content{ width: 400px; background: #ccc; align-self: center; } <h2>16.Grid + place-items</h2><div class="box box16"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; height: 150px; margin: 0 auto; place-items: center; }.content{ width: 400px; background: #ccc; }
17、Grid + place-content
適用情景:多行文字的垂直居中技巧
place-content這屬性有多少人用過,此屬性是align-content與justify-content的縮寫,簡(jiǎn)單的說就是水平與垂直的對(duì)齊方式,想當(dāng)然的,設(shè)置center就能居中了
<h2>17.Grid + place-content</h2><div class="box box17"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; height: 150px; margin: 0 auto; place-content: center; }.content{ width: 400px; background: #ccc; }
18、Grid + margin
適用情景:多行文字的垂直居中技巧
繼續(xù)用Grid來居中,由于Grid元素對(duì)空間解讀的特殊性,我們只要在父層元素設(shè)定display:grid,接著在需要垂直居中的元素上設(shè)置margin:auto即可自動(dòng)居中。怎么這描述似曾相識(shí)。
<h2>18.Grid + margin</h2><div class="box box18"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; display: grid; }.content{ width: 400px; background: #ccc; margin:auto; }
19、Display:table-cell
適用情景:多行文字的垂直居中技巧
這一招我想有點(diǎn)年紀(jì)的開發(fā)者應(yīng)該都有看過,當(dāng)然像我這么嫩的開發(fā)者當(dāng)然是第一次看到啦,這一招的原理在于使用 CSS display屬性將div設(shè)置成表格的單元格,這樣就能利用支持存儲(chǔ)單元格對(duì)齊的vertical-align屬性來將信息垂直居中
<h2>19.display: table-cell</h2><div class="box box19"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; text-align: center; display: table-cell; vertical-align: middle; }.content{ width: 400px; background: #ccc; margin: auto; }
20、calc
適用情景:多行文字的垂直居中技巧
Cale是計(jì)算機(jī)英文單詞calculator的縮寫,這個(gè)由微軟提出的css 方法,真的是網(wǎng)頁開發(fā)者的一個(gè)福音。
我們竟然可以在網(wǎng)頁中直接做計(jì)算,這真是太猛了,從此我們?cè)僖膊挥迷谀沁吔g盡腦汁的數(shù)學(xué)計(jì)算了,或是想辦法用js來動(dòng)態(tài)計(jì)算,我們可以很輕松的利用calc()這個(gè)方法,來將百分比及時(shí)且動(dòng)態(tài)的計(jì)算出實(shí)際要的是什么高度,真可謂是劃時(shí)代的一個(gè)方法啊,但這個(gè)方法需要注意的是大量使用的話,網(wǎng)頁性能會(huì)是比較差的,所以請(qǐng)謹(jǐn)慎使用。
<h2>20.calc</h2><div class="box box20"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; }.content{ width: 400px; background: #ccc; position: relative; top:calc((100% - 70px) / 2); margin:auto; height: 70px; }
21、Relative + translateY
適用情景:多行文字的垂直居中技巧
這個(gè)技巧是利用了top:50%的招式,讓你的元素上方能產(chǎn)生固定百分比的距離,接著讓要居中的元素本身使用tanslateY的百分比來達(dá)成垂直居中的需求,translate是一個(gè)很棒的屬性,由于translate的百分比單位是利用元素自身的尺寸作為100%,這樣讓我們要利用元素自身寬高做事變得方便很多。
<h2>21.relative + translateY(-50%)</h2><div class="box box21"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; }.content{ width: 400px; background: #ccc; position: relative; top: 50%; transform: translateY(-50%); margin: auto; }
22、padding
適用情景:多行文字的垂直居中技巧
什么!這也算垂直居中技巧,連我奶奶都知道這方式吧
對(duì)的,這的確也算是一種垂直居中的方式,不可諱言的這方式真的是簡(jiǎn)單過頭了,以至于有些開發(fā)者認(rèn)為這種方式都不能算是一種垂直居中的技巧,但同樣的你無法反駁的是,我的數(shù)據(jù)的確垂直居中啦,好啦,就當(dāng)我硬凹吧,你說的對(duì),好吧
<h2>22.padding</h2><div class="box box22"> <div class="content">web前端開發(fā)</div></div> h2{ text-align: center; }.box{ width: 500px; border: 1px solid #f00; margin: auto; height: auto; padding: 50px 0; }.content{ width: 400px; background: #ccc; margin: auto; }
23、Write-mode
適用情景:多行文字的垂直劇種技巧
這個(gè)方式應(yīng)該是比較少見到的有人使用的了,這個(gè)想法是被老友Paul所激發(fā)的,write-mode這個(gè)css屬性的功能基本上跟垂直居中是八竿子打不著,它的用途是改變文字書寫的方向從橫變豎,且支持度從很早期的IE5就有支持了,但當(dāng)時(shí)Amos很少使用,一來是網(wǎng)頁多是橫書較多,另外當(dāng)時(shí)除了IE瀏覽器意外,其他瀏覽器的支持度都不是很好,也就很少使用了。
使用write-mode將一整個(gè)文字容器變成直書,接著將此容器利用text-align:center來達(dá)到垂直居中的目的,白話一點(diǎn)的解說就是,你把原本橫排的文字變成豎排,所以原本橫排用到的水平對(duì)齊方式,就變成了控制直排的中間了,原理就是這么簡(jiǎn)單。
但要特別注意的是瀏覽器對(duì)此語法的支持度來說,需要拆開寫法才行,不然某些瀏覽器的語法不同,可能會(huì)讓你的網(wǎng)頁在某些瀏覽器上看起來無效,這會(huì)是最需要注意到的
<h2>23.writing-mode</h2>立馬來看Amos實(shí)際完成的<div class="box box23"> <div class="content"> <div class="txt">web前端開發(fā) </div> </div></div> h2{ text-align: center; }.box{ width: 500px; height: 250px; border: 1px solid #f00; margin: auto; writing-mode: tb-lr; /* for ie11 */ writing-mode: vertical-lr; text-align: center; margin:0 auto; }.content{ width: 400px; background: #ccc; display: inline-block; /* for ie & edge */ width: 100%; writing-mode: lr-tb; margin: auto; text-align: left; }.box .txt{ width: 80%; margin: auto; }
看完了這23種css垂直居中寫法,不知道你用過哪幾種呢?是否你也有不為人知的小技巧呢?留言區(qū)里歡迎你的分享。
家好,本篇文章分享正方形軌跡位移動(dòng)畫特效,歡迎參考和指正。
效果圖:
正方形軌跡位移動(dòng)畫特效
HTML代碼:
<div class="tui-demo-4"></div>
CSS代碼:
<style type="text/css"> .tui-demo-4 { width: 100px; height: 100px; margin: 50px auto; border: 1px solid #393D49; position: relative; } .tui-demo-4:after { content: ''; background: #393D49; width: 30px; height: 30px; position: absolute; top: 0; left: 0; animation: tui-demo-4 2.0s infinite; } @keyframes tui-demo-4 { 0% { transform: translateX(0) translateY(0); } 25% { transform: translateX(70px) translateY(0); } 50% { transform: translateX(70px) translateY(70px); } 75% { transform: translateX(0) translateY(70px); } 100% { transform: translateX(0) translateY(0); } } </style>
知識(shí)點(diǎn):
animation:是CSS3的動(dòng)畫屬性,這里把a(bǔ)nimation綁定到tui-demo-4元素上,并指定該動(dòng)畫需要2.0秒完成,infinite則表示無限次播放該動(dòng)畫。
@keyframes:該規(guī)則表示動(dòng)畫可以逐步從一個(gè)CSS樣式改變?yōu)榱硪粋€(gè)CSS樣式。這里0%是動(dòng)畫開頭,100%是動(dòng)畫結(jié)束。
transform:該屬性應(yīng)用于2D元素或3D元素的轉(zhuǎn)換,允許元素發(fā)生旋轉(zhuǎn),縮放,移動(dòng),傾斜等效果。translateX表示元素沿著水平方向或者X軸移動(dòng),translateY表示元素沿著垂直方向或者Y軸移動(dòng)。
最后注意下瀏覽器兼容問題,-webkit-,-ms-或-moz-,有問題可以留言,大家一起學(xué)習(xí)HTML+CSS基礎(chǔ)入門開發(fā)。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。