如何使用 html css 居中对齐

发布于 2024-11-28 07:44:22 字数 387 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦在深巷 2024-12-05 07:44:22
#formWrapper{
    width:550px;
    padding: 2em 0 2em 0;                    
    border:solid 5px #F1F1F1;
    margin:0 auto;
    background-color: #AFC8DE;
}

对于垂直对齐 div,请查看此处的示例

http://stylizedweb.com /2008/02/01/垂直对齐-div/

#formWrapper{
    width:550px;
    padding: 2em 0 2em 0;                    
    border:solid 5px #F1F1F1;
    margin:0 auto;
    background-color: #AFC8DE;
}

And for verticaly align the div look here for an example

http://stylizedweb.com/2008/02/01/vertical-align-div/

无声情话 2024-12-05 07:44:22

padding 之后缺少冒号

you're missing colon after padding

昨迟人 2024-12-05 07:44:22

auto 的边距仅在定义了显式宽度时才起作用,但不适用于垂直居中的东西 - 这实际上在 CSS 中不太容易做到。最简单的方法是这样做

#formWrapper {
    height: 400px;
    width: 550px;
    position: absolute;
    top: 50%; /*position halfway down the page */
    margin-top: -200px; /*shift item up half it's width (assuming here the height is 400px)*/
    left: 50%; /*position in center of page */
    margin-left: -275px; /*shift item left half it's width (working with your width of 550px)*/
    /*add your other values here, 
    but not anything that will conflict with the above */

}

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

#formWrapper {
    height: 400px;
    width: 550px;
    position: absolute;
    top: 50%; /*position halfway down the page */
    margin-top: -200px; /*shift item up half it's width (assuming here the height is 400px)*/
    left: 50%; /*position in center of page */
    margin-left: -275px; /*shift item left half it's width (working with your width of 550px)*/
    /*add your other values here, 
    but not anything that will conflict with the above */

}
飘落散花 2024-12-05 07:44:22

填充后需要一个“:”!

#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;
}

you need a ":" after your padding!

#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;
}
你的笑 2024-12-05 07:44:22

如果直接复制/粘贴,则需要解决一些语法错误:

padding 之后需要一个冒号,如果将所有边距设置为自动,则无需指定每个边距对于单个子集,您可以只指定 margin: auto ,这将为所有边距使用 auto 。

#formWrapper
{
width: 550px;
padding: 2em 0 2em 0;                    
border: solid 5px #F1F1F1;
margin: auto;
background-color: #AFC8DE;
}

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 state margin: auto which will use auto for all the margins.

#formWrapper
{
width: 550px;
padding: 2em 0 2em 0;                    
border: solid 5px #F1F1F1;
margin: auto;
background-color: #AFC8DE;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文