如何使用CSS中心水平绝对div? [英] How to center absolute div horizontally using CSS?

查看:120
本文介绍了如何使用CSS中心水平绝对div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,并希望它水平居中 - 虽然我给它 margin:0 auto; 它不居中...

I've a div and want it to be centered horizontally - although I'm giving it margin:0 auto; it's not centered...

.container {
    position: absolute;
    top: 15px;
    z-index: 2;
    width:40%;
    max-width: 960px;
    min-width: 600px;
    height: 60px;
    overflow: hidden;
    background: #fff; 
    margin:0 auto;
}


推荐答案

c $ c> left:0; right:0; 。

You need to set left:0; right:0;.

指定从窗口边缘偏移边距的距离。

This specifies how far to offset the margin edges from the sides of the window.

http://www.w3.org/TR/CSS2/ visuren.html#position-props

http://jsfiddle.net/SS6DK/

.container
{
  position: absolute;
  top: 15px;
  z-index: 2;
  width:40%;
  max-width: 960px;
  min-width: 600px;
  height: 60px;
  overflow: hidden;
  background: #fff;
  margin: 0 auto;
  left: 0;
  right: 0;
}

注意:您必须定义宽度,否则此答案将 。这意味着这个答案对具有动态宽度的元素无用。

Note: You must define the width or this Answer will not work. That means this answer is not useful for elements with dynamic widths.

这篇关于如何使用CSS中心水平绝对div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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