2016-05-22 8 views
1

私はクライアント用の簡単なホームページを作成しています。私はフレックスボックスを使ってページのメインコンテンツをレイアウトしています。私は2 div.containerのものを持っていて、それぞれ2つのimgを含んでいます。フレックスアイテムを大きなスクリーンに並べて表示するが、小さなスクリーンに縦に積み重ねる

デスクトップビューでは、2 div.containerをインラインで表示したいが、ウェブサイトがモバイルビューである場合は、それらを重ねて表示したい。

私はまた、コンテンツの残りの部分は関係なく、それがであるもののビュー積み重ねないままにしておきたい。

私はこれを実現するにはどうすればよいですか?

function setHeight() { 
 
\t $('.img-c').css('height', $('.img-c').innerWidth()); 
 
} 
 

 
setInterval(setHeight, 10);
:root { 
 
    background: #fff; 
 
} 
 

 
#logo { 
 
    display: block; 
 
    margin: auto; 
 
    width: 200px; 
 
    text-align: center; 
 
} 
 

 
nav ul { 
 
    display: flex; 
 
    justify-content: space-around; 
 
    list-style: none; 
 
    background: #0a0; 
 
    color: #fff; 
 
    font-size: 1.5rem; 
 
    font-family: 'Segoe UI', sans-serif; 
 
} 
 

 
li.selected { 
 
    color: orange; 
 
} 
 

 
nav ul li:hover { 
 
    color: springgreen; 
 
    cursor: pointer; 
 
} 
 

 
nav ul li.selected:hover { 
 
    color: #ff7700; 
 
} 
 

 
main { 
 
    display: flex; 
 
    flex-direction: column; 
 
    align-items: center; 
 
    flex-wrap: wrap; 
 
} 
 

 
main p { 
 
    text-align: center; 
 
    max-width: 80%; 
 
    font-size: 1.5rem; 
 
    font-family: 'Segoe UI'; 
 
} 
 

 
main p:nth-of-type(3) { 
 
    font-weight: bold; 
 
} 
 

 
main hr { 
 
    width: 100%; 
 
    background: #0a0; 
 
    height: 1px; 
 
} 
 

 
.img-c { 
 
    width: 20%; 
 
    background-color: #000; 
 
    max-width: 200px; 
 
    min-width: 100px; 
 
} 
 

 
@media screen and (max-width: 416px) { 
 
    #bottom { 
 
    display: block; 
 
    order: 1; 
 
    } 
 
    
 
    .img-c { 
 
    width: 50%; 
 
    } 
 
} 
 

 
@media screen and (max-width: 358px) { 
 
    nav ul { 
 
    flex-direction: column; 
 
    padding: 0; 
 
    } 
 
    
 
    nav ul li { 
 
    width: 100%; 
 
    text-align: center; 
 
    } 
 
    
 
    nav ul li:nth-child(2n) { 
 
    background: #0c0; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<header> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/CompImmer_logo_final_web4.png" id="logo" alt="logo"/> 
 
    <nav> 
 
    <ul> 
 
    <li class="selected">Home</li> 
 
    <li>About</li> 
 
    <li>Classes</li> 
 
    <li>Jobs</li> 
 
    </ul> 
 
    </nav> 
 
</header> 
 
<main> 
 
    <div class="container"> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/2.jpg" alt="img" class="img-c"/> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/5.jpg" alt="img" class="img-c" style="background: magenta"/> 
 
     </div> 
 
     <div class="container"> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/1.jpg" alt="img" class="img-c"/> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/4.jpg" alt="img" class="img-c" style="background: magenta"/> 
 
    </div> 
 
    <p>Are you looking for computer classes that go beyond the typical summer camp or one-day experience?</p> 
 
    <hr/> 
 
    <p>Does your child have an interest in coding or computer hardware and want to learn what to do next?</p> 
 
    <hr/> 
 
    <p>Computer Immersion has the answer!</p> 
 
    <hr/> 
 
    <p>We teach computer science and tech culture to children who are 12-18 years old. Our classes immerse students in a wide range of hardware and software related topics such as scripting, coding, “tech speak”, networking, and hardware.</p> 
 
    <hr/> 
 
    <p>Our classes are taught by instructors with real world industry experience and meet for 9-week sessions at locations convenient for you.</p> 
 
    <hr/> 
 
</main>

答えて

0
  • 別のdivを使用して画像.container divをラップします。
    例:<div class="gallery">

  • のみラッパーのdivにフレックスプロパティを追加します。

.gallery { 
    display: flex; 
    flex-wrap: wrap; 
    justify-content: center; 
} 

ビューのコードスニペット: https://jsfiddle.net/warkentien2/3uavncu2/1/

ボーナス、これらのフレキシボックスを試してみてくださいチュートリアル:
http://flexboxin5.com/
http://flexboxfroggy.com/

1

STEP 1:

  • 1フレックスコンテナ内のすべての4枚の画像を包みます。

    <div id="container"> 
        <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/2.jpg" alt="img" class="img-c"/> 
        <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/5.jpg" alt="img" class="img-c" style="background: magenta"/> 
        <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/1.jpg" alt="img" class="img-c"/> 
        <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/4.jpg" alt="img" class="img-c" style="background: magenta"/> 
    </div> 
    

ステップ2:

  • スタイル大画面用のフレックスアイテム。

    .container { 
        display: flex; 
        justify-content: space-around; 
    } 
    

STEP 3

  • スタイル小さい画面用のフレックスアイテム。

    @media screen and (max-width: 416px) { 
        .container { 
         flex-wrap: wrap; 
        } 
        .img-c { 
         width: 50%; 
         border: 2px solid white; 
         box-sizing: border-box; 
        } 
    } 
    

DEMO

0

あなたは、単一の.containerを使用し、インラインフレックスボックスにそれを回すことができます。

imgの最小幅には、このコンテナのブレークポイントのX値を設定できます。それ以外の場合は、既に設定したメディアクエリ内でこれを並べ替えます。

function setHeight() { 
 
    $('.img-c').css('height', $('.img-c').innerWidth()); 
 
} 
 

 
setInterval(setHeight, 10);
.container { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    justify-content: center; 
 
    /* seems redundant but necessary, not inherited from parent flexbox */ 
 
} 
 
:root { 
 
    background: #fff; 
 
} 
 
#logo { 
 
    display: block; 
 
    margin: auto; 
 
    width: 200px; 
 
    text-align: center; 
 
} 
 
nav ul { 
 
    display: flex; 
 
    justify-content: space-around; 
 
    list-style: none; 
 
    background: #0a0; 
 
    color: #fff; 
 
    font-size: 1.5rem; 
 
    font-family: 'Segoe UI', sans-serif; 
 
} 
 
li.selected { 
 
    color: orange; 
 
} 
 
nav ul li:hover { 
 
    color: springgreen; 
 
    cursor: pointer; 
 
} 
 
nav ul li.selected:hover { 
 
    color: #ff7700; 
 
} 
 
main { 
 
    display: flex; 
 
    flex-direction: column; 
 
    align-items: center; 
 
    flex-wrap: wrap; 
 
} 
 
main p { 
 
    text-align: center; 
 
    max-width: 80%; 
 
    font-size: 1.5rem; 
 
    font-family: 'Segoe UI'; 
 
} 
 
main p:nth-of-type(3) { 
 
    font-weight: bold; 
 
} 
 
main hr { 
 
    width: 100%; 
 
    background: #0a0; 
 
    height: 1px; 
 
} 
 
.img-c { 
 
    width: 20%; 
 
    background-color: #000; 
 
    max-width: 200px; 
 
    min-width: 100px; 
 
} 
 
@media screen and (max-width: 416px) { 
 
    #bottom { 
 
    display: block; 
 
    order: 1; 
 
    } 
 
    .img-c { 
 
    width: 50%; 
 
    } 
 
} 
 
@media screen and (max-width: 358px) { 
 
    nav ul { 
 
    flex-direction: column; 
 
    padding: 0; 
 
    } 
 
    nav ul li { 
 
    width: 100%; 
 
    text-align: center; 
 
    } 
 
    nav ul li:nth-child(2n) { 
 
    background: #0c0; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<header> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/CompImmer_logo_final_web4.png" id="logo" alt="logo" /> 
 
    <nav> 
 
    <ul> 
 
     <li class="selected">Home</li> 
 
     <li>About</li> 
 
     <li>Classes</li> 
 
     <li>Jobs</li> 
 
    </ul> 
 
    </nav> 
 
</header> 
 
<main> 
 
    <div class="container"> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/2.jpg" alt="img" class="img-c" /> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/5.jpg" alt="img" class="img-c" style="background: magenta" /> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/1.jpg" alt="img" class="img-c" /> 
 
    <img src="http://www.computerimmersion.com/wp-content/uploads/2015/10/4.jpg" alt="img" class="img-c" style="background: magenta" /> 
 
    </div> 
 
    <p>Are you looking for computer classes that go beyond the typical summer camp or one-day experience?</p> 
 
    <hr/> 
 
    <p>Does your child have an interest in coding or computer hardware and want to learn what to do next?</p> 
 
    <hr/> 
 
    <p>Computer Immersion has the answer!</p> 
 
    <hr/> 
 
    <p>We teach computer science and tech culture to children who are 12-18 years old. Our classes immerse students in a wide range of hardware and software related topics such as scripting, coding, “tech speak”, networking, and hardware.</p> 
 
    <hr/> 
 
    <p>Our classes are taught by instructors with real world industry experience and meet for 9-week sessions at locations convenient for you.</p> 
 
    <hr/> 
 
</main>

関連する問題