html - 没有宽度的中心 float 容器

标签 html css width center

我有这个 HTML:

<div id="container">
    <article></article>
    <article></article>
    <article></article>
    <article></article>
    <article></article>
</div>

和这个 CSS:

#container {
    overflow: hidden
}

article {
    float:left;
    height: 200px;
    width: 200px;
    background-color: red;
    margin: 5px;
}

我想在不指定宽度的情况下将#container 居中。可能吗?
这是 Fiddle .

编辑: 我需要更改#container 的宽度以适合文章。如果主体宽度为 450px,则每行将有两篇文章,因此容器的宽度应为 400px 左右。如果主体宽度为 1100px,则每行将有 5 篇文章,因此容器的宽度应为 1000px 左右。

最佳答案

你可以尝试这样的事情(jsfiddle):

HTML:

<html>
    <body>
        <div id="container">
            <article></article>
            <article></article>
            <article></article>
            <article></article>
            <article></article>
        </div>
    </body>
</html>​

CSS:

html, body { margin: 0; padding: 0; }
body { text-align: center; }

#container {
    overflow: hidden;
    margin: 0 auto;
    width: 90%;
    text-align: left;
}

article {
    display:inline-block;
    height: 200px;
    width: 200px;
    background-color: red;
    margin: 5px;
}
​

关于html - 没有宽度的中心 float 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998402/

相关文章:

html - <caption>标签可以和div一起使用吗?

javascript - 从主体宽度中减去元素宽度,并作为边距添加到元素

html - 为什么我的网站右边有空白?

android - Google Map v2 getWidth 以像素为单位

css - 绝对 div 是否有可能具有宽度 : auto, 并且也忽略它的父宽度?

javascript - 未捕获的类型错误 : Cannot read property 'text' of undefined for getting selected value in java script

javascript - 确保单击“加载更多”不会导致用户滚动到顶部

javascript - 你能用元素得到封面的 "state"吗

jquery - 半透明 div 中的非透明图像

jquery - 超出其 DIV 指定宽度的文本