布局在我們前端日常開發來說是非常重要的,一個好的布局能簡化代碼的同時還能提高網頁的性能。常見的布局方法有浮動(float)布局、絕對定位(position)布局、表格布局(table)、彈性(flex)布局、網格(grid)布局。關于布局方法本文不做詳細講解,筆者推薦看阮一峰老師 flex布局教程 和阮一峰老師 grid布局教程。
本文主要講解水平垂直居中、單欄布局、雙欄布局、三欄布局一些項目中常用的布局方案。
本文代碼全部使用codepen在線代碼工具進行演示。
居中在我們日常工作中還是會經常碰到。
對于水平居中一般可以使用如下四種方式
<div class="div1">
<span>行內元素水平居中</span>
</div>
<div class="div2">
<span>行內元素水平居中</span>
<div>塊級元素水平居中</div>
</div>
<div class="div3">
<span>行內元素水平居中</span>
<div>塊級元素水平居中</div>
</div>
<div class="div4">塊級元素水平居中</div>
.div1 {
text-align: center;
}
.div2 {
display: flex;
justify-content: center;
}
.div3 {
display: grid;
justify-content: center;
}
.div4 {
width: 130px;
margin: 0 auto;
}
效果如下
點擊查看代碼運行實例
對于垂直居中一般可以使用如下三種方式
<div class="div1">
<span>行內元素垂直居中</span>
<!-- <div>塊級元素垂直居中</div> -->
</div>
<div class="div2">
<span>行內元素垂直居中</span>
<div>塊級元素垂直居中</div>
</div>
<div class="div3">
<span>行內元素垂直居中</span>
<div>塊級元素垂直居中</div>
</div>
<div class="div4">
<span>行內元素垂直居中</span>
<div>塊級元素垂直居中</div>
</div>
.div1 {
height: 100px;
background: lightgreen;
line-height: 100px;
}
.div2 {
height: 100px;
background: lightblue;
display: flex;
align-items: center;
}
.div3 {
height: 100px;
background: lightgreen;
display: grid;
align-content: center;
}
.div4 {
height: 100px;
background: lightblue;
display: table-cell;
vertical-align: middle;
}
效果如下
點擊查看代碼運行實例
比如我們想實現如下水平垂直同時居中的效果
實現水平垂直同時居中我們可以使用絕對定位、table布局、flex布局 或 grid布局來實現。
首先我們創建一個需要居中的盒子。
<div class="box"></div>
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
點擊查看代碼運行實例
這種方式需要知道居中元素的具體寬高,不然負的margin沒法設置。
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -50px;
}
點擊查看代碼運行實例
這種平移的方式就不需要考慮居中盒子的具體寬高了。
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
點擊查看代碼運行實例
html,body {
height: 100%;
}
body {
background: gray;
display: flex;
align-items: center;
justify-content: center;
}
.box {
width: 200px;
height: 100px;
background: red;
}
點擊查看代碼運行實例
html,body {
height: 100%;
}
body {
background: gray;
display: grid;
/* align-content: center;
justify-content: center; */
/* align-content和justify-content的簡寫 */
place-content: center;
}
.box {
width: 200px;
height: 100px;
background: red;
}
點擊查看代碼運行實例
使用table布局需要注意如下
<div class="box">
<div class="child"></div>
</div>
.box {
background: red;
height: 300px;
width: 600px;
display: table-cell;
vertical-align: middle;
}
.child {
width: 200px;
height: 200px;
background: lightgreen;
margin: 0 auto;
}
點擊查看代碼運行實例
單欄布局我們常用在網頁框架上,一般我們把網頁分為 header、content、footer三部分。
在不同的項目我們可能對這三部分的樣式需求有所差別,比如需要頂部固定、需要底部固定等等。
比如想實現如下效果,footer在內容不足的時候吸附在窗口底部,當內容多的時候又可以被抵到窗口下面。
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
</div>
<div class="footer">footer</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
min-height: 100%;
padding-bottom: 50px;
overflow: auto;
box-sizing: border-box;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
/* 這里的高度只是為了模擬內容多少 */
height: 100px;
/* height: 1000px; */
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
flex-direction: column;
min-height: 100%;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
/* 這里的高度只是為了模擬內容多少 */
height: 100px;
/* height: 1000px; */
flex-grow: 1;
}
.footer {
height: 50px;
background: lightgreen;
}
點擊查看代碼運行實例
<div class="header">header</div>
<div class="wrap">
<div class="content">content</div>
</div>
<div class="footer">footer</div>
html, body {
height: 100%;
margin: 0;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.wrap {
min-height: 100%;
padding-bottom: 50px;
overflow: auto;
box-sizing: border-box;
}
.content {
margin-top: 50px;
background: lightpink;
/* 這里的高度只是為了模擬內容多少 */
height: 100px;
/* height: 1000px; */
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
min-height: 100%;
flex-direction:column;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.content {
background: lightpink;
/* 這里的高度只是為了模擬內容多少 */
/* height: 100px; */
height: 1000px;
margin-top: 50px;
flex-grow: 1;
}
.footer {
height: 50px;
background: lightgreen;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
</div>
<div class="footer">footer</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
height: 100%;
padding-bottom: 50px;
overflow: auto;
box-sizing: border-box;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
height: 100px;
height: 1000px;
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
min-height: 100%;
flex-direction:column;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
/* 這里的高度只是為了模擬內容多少 */
/* height: 100px; */
height: 1000px;
flex-grow: 1;
margin-bottom: 50px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
width: 100%;
bottom: 0;
}
點擊查看代碼運行實例
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
復制代碼
html, body {
height: 100%;
margin: 0;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.content {
background: lightpink;
padding-top: 50px;
padding-bottom: 50px;
/* height: 100px; */
height: 1000px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
bottom: 0;
width: 100%;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
min-height: 100%;
flex-direction:column;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.content {
background: lightpink;
/* 這里的高度只是為了模擬內容多少 */
/* height: 100px; */
height: 1000px;
flex-grow: 1;
margin-bottom: 50px;
margin-top: 50px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
width: 100%;
bottom: 0;
}
點擊查看代碼運行實例
兩欄布局就是一邊固定,另外一邊自適應,效果如下
實現兩欄布局的方法也有很多,筆者接下來介紹用的比較多的幾種方式。
<div class="aside"></div>
<div class="main"></div>
div {
height: 500px;
}
.aside {
width: 300px;
float: left;
background: yellow;
}
.main {
background: aqua;
margin-left: 300px;
}
點擊查看代碼運行實例
<div class="aside"></div>
<div class="main"></div>
div {
height: 500px;
}
.aside {
width: 300px;
float: right;
background: yellow;
}
.main {
background: aqua;
margin-right: 300px;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
div {
height: 500px;
}
.wrap {
position: relative;
}
.aside {
width: 300px;
background: yellow;
position: absolute;
}
.main {
background: aqua;
margin-left: 300px;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
div {
height: 500px;
}
.wrap {
position: relative;
}
.aside {
width: 300px;
background: yellow;
position: absolute;
right: 0;
}
.main {
background: aqua;
margin-right: 300px;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
div {
height: 500px;
}
.wrap {
display: flex;
}
.aside {
flex: 0 0 300px;
background: yellow;
}
.main {
background: aqua;
flex: 1 1;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
height: 500px;
}
.wrap {
display: grid;
grid-template-columns: 300px auto;
}
.aside {
background: yellow;
}
.main {
background: aqua;
}
點擊查看代碼運行實例
三欄布局就是兩邊固定,中間自適應布局,效果如下
實現三欄布局的方法也有很多,筆者接下來介紹用的比較多的幾種方式。
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.left {
position: absolute;
left: 0;
top: 0;
width: 200px;
background: green;
}
.right {
position: absolute;
right: 0;
top: 0;
width: 200px;
background: red;
}
.middle {
margin-left: 200px;
margin-right: 200px;
background: lightpink;
}
點擊查看代碼運行實例
<div class="left"></div>
<div class="right"></div>
<div class="middle"></div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.left {
width: 200px;
background: green;
float: left;
}
.right {
width: 200px;
background: yellow;
float: right;
}
.middle {
margin-left: 200px;
margin-right: 200px;
background: lightpink;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.wrap {
display: flex;
}
.left {
flex: 0 0 200px;
background: green;
}
.right {
flex: 0 0 200px;
background: yellow;
}
.middle {
background: lightpink;
flex: 1 1;
}
點擊查看代碼運行實例
<div class="wrap">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.wrap {
display: grid;
grid-template-columns: 200px auto 200px;
}
.left {
background: green;
}
.right {
background: yellow;
}
.middle {
background: lightpink;
}
點擊查看代碼運行實例
圣杯布局在項目中基本上不會再使用了,在面試中我們會經常碰到,所以需要了解。
主要用到了浮動和和相對定位。
<div class="container">
<div class="content">中間內容</div>
<div class="left">左側區域</div>
<div class="right">右側區域</div>
</div>
div {
height: 500px;
}
.container {
padding: 0 200px 0 200px;
border: 1px solid black;
}
.content {
float: left;
width: 100%;
background: #f00;
}
.left {
width: 200px;
background: #0f0;
float: left;
margin-left: -100%;
position: relative;
left: -200px;
}
.right {
width: 200px;
background: #00f;
float: left;
margin-left: -200px;
position: relative;
right: -200px;
}
點擊查看代碼運行實例
雙飛翼布局在項目中基本上不會再使用了,在面試中我們會經常碰到,所以需要了解。
主要用到了浮動。
<div class="main">
<div class="content">content</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
div {
height: 500px;
}
.main {
float: left;
width: 100%;
background: #f00;
}
.main .content {
/* margin、padding這兩種方式都可以 */
/* margin-left: 200px;
margin-right: 300px; */
padding-left: 200px;
padding-right: 300px;
}
.left {
width: 200px;
background: #0f0;
float: left;
margin-left: -100%;
}
.right {
width: 200px;
background: #00f;
float: left;
margin-left: -200px;
}
點擊查看代碼運行實例
因為flex和grid布局方法已經很強大了,日常工作中99%的布局都可以使用這兩種方式來實現。所以筆者建議能使用flex或者grid布局方法實現的就盡量使用這兩種布局方式實現。因為不僅簡單而且負面作用也很少。
浮動布局和絕對定位布局會導致元素脫離文檔流,會帶來一些負面作用,有時會導致一些意想不到的結果。
關于flex布局的兼容性和grid布局的兼容性,目前已經支持的很好了,大家可以放心使用。
flex布局的兼容性
grid布局的兼容性
感謝小伙伴們的耐心觀看,本文為筆者個人學習筆記,如有謬誤,還請告知,萬分感謝!如果本文對你有所幫助,還請點個關注點個贊~,您的支持是筆者不斷更新的動力!
擊右上方紅色按鈕關注“web秀”,讓你真正秀起來
在日常項目開發中,在布局方面有遇到哪些問題了?今天來一起看看CSS布局有哪些小技巧,后續開發更輕松。本文主要通過簡單的示例,講述開發中遇到的布局等問題,但不僅限于布局相關,會有其他相關知識點。
CSS實用技巧第二講:布局處理
移動端使用rem布局需要通過JS設置不同屏幕寬高比的font-size,結合vw單位和calc()可脫離JS的控制,代碼如下:
/* 基于UI width=750px DPR=2的頁面 */ html { font-size: calc(100vw / 7.5); }
rem 頁面布局, 不兼容低版本移動端,使用需謹慎。
通過flexbox或inline-block的形式橫向排列元素,對父元素設置overflow-x:auto橫向滾動查看。注意場景: 橫向滾動列表、元素過多但位置有限的導航欄。
CSS實用技巧第二講:布局處理
<div class="horizontal-list flex"> <ul> <li>web秀</li> <li>阿里巴巴</li> <li>字節跳動</li> <li>騰訊</li> <li>百度</li> <li>美團</li> </ul> </div> <div class="horizontal-list inline"> <ul> <li>web秀</li> <li>阿里巴巴</li> <li>字節跳動</li> <li>騰訊</li> <li>百度</li> <li>美團</li> </ul> </div>
scss樣式
.horizontal-list { width: 300px; height: 100px; ul { overflow-x: scroll; cursor: pointer; margin: 0; padding: 0; &::-webkit-scrollbar { height: 6px; } &::-webkit-scrollbar-track { background-color: #f0f0f0; } &::-webkit-scrollbar-thumb { border-radius: 5px; background: linear-gradient(to right, #32bbff, #008cf4); } } li { overflow: hidden; margin-left: 10px; height: 90px; background-color: #00b7a3; line-height: 90px; text-align: center; font-size: 16px; color: #fff; &:first-child { margin-left: 0; } } } .flex { ul { display: flex; flex-wrap: nowrap; justify-content: space-between; } li { flex-shrink: 0; flex-basis: 90px; } } .inline { margin-top: 10px; height: 102px; ul { overflow-y: hidden; white-space: nowrap; } li { display: inline-block; width: 90px; } }
知識點解析:
1、display: flex布局:又名“彈性布局”,任何一個容器都可以指定為Flex布局。詳細內容請點擊
《CSS3中Flex彈性布局該如何靈活運用?》
2、滾動條樣式美化。
如何自定義滾動條樣式了? 掌握這3個選擇器即可。
(1)、::-webkit-scrollbar 定義了滾動條整體的樣式;
(2)、::-webkit-scrollbar-thumb 滑塊部分;
(3)、::-webkit-scrollbar-track 軌道部分;
所以上面scss代碼中,我們書寫了這3個選擇器的樣式,改變了滾動條的樣式。
3、linear-gradient線性漸變。語法如下:
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction用角度值指定漸變的方向(或角度), color-stop1, color-stop2,...用于指定漸變的起止顏色。
to right的意思就是從左到右,從一個顏色過渡到另外一個顏色。
請看示例:
CSS實用技巧第二講:布局處理
更多詳細內容請點擊:
《CSS3線性漸變、陰影、縮放實現動畫下雨效果》
《CSS3線性徑向漸變、旋轉、縮放動畫實現王者榮耀匹配人員加載頁面》
《CSS3徑向漸變實現優惠券波浪造型》
在retina屏中,像素比為2(iPhone6/7/8)或3(iPhone6Plus/7Plus/8Plus),1px的邊框看起來比真的1px更寬。
我們可以通過偽類加transform的方式解決。
CSS實用技巧第二講:布局處理
transform:用于元素進行旋轉、縮放、移動或傾斜,和設置樣式的動畫并沒有什么關系,就相當于color一樣用來設置元素的“外表”。
詳細transform了解,請點擊:
《CSS3最容易混淆屬性transition transform animation translate》
非常簡單的方式,flexbox橫向布局時,最后一個元素通過margin-left:auto實現向右對齊。
請看示例:
<ul class="nav-list"> <li>HTML5</li> <li>CSS3</li> <li>JAVASCRIPT</li> <li>TYPESCRIPT</li> <li>THREE.JS</li> <li>NODE</li> </ul>
css樣式
.nav-list { display: flex; align-items: center; padding: 0 10px; width: 700px; height: 60px; background-color: #00b7a3; } .nav-list li { padding: 0 10px; height: 40px; line-height: 40px; font-size: 16px; color: #fff; list-style: none; } .nav-list li + li { margin-left: 10px; } .nav-list li:last-child { margin-left: auto; }
CSS實用技巧第二講:布局處理
<div class="accordion"> <input type="checkbox" id="collapse1"> <input type="checkbox" id="collapse2"> <input type="checkbox" id="collapse3"> <article> <label for="collapse1">web秀</label> <p>html<br>javascript<br>css<br>uni app</p> </article> <article> <label for="collapse2"></label> <p>新聞<br>圖片<br>視頻<br>養生</p> </article> <article> <label for="collapse3">阿里巴巴</label> <p>淘寶<br>阿里云<br>閑魚<br>天貓</p> </article> </div>
scss樣式
.accordion { width: 300px; article { margin-top: 5px; cursor: pointer; &:first-child { margin-top: 0; } } input { display: none; padding: 0; margin: 0; &:nth-child(1):checked ~ article:nth-of-type(1) p, &:nth-child(2):checked ~ article:nth-of-type(2) p, &:nth-child(3):checked ~ article:nth-of-type(3) p { border-bottom-width: 1px; max-height: 600px; } } label { display: block; padding: 0 20px; height: 40px; background-color: #00b7a3; cursor: pointer; line-height: 40px; font-size: 16px; color: #fff; } p { overflow: hidden; padding: 0 20px; margin: 0; border: 1px solid #00b7a3; border-top: none; border-bottom-width: 0; max-height: 0; line-height: 30px; transition: all 500ms; } }
CSS實用技巧第二講:布局處理
<div class="tab-navbar"> <input type="radio" name="tab" id="tab1" checked> <input type="radio" name="tab" id="tab2"> <input type="radio" name="tab" id="tab3"> <input type="radio" name="tab" id="tab4"> <nav> <label for="tab1">首頁</label> <label for="tab2">列表</label> <label for="tab3">其他</label> <label for="tab4">我的</label> </nav> <main> <ul> <li>首頁</li> <li>列表</li> <li>其他</li> <li>我的</li> </ul> </main> </div>
scss樣式
*{ padding: 0; margin: 0; } .active { background-color: #00b7a3; color: #fff; } .tab-navbar { display: flex; overflow: hidden; flex-direction: column-reverse; border-radius: 10px; width: 300px; height: 400px; margin: 100px auto; input { display: none; &:nth-child(1):checked { & ~ nav label:nth-child(1) { @extend .active; } & ~ main ul { background-color: #f13f84; transform: translate3d(0, 0, 0); } } &:nth-child(2):checked { & ~ nav label:nth-child(2) { @extend .active; } & ~ main ul { background-color: #44bb44; transform: translate3d(-25%, 0, 0); } } &:nth-child(3):checked { & ~ nav label:nth-child(3) { @extend .active; } & ~ main ul { background-color: #ff7632; transform: translate3d(-50%, 0, 0); } } &:nth-child(4):checked { & ~ nav label:nth-child(4) { @extend .active; } & ~ main ul { background-color: #00bbdd; transform: translate3d(-75%, 0, 0); } } } nav { display: flex; height: 40px; background-color: #f0f0f0; line-height: 40px; text-align: center; label { flex: 1; cursor: pointer; transition: all 300ms; } } main { flex: 1; ul { display: flex; flex-wrap: nowrap; width: 400%; height: 100%; transition: all 300ms; } li { display: flex; justify-content: center; align-items: center; flex: 1; font-weight: bold; font-size: 20px; color: #fff; } } }
CSS實用技巧第二講:布局處理
喜歡小編或者覺得小編文章對你有幫助的,可以點擊一波關注哦!同時,要源碼的小伙伴可以點擊下方“了解更多”。
最后推薦一個專欄文章,感謝小伙伴們多多支持,謝謝大家!
(OF作品展示)
OF之前介紹了用python實現數據可視化、數據分析及一些小項目,但基本都是后端的知識。想要做一個好看的小系統,我們還要學一些前端的知識,今天OF將講解如何用pycharm(全棧開發不錯的工具)做一張好看的網頁,以后我們就可以自己開發網頁/網站放到互聯網上。
主要內容:網頁前端布局
適用人群:Python初學者,前端初學者
準備軟件:pycharm
1) 下載完成pycharm, 點擊file-New Project...
2) 按下圖步驟創建一個項目,目前我們選擇Pure python即可,以后我們可以學習用Django/flask等框架來做完整的系統
1)在創建的項目空白處鼠標右鍵-New-HTML File
2)輸入英文的網頁名字,盡量不要輸入中文/特殊字符
當雙擊打開我們創建后的HTML文件,大家會看到下面的界面
在開始開發網頁前,我們需要自己設計下想要把網頁做成什么樣,為了節省成本OF都是自己設計的頁面樣式,可以手繪,也可以在PPT上畫。
我們先學習一個比較簡單的布局如下圖,大家可以看到下圖已經畫出了我們需要添加的內容,要注意的地方是比如Taylor的圖片和文字一定要用<div class=bord>框住,否則Taylor圖片與文字將會是左右的關系,而不是上下
根據上述的css名字定義,先填充<body>內的代碼,那么我們就完成一半的工作了,代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="intro">
<p class="peo">人物介紹</p>
</div>
<div id="pic1">
<div class="bord">
<img class="img" src="pic/Taylor.png"/>
<p class="word">Taylor</p>
</div>
<div class="bord">
<img class="img" src="pic/yan.png"/>
<p class="word">東</p>
</div>
<div class="bord">
<img class="img" src="pic/song.png"/>
<p class="word">喬</p>
</div>
</div>
</body>
</html>
1)鼠標移到代碼處,在右上角我們會看到一排瀏覽器,我們點擊其中一個運行
2)目前看到的網頁內容從上到下顯示
首先我們簡要了解下flex布局,大家可以看到下圖中#main的style樣式中display:flex,在body部分將3個顏色內容框在<div id="main">中,運行結果是3個顏色的內容橫向展示了,而不是上下
1)那么我們先從“人物介紹”的布局開始,“人物介紹”居中展現(用flex中justify-content:center),而且下面有一條黑線,OF準備用border樣式來實現,所以在<div id=intro>里另加了個<div class=peo>,代碼如下:
(注:style中的#main對應body中的id=main, .main對應class=main)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#intro {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
.peo {
max-width: 10rem;
border-bottom: 0.2rem solid #000000;
font-family: sans-serif;
font-size: 1.5rem;
color: #0070C0;
line-height: 3rem;
}
</style>
</head>
<body>
<div id="intro">
<p class="peo">人物介紹</p>
</div>
<div id="pic1">
<div class="bord">
<img class="img" src="pic/Taylor.png"/>
<p class="word">Taylor</p>
</div>
<div class="bord">
<img class="img" src="pic/yan.png"/>
<p class="word">東</p>
</div>
<div class="bord">
<img class="img" src="pic/song.png"/>
<p class="word">喬</p>
</div>
</div>
</body>
</html>
運行結果:
2)圖片部分是3個<div class=bord>橫向展示,所以要在框住它們的<div id=pic1>樣式中設置flex布局,在<style>里加入以下代碼:
#pic1 {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
運行結果:
3)圖片之間靠太近,圖片大小不一致,文字沒居中,我們在<style>里加入以下代碼:
.bord{
padding: 1rem 2rem;
}
.img {
border: 0.2rem solid #e3e3e3;
max-width: 15rem;
height: 22rem;
}
.word {
text-align: center;
}
運行結果:
今天我們學會了flex布局,今后所有的網頁排版都可以實現了,祝愿大家都有所收獲,完整的代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#intro {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
.peo {
max-width: 10rem;
border-bottom: 0.2rem solid #000000;
font-family: sans-serif;
font-size: 1.5rem;
color: #0070C0;
line-height: 3rem;
}
#pic1 {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
.bord{
padding: 1rem 2rem;
}
.img {
border: 0.2rem solid #e3e3e3;
max-width: 15rem;
height: 22rem;
}
.word {
text-align: center;
}
</style>
</head>
<body>
<div id="intro">
<p class="peo">人物介紹</p>
</div>
<div id="pic1">
<div class="bord">
<img class="img" src="pic/Taylor.png"/>
<p class="word">Taylor</p>
</div>
<div class="bord">
<img class="img" src="pic/yan.png"/>
<p class="word">東</p>
</div>
<div class="bord">
<img class="img" src="pic/song.png"/>
<p class="word">喬</p>
</div>
</div>
</body>
</html>
今天的知識比較基礎但非常實用,每天學會一個小技能,積少成多,以后就能成為大神。如果大家對網頁上的實現有什么不懂的,盡請留言,OF一定會一一解答的。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。