简体   繁体   English

如何在嵌套div中对齐h2?

[英]How to align h2 within a nested div?

I'm trying to align the text within my h2 element. 我正在尝试在h2元素中对齐文本。 I would like it to be centered vertically within the div. 我希望它在div中垂直居中。 I'm working in .NET MVC 我在.NET MVC工作

My html: (this is the only part giving me trouble) Specifically: 我的HTML :(这是给我带来麻烦的唯一部分)具体来说:

 .banner-side-container { height: 240px; width: 180px; vertical-align: middle; } .banner-side-container h2 { margin: 0; padding: 0; text-align: center; } .view-teams-banner { margin-top: 10px; height: 110px; width: 180px; background-color: aqua; border-radius: 20px; } .view-matches-banner { margin-top: 10px; height: 110px; width: 180px; background-color: darkorchid; border-radius: 20px; } 
 <td> <div class="banner-side-container"> <div class="view-teams-banner"> <h2>View Teams</h2> </div> <div class="view-matches-banner"> <h2>View Matches</h2> </div> </div> </td> 

I've tried adding vertical-align: middle; 我试过添加vertical-align:middle; to all 4 selectors with no success. 所有4个选择器都没有成功。 Further info: the text-align: center; 更多信息:text-align:center; worked perfectly. 工作得很好。

You can use flex and add the following settings to the three elements wrapping the h2 s and their parent elements as follows: 您可以使用flex并将以下设置添加到包含h2及其父元素的三个元素,如下所示:

display: flex;
flex-direction: column;
justify-content: center;

 .banner-side-container { height: 240px; width: 180px; display: flex; flex-direction: column; justify-content: center; } .banner-side-container h2 { margin: 0; padding: 0; text-align: center; } .view-teams-banner { margin-top: 10px; height: 110px; width: 180px; background-color: aqua; border-radius: 20px; display: flex; flex-direction: column; justify-content: center; } .view-matches-banner { margin-top: 10px; height: 110px; width: 180px; background-color: darkorchid; border-radius: 20px; display: flex; flex-direction: column; justify-content: center; } 
 <td> <div class="banner-side-container"> <div class="view-teams-banner"> <h2>View Teams</h2> </div> <div class="view-matches-banner"> <h2>View Matches</h2> </div> </div> </td> 

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

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