简体   繁体   English

如何首先居中放置图像,然后在该图像的任一侧垂直居中放置文本?

[英]How do I first center an image and then vertically center text on either side of that image?

I want to do two things: 我想做两件事:

  1. Center an image on a page horizontally. 将图像水平居中放置在页面上。
  2. Write text on both sides of the image. 写在图像的两侧文字。
    • On the right side, the text should be left-aligned, and on the left side, it should be right-aligned. 在右侧,文本应左对齐,在左侧,文本应右对齐。
    • Each chunk of text should be vertically centered with respect to the image. 每个文本块应相对于图像垂直居中。

Here's the code I've got so far, but I just can't quite get it to work: 这是到目前为止我得到的代码,但是我不能完全正常工作:

 <div> <div style="vertical-align: middle; float: left; text-align: right;"> <strong style="font-size: large;">Credentials</strong> <ul> <li>dsadsad</li> <li>cxzcxzc</li> <li>hgfhgfhg</li> <li>uytuty</li> <li>eqwewqeqwewq</li> </ul> </div> <div style="text-align: center; display: inline-block;"> <a href="https://i.imgur.com/XIlpRN5.jpg"><img style="height: 400px; width: auto;" src="https://i.imgur.com/XIlpRN5.jpg" /></a> </div> <div style="vertical-align: middle; float: left; text-align: left;"> <strong style="font-size: large;">Likes</strong> <ul> <li>dsadsadsa</li> <li>cxzcxzc</li> <li>gdgfdgfdgdf</li> <li>jhjghjhgj</li> <li>ouioiuoiuo</li> <li>..,m/.m,.m,</li> <li>opupoiuyuyi</li> <li>adsaeqw421312</li> <li>4356436546</li> </ul> </div> </div> 

How can I fix this to achieve what I want? 我该如何解决这个问题以实现我想要的? Thanks! 谢谢!

Here is one way to do it using display flex on the parent element. 这是在父元素上使用display flex的一种方法。

 .row { display: flex; align-items: center; justify-content: center; position: relative; } .clear { clear:both; } .clear:before, .clear:after { content:""; display:table; } .clear:after { clear:both; } .column { float: left; width: 33.333%; border: 1px solid #ccc; box-sizing: border-box; text-align: center; } .column img { max-width: 100%; } .column:first-of-type { text-align: right; } .column:last-of-type { text-align: left; } 
 <div class="row clear"> <div class="column"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae magnam distinctio vel necessitatibus vero quaerat soluta ducimus, amet minima consequuntur nulla! Inventore maiores suscipit minus animi corporis porro illo quaerat!</p> </div> <div class="column"> <img src="http://i.imgur.com/I9QJ2wP.png"> </div> <div class="column"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae magnam distinctio vel necessitatibus vero quaerat soluta ducimus, amet minima consequuntur nulla! Inventore maiores suscipit minus animi corporis porro illo quaerat!</p> </div> </div> 

Here is a cleaned up version of your sample 这是您的样本的清理版本

 .table { display: table; width: 100%; } .cell { display: table-cell; padding: 10px; width: 33%; vertical-align: middle; } .cell:first-child { text-align: left; } .cell:last-child { text-align: right; } .font-large { font-size: large; font-weight: bold; } .img { height: 400px; width: auto; } 
 <div class="table"> <div class="cell"> <span class="font-large">Credentials</span> <ul> <li>dsadsad</li> <li>cxzcxzc</li> <li>hgfhgfhg</li> <li>uytuty</li> <li>eqwewqeqwewq</li> </ul> </div> <div class="cell"> <a href="https://i.imgur.com/XIlpRN5.jpg"><img class="img" src="https://i.imgur.com/XIlpRN5.jpg" /></a> </div> <div class="cell"> <span class="font-large">Likes</span> <ul> <li>dsadsadsa</li> <li>cxzcxzc</li> <li>gdgfdgfdgdf</li> <li>jhjghjhgj</li> <li>ouioiuoiuo</li> <li>..,m/.m,.m,</li> <li>opupoiuyuyi</li> <li>adsaeqw421312</li> <li>4356436546</li> </ul> </div> </div> 

And here is a cleaned up flex version 这是一个清理过的flex版本

 .table { display: flex; align-items: center; width: 100%; } .cell { padding: 10px; width: 33%; } .cell:first-child { text-align: left; } .cell:last-child { text-align: right; } .font-large { font-size: large; font-weight: bold; } .img { height: 400px; width: auto; } 
 <div class="table"> <div class="cell"> <span class="font-large">Credentials</span> <ul> <li>dsadsad</li> <li>cxzcxzc</li> <li>hgfhgfhg</li> <li>uytuty</li> <li>eqwewqeqwewq</li> </ul> </div> <div class="cell"> <a href="https://i.imgur.com/XIlpRN5.jpg"><img class="img" src="https://i.imgur.com/XIlpRN5.jpg" /></a> </div> <div class="cell"> <span class="font-large">Likes</span> <ul> <li>dsadsadsa</li> <li>cxzcxzc</li> <li>gdgfdgfdgdf</li> <li>jhjghjhgj</li> <li>ouioiuoiuo</li> <li>..,m/.m,.m,</li> <li>opupoiuyuyi</li> <li>adsaeqw421312</li> <li>4356436546</li> </ul> </div> </div> 

Try using flexboxes. 尝试使用弹性框。

CSS + HTML: CSS + HTML:

 body > div { display: flex; flex-flow: horizontal; align-items: center; justify-items: space-around; justify-content: center; flex-wrap: no-wrap; /* For IE 10, 11 */ display: -ms-flexbox; flex-direction: row; -ms-flex-wrap: nowrap; -ms-flex-pack: center; -ms-flex-align: center; } ul{ list-style: none; padding-left: 0;} body > div > div {margin: 10px;} 
 <div> <div style="text-align: right;"> <strong style="font-size: large;">Credentials</strong> <ul> <li>dsadsad</li> <li>cxzcxzc</li> <li>hgfhgfhg</li> <li>uytuty</li> <li>eqwewqeqwewq</li> </ul> </div> <div style="text-align: center; display: inline-block;"> <a href="image.jpg"> <img src="http://placekitten.com/500/440" style="height: 400px; width: auto;" src="image.jpg" /></a> </div> <div style="text-align: left;"> <strong style="font-size: large;">Likes</strong> <ul> <li>dsadsadsa</li> <li>cxzcxzc</li> <li>gdgfdgfdgdf</li> <li>jhjghjhgj</li> <li>ouioiuoiuo</li> <li>..,m/.m,.m,</li> <li>opupoiuyuyi</li> <li>adsaeqw421312</li> <li>4356436546</li> </ul> </div> </div> 

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

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