ITPub博客

首页 > IT职业 > IT职场 > 使用CSS Flexbox创建等高定价表

使用CSS Flexbox创建等高定价表

原创 IT职场 作者:cenfeng 时间:2019-07-22 17:57:07 0 删除 编辑

HTML标记

让我从HTML标记开始,我希望尽可能保持干净和精益。 为此,我只为每个单独的定价表使用UL列表,所有这些都包含在DIV容器中:

1
2
3
4
五
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" ><div class =“pricingdiv”></font></font><font></font><font style= "vertical-align: inherit;" ><font style= "vertical-align: inherit;" >
     <ul class =“theplan”></font></font><font></font>
         <li class= "title" ><b>2nd Place</b><br />Herman Miler</li><font></font>
         <li><b>Weight:</b> 55 lbs</li><font></font>
         <li><b>Max Weight:</b> 330 lbs</li><font></font>
         <li><a class= "pricebutton" href= "#" ><span class= "icon-tag" ></span> Check Out</a></li><font></font>
     </ul><font></font>
     <ul class= "theplan" ><font></font>
         <li class= "title" ><b>1st Place</b><br />Argomax Chair</li><font></font>
         <li><b>Material:</b> Nylon w/ Breathable Glass Fiber</li><font></font>
         <li><b>Head Rest:</b> Yes</li><font></font>
         "<font></font>
         "<font></font>
         <li><a class= "pricebutton" href= "#" ><span class= "icon-tag" ></span> Check Out</a></li><font></font>
     </ul><font></font>
     <ul class= "theplan" ><font></font>
         <li class= "title" ><b>3rd Place</b><br />Eurotech Mesh</li><font></font>
         <li class= "ethighlight" ><b>Dimensions:</b> 24.8W x 47.3H</li><font></font>
         "<font></font>
         <li><a class= "pricebutton" href= "" ><span class= "icon-tag" ></span> Check Out</a></li><font></font>
     </ul><font></font>
</div>

如您所见,每个 UL.theplan 元素包含不同数量的LI条目。 目标是使每个UL具有相同的高度,并且每个定价计划的最后一个LI条目在最底部排列。

我发现这是最简单的方法吗? 使用CSS flexbox并设置每个UL,  flex-direction:column 使它们垂直扩展以匹配最长的flex儿童的高度。 我将在下面详细解释。

CSS

这是等高定价表的CSS。 我已经删除了不重要的位,因此您可以专注于重要的事情:

1
2
3
4
五
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
三十
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
.pricingdiv{<font></font>
     display: flex;<font></font>
     flex-wrap: wrap;<font></font>
     justify-content: center;<font></font>
}<font></font>
<font></font>
.pricingdiv ul.theplan{<font></font>
     list-style: none;<font></font>
     margin: 0;<font></font>
     padding: 0;<font></font>
     display: flex;<font></font>
     flex-direction: column;<font></font>
     width: 260px; /* width of each table */ <font></font>
     margin-right: 20px; /* spacing between tables */ <font></font>
     margin-bottom: 1em;<font></font>
     border: 1px solid gray;<font></font>
     transition: all .5s;<font></font>
}<font></font>
<font></font>
.pricingdiv ul.theplan:hover{ /* when mouse hover over pricing table */ <font></font>
     transform: scale(1.05);<font></font>
     transition: all .5s;<font></font>
     z-index: 100;<font></font>
     box-shadow: 0 0 10px gray;<font></font>
}<font></font>
<font></font>
.pricingdiv ul.theplan:last-of-type{ /* remove right margin in very last table */ <font></font>
     margin-right: 0;<font></font>
}<font></font>
<font></font>
/*very last LI within each pricing UL */ <font></font>
.pricingdiv ul.theplan li:last-of-type{<font></font>
     text-align: center;<font></font>
     margin-top: auto; /*align last LI (price botton li) to the very bottom of UL */ <font></font>
}  <font></font>
<font></font>
<font></font>
@media only screen and (max-width: 600px) {<font></font>
     .pricingdiv ul.theplan{<font></font>
         border-radius: 0;<font></font>
         width: 100%;<font></font>
         margin-right: 0;<font></font>
     }<font></font>
<font></font>
     .pricingdiv ul.theplan:hover{<font></font>
         transform: none;<font></font>
         box-shadow: none;<font></font>
     }<font></font>
     <font></font>
     .pricingdiv a.pricebutton{<font></font>
         display: block;<font></font>
     }<font></font>
}

我首先将父DIV容器设置为 display:flex ,并允许flex子项包装并使用 flex-wrap: wrap  和 水平居中 justify-content: center 。 所有儿童UL元素都被认为是儿童。

对于每个由UL元素组成的定价表,我都设置了  flex-direction:column 。 默认情况下,flex子项在水平轴上播放。 通过设置 direction:column ,我强制Flex子项的所有默认行为发生在垂直平面上,包括 默认情况下的 金奖 等于高度flex儿童 。

底部对齐每个UL定价表中的最后一个LI

因此,DIV中的所有单独定价表现在都是相同的高度,但是仍然需要一个重要的改进来使一切看起来都很精致。 我希望调用操作按钮(包含在每个UL的最后一个LI中)与表格的最底部对齐。

要做到这一点涉及两个步骤。 首先,我将每个UL定价表设置为一个flexbox容器本身( display: flex )。 完成后,我可以使用该 margin 属性将特定子元素与其对等元素对齐,例如左对齐或右对齐以用于水平弹性子元素,或者在此情况下为垂直弹性子元素,顶部或底部。

为了使最后一个LI元素在底部对齐,要添加的神奇成分 margin-top: auto 在这些元素中:

1
2
3
4
.pricingdiv ul.theplan li:last-of-type{<font></font>
     text-align: center;<font></font>
     margin-top: auto; /*align last LI (price botton li) to the very bottom of UL */ <font></font>
}


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

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

注册时间:2019-06-18

  • 博文量
    125
  • 访问量
    129476