如何使用 html css 居中对齐
我对 html 和整个 Web 开发过程很陌生(如果这是一个愚蠢的问题,请原谅我),但是我如何才能将表单居中于页面中间呢?我有下面的代码,但是当我应用它时,表单居中对齐但粘在页面顶部 - 为什么?我可以手动调整它,但我想根据稍后查看网站的分辨率,会出现问题。
#formWrapper{
width:550px;
padding 2em 0 2em 0; border:solid 5px #F1F1F1;
margin-top: auto;
margin-right: auto;
margin-bottom: auto;
margin-left: auto;
background-color: #AFC8DE;
}
i'm new to html and the whole web development process (so excuse me if this is a stupid question) but how can i center a form in the middle of the page? I have the below code, but when i apply it, the form aligns centre but sticks to the top of the page - why? i can adjust it manually but i imagine that there will be problems depending on the resolution the site is viewed later down the line.
#formWrapper{
width:550px;
padding 2em 0 2em 0; border:solid 5px #F1F1F1;
margin-top: auto;
margin-right: auto;
margin-bottom: auto;
margin-left: auto;
background-color: #AFC8DE;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于垂直对齐 div,请查看此处的示例
http://stylizedweb.com /2008/02/01/垂直对齐-div/
And for verticaly align the div look here for an example
http://stylizedweb.com/2008/02/01/vertical-align-div/
padding
之后缺少冒号you're missing colon after
padding
auto
的边距仅在定义了显式宽度时才起作用,但不适用于垂直居中的东西 - 这实际上在 CSS 中不太容易做到。最简单的方法是这样做auto
for margins will only work when an explicit width has been defined but doesn't work for vertically centering things - this is actually not very easy to do in CSS. the simplest way is to do this填充后需要一个“:”!
you need a ":" after your padding!
如果直接复制/粘贴,则需要解决一些语法错误:
在
padding
之后需要一个冒号,如果将所有边距设置为自动,则无需指定每个边距对于单个子集,您可以只指定margin: auto
,这将为所有边距使用 auto 。If that's directly copy/pasted, there's some syntax errors that need to be addressed:
You need a colon after
padding
, and if you have all the margins set to auto, you don't need to specify each individual subset, you can just statemargin: auto
which will use auto for all the margins.