2017-10-13 144 views
0

我希望我的main-content div在我的导航栏下移动,该屏幕在屏幕上重新调整为水平导航栏。当导航在屏幕上变化时,在另一个屏幕上移动div

我认为一些简单的像这样的工作:

@media screen and (max-width: 540px) { 
    #main-content{ 
    display:block; 
    width:100%; 
    } 
} 

但两者的div是并排显示。如何让我的main-content在我的导航栏下变为水平栏时立即移动?

这是我目前的做法证明了问题:

.site-wrapper { 
 
    height: 100%; 
 
    min-height: 100%; 
 
    display: flex; 
 
} 
 

 

 
/* NAVIGATION */ 
 

 
.nav-container { 
 
    border-right: 1px solid #E4E2E2; 
 
    height: 100%; 
 
    width: 200px; 
 
    background-color: #f4f3f3; 
 
} 
 

 
.logo-holder { 
 
    text-align: center; 
 
} 
 

 
.nav { 
 
    text-align: justify; 
 
} 
 

 
.nav-link { 
 
    display: block; 
 
    text-align: left; 
 
    color: #333; 
 
    text-decoration: none; 
 
    margin-left: 0px; 
 
    padding-left: 15px; 
 
} 
 

 
.nav-link:hover { 
 
    background-color: #333; 
 
    color: #f4f3f3; 
 
} 
 

 
.nav ul { 
 
    width: 100%; 
 
    padding: 0px; 
 
} 
 

 
.nav ul li { 
 
    margin-left: 5px; 
 
    list-style-type: none; 
 
    height: 25px; 
 
} 
 

 
.nav ul li a { 
 
    text-align: left; 
 
    padding: 5px; 
 
    color: #333; 
 
    text-decoration: none; 
 
    margin-left: 15px; 
 
} 
 

 
.nav li:hover a { 
 
    color: #f4f3f3; 
 
} 
 

 

 
/* MAIN CONTENT */ 
 

 
#main-content { 
 
    float: left; 
 
    margin: 10px; 
 
    border: 1px solid black; 
 
    width: 80%; 
 
    height: 500px; 
 
} 
 

 
@media screen and (max-width: 540px) { 
 
    #main-content { 
 
    display: block; 
 
    width: 100%; 
 
    } 
 
} 
 

 
.reg-div { 
 
    border: 1px solid red; 
 
    width: 80%; 
 
} 
 

 
.reg-div ul li { 
 
    list-style: none; 
 
    text-decoration: none; 
 
    color: #333; 
 
} 
 

 

 
/* MEDIA QUERIES */ 
 

 
@media screen and (max-width: 540px) { 
 
    .nav-container { 
 
    width: 100%; 
 
    height: 100px; 
 
    background-color: #f4f3f3; 
 
    border-bottom: 0.5px solid #f4f3f3; 
 
    } 
 
    .nav-link { 
 
    padding: 10px; 
 
    } 
 
    .logo-holder { 
 
    overflow: hidden; 
 
    display: block; 
 
    width: 40%; 
 
    } 
 
    .nav-container nav ul { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    justify-content: center; 
 
    } 
 
    .logo-holder { 
 
    text-align: left; 
 
    } 
 
    #navigation-div { 
 
    background-color: #f4f3f3; 
 
    margin-top: 0; 
 
    } 
 
}
<div class="site-wrapper"> 
 
    <!-- NAV-CONTAINER - LEFT OF PAGE --> 
 
    <div class="nav-container"> 
 
    <div class="logo-holder"> 
 
     <img class="user-select-none" width="150px" height="150px" src="" alt="logo here" /> 
 
    </div> 
 
    <div id="navigation-div"> 
 
     <nav class="nav"> 
 
     <ul class="nav-ul"> 
 
      <a class="nav-link active" href=""> 
 
      <li>test 1</li> 
 
      </a> 
 
      <a class="nav-link " href=""> 
 
      <li>test 2</li> 
 
      </a> 
 
      <a class="nav-link" href=""> 
 
      <li>test 3</li> 
 
      </a> 
 
     </ul> 
 
     </nav> 
 
    </div> 
 
    </div> 
 
    <!-- NAV-CONTAINER END --> 
 

 
    <div id="main-content"> 
 
    <div class="reg-div"> 
 
     <ul> 
 
     <li><a href="">Login</a> | <a href="">Sign Up</a></li> 
 
     </ul> 
 
    </div> 
 
    <!-- MAIN CONTENT END --> 
 
    </div>

JS提琴:https://jsfiddle.net/gtLhje4z/

回答

0

既然你已经设置了.site-wrapper是一个Flexbox的,加上媒体查询对于包装也是flex-direction: columnmax-width: 540px。默认情况下,flexbox被设置为一行。

另外,您不需要浮动'#main-content',因为flexbox会为您完成所有工作。

.site-wrapper { 
 
    height: 100%; 
 
    min-height: 100%; 
 
    display: flex; 
 
} 
 

 
@media screen and (max-width: 540px) { 
 
    .site-wrapper { 
 
     flex-direction: column; 
 
    } 
 
} 
 

 

 
/* NAVIGATION */ 
 

 
.nav-container { 
 
    border-right: 1px solid #E4E2E2; 
 
    height: 100%; 
 
    width: 200px; 
 
    background-color: #f4f3f3; 
 
} 
 

 
.logo-holder { 
 
    text-align: center; 
 
} 
 

 
.nav { 
 
    text-align: justify; 
 
} 
 

 
.nav-link { 
 
    display: block; 
 
    text-align: left; 
 
    color: #333; 
 
    text-decoration: none; 
 
    margin-left: 0px; 
 
    padding-left: 15px; 
 
} 
 

 
.nav-link:hover { 
 
    background-color: #333; 
 
    color: #f4f3f3; 
 
} 
 

 
.nav ul { 
 
    width: 100%; 
 
    padding: 0px; 
 
} 
 

 
.nav ul li { 
 
    margin-left: 5px; 
 
    list-style-type: none; 
 
    height: 25px; 
 
} 
 

 
.nav ul li a { 
 
    text-align: left; 
 
    padding: 5px; 
 
    color: #333; 
 
    text-decoration: none; 
 
    margin-left: 15px; 
 
} 
 

 
.nav li:hover a { 
 
    color: #f4f3f3; 
 
} 
 

 

 
/* MAIN CONTENT */ 
 

 
#main-content { 
 

 
    margin: 10px; 
 
    border: 1px solid black; 
 
    width: 80%; 
 
    height: 500px; 
 
} 
 

 
@media screen and (max-width: 540px) { 
 
    #main-content { 
 
    display: block; 
 
    width: 100%; 
 
    } 
 
} 
 

 
.reg-div { 
 
    border: 1px solid red; 
 
    width: 80%; 
 
} 
 

 
.reg-div ul li { 
 
    list-style: none; 
 
    text-decoration: none; 
 
    color: #333; 
 
} 
 

 

 
/* MEDIA QUERIES */ 
 

 
@media screen and (max-width: 540px) { 
 
    .nav-container { 
 
    width: 100%; 
 
    height: 100px; 
 
    background-color: #f4f3f3; 
 
    border-bottom: 0.5px solid #f4f3f3; 
 
    } 
 
    .nav-link { 
 
    padding: 10px; 
 
    } 
 
    .logo-holder { 
 
    overflow: hidden; 
 
    display: block; 
 
    width: 40%; 
 
    } 
 
    .nav-container nav ul { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    justify-content: center; 
 
    } 
 
    .logo-holder { 
 
    text-align: left; 
 
    } 
 
    #navigation-div { 
 
    background-color: #f4f3f3; 
 
    margin-top: 0; 
 
    } 
 
}
<div class="site-wrapper"> 
 
    <!-- NAV-CONTAINER - LEFT OF PAGE --> 
 
    <div class="nav-container"> 
 
    <div class="logo-holder"> 
 
     <img class="user-select-none" width="150px" height="150px" src="" alt="logo here" /> 
 
    </div> 
 
    <div id="navigation-div"> 
 
     <nav class="nav"> 
 
     <ul class="nav-ul"> 
 
      <a class="nav-link active" href=""> 
 
      <li>test 1</li> 
 
      </a> 
 
      <a class="nav-link " href=""> 
 
      <li>test 2</li> 
 
      </a> 
 
      <a class="nav-link" href=""> 
 
      <li>test 3</li> 
 
      </a> 
 
     </ul> 
 
     </nav> 
 
    </div> 
 
    </div> 
 
    <!-- NAV-CONTAINER END --> 
 

 
    <div id="main-content"> 
 
    <div class="reg-div"> 
 
     <ul> 
 
     <li><a href="">Login</a> | <a href="">Sign Up</a></li> 
 
     </ul> 
 
    </div> 
 
    <!-- MAIN CONTENT END --> 
 
    </div>