1

次のコードがあります

<div id="main">
    <div id="one"> </div>
    <div id="two"> </div>
    <div id="three"> </div>
    <div id="four"> </div>
</div>

以下のように中央の 4 div を揃える必要があります。各側に等しいスペースを維持します (上部スペース = 下部スペースと右側スペース = 左スペース)。

______________________________________
|                                    |
|         ________  ________         |
|        |        ||        |        |
|        |  one   ||   two  |        |
|        |        ||        |        |
|        |________||________|        |
|         ________  ________         |
|        |        ||        |        |
|        | three  ||  four  |        |
|        |        ||        |        |
|        |________||________|        |
|                                    |
|____________________________________|

4 つの div が等間隔に配置されています。css スニペットを使ってここで私を助けてくれる人はいますか? また、これについて多くの質問が寄せられていますが、これを修正することはできません。誰かが div 配置に関連するすべての概念を完全に説明する便利なリンクを教えてもらえますか?

(皆さん、これが重複していることはわかっていますが、グーグルでぐるぐる回っているだけなので助けてください。)

前もって感謝します :)

4

4 に答える 4

3

IE8 を含む最新のすべてのブラウザーで機能する 1 つの方法を次に示します: jsFiddle example

HTML

<div id="main">
    <div id="one"></div>
    <div id="two"></div><br />
    <div id="three"></div>
    <div id="four"></div>
</div>​

CSS

div {
    border:1px solid #999;
}
#main {
    width:400px;
    height:400px;
    display:table-cell;
    vertical-align:middle;
    text-align:center;
}
#one,#two,#three,#four{
    width:100px;
    height:100px;   
    display:inline-block;    
}

<br />コードにブレークタグ ( ) を 1 つ追加する必要があったことに注意してください。

于 2012-06-18T20:27:04.283 に答える
1

@ j08691 良い例があります。しかし、これが役に立つなら、ここに私のものがあります...

<html>
<body>

<div style="width: 960px; margin: 0 auto;">

    <div>
        <div style="width: 480px; float: left;">
            <div style="padding: 10px; border: 1px solid #F00;">
                1
            </div>
        </div>
        <div style="width: 480px; float: left;">
            <div style="padding: 10px; border: 1px solid #F00;">
                2
            </div>
        </div>
    </div>

    <div>
        <div style="width: 480px; float: left;">
            <div style="padding: 10px; border: 1px solid #F00;">
                3
            </div>
        </div>
        <div style="width: 480px; float: left;">
            <div style="padding: 10px; border: 1px solid #F00;">
                4
            </div>
        <div>
    </div>

</div>

</body>
</html>
于 2012-06-18T20:31:02.660 に答える
1

水平方向の中央揃えは簡単ですが、絶対配置と負のマージンを使用して垂直方向に配置するための巧妙なトリックがあります。これは、私が数年前に書いた実用的な例です。

ここにいくつかのコードと説明があります:

<div id="main">
    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>
    <div id="four"></div>    
</div>

CSS

#main {
    position: absolute;
    top: 50%;  /* gets the first pixel in the center of the browser */  
    left: 50%;
    height: 860px;
    width: 860px;
    margin-top: -430px; /* negative margin half the height of the div to make it appear center */  
    margin-left: -430px;
    border: solid 1px #000;
    overflow: visible; /* allows an absolutely positioned element to contain floats */ 
    }
#one, #two, #three, #four { 
    float: left;
    height: 400px; 
    width: 400px;
    background-color: blue;
    margin: 20px; 
    }
#one, #three { 
     margin-right: 0; 
     }
#one, #two {  
    margin-top: 20px;  
    margin-bottom: 0;  
    }
于 2012-06-18T21:41:04.893 に答える
0

私は少し違う答えを求めました(しかし、@ j08691には良い解決策があります)、

#main{
    position:absolute;
    top:50%;
    left:50%;

    width:100px;
    height:100px;
    margin:auto;
}

#one, #two, #three, four{
    float:left;
    width:50px;
    height:50px;
}

私がテストした完全な動作コードは、

    <html>
<head>

<style media="screen" type="text/css">

#main{
    position:absolute;
    top:50%;
    left:50%;

    width:100px;
    height:100px;
    margin:auto;
}

#one, #two, #three, four{
    float:left;
    width:50px;
    height:50px;
}

.box{
    float:left;
    width:50px;
    height:50px;
}


#one{
    background-color:#f00;
}
#two{
    background-color:#0f0;
}
#three{
    background-color:#00f;
}
#four{
    background-color:#000;
}


</style>
</head>
<body>
<div id="main">
    <div id="one" class="box"> </div>
    <div id="two" class="box"> </div>
    <div id="three" class="box"> </div>
    <div id="four"class="box" > </div>
</div>
</body>
</html>
于 2012-06-18T20:32:18.173 に答える