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
布局在我們前端日常開(kāi)發(fā)來(lái)說(shuō)是非常重要的,一個(gè)好的布局能簡(jiǎn)化代碼的同時(shí)還能提高網(wǎng)頁(yè)的性能。常見(jiàn)的布局方法有浮動(dòng)(float)布局、絕對(duì)定位(position)布局、表格布局(table)、彈性(flex)布局、網(wǎng)格(grid)布局。關(guān)于布局方法本文不做詳細(xì)講解,筆者推薦看阮一峰老師 flex布局教程 和阮一峰老師 grid布局教程。
本文主要講解水平垂直居中、單欄布局、雙欄布局、三欄布局一些項(xiàng)目中常用的布局方案。
本文代碼全部使用codepen在線代碼工具進(jìn)行演示。
居中在我們?nèi)粘9ぷ髦羞€是會(huì)經(jīng)常碰到。
對(duì)于水平居中一般可以使用如下四種方式
<div class="div1">
<span>行內(nèi)元素水平居中</span>
</div>
<div class="div2">
<span>行內(nèi)元素水平居中</span>
<div>塊級(jí)元素水平居中</div>
</div>
<div class="div3">
<span>行內(nèi)元素水平居中</span>
<div>塊級(jí)元素水平居中</div>
</div>
<div class="div4">塊級(jí)元素水平居中</div>
.div1 {
text-align: center;
}
.div2 {
display: flex;
justify-content: center;
}
.div3 {
display: grid;
justify-content: center;
}
.div4 {
width: 130px;
margin: 0 auto;
}
效果如下
點(diǎn)擊查看代碼運(yùn)行實(shí)例
對(duì)于垂直居中一般可以使用如下三種方式
<div class="div1">
<span>行內(nèi)元素垂直居中</span>
<!-- <div>塊級(jí)元素垂直居中</div> -->
</div>
<div class="div2">
<span>行內(nèi)元素垂直居中</span>
<div>塊級(jí)元素垂直居中</div>
</div>
<div class="div3">
<span>行內(nèi)元素垂直居中</span>
<div>塊級(jí)元素垂直居中</div>
</div>
<div class="div4">
<span>行內(nèi)元素垂直居中</span>
<div>塊級(jí)元素垂直居中</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;
}
效果如下
點(diǎn)擊查看代碼運(yùn)行實(shí)例
比如我們想實(shí)現(xiàn)如下水平垂直同時(shí)居中的效果
實(shí)現(xiàn)水平垂直同時(shí)居中我們可以使用絕對(duì)定位、table布局、flex布局 或 grid布局來(lái)實(shí)現(xiàn)。
首先我們創(chuàng)建一個(gè)需要居中的盒子。
<div class="box"></div>
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
這種方式需要知道居中元素的具體寬高,不然負(fù)的margin沒(méi)法設(shè)置。
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -50px;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
這種平移的方式就不需要考慮居中盒子的具體寬高了。
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
html,body {
height: 100%;
}
body {
background: gray;
display: flex;
align-items: center;
justify-content: center;
}
.box {
width: 200px;
height: 100px;
background: red;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
html,body {
height: 100%;
}
body {
background: gray;
display: grid;
/* align-content: center;
justify-content: center; */
/* align-content和justify-content的簡(jiǎn)寫(xiě) */
place-content: center;
}
.box {
width: 200px;
height: 100px;
background: red;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
使用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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
單欄布局我們常用在網(wǎng)頁(yè)框架上,一般我們把網(wǎng)頁(yè)分為 header、content、footer三部分。
在不同的項(xiàng)目我們可能對(duì)這三部分的樣式需求有所差別,比如需要頂部固定、需要底部固定等等。
比如想實(shí)現(xiàn)如下效果,footer在內(nèi)容不足的時(shí)候吸附在窗口底部,當(dāng)內(nèi)容多的時(shí)候又可以被抵到窗口下面。
<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;
/* 這里的高度只是為了模擬內(nèi)容多少 */
height: 100px;
/* height: 1000px; */
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
/* 這里的高度只是為了模擬內(nèi)容多少 */
height: 100px;
/* height: 1000px; */
flex-grow: 1;
}
.footer {
height: 50px;
background: lightgreen;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
/* 這里的高度只是為了模擬內(nèi)容多少 */
height: 100px;
/* height: 1000px; */
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
/* 這里的高度只是為了模擬內(nèi)容多少 */
/* height: 100px; */
height: 1000px;
margin-top: 50px;
flex-grow: 1;
}
.footer {
height: 50px;
background: lightgreen;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
/* 這里的高度只是為了模擬內(nèi)容多少 */
/* height: 100px; */
height: 1000px;
flex-grow: 1;
margin-bottom: 50px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
width: 100%;
bottom: 0;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
復(fù)制代碼
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%;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
/* 這里的高度只是為了模擬內(nèi)容多少 */
/* height: 100px; */
height: 1000px;
flex-grow: 1;
margin-bottom: 50px;
margin-top: 50px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
width: 100%;
bottom: 0;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
兩欄布局就是一邊固定,另外一邊自適應(yīng),效果如下
實(shí)現(xiàn)兩欄布局的方法也有很多,筆者接下來(lái)介紹用的比較多的幾種方式。
<div class="aside"></div>
<div class="main"></div>
div {
height: 500px;
}
.aside {
width: 300px;
float: left;
background: yellow;
}
.main {
background: aqua;
margin-left: 300px;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<div class="aside"></div>
<div class="main"></div>
div {
height: 500px;
}
.aside {
width: 300px;
float: right;
background: yellow;
}
.main {
background: aqua;
margin-right: 300px;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
三欄布局就是兩邊固定,中間自適應(yīng)布局,效果如下
實(shí)現(xiàn)三欄布局的方法也有很多,筆者接下來(lái)介紹用的比較多的幾種方式。
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
圣杯布局在項(xiàng)目中基本上不會(huì)再使用了,在面試中我們會(huì)經(jīng)常碰到,所以需要了解。
主要用到了浮動(dòng)和和相對(duì)定位。
<div class="container">
<div class="content">中間內(nèi)容</div>
<div class="left">左側(cè)區(qū)域</div>
<div class="right">右側(cè)區(qū)域</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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
雙飛翼布局在項(xiàng)目中基本上不會(huì)再使用了,在面試中我們會(huì)經(jīng)常碰到,所以需要了解。
主要用到了浮動(dòng)。
<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;
}
點(diǎn)擊查看代碼運(yùn)行實(shí)例
因?yàn)?strong>flex和grid布局方法已經(jīng)很強(qiáng)大了,日常工作中99%的布局都可以使用這兩種方式來(lái)實(shí)現(xiàn)。所以筆者建議能使用flex或者grid布局方法實(shí)現(xiàn)的就盡量使用這兩種布局方式實(shí)現(xiàn)。因?yàn)椴粌H簡(jiǎn)單而且負(fù)面作用也很少。
浮動(dòng)布局和絕對(duì)定位布局會(huì)導(dǎo)致元素脫離文檔流,會(huì)帶來(lái)一些負(fù)面作用,有時(shí)會(huì)導(dǎo)致一些意想不到的結(jié)果。
關(guān)于flex布局的兼容性和grid布局的兼容性,目前已經(jīng)支持的很好了,大家可以放心使用。
flex布局的兼容性
grid布局的兼容性
感謝小伙伴們的耐心觀看,本文為筆者個(gè)人學(xué)習(xí)筆記,如有謬誤,還請(qǐng)告知,萬(wàn)分感謝!如果本文對(duì)你有所幫助,還請(qǐng)點(diǎn)個(gè)關(guān)注點(diǎn)個(gè)贊~,您的支持是筆者不斷更新的動(dòng)力!
擊右上方紅色按鈕關(guān)注“web秀”,讓你真正秀起來(lái)
在日常項(xiàng)目開(kāi)發(fā)中,在布局方面有遇到哪些問(wèn)題了?今天來(lái)一起看看CSS布局有哪些小技巧,后續(xù)開(kāi)發(fā)更輕松。本文主要通過(guò)簡(jiǎn)單的示例,講述開(kāi)發(fā)中遇到的布局等問(wèn)題,但不僅限于布局相關(guān),會(huì)有其他相關(guān)知識(shí)點(diǎn)。
CSS實(shí)用技巧第二講:布局處理
移動(dòng)端使用rem布局需要通過(guò)JS設(shè)置不同屏幕寬高比的font-size,結(jié)合vw單位和calc()可脫離JS的控制,代碼如下:
/* 基于UI width=750px DPR=2的頁(yè)面 */ html { font-size: calc(100vw / 7.5); }
rem 頁(yè)面布局, 不兼容低版本移動(dòng)端,使用需謹(jǐn)慎。
通過(guò)flexbox或inline-block的形式橫向排列元素,對(duì)父元素設(shè)置overflow-x:auto橫向滾動(dòng)查看。注意場(chǎng)景: 橫向滾動(dòng)列表、元素過(guò)多但位置有限的導(dǎo)航欄。
CSS實(shí)用技巧第二講:布局處理
<div class="horizontal-list flex"> <ul> <li>web秀</li> <li>阿里巴巴</li> <li>字節(jié)跳動(dòng)</li> <li>騰訊</li> <li>百度</li> <li>美團(tuán)</li> </ul> </div> <div class="horizontal-list inline"> <ul> <li>web秀</li> <li>阿里巴巴</li> <li>字節(jié)跳動(dòng)</li> <li>騰訊</li> <li>百度</li> <li>美團(tuán)</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; } }
知識(shí)點(diǎn)解析:
1、display: flex布局:又名“彈性布局”,任何一個(gè)容器都可以指定為Flex布局。詳細(xì)內(nèi)容請(qǐng)點(diǎn)擊
《CSS3中Flex彈性布局該如何靈活運(yùn)用?》
2、滾動(dòng)條樣式美化。
如何自定義滾動(dòng)條樣式了? 掌握這3個(gè)選擇器即可。
(1)、::-webkit-scrollbar 定義了滾動(dòng)條整體的樣式;
(2)、::-webkit-scrollbar-thumb 滑塊部分;
(3)、::-webkit-scrollbar-track 軌道部分;
所以上面scss代碼中,我們書(shū)寫(xiě)了這3個(gè)選擇器的樣式,改變了滾動(dòng)條的樣式。
3、linear-gradient線性漸變。語(yǔ)法如下:
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction用角度值指定漸變的方向(或角度), color-stop1, color-stop2,...用于指定漸變的起止顏色。
to right的意思就是從左到右,從一個(gè)顏色過(guò)渡到另外一個(gè)顏色。
請(qǐng)看示例:
CSS實(shí)用技巧第二講:布局處理
更多詳細(xì)內(nèi)容請(qǐng)點(diǎn)擊:
《CSS3線性漸變、陰影、縮放實(shí)現(xiàn)動(dòng)畫(huà)下雨效果》
《CSS3線性徑向漸變、旋轉(zhuǎn)、縮放動(dòng)畫(huà)實(shí)現(xiàn)王者榮耀匹配人員加載頁(yè)面》
《CSS3徑向漸變實(shí)現(xiàn)優(yōu)惠券波浪造型》
在retina屏中,像素比為2(iPhone6/7/8)或3(iPhone6Plus/7Plus/8Plus),1px的邊框看起來(lái)比真的1px更寬。
我們可以通過(guò)偽類加transform的方式解決。
CSS實(shí)用技巧第二講:布局處理
transform:用于元素進(jìn)行旋轉(zhuǎn)、縮放、移動(dòng)或傾斜,和設(shè)置樣式的動(dòng)畫(huà)并沒(méi)有什么關(guān)系,就相當(dāng)于color一樣用來(lái)設(shè)置元素的“外表”。
詳細(xì)transform了解,請(qǐng)點(diǎn)擊:
《CSS3最容易混淆屬性transition transform animation translate》
非常簡(jiǎn)單的方式,flexbox橫向布局時(shí),最后一個(gè)元素通過(guò)margin-left:auto實(shí)現(xiàn)向右對(duì)齊。
請(qǐng)看示例:
<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實(shí)用技巧第二講:布局處理
<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>養(yǎng)生</p> </article> <article> <label for="collapse3">阿里巴巴</label> <p>淘寶<br>阿里云<br>閑魚(yú)<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實(shí)用技巧第二講:布局處理
<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">首頁(yè)</label> <label for="tab2">列表</label> <label for="tab3">其他</label> <label for="tab4">我的</label> </nav> <main> <ul> <li>首頁(yè)</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實(shí)用技巧第二講:布局處理
喜歡小編或者覺(jué)得小編文章對(duì)你有幫助的,可以點(diǎn)擊一波關(guān)注哦!同時(shí),要源碼的小伙伴可以點(diǎn)擊下方“了解更多”。
最后推薦一個(gè)專欄文章,感謝小伙伴們多多支持,謝謝大家!
(OF作品展示)
OF之前介紹了用python實(shí)現(xiàn)數(shù)據(jù)可視化、數(shù)據(jù)分析及一些小項(xiàng)目,但基本都是后端的知識(shí)。想要做一個(gè)好看的小系統(tǒng),我們還要學(xué)一些前端的知識(shí),今天OF將講解如何用pycharm(全棧開(kāi)發(fā)不錯(cuò)的工具)做一張好看的網(wǎng)頁(yè),以后我們就可以自己開(kāi)發(fā)網(wǎng)頁(yè)/網(wǎng)站放到互聯(lián)網(wǎng)上。
主要內(nèi)容:網(wǎng)頁(yè)前端布局
適用人群:Python初學(xué)者,前端初學(xué)者
準(zhǔn)備軟件:pycharm
1) 下載完成pycharm, 點(diǎn)擊file-New Project...
2) 按下圖步驟創(chuàng)建一個(gè)項(xiàng)目,目前我們選擇Pure python即可,以后我們可以學(xué)習(xí)用Django/flask等框架來(lái)做完整的系統(tǒng)
1)在創(chuàng)建的項(xiàng)目空白處鼠標(biāo)右鍵-New-HTML File
2)輸入英文的網(wǎng)頁(yè)名字,盡量不要輸入中文/特殊字符
當(dāng)雙擊打開(kāi)我們創(chuàng)建后的HTML文件,大家會(huì)看到下面的界面
在開(kāi)始開(kāi)發(fā)網(wǎng)頁(yè)前,我們需要自己設(shè)計(jì)下想要把網(wǎng)頁(yè)做成什么樣,為了節(jié)省成本OF都是自己設(shè)計(jì)的頁(yè)面樣式,可以手繪,也可以在PPT上畫(huà)。
我們先學(xué)習(xí)一個(gè)比較簡(jiǎn)單的布局如下圖,大家可以看到下圖已經(jīng)畫(huà)出了我們需要添加的內(nèi)容,要注意的地方是比如Taylor的圖片和文字一定要用<div class=bord>框住,否則Taylor圖片與文字將會(huì)是左右的關(guān)系,而不是上下
根據(jù)上述的css名字定義,先填充<body>內(nèi)的代碼,那么我們就完成一半的工作了,代碼如下:
<!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)鼠標(biāo)移到代碼處,在右上角我們會(huì)看到一排瀏覽器,我們點(diǎn)擊其中一個(gè)運(yùn)行
2)目前看到的網(wǎng)頁(yè)內(nèi)容從上到下顯示
首先我們簡(jiǎn)要了解下flex布局,大家可以看到下圖中#main的style樣式中display:flex,在body部分將3個(gè)顏色內(nèi)容框在<div id="main">中,運(yùn)行結(jié)果是3個(gè)顏色的內(nèi)容橫向展示了,而不是上下
1)那么我們先從“人物介紹”的布局開(kāi)始,“人物介紹”居中展現(xiàn)(用flex中justify-content:center),而且下面有一條黑線,OF準(zhǔn)備用border樣式來(lái)實(shí)現(xiàn),所以在<div id=intro>里另加了個(gè)<div class=peo>,代碼如下:
(注:style中的#main對(duì)應(yīng)body中的id=main, .main對(duì)應(yīng)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>
運(yùn)行結(jié)果:
2)圖片部分是3個(gè)<div class=bord>橫向展示,所以要在框住它們的<div id=pic1>樣式中設(shè)置flex布局,在<style>里加入以下代碼:
#pic1 {
display: -webkit-flex; /* Safari */
display: flex;
justify-content: center;
}
運(yùn)行結(jié)果:
3)圖片之間靠太近,圖片大小不一致,文字沒(méi)居中,我們?cè)?lt;style>里加入以下代碼:
.bord{
padding: 1rem 2rem;
}
.img {
border: 0.2rem solid #e3e3e3;
max-width: 15rem;
height: 22rem;
}
.word {
text-align: center;
}
運(yùn)行結(jié)果:
今天我們學(xué)會(huì)了flex布局,今后所有的網(wǎng)頁(yè)排版都可以實(shí)現(xiàn)了,祝愿大家都有所收獲,完整的代碼如下:
<!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>
今天的知識(shí)比較基礎(chǔ)但非常實(shí)用,每天學(xué)會(huì)一個(gè)小技能,積少成多,以后就能成為大神。如果大家對(duì)網(wǎng)頁(yè)上的實(shí)現(xiàn)有什么不懂的,盡請(qǐng)留言,OF一定會(huì)一一解答的。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。