Html CSS - Div 对象居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12211351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:34:58  来源:igfitidea点击:

CSS - Div object centering?

csshtml

提问by marius

I'd simply like to center a DIV object to the browser screen.

我只是想将 DIV 对象居中到浏览器屏幕。

It should be something like that I think..

我想应该是这样的。。

<div id="root">
    <div id="centeredElement"></div>
</div>

div#root {
    width:100%; 
}
div#centeredElement {
    width:500px;
    height:200px;
    align:center;
}

回答by Rohit Azad



text-align:centerormargin:0 auto;

text-align:center或者margin:0 auto;

as like this

像这样

div#centeredElement{
text-align:center;
}

or

或者

div#centeredElement{
margin:0 auto;
}

回答by T.Todua

you have several options to center a div, image or other object:

您有多种选项可以将 div、图像或其他对象居中:

1) everything inside div

1)div内的一切

<div align="center"> blabla </div>

2) for images or frames

2) 用于图像或帧

<img style="display:block;margin-left:auto;margin-right:auto;" align="center" src="xx.jpg" />

3) another:

3)另一个:

<div style="width:80%;margin-left:10%;"> blabla </div>

回答by ragatskynet

Change

改变

align:center

居中对齐

to

margin: 0px auto;

边距:0px 自动;

回答by Hassan

There is a lot of answers to that questions in Stackoverflow topics

Stackoverflow 主题中有很多这些问题的答案

Example: Cross browser div center alignment using CSS

示例: 使用 CSS 的跨浏览器 div 中心对齐