2017-10-31 16 views
0

ページに4つの内部divを含むBootstrapコンテナがあります(これらは実際のWebアプリケーションで動的にレンダリングされます)。特定の例では、3つの内部divをレンダリングするだけですが、段落の1つを削除すると、コンテナ内の残りの内容が左側に浮動します(CSSのfloatタグのため)。 3段落を左に浮かせるのではなく、どのように配置することができますか?コンテナ内の可変個数のdivの中心合わせ

<div class="expertise_section text-center"> 
<div class="row"> 
<div class="container"> 
<h2>Centered Headline</h2> 
<div class="expertise_section_inner row"> 
</div> 

<!-- ***** if the below is removed, the remaining 3 divs do not center correctly --> 
<div class="expertise_div blue_div" > 
<div class="expertise_content"> 
<p>Paragraph 1...</p> 
</div> 
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div> 

<div class="expertise_div green_div" > 
<div class="expertise_content"> 
<p>Paragraph 2...</p> 
</div> 
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div> 

<div class="expertise_div maroon_div"> 
<div class="expertise_content"> 
<p>Paragraph 3...</p> 
</div> 
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div> 

<div class="expertise_div orange_div"> 
<div class="expertise_content"> 
<p>Paragraph 4...</p> 
</div> 
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div> 

</div> 
</div> 
</div> 

CSSは以下の通りです:

@charset "utf-8"; 

.expertise_section { 
    padding: 85px 0; 
} 
.expertise_section h2 { 
    font-weight:500; 
    margin-top:7px; 
} 
.expertise_div { 
    float: left; 
    width: 24.2%; 
    margin-right:1.05%; 
} 
.expertise_div:nth-child(4n+4) { 
    margin-right:0px; 
} 
.expertise_div p { 
    font-family: 'Open Sans', sans-serif; 
    color: #6c6c6c; 
    font-weight: 300; 
} 
.expertise_content { 
    border-bottom: 2px solid #45acba; 
    border-top: 2px solid #45acba; 
    margin: 18px 0; 
    padding: 26px 0; 
} 
.expertise_content p:last-child { 
    margin-bottom:2px; 
} 
.expertise_div a { 
    color: #3a9cab; 
    font-size: 14px; 
    font-weight: 500; 
} 
.expertise_section_inner { 
    margin-top:36px; 
} 
.green_div .expertise_content { 
    border-color:#8ebb29; 
} 
.green_div a { 
    color:#8ebb29; 
} 
.maroon_div .expertise_content { 
    border-color:#81515d; 
} 
.maroon_div a { 
    color:#81515d; 
} 
.orange_div .expertise_content { 
    border-color:#d86435; 
} 
.orange_div a { 
    color:#d86435; 
} 
.show_1023 { 
    display:none; 
} 

@media only screen and (max-width: 1199px) { 
    .container { 
     padding:0px 35px !important; 
    } 
} 
@media only screen and (max-width: 1023px) { 
    .expertise_div { 
     float: left; 
     margin-right: 3.2%; 
     width: 48%; 
     margin-bottom:53px; 
    } 
    .expertise_div:nth-child(2n+2) { 
     margin-right: 0; 
    } 
    .expertise_section { 
     padding: 85px 0 30px; 
    } 
    .expertise_section_inner { 
     padding: 0 101px; 
    } 
    header { 
     padding: 20px 15px; 
    } 
} 

@media only screen and (max-width: 951px) { 
    .container { 
     padding: 0 30px !important; 
    text-align: center; 
    } 
} 

@media only screen and (max-width: 767px) { 
    .container { 
     padding: 0 30px !important; 
    text-align: center; 
    } 
    .expertise_section { 
     padding: 78px 0 45px; 
    } 
    .expertise_div { 
     width:100%; 
     margin-right:0px; 
     margin-bottom:31px; 
    } 
    .expertise_section_inner { 
     padding: 0; 
     margin-top:33px; 
    } 
    .hide_767 { 
     display:none; 
    } 
    .show_767 { 
     display:block; 
    } 
} 

@media only screen and (max-width: 479px) { 
    .container { 
     padding: 0 30px !important; 
    text-align: center; 
    } 
    .hide_480 { 
     display:none !important; 
    } 
    .show_479 { 
     display:block !important; 
    } 
} 

jsfiddleはここにある:https://jsfiddle.net/PaulPerkins/4zh6k6oL/2/

+0

わかりません新しいBootstrap v4では、フレックスボックスベースの列クラスを利用できます。これはあなたが必要とするものを行うはずです。 – bobbyz

答えて

2

.container { 
    text-align: center; 
} 

を追加し、どこでもfloat: left;を削除します。代わりに、div -sは、.expertise_divdisplay: inline-block;のインライン要素のように動作します。これらの要素は常にインライン要素.containerの中央に配置されます。また、要素の可変数を考慮に入れて、各ビューポートサイズの要素の余白に関するスタイルを絞り込む必要があります。 Flex Box Layoutは、これらのすべてのマージンやケースについて心配したくない場合は、代替案となる可能性があります。

@charset "utf-8"; 
 
.container { 
 
    text-align: center; 
 
} 
 
.expertise_section { 
 
    padding: 85px 0; 
 
} 
 
.expertise_section h2 { 
 
    font-weight:500; 
 
    margin-top:7px; 
 
} 
 
.expertise_div { 
 
    display: inline-block; 
 
    width: 24.2%; 
 
    margin-right:1.05%; 
 
} 
 
.expertise_div:nth-child(4n+4) { 
 
    margin-right:0px; 
 
} 
 
.expertise_div p { 
 
    font-family: 'Open Sans', sans-serif; 
 
    color: #6c6c6c; 
 
    font-weight: 300; 
 
} 
 
.expertise_content { 
 
    border-bottom: 2px solid #45acba; 
 
    border-top: 2px solid #45acba; 
 
    margin: 18px 0; 
 
    padding: 26px 0; 
 
} 
 
.expertise_content p:last-child { 
 
    margin-bottom:2px; 
 
} 
 
.expertise_div a { 
 
    color: #3a9cab; 
 
    font-size: 14px; 
 
    font-weight: 500; 
 
} 
 
.expertise_section_inner { 
 
    margin-top:36px; 
 
} 
 
.green_div .expertise_content { 
 
    border-color:#8ebb29; 
 
} 
 
.green_div a { 
 
    color:#8ebb29; 
 
} 
 
.maroon_div .expertise_content { 
 
    border-color:#81515d; 
 
} 
 
.maroon_div a { 
 
    color:#81515d; 
 
} 
 
.orange_div .expertise_content { 
 
    border-color:#d86435; 
 
} 
 
.orange_div a { 
 
    color:#d86435; 
 
} 
 
.show_1023 { 
 
    display:none; 
 
} 
 

 
@media only screen and (max-width: 1199px) { 
 
    .container { 
 
     padding:0px 35px !important; 
 
    } 
 
} 
 
@media only screen and (max-width: 1023px) { 
 
    .expertise_div { 
 
     margin-right: 3.2%; 
 
     width: 48%; 
 
     margin-bottom:53px; 
 
    } 
 
    .expertise_div:nth-child(2n+2) { 
 
     margin-right: 0; 
 
    } 
 
    .expertise_section { 
 
     padding: 85px 0 30px; 
 
    } 
 
    .expertise_section_inner { 
 
     padding: 0 101px; 
 
    } 
 
    header { 
 
     padding: 20px 15px; 
 
    } 
 
} 
 

 
@media only screen and (max-width: 951px) { 
 
    .container { 
 
     padding: 0 30px !important; 
 
    text-align: center; 
 
    } 
 
} 
 

 
@media only screen and (max-width: 767px) { 
 
    .container { 
 
     padding: 0 30px !important; 
 
    text-align: center; 
 
    } 
 
    .expertise_section { 
 
     padding: 78px 0 45px; 
 
    } 
 
    .expertise_div { 
 
     width:100%; 
 
     margin-right:0px; 
 
     margin-bottom:31px; 
 
    } 
 
    .expertise_section_inner { 
 
     padding: 0; 
 
     margin-top:33px; 
 
    } 
 
    .hide_767 { 
 
     display:none; 
 
    } 
 
    .show_767 { 
 
     display:block; 
 
    } 
 
} 
 

 
@media only screen and (max-width: 479px) { 
 
    .container { 
 
     padding: 0 30px !important; 
 
    text-align: center; 
 
    } 
 
    .hide_480 { 
 
     display:none !important; 
 
    } 
 
    .show_479 { 
 
     display:block !important; 
 
    } 
 
}
<div class="expertise_section text-center"> 
 
<div class="row"> 
 
<div class="container"> 
 
<h2>Centered Headline</h2> 
 
<div class="expertise_section_inner row"> 
 
</div> 
 

 
<!-- ***** if the below is removed, the remaining 3 divs do not center correctly --> 
 
<div class="expertise_div blue_div" > 
 
<div class="expertise_content"> 
 
<p>Paragraph 1...</p> 
 
</div> 
 
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div> 
 

 
<div class="expertise_div green_div" > 
 
<div class="expertise_content"> 
 
<p>Paragraph 2...</p> 
 
</div> 
 
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div> 
 

 
<div class="expertise_div maroon_div"> 
 
<div class="expertise_content"> 
 
<p>Paragraph 3...</p> 
 
</div> 
 
<a href="https://www.jsfiddle.net" target="_blank">READ MORE</a></div> 
 

 

 

 
</div> 
 
</div> 
 
</div>

1

あなたはフロートを持っている要素をセンタリングすることはできません。財産を残しました。ラッパーの中央:あなたは何ができるか

インラインブロックとしてあなたの要素をレンダリングしテキスト整列を使用しています。

このフィドルに従ってください:

HTML:

<div class="wrapper"> 
    <div class="boxes"> 
    <div class="box"> 
    </div> 
    <div class="box"> 
    </div> 
    <div class="box"> 
    </div> 
    </div> 
</div> 

CSS:あなたがに切り替える場合は、このプロジェクトのために可能ですが、場合

.wrapper{ 
    width: 600px; 
    margin: 0 auto; 
    font-size: 0; 
    text-align: center; 
} 

.boxes{ 
    width: 100%: 
} 

.box{ 
    font-size: 0; 
    width: 250px; 
    height: 150px; 
    background: #fff; 
    border: 1px solid #eee; 
    display: inline-block; 
} 

http://jsfiddle.net/nftxsw7h/

関連する問題