简体   繁体   English

如何使这个HTML元素居中?

[英]How do I get this HTML element centered?

How I want it to look: http://i.imgur.com/1nVr0ZM.png 我希望它看起来如何: http : //i.imgur.com/1nVr0ZM.png

Please note : Setting a fixed width is not useful here, because that would prevent the boxes from using all available screen space. 请注意 :此处设置固定宽度不是有用的,因为这将阻止框使用所有可用的屏幕空间。 I need the boxes to stay responsive, but want them to appear centered. 我需要使框保持响应状态,但希望它们显得居中。

The problem is not so much the centering of the element, but the fact that the element takes up 100% width when it doesn't need to. 问题不仅仅在于元素居中,还在于元素在不需要时占据100%宽度的事实。


I have a container with several boxes, which are set to float. 我有一个带有几个盒子的容器,这些盒子被设置为浮动的。

<div id="contentBox">
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
</div>

http://jsfiddle.net/56BJP/ http://jsfiddle.net/56BJP/

The container div takes up 100% width, even if it doesn't require it all, so I can't get it centered with margin: 0 auto. 容器div占用100%的宽度,即使它不需要全部,所以我不能以margin:0 auto为中心。

How do I get the container to not needlessly take up 100% width? 如何使容器不必要地占用100%的宽度?

Add a width to your div and center : 为您的div和center添加一个width

<div id="contentBox" style="width:500px;margin:0 auto">
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
</div>

In your fiddle , you should add width to your class : 在您的小提琴中,您应该为班级增加width

#contentBox
{
    position: relative;
    margin: 0 auto;
    width:50%;//fixe a width
}

DEMO HERE 此处演示

Alternatively, if you do not want to set the width of the container div explicitly, you can remove float: left; 另外,如果您不想显式设置容器div的宽度,则可以删除float: left; from a and set the container's display to table . a与设置在容器的display ,以table

Like this http://jsfiddle.net/56BJP/2/ 像这样http://jsfiddle.net/56BJP/2/

#contentBox
{
    position: relative;
    margin: 0 auto;
    display: table;
}

a.content
{
    position: relative;
    display: block;
    width: 310px;
    height: 174px;
    margin: 0;
    padding: 0;
    cursor: pointer;
    background: #000 no-repeat;
    background-size: 100% 100%;
}

You need fix width for margin to get worked. 您需要固定宽度以确保margin正常工作。 More importantly you always need to clear floated elements with clearfix: 更重要的是,您始终需要使用clearfix清除浮动元素:

<div id="contentBox">
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <a class="content"></a>
    <div class="clear"></div> <!-- clear float elements -->
</div>

CSS 的CSS

#contentBox {
  width: 300px;
  margin: 0 auto;
}

.clear{
    clear:both;
}

Demo 演示版

You don't need to set a width for that element to shrink to fit it's content. 您无需为该元素设置宽度即可缩小以适合其内容。 You can simply add the CSS display: inline-block; 您可以简单地添加CSS display: inline-block; and it will shrink to the right size. 它将缩小到合适的尺寸。 Additionally, you can do the same with the child blocks, removing the need to float the elements in the first place. 另外,您可以对子块执行相同的操作,从而无需首先将元素浮动。

#contentBox {
  width: 300px;
  margin: 0 auto;
}

http://jsfiddle.net/56BJP/4/ http://jsfiddle.net/56BJP/4/

If #contentBox doesn't have a fixed width, you can add margin: 0 auto to a.content , which do have it. 如果#contentBox没有固定的宽度,则可以在a.content添加margin: 0 auto Then, just use 然后,只需使用

a.content {
    display: block;
    width: 310px;
    height: 174px;
    margin: 0 auto;
}

Demo 演示版

try using the css 尝试使用CSS

#contentBox {
  width: 500px;
  margin: 0 auto;  }

only because inline styling's have been deprecated in modern browers 只是因为在现代浏览器中不推荐使用内联样式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM