简体   繁体   English

如何在中间垂直对齐图像和文本?

[英]How to align an image and text vertically in the middle?

I have a DIV at the top and a few anchors.我在顶部有一个 DIV 和一些锚点。 First is styled with a logo and the rest are text.首先是带有徽标的样式,rest 是文本。 The styles I've set are as follows.我设置的styles如下。

div.nav-top {
  height: 120px;
  vertical-align: middle;
}

a.nav-logo {
  background: no-repeat url(Logo.png);
  background-size: 200px 40px;
  width: 200px;
  height: 40px;
  display: inline-block;
}

a.nav-link {
  vertical-align: middle;
}

However, these elements are not centered in the div .但是,这些元素不在div 的中心。 The alignment seems to follow the image, which is at the top. alignment 似乎遵循顶部的图像。 The text gets in the middle relative to the image .文本相对于图像位于中间。 It's not a bad idea but I need the whole system img-aaaa to be centered vertically.这不是一个坏主意,但我需要整个系统 img-aaaa 垂直居中。 At the moment I pad it but as soon as the height of the outer DIV changes, everything breaks.此刻我填充它,但一旦外部 DIV 的高度发生变化,一切都会崩溃。

What am I doing wrong?我究竟做错了什么?

(I've found this post but here they apply tables and such. Doesn't seem like the most appropriate solution. Perhaps I'm mistaken? (我找到了这篇文章,但他们在这里应用了表格等。似乎不是最合适的解决方案。也许我错了?

在此处输入图像描述

Using Flexbox 使用Flexbox

 nav, .menu { display: flex; flex-direction: row; align-items: center; list-style-type: none; } li { padding: 0 5px; } 
 <nav> <div class="logo"> <img src="http://placehold.it/150x50"> </div> <ul class="menu"> <li><a href="">Link</a></li> <li><a href="">Link</a></li> <li><a href="">Link</a></li> </ul> </nav> 

Using CSS Table 使用CSS表

 nav, .menu { display: table; } .logo, .menu { display: table-cell; vertical-align: middle; } li { padding: 0 15px; display: table-cell; vertical-align: middle; } 
 <nav> <div class="logo"> <img src="http://placehold.it/150x50"> </div> <ul class="menu"> <li><a href="">Link</a></li> <li><a href="">Link</a></li> <li><a href="">Link</a></li> </ul> </nav> 

You can refer to this blog , where the owner describes centering issues. 您可以参考此博客 ,所有者在其中描述居中问题。 He started 2001 but be aware that the last update's been made last summer, and I admire his persistence. 他从2001年开始, 要知道最后一次更新是在去年夏天进行的,我很欣赏他的坚持不懈。 He presents clear examples on how to center in the different versions of CSS. 他提供了有关如何在不同版本的CSS中居中的清晰示例。

In your case, I'd suggest that you use display: flex like so. 对于您的情况,我建议您像这样使用display:flex Note that the whole centering magic's done from the containing control and imposed on the underlying children. 请注意,整个居中魔术是通过包含控件完成的,并强加于基础子级。 There's actually no need to style nav-link at all (see remark below the example, though). 实际上,根本不需要设置nav-link的样式(不过,请参见示例下方的备注)。

div.nav-top {
  height: 120px;
  background-color: purple;
  padding: 10px;
  display: flex;
  align-items: center;
}

a.nav-logo {
  background: no-repeat url(Logo.png);
  background-size: 200px 40px;
  background-position: center center;
  width: 200px;
  height: 100%;
}

a.nav-link { }

That's a new thing in styling and recently implemented. 这是样式上的新事物,最近才实现。 The disadvantage of that is that older browsers might have problems rendering it. 这样做的缺点是较旧的浏览器可能无法呈现它。 But the upside is that it's much more intuitive to the designers (and fetching a decent version of a browser is not a far reach, unless managed centrally by the corporate). 但是好处是,对于设计人员而言,它更加直观(并且除非获得公司的集中管理,否则获取像样的浏览器版本并不遥不可及)。

You might want to use margin for the anchor controls, as they're going to be squashed toghether. 您可能要为锚控件使用边距 ,因为它们将被挤压在一起。 Also, the sizes and fonts will need adjusting. 另外,大小和字体将需要调整。 If you do that, you'll probably end up needing the style which I suggested wasn't needed but that's specifics only you're aware of. 如果这样做,最终可能会需要我建议的样式,但这只是您所知道的细节。

a.nav-link {
  margin: 10px;
  font-size: 20px;
  font-family: "Comic Sans MS", "Comic Sans", cursive;
}

It's always a good practice to control height with paddings instead of a fixed height and margins to center. 最好使用填充物来控制高度,而不是使用固定的高度和中心距来控制高度。 So, keeping that in mind: 因此,请记住以下几点:

  • Wrap the div with the logo and the div with the anchortags as children of a container div. 将div加上徽标,将div加上锚标签作为容器div的子级。
  • give the purple box there (or whatever it represents the corresponding padding top and bottom to center that parent div you created inside. 在此处提供紫色框(或任何代表相应填充顶部和底部的填充框,以将您在其中创建的父div居中放置。

     .purple{ background:purple; width:400px; display:inline-block; padding: 20px 15px; height:auto; } 

EDIT 编辑

or, if you MUST have a fixed height on the parent; 或者,如果您必须在父母身高上固定身高;

    .purple{
        background:purple;
        width:400px;
        display:inline-block;
        height:60px;
        padding: 0px 15px;
    }

See this demo 看这个演示

You can wrap your elements into a div and use margin: 0, auto; 您可以将元素包装成div并使用margin: 0, auto; for the elements and the inner container as well. 用于元素和内部容器。

Not an elegant solution, but if you make a table with the text in one cell and the image in the next cell it's aligned in the middle.这不是一个优雅的解决方案,但如果你制作一个表格,其中一个单元格中有文本,下一个单元格中有图像,它会在中间对齐。

Use display: flex: in the div above the text then just repeat from there使用 display: flex: 在文本上方的 div 中,然后从那里重复

div {

display: flex;
justify-contents: center;
align-items: center;

}

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

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