CSS基礎佈局--居中對齊,左側定寬右側自適應

CSS頁面佈局是web前端開發的最基本的技能,本文將介紹一些常見的佈局方法,涉及到盒子佈局,column佈局,flex佈局等內容。本文中,你可以看到一些水平垂直居中的方法,左側固定寬度,右側自適應的一些方法。如果你有更多關於佈局方面的技巧,歡迎留言交流。 一、神奇的居中 經常看到有一些面試題問如何實現水平垂直居中,還要求用多種方法。唉唉唉!下面介紹一下我所知道的實現居中的方法。 (1)父元素relative;子元素absolute,top:50%;left:50%;margin-left:-自己寬度的一半;margin-right:-自己高度的一半。
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>水平垂直居中2</title>
 6     <style type="text/css">
 7         .container{
 8             width: 100%;
 9             height: 500px;
10             background: red;
11             position: relative;
12         }
13 
14         .child{
15             width: 300px;
16             height: 300px;
17             background: blue;
18             position: absolute;
19             left: 50%;
20             margin-left: -150px;
21             top: 50%;
22             margin-top: -150px;
23         }
24     </style>
25 </head>
26 <body>
27     <div class="container">
28         <div class="child"></div>
29     </div>
30 </body>
31 </html>

 這種方法需要知道子元素的寬高。

(2)父元素:relative;子元素:absolute;top:50%;left:50%;transform:translate(-50%,-50%);
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>水平垂直居中3</title>
 6     <style type="text/css">
 7         .container{
 8             background: red;
 9             width: 100%;
10             height: 500px;
11             position: relative;
12         }
13 
14         .child{
15             background: blue;
16             width: 300px;
17             height: 300px;
18             position: absolute;
19             top: 50%;
20             left: 50%;
21             transform: translate(-50%,-50%);
22         }
23     </style>
24 </head>
25 <body>
26     <div class="container">
27         <div class="child"></div>
28     </div>
29 </body>
30 </html>
此法跟上面的相似,但是用到了transform,好處是不需要知道子元素的寬高,兼容性方面我查了一下,看着辦吧。


(3)父元素:display: flex;justify-content: center;align-items: center;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平垂直居中1</title>
    <style type="text/css">
        .container{
            width: 100%;
            height: 400px;
            background: red;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .child{
            width: 300px;
            height: 300px;
            background: blue;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="child"></div>
    </div>
</body>
</html>

這種方法看起來有些高大上,根本不用理會子元素。

(4)水平居中的方法,父元素:text-align:center
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>水平垂直居中4</title>
 6     <style type="text/css">
 7         .container{
 8             background: red;
 9             width: 100%;
10             height: 500px;
11             text-align: center;
12         }
13 
14         .child{
15             background: blue;
16             width: 300px;
17             height: 300px;
18             margin: auto;
19         }
20     </style>
21 </head>
22 <body>
23     <div class="container">
24         <div class="child"></div>
25     </div>
26 </body>
27 </html>
如果子元素裏的文字不要水平居中的話,那麼用此法將遇到不少麻煩。
(5)水平居中方法,子元素:margin:0 auto;這個好說,不上代碼了
好了,關於居中問題就說這麼多,如果你還有更好的方法,請告訴我。
二、左側固定寬度,右側自適應 這是一個比較常見的需求,下面介紹幾種實現方法。 (1)左邊定寬,左浮動,右邊不指定寬。
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>做固定,右邊自適應</title>
 6     <style type="text/css">
 7     body{
 8         margin: 0;
 9     }
10         .aside{
11             background: red;
12             width:200px;
13             height: 500px;
14             float: left;
15         }
16         .main {
17             background: blue;
18             height: 500px;
19 
20         }
21     </style>
22 </head>
23 <body>
24     <div class="aside">
25         我是左邊的
26     </div>
27     <div class="main">
28         我是主體
29         我是主體
30         我是主體
31         我是主體
32         我是主體
33     </div>
34 </body>
35 </html>

做實驗時無意發現了這種方法,意外之喜。

(2)用padding佔位子的方法

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>左側固定右側自適應</title>
 6     <style type="text/css">
 7         .container {
 8             padding-left: 200px;
 9             width: 100%;
10             position: relative;
11         }
12         .left{
13             position: absolute;
14             left: 0;
15             right: 0;
16             background: red;
17             height: 500px;
18             width: 200px;
19         }
20         .right{
21             background: blue;
22             width: 100%;
23             height: 500px;
24         }
25     </style>
26 </head>
27 <body>
28     <div class="container">
29         <div class="left">zuobian</div>
30         <div class="right">
31             新華社俄羅斯喀山3月23日電(記者 魏良磊)中俄執政黨對話機制第六次會議和第五屆中俄政黨論壇23日在俄羅斯喀山舉行。和俄羅斯聯邦總統普京分別致賀信。
32         </div>
33     </div>
34 </body>
35 </html>

注意了,absolute是脫離文檔流的。.right的100%是相對於父容器的內容寬度的,不是整個寬度。

(3)父:display:flex;左定寬;右flex:1。ok

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>左邊固定,右邊自適應</title>
 6     <style type="text/css">
 7         .container{
 8             display: flex;
 9         }
10         .left{
11             width: 200px;
12             height: 500px;
13             background: red;
14         }
15         .right{
16             background: blue;
17             height: 500px;
18             flex: 1;
19         }
20     </style>
21 </head>
22 <body>
23     <div class="container">
24         <div class="left">zuobian</div>
25         <div class="right">
26             新華社俄羅斯喀山3月23日電(記者 魏良磊)中俄執政黨對話機制第六次會議和第五屆中俄政黨論壇23日在俄羅斯喀山舉行。和俄羅斯聯邦總統普京分別致賀信。
27         </div>
28     </div>
29 </body>
30 </html>
彈性盒子很強,有木有。但是有的是要加前綴的,哪些要加自己查去,有一次做實驗,電腦樣式正確,手機就是不對,搞了好半天。
(4)父:display:table;左右:display:table-cell;左:定寬。
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>左邊固定,右邊自適應</title>
 6     <style type="text/css">
 7         .container{
 8             display: table;
 9         }
10         .left{
11             width: 200px;
12             height: 500px;
13             background: red;
14             display: table-cell;
15         }
16         .right{
17             background: blue;
18             height: 500px;
19             display: table-cell;
20         }
21     </style>
22 </head>
23 <body>
24     <div class="container">
25         <div class="left">zuobian</div>
26         <div class="right">
27             新華社俄羅斯喀山3月23日電(記者 魏良磊)中俄執政黨對話機制第六次會議和第五屆中俄政黨論壇23日在俄羅斯喀山舉行。羅斯聯邦總統普京分別致賀信。
28         </div>
29     </div>
30 </body>
31 </html>
據說這是一種古老的方法,我咋不知道呢?可能我比較年輕吧!

三、總結

CSS這個東西看起來挺簡單的,要掌握好還真實不簡單。特別佩服張鑫旭,他對CSS的研究真的非常非常深入,雖然說不太喜歡他的風格。先到這,以後在再補充下相關的內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章