前言
在日常項目開發中,在布局方面有遇到哪些問題了?今天來一起看看css布局有哪些小技巧,后續開發更輕松。本文主要通過簡單的示例,講述開發中遇到的布局等問題,但不僅限于布局相關,會有其他相關知識點。
CSS實用技巧第二講:布局處理
rem自適應布局
移動端使用rem布局需要通過JS設置不同屏幕寬高比的font-size,結合vw單位和calc()可脫離JS的控制,代碼如下:
/* 基于UI width=750px DPR=2的頁面 */
html {
font-size: calc(100vw / 7.5);
}
rem 頁面布局, 不兼容低版本移動端,使用需謹慎。
overflow-x排版橫向列表
通過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徑向漸變實現優惠券波浪造型》
移動端1px邊框解決方案
在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實用技巧第二講:布局處理
純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實用技巧第二講:布局處理
純CSS Tabbar切換效果
<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實用技巧第二講:布局處理
喜歡小編或者覺得小編文章對你有幫助的,可以點擊一波關注哦!同時,要源碼的小伙伴可以點擊下方“了解更多”。
最后推薦一個專欄文章,感謝小伙伴們多多支持,謝謝大家!






