ITPub博客

首页 > 应用开发 > Html/css > 用CSS Frasbox创建一个干净的CSS比较表

用CSS Frasbox创建一个干净的CSS比较表

原创 Html/css 作者:sadf123 时间:2019-11-08 11:50:18 0 删除 编辑

比较表是由多个相互堆叠的ULs组成的。我将使用CSS Flex框使它们都相等的宽度,并以相同的速率扩展和收缩,因此它们的行为与表中的行类似。

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
Thirteen
Fourteen
Fifteen
Sixteen
Seventeen
Eighteen
Nineteen
Twenty
Twenty-one
Twenty-two
Twenty-three
Twenty-four
Twenty-five
Twenty-six
Twenty-seven
Twenty-eight
Twenty-nine
Thirty
Thirty-one
Thirty-two
Thirty-three
Thirty-four
Thirty-five
Thirty-six
Thirty-seven
Thirty-eight
Thirty-nine
Forty
Forty-one
Forty-two
Forty-three
Forty-four
Forty-five
Forty-six
Forty-seven
Forty-eight
Forty-nine
Fifty
Fifty-one
<div class= "comparisontable" >
     <ul class= "row" &#62;
         <li class= "legend" >Office Chairs</li>
         <li><img src= "Eurotech-chair.jpg" /><br />Eurotech</li>
         <li><img src= "Hbada-chair.jpg" /><br />Hbada</li>
         <li><img src= "Zenith-chair.jpg" /><br />Zenith</li>
         <li><img src= "amazonbasics-chair.jpg" /><br />Amazonbasics</li>
     </ul>
    
     <ul class= "row" &#62;
         <li class= "legend" >Weight</li>
         <li>25kg</li>
         <li>13kg</li>
         <li>17kg</li>
         <li>28kg</li>
     </ul>
    
     <ul class= "row" &#62;
         <li class= "legend" >Cost</li>
         <li>$$</li>
         <li>$</li>
         <li>$$</li>
         <li>$</li>
     </ul>
    
     <ul class= "row" &#62;
         <li class= "legend" >Delivery</li>
         <li>Domestic</li>
         <li>International</li>
         <li>International</li>
         <li>Domestic</li>
     </ul>
    
     <ul class= "row" &#62;
         <li class= "legend" >Verdict</li>
         <li>Best Chair for Back Pain</li>
         <li>Best budget chair</li>
         <li>All Mesh for Cooling</li>
         <li>Only Chair with Top Grain Leather</li>
     </ul>
    
     <ul class= "row" &#62;
         <li class= "legend" ></li>
         <li><a href= "" class= "calltoaction" >Buy Now</a></li>
         <li><a href= "" class= "calltoaction" >Buy Now</a></li>
         <li><a href= "" class= "calltoaction" >Buy Now</a></li>
         <li><a href= "" class= "calltoaction" >Buy Now</a></li>
     </ul>
</div>

每个UL中的第一个Li元素是特征/图例。为了轻松地将它们与其他的包装不同,我把那些Li元素作为一个“传奇”的CSS类。

CSS:

现在是有趣的部分-将标记转换为比较表。使用CSS FrasBox,转换UL元素相对容易,因此它们在布局、弯曲和拉伸方面是水平的,因此它们的行为更像表单元。

我已经删除了一些不必要的线,所以你可以把重点放在下面的重要位置:

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
Thirteen
Fourteen
Fifteen
Sixteen
Seventeen
Eighteen
Nineteen
Twenty
Twenty-one
Twenty-two
Twenty-three
Twenty-four
Twenty-five
Twenty-six
Twenty-seven
Twenty-eight
Twenty-nine
Thirty
Thirty-one
Thirty-two
Thirty-three
Thirty-four
Thirty-five
Thirty-six
Thirty-seven
Thirty-eight
Thirty-nine
Forty
Forty-one
Forty-two
Forty-three
Forty-four
Forty-five
Forty-six
Forty-seven
Forty-eight
div.comparisontable{
     显示:flex;
     FLEX-Direction:Column; /* turn children ul elements into stacked rows */
}
div.comparisontable ul.row{
     列表样式:无;
     显示:flex; /* turn children li elements into flex children */
     Flex:1;
     flex-wrap: wrap;
}
div.comparisontable ul.row li{
     背景: #c9f4ca;
     Flex:1;
     PADDING:10PX;
     Border-bottom:1px solid gray;
}
/* the legend column (first li within each row) */
div.comparisontable ul.row li.legend{
     背景: #6640d8;
     color: white;
     边界:无;
     宽度:200px;
     border-bottom: 1px solid white;
}
/* very first row */
div.comparisontable ul.row:first-of-type li{
     Text-align:Center;
}
/* very last row */
div.comparisontable ul.row:last-of-type li{
     Text-align:Center;
     边界底部:无;
     P.O.Box-Shadow:0 6PX 6PX RGBA(0.0,0.23);
}
/* first and last cell within legend column */
div.comparisontable ul.row:first-of-type li.legend.legend,
div.comparisontable ul.row:last-of-type li.legend{
     背景:透明;
     盒影:无;
}

“特征列表”或“传奇”列是每行的第一个Li元素。它具有明确的宽度为200 px,不像其他的Li元素是Flex宽度(Flex:1)。

比较表的制作

现在,比较表没有响应。也就是说,即使屏幕尺寸变得非常小,每行中的“列”仍然并排。

在CSS Flex框中,通过将Flex方向属性从“行”设置为“列”,可以轻松地将Flex子元素的显示顺序从默认的“并排”行为改为“在下一个上叠加”。

现在在我的比较表中,UL元素本身是堆叠的,尽管孩子的Li元素像表中的单元格一样并排出现。当屏幕足够小的时候,我会把它们改成堆叠。

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Eleven
Twelve
Thirteen
Fourteen
Fifteen
Sixteen
Seventeen
Eighteen
Nineteen
Twenty
Twenty-one
Twenty-two
Twenty-three
Twenty-four
Twenty-five
Twenty-six
Twenty-seven
@media screen and (max-width:650px){
div.comparisontable ul.row{
     FLEX-Direction:Column;
}
div.comparisontable img{
     width: auto;
     高度:自动;
}
div.comparisontable ul.row li{
     margin-right: 0;
     width: auto;
     flex: auto;
}
/* first and last cell within legend column */
div.comparisontable ul.row:first-of-type li.legend.legend,
div.comparisontable ul.row:last-of-type li.legend{
     显示:无;
}
div.comparisontable ul.row li.legend{
     width: auto;
}
}

现在,当我调整窗口大小时,比较表崩溃,每个“单元格”出现在自己的行上:


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69953037/viewspace-2663204/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-11-07

  • 博文量
    7
  • 访问量
    8599