首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使div中的三个div居中

使div中的三个div居中
EN

Stack Overflow用户
提问于 2012-07-21 10:06:43
回答 3查看 730关注 0票数 1

我正在尝试将三个div放在另一个div中。我似乎不能让它工作。我的网站是响应式的,也许这与它有关?这是我到目前为止的代码和CSS,任何帮助都是非常感谢的!

我正在尝试将所有三个div .hp-highlight都集中在.home-highlights

代码语言:javascript
复制
<div id="home-highlights" class="clearfix">

    <div class="heading">
        <h2><a href="http://kompufast.com/?page_id=1909" title="What We Do"><span>What We Do</span></a></h2>
    </div>
    <!-- /heading -->

    <div class="hp-highlight  ">
        <img src="http://kompufast.com/wp-content/uploads/2012/06/kompufast_sales1.jpg" title="Sales" class="hp-highlight-img" />
        <h3><a href="http://kompufast.com/?services=safe-computing" title="Sales" target="_self">Sales</a></h3>
        <p>KompuFAST+ is the right company to help you address your need for all kind of consumer technology products.</p>
    </div>
    <!-- /hp-highlight -->

    <div class="hp-highlight  ">
        <img src="http://kompufast.com/wp-content/uploads/2012/02/kompufast_order1.jpg" title="Order" class="hp-highlight-img" />
        <h3><a href="http://kompufast.com/?services=social-media" title="Order" target="_self">Order</a></h3>
        <p>KompuFAST-Order facilitates the ordering of products, without a fee for special order.</p>
    </div>
    <!-- /hp-highlight -->

    <div class="hp-highlight highlight-third ">
        <img src="http://kompufast.com/wp-content/uploads/2012/02/kompufast_fix1.jpg" title="Fix" class="hp-highlight-img" />
        <h3><a href="http://kompufast.com/?services=shared-hosting " title="Fix" target="_self">Fix</a></h3>
        <p>KompuFAST+ has a team of dedicated repair technicians ready to diagnose your computer for all sorts of problems.</p>
    </div>

这是我一直在尝试的CSS:

代码语言:javascript
复制
.home-highlights {
    margin-right:auto;
    margin-left:auto;
    width: 100%;
}
.hp-highlight {
    width: 280px;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-07-21 10:45:24

不能在IE7或更低版本下工作,但这是你的。

代码语言:javascript
复制
.hp-highlight {
   display: inline-block;
   width: 280px;
   vertical-align: top;
}

如果你需要IE7和更低的支持,你必须使用float。

代码语言:javascript
复制
.hp-highlight {
   width: 280px;
   float: left;
}

在该点之后,所有3个项目的居中将通过使用text-align: center on .home-highlight或将这3个项目包装在另一个div中并将其左/右边距设置为自动。

票数 1
EN

Stack Overflow用户

发布于 2012-07-21 10:12:59

向您的.hp-highlight类添加margin:auto;

下面是你的css应该是什么样子的

代码语言:javascript
复制
.hp-highlight {
    margin:auto;
    width: 280px;
}

请参阅此处的工作:http://jsfiddle.net/RhMZ7/1/

票数 0
EN

Stack Overflow用户

发布于 2012-07-21 22:43:55

即使在IE7中,这也可以工作;不需要浮动。

代码语言:javascript
复制
.home-highlights {font-size: 0; text-align: center;}
.hp-highlight {
    display: inline-block;
    *display: inline; 
    vertical-align: top; 
    width: 280px;
    zoom: 1;
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11588912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档