简体   繁体   English

css中的垂直对齐

[英]vertical alignment in css

I am using bootstrap. 我正在使用bootstrap。 Here is a sample of the code I'm using. 这是我正在使用的代码示例。

<div class="row">
    <div class="col-lg-6"><img src="#" alt=""></div>
    <div class="col-lg-6">
        <h2>Heading</h2>
        <p>Some Text</p>
        <p>Some more text</p>
    </div>
</div>

Those p tags have multiple lines of text. 那些p标签有多行文字。 The image that is to the left is much larger than the text area, so I would like those elements to be vertically aligned. 左边的图像比文本区域大得多,所以我希望这些元素垂直对齐。 Here is what I have tried: 这是我尝试过的:

1. The table/table-cell trick.
2. The transform/Translatey trick.
3. Setting margin-top to 50% (didn't think that would work).
4. flex and align-items/justify-content.

Problem is this, the parent element will not have a height, so every solution I found online and through StackOverflow do not seem to work for me. 问题是这个,父元素不会有高度,所以我在网上和通过StackOverflow找到的每个解决方案似乎都不适合我。 I appreciate any assistance in this. 我很感激你的帮助。 Any help will be appreciated. 任何帮助将不胜感激。

Thanks in advance 提前致谢

If you can wrap those paragraph into div then you can use following solution. 如果你可以将这些段落包装成div那么你可以使用以下解决方案。 col-lg-6 is changed to col-xs-6 just to show left-right layout in the code snippet. col-lg-6更改为col-xs-6只是为了在代码片段中显示左右布局。

 .outer { display: flex; align-items: stretch; } .middle { display: flex; align-items: center; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="row outer"> <div class="col-xs-6 middle"><img src="https://s-media-cache-ak0.pinimg.com/236x/f6/16/d0/f616d044c7937dde2e8ec2b4f3e6f79c.jpg" alt=""></div> <div class="col-xs-6 middle"> <div class="inner"> <h2>Heading</h2> <p>Some Text</p> <p>Some more text</p> </div> </div> </div> </div> 

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

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