简体   繁体   English

垂直和水平中心对齐容器div内的图像和文本

[英]Vertical and horizontal centre aligning an image and a text inside a container div

I am trying to create a list of thumbnail type divs. 我正在尝试创建缩略图类型div的列表。 The divs have fixed height and width. div具有固定的高度和宽度。 The div containes an image and a title text. div包含图像和标题文本。 Both the image and the title text need to be in the vertical and horizontal cen ter of the container div. 图像和标题文本都必须位于容器div的垂直和水平中心。 I am able to center the image but cannot quite make out how to position the text. 我能够使图像居中,但无法完全确定如何放置文本。 I need to design it with css only. 我只需要使用CSS设计它。

 .thumb { height: 100px; width: 96px; padding: 3px; cursor: pointer; } .thumb img, .thumb svg { height: 100%; width: 100%; max-height: 100%; max-width: 100%; } .thumb div { position: absolute; } 
 <div id="Thumb" class="thumb ml-10 mt-10 float-left ui-sortable-handle"> <img src="../something.svg"> <div>Name of something</div> </div> 

Please suggest what changes I need to make to the CSS or HTML. 请提出我需要对CSS或HTML进行哪些更改。

first what do you mean 'Both the image and the title text need to be in the vertical and horizontal center of the container div' ?. 首先,您的意思是“图像和标题文本都必须位于容器div的垂直和水平中心”?

At the same time, they whould be in horizontal and vertical ? 同时,他们应该处于水平和垂直方向吗? Anyway if you want them to be in horizontal, you can use flexbox like this : 无论如何,如果您希望它们水平放置,则可以使用flexbox,如下所示:

.thumb {
 display : flex;
 flex-direction: row; // change this to 'column' if you want vertical
 // for center
 align-items: center;

} }

Hope this will help you. 希望这会帮助你。

/*The ans is simple you use a wrapper*/

 .thumb { height: 100px; width: 96px; padding: 3px; cursor: pointer; } .thumb img, .thumb svg { height: 100%; width: 100%; max-height: 100%; max-width: 100%; } .thumb div { position: absolute; } /*The ans is simple you use a wrapper make it flex*/ .wrapper{ display: flex; align-items: center; justify-content: center; } 
 <div class="wrapper"> <div id="Thumb" class="thumb ml-10 mt-10 float-left ui-sortable-handle"> <img src="https://www.f-cdn.com/assets/img/fl-logo-c555380d.svg"> <div>Name of something</div> </div> </div> 

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

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