css background 一半_CSS小技巧
左右布局
將內(nèi)部的子元素加浮動(dòng),父元素清除浮動(dòng)即可。
代碼:
DOCTYPE html><html><head><title>testtitle><style type="text/css">.big{
width: 300px;
height: 300px;
background-color: #999;}
.smallOne{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.smallTwo{
width: 100px;
height: 100px;
background-color: red;
float: right;}
.clearfix{
content: "";
display: block;
clear: both;}style>head><body><div class="big clearfix"><div class="smallOne">div><div class="smallTwo">div>div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
效果:
左中右布局
?浮動(dòng) 清除浮動(dòng)
代碼:
DOCTYPE html><html><head><title>testtitle><style type="text/css">.big{
width: 300px;
height: 300px;
background-color: #999;}
.smallOne{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.smallTwo{
width: 100px;
height: 100px;
background-color: green;
float: left;}
.smallThree{
width: 100px;
height: 100px;
background-color: red;
float: left;}
.clearfix{
content: "";
display: block;
clear: both;}style>head><body><div class="big clearfix"><div class="smallOne">div><div class="smallTwo">div><div class="smallThree">div>div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
效果:
?水平居中
?內(nèi)聯(lián)元素居中:
text-align: center;
代碼:
DOCTYPE html><html><head><title>testtitle><style type="text/css">.big{
width: 200px;
height: 100px;
background-color: #999;
text-align: center;}style>head><body><div class="big"><span>我在這里span>div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
效果圖:
?塊級(jí)元素居中:
margin:0 auto
代碼:
DOCTYPE html><html><head><title>testtitle><style type="text/css">.big{
width: 200px;
height: 100px;
background-color: #999;
text-align: center;}
.small{
width: 100px;
height: 20px;
background-color: red;
margin: 0 auto;}style>head><body><div class="big"><div class="small">我在這里div>div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
效果圖:
垂直居中
父元素高度確定的單行文本
設(shè)置 height = line-height
?代碼:
DOCTYPE html><html><head><title>testtitle><style type="text/css">.big{
width: 200px;
height: 100px;
background-color: #aaa;
line-height: 100px;}style>head><body><div class="big">
這里是文本!div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
效果圖:
塊級(jí)元素垂直居中
利用父元素相對(duì)定位,子元素絕對(duì)定位,并且處置、水平方向個(gè)偏移50%,子元素利用負(fù)值偏移自身寬度、長(zhǎng)度的一半,這種方式同樣適用于水平居中。
代碼:
DOCTYPE html><html><head><title>testtitle><style type="text/css">.big{
height: 200px;
width: 200px;
background-color: red;
position: relative;}
.small{
height: 50px;
width: 50px;
background-color: black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);}style>head><body><div class="big"><div class="small">div>div>body>html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
效果圖:
更多精彩請(qǐng)繼續(xù)關(guān)注小編了解哦!
計(jì)算機(jī)畢業(yè)設(shè)計(jì)(源程序+論文+開題報(bào)告+文獻(xiàn)綜述+翻譯+答辯稿)
聯(lián)系QQ:2932963541進(jìn)行咨詢
網(wǎng)站地址:http://www.webtmall.com/掃碼關(guān)注最新動(dòng)態(tài)點(diǎn)擊此處“閱讀全文”查看更多內(nèi)容總結(jié)
以上是生活随笔為你收集整理的css background 一半_CSS小技巧的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单片机小精灵t2_搭建S5P4418 A
- 下一篇: java分隔符 字符串_用Java构建带