Flexbox for mobile border width of 100% - html

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.
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 to display block, it's not working. I also tried making flex basis 100% for the a elements.
Edit: I think I figured it out for the positioning. I made the #nav ul. not just #nav column wrap. However, I'm still experiencing the border problem unfortunately.
#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> Calendar </li>
<li> Events </li>
<li> Hours </li>
<li> Contact </li>
<li> About </li>
</ul>
</div>

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;
}
For the flexbox part, you should set display: flex; on #nav ul, rather than #nav. You don't need to set any style on #nav actually. If you want the each li to take 100% width in the media query, you can set flex item to flex: 1 1 100%;, working example in the fiddle below.
#media all and (max-width: 500px) {
#nav li {
flex: 1 1 100%;
padding: 5px;
border-top: 1px solid black;
border-bottom: 1px solid black;
}
}
jsFiddle

Related

How to get rid of unwanted space in a Chromium Flexbox?

I'm quite new to html 5 and css 3 but I wanted to make project page with flexbox and ran into a problem:
The site looks good in desktop mode but when it switches to the mobile view and "flex-flow: column" there is way too much space between the items as you can see in the pictures below.
Desktop version
Smartphones
The problem only occurs in chromium based browsers (Google Chrome, Vivaldi, Iron, Opera) Firefox, IE and Edge work well.
I'm not allowed to post images yet so I just put the links here.
Down there you can see my CSS code. I hope some can help me with this!
body {
display: flex;
flex-flow: row wrap;
margin: 0 auto;
background-color: f4f4f4;
font-family: Hind, Sans-serif;
justify-content: space-between;
}
header,
nav,
nav a,
article,
aside,
footer {
border-radius: 0px 0,5em 0em;
padding: 15px;
margin: 0.5em;
flex: 1 100%;
}
header {
background: var(--color-primary);
display: flex;
flex-flow: column;
align-items: center;
}
header * {
flex: 1 1 0%;
}
header nav {
flex: 1 1 100%;
}
nav,
nav ul,
nav li {
margin: 0;
padding: 0;
border: none;
}
nav ul {
display: flex;
flex-direction: column;
}
nav li {
list-style-type: none;
margin: 1.3em 0;
flex: 1 1 100%;
}
nav a {
display: inline-block;
width: 95%;
/*background: #fffbf0;*/
border-bottom: solid 0.1em;
border-color: var(--color-primary);
margin: 0;
font-weight: bold;
text-decoration: none;
text-align: center;
color: var(--color-secondary-2);
}
#media all and (min-width: 35em) {
header {
flex-flow: row wrap;
}
nav ul {
flex-direction: row;
}
nav li {
margin: 0 0.5em;
flex: 1 1 0%;
}
Now for the essential HTML part:
<header>
<nav>
<ul>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
<li>item6</li>
<li>item7</li>
</ul>
</nav>
</header>
It will work if you remove align-items: center; from the header rule
header {
background: var(--color-primary);
display: flex;
flex-flow: column;
/* align-items: center; commented out */
}
and change to flex: 1 1 0%; instead of flex: 1 1 100%; in your nav li rule
nav li {
list-style-type: none;
margin: 1.3em 0;
flex: 1 1 0%; /* change basis to 0% */
}
You also need to close your media query properly by adding its missing end bracket }

flexbox mobile first menu issue

I have tried to emulate the excellent flexbox tutorials by Wes Bos. I wanted to convert one specific tutorial he has on responsive flexbox menu. But I wanted my menu to be done with mobile first so I did my media queries with min-width.
But I am not able to make it work properly on the default mobile layout. In the menu created by Wes, the li items are stacked upon each other and the social icons at the bottom have flex:1 1 25%. But my social icons are also stacked.
On the other breakpoints my layout follows the one that Wes created.
I have set up a codepen for my code.
.flex-nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
}
.flex-nav .social {
flex: 1 1 25%;
}
#media all and (min-width:500px) {
.flex-nav li {
flex: 1 1 50%;
}
.flex-nav ul {
flex-wrap: wrap;
flex-direction: row;
}
.flex-nav ul {
border: 1px solid black;
}
}
#media all and (min-width:800px) {
.flex-nav li {
flex: 3;
}
.flex-nav .social {
flex: 1;
}
}
This is your default code (no media queries applied):
.flex-nav ul {
display: flex;
flex-direction: column;
}
.flex-nav .social {
flex: 1 1 25%;
}
Yes, you've given each social media icon a flex-basis: 25%.
BUT, your container is flex-direction: column.
So the flex rule applied to your social media icons works vertically, not horizontally.
Consider this method instead:
.flex-nav ul {
display: flex;
flex-wrap: wrap;
}
li {
flex: 0 0 100%; /* sized to fit one item per row */
}
.flex-nav .social {
flex: 0 0 25%; /* sized to fit four items per row */
}
revised demo
I've created a container for the social links so it's more easy (at least for me) structure the menu.
SEE IN CODEPEN
Here the html:
<div class="wrapper">
<nav class="flex-nav">
<ul>
<li>item01</li>
<li>item02</li>
<li>item03</li>
<li>item04</li>
<li>item05</li>
<li>item06</li>
<div class="social-container">
<li class="social"><i class="fa fa-gift"></i></li>
<li class="social"><i class=" fa fa-glass"></i></li>
<li class="social"><i class=" fa fa-calendar"></i></li>
<li class="social"><i class=" fa fa-cutlery"></i></li>
</div>
</ul>
</nav>
</div>
CSS:
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
font-family: sans-serif;
margin: 0;
}
a {
color: white;
font-weight: 100;
letter-spacing: 2px;
text-decoration: none;
background: rgba(0, 0, 0, 0.2);
padding: 20px 5px;
display: inline-block;
width: 100%;
text-align: center;
transition: all 0.5s;
}
a:hover {
background: rgba(0, 0, 0, 0.3);
}
.wrapper {
max-width: 1000px;
margin: 0 auto;
padding: 50px;
}
.flex-nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100%; /* ADDED */
}
.flex-nav .social {
flex: 1 1 25%;
}
.social-container { //just make it flex container
display: flex;
width: 100%;
}
#media all and (min-width:500px) {
.flex-nav li {
flex: 1 1 50%;
}
.flex-nav ul {
flex-wrap: wrap;
flex-direction: row;
}
.flex-nav ul {
border: 1px solid black;
}
}
#media all and (min-width:800px) {
.flex-nav li {
flex: 1;
}
.flex-nav .social {
/*flex: 1;*/
}
.social-container {
flex: 2; /* set the value as many as you want */
}
.flex-nav ul { //change the direction
flex-direction: row;
flex-wrap: no-wrap;
}
}

How to stretch flex child so that background color matches the height of sibling

I have two links in li, one with text which can be short or long, and the other is icon. The background-color of the icon should match the height of text link while text should have defined padding. It doesn't have to be flexbox, I just thought it should be easier.
http://codepen.io/anon/pen/GrQZKN
li {
display: flex;
justify-content: space-between;
align-items: center;
.first {
background-color: #333;
padding: 20px;
color: #fff;
flex: 1;
}
.second {
background-color: orange;
align-items: stretch;
display: flex;
}
}
<ul>
<li>
Link
(icon)
</li>
<li>
Longer LongerLonger Longer link
(icon)
</li>
</ul>
You can just do the below:
Remove align-items: center from li (for stretching the second)
Replace align-items: stretch with align-items: center in li.second (for vertical alignment)
See demo below:
body {
max-width: 300px;
background-color: #ddd;
}
li {
display: flex;
justify-content: space-between;
/*align-items: center;*/
border-bottom: 1px solid #eee;
}
li .first {
background-color: #333;
padding: 20px;
color: #fff;
flex: 1;
}
li .second {
background-color: orange;
align-items: center; /*CHANGED THIS*/
display: flex;
}
<ul>
<li>
Link
(icon)
</li>
<li>
Longer LongerLonger Longer link
(icon)
</li>
</ul>

Flexbox not providing intended margins

My list is (somewhat) successfully calling flexbox's space-around alignment, except the first-child of the footer list in question has a relatively larger margin on it's left. Cannot figure this out and would rather not risk its future with a cheat. Here's the code; any input helps, thank you:
#footer {
width: 75%;
background: dimgray;
height: 150px;
/* position: relative;
*/ bottom: 0;
position: fixed;
display: flex;
display: -webkit-flex;
}
#footer ul {
width: 100%;
display: -webkit-flex;
display: flex;
flex-direction: row;
justify-content: space-around;
-webkit-justify-content: space-around;
}
#footer li {
width: 30%;
height: 130px;
list-style: none;
border: 1px solid white;
}
The ul element automatically gets a default padding from the browser.
#footer ul {
padding-left: 0;
}

CSS3 Flex: Pull child to the right

here's what I have Fiddle
ul {
display: flex;
justify-content: flex-start;
flex-direction: row;
align-items: center;
width: 100%;
height: 100px;
background: #333;
padding: 15px;
}
ul li {
padding: 15px;
margin: 5px;
background: #efefef;
border: 1px solid #ccc;
display: inline-block;
list-style: none;
}
#item-1 {
height: 50px;
}
#item-2 {
height: 70px;
}
<ul>
<li id="item-1">Home</li>
<li id="item-2">Menu</li>
<li>More</li>
<li>Stuff</li>
<li>Settings</li>
</ul>
I want the last item inside the flex-box to be pulled to the right ("Settings" in my fiddle) while keeping all other items the way they are. The "Settings"-item should also be centered vertically and everything.
align-self: flex-end pushes the item to the bottom (I want it on the right).
I would very much prefer a solution using flex-box because my items have variable heights and should always be centered vertically.
What is the cleanest way to achieve this?
Thanks for your help!
Simple fix, use an auto-adjusting margin:
ul li:last-child {
margin-left: auto;
}
You may also want to not use width: 100% so that the element stays inside the visible area:
ul {
display: flex;
justify-content: flex-start;
flex-direction: row;
align-items: center;
/* width: 100%; */
height: 100px;
background: #333;
padding: 15px;
}
http://jsfiddle.net/dwLHE/
See also https://www.w3.org/TR/css-flexbox-1/#auto-margins