简体   繁体   English

Flex项目中的底部对齐文本

[英]Bottom-align text in a flex item

I am working on a website where we have rows of buttons - they are an image, then an h3, and then a button, all stacked on top of each other. 我正在一个网站上工作,我们有一排按钮 - 它们是一个图像,然后是一个h3,然后是一个按钮,它们全部堆叠在一起。

These are all displayed using flexbox and flex-direction: column and a series of margin-top: auto rules. 这些都是使用flexbox和flex-direction: column和一系列margin-top: auto规则显示的。

The h3s sometimes break onto two lines. h3有时会分成两行。 When this happens, the button text aligns to the center of the h3 tag: sort of like a hyphen and an equal sign next to each other. 当发生这种情况时,按钮文本与h3标签的中心对齐:有点像连字符和彼此相邻的等号。

- = = instead of - - - - = =而不是- - -

Client would like the bottom line of the h3 text to align. 客户希望h3文本的底线对齐。 I can't figure out how to do this exactly... Can anyone help? 我无法弄清楚如何做到这一点......任何人都可以帮忙吗?

文本行在容器底部对齐

https://codepen.io/anon/pen/zLVMPo https://codepen.io/anon/pen/zLVMPo

 div.row { width: 100%; float: left; background-repeat: no-repeat !important; } a { color: #337ab7; text-decoration: none; } .service-square-row .span4, .service-square-row .span3, .service-square-row .span6 { display: flex; flex-direction: column; } .service-square-row .wrapper { display: flex; justify-content: space-evenly; flex-wrap: wrap; } .services.service-square-row .span4 { width: 260px; border-radius: 3px; margin: 10px; position: relative; background: #013ca6; max-width: 260px; height: 300px; } .service-square p:first-child { margin-top: 15px; height: 126px; } .service-square-row .flex-link .span4 img, .service-square-row .flex-link .span3 img { max-height: 126px; } img { vertical-align: middle; } img, video { height: auto; max-width: 100%; } .service-square-row .flex-link img, .service-square-row .flex-link img { height: 126px; width: 126px } .service-square-row h3 { margin-top: auto; font-size: 20px; width: 95%; padding: 0; margin-left: 2.5%; margin-right: 2.5%; color: white; text-transform: uppercase; font-weight: 400; font-size: 25px; } .service-square-row .service-square p:last-child { margin-top: auto; margin-bottom: 30px; text-align: center; } a.btn-bt.default, a.btn-bt.alternate { font-weight: 400; border-radius: 4px; text-transform: uppercase; text-align: center; letter-spacing: 0; padding: 10px 50px; } a.btn-bt.alternate { color: #ffffff; font-size: 14px; background: #e31d1a; letter-spacing: px; } 
 <div class="row three-thirds services service-square-row default-padding light-header light-content" style=""> <div class="wrapper"> <a class="flex-link" href="/electronics-restoration/"> <div class="span4 service-square sq-1 service-1" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-484" src="https://via.placeholder.com/120x120" alt="" width="120" height="120"></p> <h3 style="text-align: center;">Electronics</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="/electronics-restoration/">Learn More</a></p> </div> </a> <a class="flex-link" href="/art-and-document-recovery/"> <div class="span4 service-square sq-2 service-2" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-485" src="https://via.placeholder.com/120x120" alt="" width="106" height="139"></p> <h3 style="text-align: center;">Art &amp; Document Recovery</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="/art-and-document-recovery/">Learn More</a></p> </div> </a> <a class="flex-link" href="#"> <div class="span4 service-square sq-3 service-3" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-486" src="https://via.placeholder.com/120x120" alt="" width="140" height="143"></p> <h3 style="text-align: center;">Wind, Hurricane, &amp; Tornado</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="#">Learn More</a></p> </div> </a> </div> </div> 

EDIT: Unfortunately, codepen seems to have interpreted the HTML I pasted into it differently that Wordpress did... And so the codepen I had everyone work produced solutions I couldn't use on my live site. 编辑:不幸的是,codepen似乎已经解释了我粘贴到它的HTML不同于Wordpress所做的...所以我让每个人都工作的codepen产生了我无法在我的实时网站上使用的解决方案。 Oh well -- I ended up figuring it out on the live site anyways. 哦 - 好吧 - 无论如何我最终在现场网站上搞清楚了。 Sorry for not noticing earlier. 很抱歉没有提前注意到。

You have the "buttons" set to column-direction flex containers: 您将“按钮”设置为列方向Flex容器:

.service-square-row .span4  {
    display: flex;
    flex-direction: column;
}

But the first child (flex item) of the container is set to flex-grow: 0 , which is the default setting. 但是容器的第一个子项(flex项)设置为flex-grow: 0 ,这是默认设置。 So the h3 text has a limited range of motion. 所以h3文本的运动范围有限。

Try this instead: 试试这个:

.span4 > .flex-link {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background-color: green;
}

.service-square-row .service-square p:last-child {
    /* margin-top: auto; */
    margin-bottom: 30px;
    text-align: center;
}

revised codepen 修改后的codepen

 div.row { width: 100%; float: left; background-repeat: no-repeat !important; } a { color: #337ab7; text-decoration: none; } .service-square-row .span4, .service-square-row .span3, .service-square-row .span6 { display: flex; flex-direction: column; } .service-square-row .wrapper { display: flex; justify-content: space-evenly; flex-wrap: wrap; } .services.service-square-row .span4 { width: 260px; border-radius: 3px; margin: 10px; position: relative; background: #013ca6; max-width: 260px; height: 300px; } .service-square p:first-child { margin-top: 15px; height: 126px; } .service-square-row .flex-link .span4 img, .service-square-row .flex-link .span3 img { max-height: 126px; } img { vertical-align: middle; } img, video { height: auto; max-width: 100%; } .service-square-row .flex-link img, .service-square-row .flex-link img { height: 126px; width: 126px } .service-square-row h3 { margin-top: auto; font-size: 20px; width: 95%; padding: 0; margin-left: 2.5%; margin-right: 2.5%; color: white; text-transform: uppercase; font-weight: 400; font-size: 25px; } .service-square-row .service-square p:last-child { /* margin-top: auto; */ margin-bottom: 30px; text-align: center; } a.btn-bt.default, a.btn-bt.alternate { font-weight: 400; border-radius: 4px; text-transform: uppercase; text-align: center; letter-spacing: 0; padding: 10px 50px; } a.btn-bt.alternate { color: #ffffff; font-size: 14px; background: #e31d1a; letter-spacing: px; } .span4>.flex-link { flex: 1; display: flex; flex-direction: column; justify-content: space-between; background-color: green; /* demo only; illustrates full range of element */ } 
 <div class="row three-thirds services service-square-row default-padding light-header light-content" style=""> <div class="wrapper"> <a class="flex-link" href="/electronics-restoration/"> <div class="span4 service-square sq-1 service-1" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-484" src="https://via.placeholder.com/120x120" alt="" width="120" height="120"></p> <h3 style="text-align: center;">Electronics</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="/electronics-restoration/">Learn More</a></p> </div> </a> <a class="flex-link" href="/art-and-document-recovery/"> <div class="span4 service-square sq-2 service-2" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-485" src="https://via.placeholder.com/120x120" alt="" width="106" height="139"></p> <h3 style="text-align: center;">Art &amp; Document Recovery</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="/art-and-document-recovery/">Learn More</a></p> </div> </a> <a class="flex-link" href="#"> <div class="span4 service-square sq-3 service-3" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-486" src="https://via.placeholder.com/120x120" alt="" width="140" height="143"></p> <h3 style="text-align: center;">Wind, Hurricane, &amp; Tornado</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="#">Learn More</a></p> </div> </a> </div> </div> 

If you want something to align itself to the bottom center of a container, the route I usually take is to set the parent as display: flex; 如果你想让一些东西与容器的底部中心对齐,我通常采用的路线是将父设置为display: flex; and the children as align-self: flex-end; 和孩子们作为align-self: flex-end;

If you want to maintain the same structure of your HTML, the best route would be to set the height of your link container to be a percentage of the parent, align the <h3> to the bottom of its container, and center it. 如果要保持HTML的相同结构,最好的方法是将链接容器的高度设置为父级的百分比,将<h3>与其容器的底部对齐,然后将其居中。

.service-square .flex-link {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  height: 80%;
}

.service-square h3 {
  align-self: flex-end;
  margin-bottom: 0;
}

Hopefully I understood your question correctly, let me know if you have any questions or I misunderstood the initial request. 希望我能正确理解你的问题,如果你有任何问题或者我误解了最初的要求,请告诉我。

 div.row { width: 100%; float: left; background-repeat: no-repeat !important; } a { color: #337ab7; text-decoration: none; } .service-square-row .span4, .service-square-row .span3, .service-square-row .span6 { display: flex; flex-direction: column; } .service-square-row .wrapper { display: flex; justify-content: space-evenly; flex-wrap: wrap; } .services.service-square-row .span4 { width: 260px; border-radius: 3px; margin: 10px; position: relative; background: #013ca6; max-width: 260px; height: 300px; } .service-square p:first-child { margin-top: 15px; height: 126px; } .service-square-row .flex-link .span4 img, .service-square-row .flex-link .span3 img { max-height: 126px; } img { vertical-align: middle; } img, video { height: auto; max-width: 100%; } .service-square-row .flex-link img, .service-square-row .flex-link img { height: 126px; width: 126px } .service-square-row h3 { margin-top: auto; font-size: 20px; width: 95%; padding: 0; margin-left: 2.5%; margin-right: 2.5%; color: white; text-transform: uppercase; font-weight: 400; font-size: 25px; } .service-square-row .service-square p:last-child { margin-top: auto; margin-bottom: 30px; text-align: center; } a.btn-bt.default, a.btn-bt.alternate { font-weight: 400; border-radius: 4px; text-transform: uppercase; text-align: center; letter-spacing: 0; padding: 10px 50px; } a.btn-bt.alternate { color: #ffffff; font-size: 14px; background: #e31d1a; letter-spacing: px; } /* ===== New content ===== */ .service-square .flex-link { display: flex; flex-wrap: wrap; justify-content: center; height: 80%; } .service-square h3 { align-self: flex-end; margin-bottom: 0; } 
 <div class="row three-thirds services service-square-row default-padding light-header light-content" style=""> <div class="wrapper"> <a class="flex-link" href="/electronics-restoration/"> <div class="span4 service-square sq-1 service-1" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-484" src="https://via.placeholder.com/120x120" alt="" width="120" height="120"></p> <h3 style="text-align: center;">Electronics</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="/electronics-restoration/">Learn More</a></p> </div> </a> <a class="flex-link" href="/art-and-document-recovery/"> <div class="span4 service-square sq-2 service-2" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-485" src="https://via.placeholder.com/120x120" alt="" width="106" height="139"></p> <h3 style="text-align: center;">Art &amp; Document Recovery</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="/art-and-document-recovery/">Learn More</a></p> </div> </a> <a class="flex-link" href="#"> <div class="span4 service-square sq-3 service-3" style="border-style:;"> <p style="text-align: center;"><img class="alignnone size-full wp-image-486" src="https://via.placeholder.com/120x120" alt="" width="140" height="143"></p> <h3 style="text-align: center;">Wind, Hurricane, &amp; Tornado</h3> <p style="text-align: center;"><a class="btn-bt alternate" href="#">Learn More</a></p> </div> </a> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM