简体   繁体   English

如何在 Bootstrap 5“col”class div 中垂直对齐我的文本内容?

[英]How do I vertically align my text content in a Bootstrap 5 "col" class div?

I'm trying to build a page with Bootstrap 5 but I'm having issues vertically-aligning my text in their divs.我正在尝试使用 Bootstrap 5 构建一个页面,但我在垂直对齐我的 div 中的文本时遇到了问题。 Here's what I have:这是我所拥有的:

 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <div class="container"> <br> <div class="row"> <div class="col" style="background-color:#cbedca;"> <div style="margin-top:80px;margin-bottom:80px;"> <center> <h3>Ocean pollution is a massive environmental crisis.</h3> There is currently over 5.25 trillion macro and micro pieces of plastic in our oceans. Plastic bottles are a single-use plastic, meaning they are a product designed to be used only once and then discarded. CODi is committed to giving back to the environment in any way we can. </center> </div> </div> </div> <div class="row"> <div class="col-6"> <img src="https://cdn.shopify.com/s/files/1/0615/5403/2853/files/CODi_TerraPlasticBottles.png?v=1664543910" style="width:100%;"> </div> <div class="col" style="background-color:#d9f4ff;line-height:1em;"> <div> <center> Made from <br><b style="font-size:3em;line-height:1em">30</b> <br><b>OCEAN-BOUND <br>PLASTIC BOTTLES</b> </center> </div> </div> </div> <div class="row"> <div class="col-6"> <img src="https://cdn.shopify.com/s/files/1/0615/5403/2853/files/CODi_PlasticBottles1.png?v=1664550375" style="width:100%"> </div> <div class="col"> <h4>Every day around 8 million pieces of plastic makes their way into our oceans.</h4> Once in the ocean, a single-use bottle moves with the currents of the wind and the ocean and can take <b>hundreds of years to break down into microplastic</b>. </div> </div> </div>

I've tried assigning the "align-middle" class to the col divs but have had no luck.我试过将“align-middle”class 分配给 col div,但没有成功。 I'm sure I'm missing something simple but I just can't seem to figure it out.我确定我错过了一些简单的东西,但我似乎无法弄清楚。

You may have to tweak your html markup a little, but you can put align-items: center;您可能需要稍微调整一下 html 标记,但您可以放置align-items: center; onto the "row" div which will give you vertical alignment"row" div 上,它会给你垂直 alignment

You can use d-flex and align-items-center on the cols to center their contents vertically, and put the contents of the cols in a single <div class="text-center"> to center its contents horizontally as a single flex item.您可以在 cols 上使用d-flexalign-items-center将其内容垂直居中,并将 cols 的内容放在单个<div class="text-center">中以将其内容水平居中作为单个 flex物品。 (Don't use <center> to center items, as it is deprecated.): (不要使用<center>来居中项目,因为它已被弃用。):

 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <div class="container"> <br> <div class="row"> <div class="col" style="background-color:#cbedca;"> <div style="margin-top:80px;margin-bottom:80px;"> <center> <h3>Ocean pollution is a massive environmental crisis.</h3> There is currently over 5.25 trillion macro and micro pieces of plastic in our oceans. Plastic bottles are a single-use plastic, meaning they are a product designed to be used only once and then discarded. CODi is committed to giving back to the environment in any way we can. </center> </div> </div> </div> <div class="row"> <div class="col-6"> <img src="https://cdn.shopify.com/s/files/1/0615/5403/2853/files/CODi_TerraPlasticBottles.png?v=1664543910" style="width:100%;"> </div> <div class="col d-flex align-items-center" style="background-color:#d9f4ff;line-height:1em;"> <div class="mx-auto text-center"> Made from <br><b style="font-size:3em;line-height:1em">30</b> <br><b>OCEAN-BOUND <br>PLASTIC BOTTLES</b> </div> </div> </div> <div class="row"> <div class="col-6"> <img src="https://cdn.shopify.com/s/files/1/0615/5403/2853/files/CODi_PlasticBottles1.png?v=1664550375" style="width:100%"> </div> <div class="col d-flex align-items-center"> <div class="text-center"> <h4>Every day around 8 million pieces of plastic makes their way into our oceans.</h4> Once in the ocean, a single-use bottle moves with the currents of the wind and the ocean and can take <b>hundreds of years to break down into microplastic</b>. </div> </div> </div> </div>

I believe that using flexbox might be the way to do this.我相信使用flexbox可能是做到这一点的方法。 It's the best way to align your elements the way you need and want.这是按照您需要和想要的方式对齐元素的最佳方式。

  • use display: flex to make your element flexible.使用display: flex使您的元素灵活。
  • use align-items: center to align your items vertically to the center.使用align-items: center将您的项目垂直对齐到中心。
  • use justify-content: center to align your items horizontally to the center.使用justify-content: center将您的项目水平对齐到中心。

Also, don't use <center> , it is deprecated.另外,不要使用<center> ,它已被弃用。

 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> <div class="container"> <br> <div class="row"> <div class="col" style="background-color:#cbedca;"> <div style="margin-top:80px;margin-bottom:80px;"> <center> <h3>Ocean pollution is a massive environmental crisis.</h3> There is currently over 5.25 trillion macro and micro pieces of plastic in our oceans. Plastic bottles are a single-use plastic, meaning they are a product designed to be used only once and then discarded. CODi is committed to giving back to the environment in any way we can. </center> </div> </div> </div> <div class="row"> <div class="col-6"> <img src="https://cdn.shopify.com/s/files/1/0615/5403/2853/files/CODi_TerraPlasticBottles.png?v=1664543910" style="width:100%;"> </div> <div class="col" style="background-color:#d9f4ff;line-height:1em; display: flex; align-items: center; justify-content: center;"> <div style="text-align: center;"> Made from <br><b style="font-size:3em;line-height:1em">30</b> <br><b>OCEAN-BOUND <br>PLASTIC BOTTLES</b> </div> </div> </div> <div class="row"> <div class="col-6"> <img src="https://cdn.shopify.com/s/files/1/0615/5403/2853/files/CODi_PlasticBottles1.png?v=1664550375" style="width:100%"> </div> <div class="col" style="display: flex; align-items: center; flex-direction: column; text-align: center;"> <h4>Every day around 8 million pieces of plastic makes their way into our oceans.</h4> <label>Once in the ocean, a single-use bottle moves with the currents of the wind and the ocean and can take <b>hundreds of years to break down into microplastic</b>.</label> </div> </div> </div>

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

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