Shinji-san Shinji-san - 5 months ago 4
CSS Question

Using flexbox for mobile, getting border width of 100%

I'm using flexbox for my navigation and I can't seem to get my borders to work right for my media query specified for column wrap(the bottom query). I want the borders to stretch across the device width completely, for example I want the border all the way from the left to all the way on the right on the device, and I want each a element to take up one 'row' on the device but currently the borders only stretch half way and some of the li elements are sharing a row when others are not and I'm not too sure why. Even when I make 'a' display block, it's not working. I also tried making flex basis 100% for the a elements.



#nav{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
margin: 0;
flex-flow: row wrap;
justify-content: center;
align-items: center;
background-color: orange;
}


#nav li a{
color: white;
margin: 15px;
padding: 5px;
display: block;;
text-decoration: none;
font-size: 24px;
font-family: courier;
}

#nav li {
list-style: none;

}


#nav ul{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
margin: 0;
flex-flow: row wrap;
justify-content: center;
align-items: center;


}


@media all and(max-width: 800px) {

#nav{

justify-content: space-around;

}

}

@media all and (max-width: 500px)
{

#nav{

flex-flow: column wrap;
padding: 5px;

}





#nav li a{
flex: 1 100%;
display: block;
padding: 5px;
border-top: 1px solid black;
border-bottom: 1px solid black;
}


}

<div id="nav">

<ul>
<li> <a href="#">Calendar </a></li>
<li> <a href="#">Events </a></li>
<li> <a href="#">Hours </a></li>
<li> <a href="#">Contact </a></li>
<li> <a href="#">About </a></li>

</ul>

</div>




Answer

Try to reset the default padding on ul element.

ul {
  padding-left: 0;
}

also reset the default margin on body if needed.

body {
  margin: 0;
}

If you want the each li to take 100% width.

@media all and (max-width: 500px) {
  #nav {
    flex-direction: column; /*updated*/
  }
  #nav ul {
    padding-left: 0; /*added*/
  }
  #nav li {
    flex: 1 1 100%; /*updated*/
    padding: 5px;
    border-top: 1px solid black;
    border-bottom: 1px solid black;
  }
}

jsFiddle

Comments