首頁  >  文章  >  web前端  >  css的開發小技巧

css的開發小技巧

php中世界最好的语言
php中世界最好的语言原創
2017-11-27 10:24:551409瀏覽

關於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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn