简体   繁体   English

块元素的垂直对齐

[英]vertical alignment of block element

how can i vertical align this image that is already horizontally aligned with display: block; 我如何垂直对齐已经与display:block对齐的图像? margin-left: auto; 左边距:自动; margin-right: auto; 右边距:自动;

NOTE THAT I CANT USE top:50% left:50% transform: translate(-50%,-50%) or any display with flex methods. 注意,我不能使用top:50%left:50%transform:translate(-50%,-50%)或任何使用flex方法的显示。

fiddle : https://jsfiddle.net/3mLsL9t5/2/ 小提琴https : //jsfiddle.net/3mLsL9t5/2/

CSS CSS

.myContainer {
 width: 100px;
 height: 100px;
 background-color: lightblue;
 }

.myImage {
 width: auto;
 max-width: 20px;
 height: auto;
 max-height: 22px;
 border: 1px solid lightslategrey;
 position: relative;
 display: block;
 margin-left: auto;
 margin-right: auto;
 }

please try below css 请尝试下面的CSS

.myImage {
  border: 1px solid lightslategrey;
  display: block;
  height: 20px;
  line-height: 50px;
  margin: 0 auto;
  text-align: center;
  vertical-align: middle;
  width: 20px;
}
.myContainer {
  background-color: lightblue;
  display: table-cell;
  height: 100px;
  position: relative;
  vertical-align: middle;
  width: 100px;
}

Here we go ... just let the image be an image (inline) and add this to your .myContainer 我们开始...只需将图像作为图像(内联)并将其添加到您的.myContainer

display: table-cell;
text-align: center;
vertical-align: middle;

Sample snippet 样本片段

 .myContainer { width: 100px; height: 100px; background-color: lightblue; display: table-cell; text-align: center; vertical-align: middle; } .myImage { width: auto; max-width: 20px; height: auto; max-height: 22px; border: 1px solid lightslategrey; } 
 <div class='myContainer'> <img class='myImage' src='https://media-mediatemple.netdna-ssl.com/wp-content/themes/smashing-magazine/assets/images/sidebar-smashingconf-oxford.png'> </div> 


Update 更新

Or you can use line-height 或者您可以使用line-height

 .myContainer { width: 100px; height: 100px; background-color: lightblue; text-align: center; line-height: 111px; } .myImage { width: auto; max-width: 20px; height: auto; max-height: 22px; border: 1px solid lightslategrey; } 
 <div class='myContainer'> <img class='myImage' src='https://media-mediatemple.netdna-ssl.com/wp-content/themes/smashing-magazine/assets/images/sidebar-smashingconf-oxford.png'> </div> 

If you still need some block behavior, you can add display: inline-block; 如果仍然需要某些阻止行为,则可以添加display: inline-block; to the image 图片


Update 2 更新2

As you asked about block element, I added a version showing that too. 当您询问块元素时,我也添加了一个显示该元素的版本。

 .myContainer { width: 100px; height: 100px; background-color: lightblue; display: table-cell; vertical-align: middle; } .myImage { display: block; margin: 0 auto; width: auto; max-width: 20px; height: auto; max-height: 22px; border: 1px solid lightslategrey; } 
 <div class='myContainer'> <img class='myImage' src='https://media-mediatemple.netdna-ssl.com/wp-content/themes/smashing-magazine/assets/images/sidebar-smashingconf-oxford.png'> </div> 

This can be easily achieved using the CSS for the image: 使用图像的CSS可以轻松实现:

position: absolute,
left:0,
right:0,
top:0,
bottom:0,
margin:auto

and position:relative for the container. position:relative对于容器。

 .myContainer { width: 100px; height: 100px; background-color: lightblue; position: relative; } .myImage { width: auto; max-width: 20px; height: auto; max-height: 22px; border: 1px solid lightslategrey; position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin: auto; } 
 <div class='myContainer'> <img class='myImage' src='https://media-mediatemple.netdna-ssl.com/wp-content/themes/smashing-magazine/assets/images/sidebar-smashingconf-oxford.png'> </div> 

Please check here Working fiddle 请在这里检查工作小提琴

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

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