如何垂直居中< div>在CSS的父元素中? [英] How to vertically center <div> inside the parent element with CSS?

查看:144
本文介绍了如何垂直居中< div>在CSS的父元素中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个小的用户名和密码输入框。

I'm trying to make a small username and password input box.

我想问一下,如何垂直对齐div?

I would like to ask, how do you vertically align a div?

我拥有的是

<div id="Login" class="BlackStrip floatright">
   <div id="Username" class="floatleft">Username<br>Password</div>
   <div id="Form" class="floatleft">
   <form action="" method="post">
      <input type="text" border="0"><br>
      <input type="password" border="0">
   </form>
   </div>
</div>

如何使ID为Username和Form的div垂直对齐中心?我尝试输入:

How can I make the div with id Username and Form to vertically align itself to the center? I've tried to put:

vertical-align: middle;

,但似乎无效。任何帮助将不胜感激。

in CSS for the div with id Login, but it doesn't seem to work. Any help would be appreciated.

推荐答案

现代浏览器中最好的方法是使用flexbox:

The best approach in modern browsers is to use flexbox:

#Login {
    display: flex;
    align-items: center;
}

某些浏览器将需要供应商前缀。对于不支持Flexbox的旧版浏览器(例如IE 9及更低版本),您需要使用旧版方法之一<

Some browsers will need vendor prefixes. For older browsers without flexbox support (e.g. IE 9 and lower), you'll need to implement a fallback solution using one of the older methods.

推荐阅读

  • Browser support
  • A Guide to Flexbox
  • Using CSS Flexible Boxes

这篇关于如何垂直居中&lt; div&gt;在CSS的父元素中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆