html - center div 在屏幕中间

标签 html css

我这里有“主要”div: http://tallevi.net/daniel/?page_id=5 如果我改变屏幕分辨率,div 不在中间,目前“主”div 的 css 是:

.#main {
width: 680px;
position: absolute;
right: 35%;
padding: 0 40px 5px 40px;
font: normal 14px Verdana, Arial, sans-serif;
line-height: 20px;
display: inline-block;
z-index: 2;
background: url(images/content_back.png) repeat-y;
}

怎么了?

最佳答案

使元素居中的最佳方法是使用 margin: 0 auto;,尝试将您的代码更改为:

#main {
  width: 680px;
  margin: 0 auto;
  padding: 0 40px 5px 40px;
  font: normal 14px Verdana, Arial, sans-serif;
  line-height: 20px;
  z-index: 2;
  background: url(images/content_back.png) repeat-y;
}

Margin: 0 auto 为左右设置一个“自动”边距,本质上是居中。 (这仅在您指定设置宽度时有效)

关于html - center div 在屏幕中间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13471756/

相关文章:

php - index.php 登录成功后数据不更新?

php - 如何将php表格放入html侧边栏?

html - 如何摆脱动态断点期间的超链接中断?

html - Rails simple_format 和显示 'Read More' 链接与文本内联

jquery - 如何让 div 元素周围有阴影?

html - <custom> 没有功能的 html 标签?

javascript - 如何使喜欢后出现的Facebook评论框显示在上方而不是下方

css - 功能检测 CSS 过滤器不起作用

java - 将 CSS 和 JS 文件放在 Mavenized Java Web 应用程序中的最佳位置?

html - 低 z-index 中的高 z-index - 特殊导航栏