亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

調整頁面大小時如何使 CSS 中的這些框居中?

調整頁面大小時如何使 CSS 中的這些框居中?

Go
有只小跳蛙 2023-08-21 16:29:42
我有以下盒子(文章)當我嘗試調整頁面大小時,它變成這樣:我希望它在調整窗口大小時自動居中,以避免右側出現巨大間隙,但它需要采用與現有格式相同的格式?;旧现恍鑼⒄麄€組移動到中心,而不將各個框居中,因為我希望它們如圖所示向左浮動。編輯:這就是我所追求的:這是我當前的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>
查看完整描述

2 回答

?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

使用靈活性,盧克


display: flex;

flex-wrap: wrap;

justify-content: center;

* {

    margin: 0;

    padding: 0;

}


html, body {

    height: 100%;

}


.container {

    width: 90%;

    height: 100%;

    overflow: hidden;

    background: skyblue;

    margin: auto;

    padding: 20px;

    display: flex;

    flex-wrap: wrap;

    justify-content: center;

}


.container article {

    width: 250px;

    height: 250px;

    background: blue;

    margin: 25px

}

<!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>


查看完整回答
反對 回復 2023-08-21
?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

它沒有從左側包裹盒子,所以我使用 CSS 網格解決了它:


HTML:


<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <title>Title</title>

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

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

    </head>

    <body>

        <div class="grid-container">

            <div class="grid-item">1</div>

            <div class="grid-item">2</div>

            <div class="grid-item">3</div>  

            <div class="grid-item">4</div>

            <div class="grid-item">5</div>

            <div class="grid-item">6</div>  

            <div class="grid-item">7</div>

            <div class="grid-item">8</div>

            <div class="grid-item">9</div>

            <div class="grid-item">10</div>

            <div class="grid-item">11</div> 

            <div class="grid-item">12</div> 

            <div class="grid-item">13</div> 

            <div class="grid-item">14</div> 

        </div>

    </body>

</html>

CSS:


.grid-container {

    display: grid;

    grid-template-columns: repeat(auto-fill, 200px);

    grid-gap: 40px;

    justify-content: center;

    padding: 40px 10px 40px 40px;

    background-color: #2196F3;

}

.grid-item {

    width: 200px;

    height: 200px;

    background-color: #FFFFFF;

    font-size: 30px;

    text-align: left;

}

筆記:

  • grid-template-columns: repeat(auto-fill, 200px); 200px是盒子的寬度。

  • justify-content: center; 用于將容器與盒子一起居中。這可行,但會使盒子稍微偏離中心并偏向右側。為了補償,我在右側添加了10pxpadding: 40px 10px 40px 40px內邊距: 。這些值可能有所不同。

工作中的jsfiddle


查看完整回答
反對 回復 2023-08-21
  • 2 回答
  • 0 關注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號