css的開發小技巧
發表時間:2023-12-25 來源:明輝站整理相關軟件相關文章人氣:
[摘要]關于css技巧類的在網站里已經發了很多了,那么我今天在總結歸納一下平時在工作中可以遇到問題,并且說一下用CSS的解決方案,什么是多列等高布局?點擊增加一側文字,另一側背景也增加。html代碼:<div id="container"><div class=&qu...
關于css技巧類的在網站里已經發了很多了,那么我今天在總結歸納一下平時在工作中可以遇到問題,并且說一下用CSS的解決方案,什么是多列等高布局?
點擊增加一側文字,另一側背景也增加。
html代碼:
<div id="container">
<div class="left">haorooms多列等高布局左</div>
<div class="right" id="rights">多列等高布局,使用正負 margin 與 padding 相沖的方式實現。</div></div>
方法一: 使用正負 margin 與 padding 相沖的方式實現
#container{
width:400px;
margin:0 auto;
background:#eee;
overflow:hidden;}.left,.right{
width:200px;
float:left;
font-size: 16px;
line-height:24px;
color:#333;
padding-bottom:5000px;
margin-bottom:-5000px;}.left{
background-color: deeppink;}.right{
background-color:yellowgreen;}
給一個足夠大的padding和負margin
二、使用 display:flex 的方式實現
這個方式很簡單,移動端我們經常用,container 設置為display:flex,子元素設置為flex:1就可以了。
三、display:table-cell 實現
和上面方法類似,container 設置為display:table;,子元素設置為display:table-cell;就可以了。
四、 父容器設置背景色實現
如下:
#container{
width:400px;
margin:0 auto;
background-color: deeppink;
overflow:hidden;}.left,.right{
width:200px;
float:left;
font-size: 16px;
line-height:24px;
color:#333;}.right{
background-color:yellowgreen;}
五、父容器多重背景色--線性漸變
#container{
width:400px;
margin:0 auto;
background-image:
linear-gradient(90deg, yellowgreen 50%, deeppink 0);
overflow:hidden;}.left,.right{
width:200px;
float:left;
font-size: 16px;
line-height:24px;
color:#333;}
六、border實現
#container{
border-left:200px solid yellowgreen;
background-color:deeppink;
width:200px;
font-size: 16px;
line-height:24px;
color:#333;
}
.left{
width:200px;
margin-left:-200px;
float:left;
}
多列均勻布局
方法一:display:flex
這種方法上面也講過,實現起來比較簡單,適合移動端布局。
方法二:借助偽元素及 text-align:justify
html代碼如下:
<div class="container">
<div class="justify">
<i>1</i>
<i>2</i>
<i>3</i>
<i>4</i>
<i>5</i>
</div></div>
css代碼如下:
.justify{ text-align: justify; text-align-last: justify; // 新增這一行}.justify i{ width:24px; line-height:24px; display:inline-block; text-align:center;}
text-align-last兼容性不是很好,可以使用::after,
.justify{ text-align: justify;}.justify i{ width:24px; line-height:24px; display:inline-block; text-align:center; border-radius:50%;}.justify:after { content: ""; display: inline-block; position: relative; width: 100%;}
列表布局邊界線問題
方法一:margin負邊距
思路:
外層設置width,overflow設置為hidden,內層設置負邊距,margin-left:-1px;就可以把左側邊距隱藏
html代碼如下:
<div class="ul-container">
<ul>
<li>haorooms</li>
<li>測試</li>
<li>hao測試</li>
<li>右側</li>
<li>邊界線</li>
<li>消失</li>
<li>測試</li>
</ul></div>
css代碼:
ul{
width: 300px;
margin-left:-1px;}li{
float:left;
width:99px;
line-height:30px;
text-align:center;
border-left:1px solid #999;
font-size:18px;
margin-bottom:10px;}.ul-container{
width: 300px;
margin: 50px auto;
overflow:hidden;
background: #eee;
padding:10px 0;}
方法二 :使用偽類選擇器
// 使用偽類選擇器,選擇第 3n 個元素去掉邊框li:nth-child(3n){
border-right:none;}
在工作中常見的問題就這么多了,更多精彩請關注php中文網其它相關文章!
相關閱讀:
CSS3怎么制作蝴蝶飛舞的動畫
css3點擊顯示漣漪特效
利用CSS3怎么做出不規則圖片的切換特效制作
以上就是css的開發小技巧的詳細內容,更多請關注php中文網其它相關文章!
網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。