调整页面大小时如何使 CSS 中的这些框居中?

我有以下盒子(文章)

https://img.mukewang.com/64e320940001e6c506510544.jpg

当我尝试调整页面大小时,它变成这样:

https://img2.mukewang.com/64e320a1000163e106520547.jpg

我希望它在调整窗口大小时自动居中,以避免右侧出现巨大间隙,但它需要采用与现有格式相同的格式。基本上只需将整个组移动到中心,而不将各个框居中,因为我希望它们如图所示向左浮动。

编辑:这就是我所追求的:

https://img1.mukewang.com/64e320ab00010b0306520547.jpg

这是我当前的CSS:


* {

    margin: 0;

    padding: 0;

}


html, body {

    height: 100%;

}


.container {

    width: 90%;

    height: 100%;

    overflow: hidden;

    background: skyblue;

    margin: auto;

    padding: 20px;

}


.container article {

    float: left;

    width: 250px;

    height: 250px;

    background: blue;

    margin: 25px

}

和我的html:


<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <title>Shop</title>

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="css/style.css">

    </head>

    <body>

        <div class="container">

            <article>

                <h2>Hello</h2>

            </article>

            <article>

                <h2>Hello</h2>

            </article>

            <article>

                <h2>Hello</h2>

            </article>

            <article>

                <h2>Hello</h2>

            </article>

            <article>

                <h2>Hello</h2>

            </article>

        </div>

    </body>

</html>



有只小跳蛙
浏览 169回答 2
2回答

慕莱坞森

使用灵活性,卢克display: flex;flex-wrap: wrap;justify-content: center;* {&nbsp; &nbsp; margin: 0;&nbsp; &nbsp; padding: 0;}html, body {&nbsp; &nbsp; height: 100%;}.container {&nbsp; &nbsp; width: 90%;&nbsp; &nbsp; height: 100%;&nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; background: skyblue;&nbsp; &nbsp; margin: auto;&nbsp; &nbsp; padding: 20px;&nbsp; &nbsp; display: flex;&nbsp; &nbsp; flex-wrap: wrap;&nbsp; &nbsp; justify-content: center;}.container article {&nbsp; &nbsp; width: 250px;&nbsp; &nbsp; height: 250px;&nbsp; &nbsp; background: blue;&nbsp; &nbsp; margin: 25px}<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; &nbsp; &nbsp; <title>Shop</title>&nbsp; &nbsp; &nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="css/style.css">&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>Hello</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>Hello</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>Hello</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>Hello</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <article>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2>Hello</h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </article>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body></html>

忽然笑

它没有从左侧包裹盒子,所以我使用 CSS 网格解决了它:HTML:<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; &nbsp; &nbsp; <title>Title</title>&nbsp; &nbsp; &nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="css/style.css">&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">1</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">2</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">3</div>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">4</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">5</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">6</div>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">7</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">8</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">9</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">10</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">11</div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">12</div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">13</div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="grid-item">14</div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body></html>CSS:.grid-container {&nbsp; &nbsp; display: grid;&nbsp; &nbsp; grid-template-columns: repeat(auto-fill, 200px);&nbsp; &nbsp; grid-gap: 40px;&nbsp; &nbsp; justify-content: center;&nbsp; &nbsp; padding: 40px 10px 40px 40px;&nbsp; &nbsp; background-color: #2196F3;}.grid-item {&nbsp; &nbsp; width: 200px;&nbsp; &nbsp; height: 200px;&nbsp; &nbsp; background-color: #FFFFFF;&nbsp; &nbsp; font-size: 30px;&nbsp; &nbsp; text-align: left;}笔记:grid-template-columns: repeat(auto-fill, 200px);&nbsp;200px是盒子的宽度。justify-content: center;&nbsp;用于将容器与盒子一起居中。这可行,但会使盒子稍微偏离中心并偏向右侧。为了补偿,我在右侧添加了10pxpadding: 40px 10px 40px 40px内边距: 。这些值可能有所不同。工作中的jsfiddle
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go