2017-06-16 5 views
1

私は親divの子である2つのdivを持ち、それぞれの幅はパーセンテージで表示されます(重要:幅がパーセンテージである必要があります)。しかし、トリックはここにあります、私はマージンオートトリックを使用して不可能と思われるページの中央にこの全体を位置合わせする必要があります。助けてください!ここでコンテナの子の全幅を取る方法

は、これまでのコードです:

<div class="container" style="display: inline-block; width: 100%;"> 
    <div style="margin: 0px auto;"> 
     <div class="child1" style="width: 40%; float: left; background: lightblue;">Testing Child 1</div> 
     <div class="child2" style="width: 30%; float: left; background: lightgreen">Testing Child 2</div> 
    </div> 
</div> 
+0

おそらくmargin:0px autoを使用する代わりに、display:flex;と正当化 - コンテンツ:センター; ? – Amy

+0

最小幅を使用することができます –

答えて

4

あなたは、通常のドキュメントフローのうちの要素を取るフロートを使用しています。私はあなたの.containerをフレックスボックスとして定義し、CSS経由で子供の中心に置いた。

.container { 
 
    display: flex; 
 
    justify-content: center; 
 
} 
 

 
.child1 { 
 
    width: 40%; 
 
    background-color: lightblue; 
 
} 
 

 
.child2 { 
 
    width: 30%; 
 
    background-color: lightgreen; 
 
}
<div class="container"> 
 
    <div class="child1">Testing Child 1</div> 
 
    <div class="child2">Testing Child 2</div> 
 
</div>

+1

完全に動作します。どこでディスプレイのflexプロパティに関する良い、徹底的なガイドを取得できますか?私は技術的な参考文献が嫌いですが、理解しやすいものはあります。 –

+0

完全に動作します。どこでディスプレイのflexプロパティに関する良い、徹底的なガイドを取得できますか?私は技術的な参考文献が嫌いですが、理解しやすいものはあります。 –

+1

こちらをご覧ください:https://css-tricks.com/snippets/css/a-guide-to-flexbox/ – Gerard

0

あなたはこのように使用することができます -

<div class="container" style="display: inline-block; width: 100%;text-align: center"> 
    <div style="margin: 0px auto;"> 
     <div class="child1" style="width: 40%; float: none; background: lightblue;display: inline-block;">Testing Child 1</div><!-- 
     --><div class="child2" style="width: 30%; float: none; background: lightgreen;display: inline-block;">Testing Child 2</div> 
    </div> 
</div> 

それはあなたのために働く可能性があり:)

-1

あなたは右の15%のパディングを追加して、あなたの左側ができコンテナは次のようになります。

.container { 
 
    display: inline-block; 
 
    width: 100%; 
 
    padding: 0 15%; 
 
    background: red; 
 
} 
 
.child1 { 
 
    float: left; 
 
    width: 40%; 
 
    background: lightblue; 
 
} 
 

 
.child2 { 
 
    float: left; 
 
    width: 30%; 
 
    background: lightblue; 
 
}
<div class="container"> 
 
    <div class="child1">Testing Child 1</div> 
 
    <div class="child2">Testing Child 2</div> 
 
</div>

は、この情報がお役に立てば幸いです。

+0

downvoteの特別な理由は何ですか?それは動作し、それは小さな編集の前に働いていた.. –

0

おそらく、この方法を試してみてください。

#box { 
 
position: absolute; 
 
    transform: translate(-50%, -50%); 
 
    top: 50%; 
 
    left: 50%; 
 
    }
<div class="container" style="display: inline-block; width: 100%;text-align: center"> 
 
    <div id="box" style="margin: 0px auto; width: 100%;"> 
 
     <div class="child1" style="width: 40%; float: none; background: lightblue;display: inline-block;">Testing Child 1</div><!-- 
 
     --><div class="child2" style="width: 30%; float: none; background: lightgreen;display: inline-block;">Testing Child 2</div> 
 
    </div> 
 
</div>

1

あなたはフレキシボックスでそれを行うことができます。

.container { 
 
    display: flex; 
 
    justify-content: center; 
 
}
<div class="container"> 
 
    <div class="child1" style="width: 40%; background: lightblue">Testing Child 1</div> 
 
    <div class="child2" style="width: 30%; background: lightgreen">Testing Child 2</div> 
 
</div>

関連する問題