The code snippet below technically achieves the goal of having a footer that has layout for wide and small screens as seen in the images below.
My question, am I using flex box correctly? Is there a more optimal way to achieve the desired result as seen in the images? I ask as my css feels verbose and I'd like to learn if there's a better way.
WIDE Screen:
SMALL Screen:
.appFooter {
display: flex;
flex-flow: wrap;
justify-content: space-between;
text-align: center;
}
.appFooter ul.navigation {
margin: 0 0 48px 0;
padding: 0;
list-style: none;
}
.appFooter > * {
flex: 1 100%;
}
#media only screen and (min-width: 900px) {
.appFooter {
display: flex;
flex-flow: row wrap;
}
.appFooter ul.navigation {
margin: 0;
order: 2;
text-align: right;
}
.appFooter ul.navigation li {
display: inline;
margin-right: 20px;
}
.appFooter ul.navigation li:last-child {
margin-right: 0;
}
.appFooter .copyright {
order: 1;
text-align: left;
}
.appFooter > * {
flex: 1;
}
}
<footer class="appFooter">
<ul class="navigation">
<li>Contact Us</li>
<li>Terms of Service</li>
<li>Privacy Policy</li>
</ul>
<div class="copyright">
<span>© 2018 Site</span>
</div>
</footer>
Yes, I would say there's room for simplification in your code (both the CSS and HTML).
This should be all you need:
.appFooter {
text-align: center;
}
.navigation {
display: flex;
flex-direction: column;
margin-bottom: 48px;
}
#media (min-width: 900px) {
.appFooter {
display: flex;
justify-content: space-between;
}
.navigation {
flex-direction: row;
order: 1;
margin: 0;
}
.navigation a + a {
margin-left: 20px;
}
}
<footer class="appFooter">
<nav class="navigation">
Contact Us
Terms of Service
Privacy Policy
</nav>
<div class="copyright">© 2018 Site</div>
</footer>
https://jsfiddle.net/1sw59n4v/
For your desktop view, you don't need to worry about all the order and text-align declarations, as you can achieve the same result with flex-direction: row-reverse. Keep in mind that you'll still want to allow the elements to span a single line, you'll additionally need to remove the flex on the children with flex: inherit.
Also keep in mind that with your example, you have things like display: flex in the media query. These don't need to be re-declared in the media query, as the rules are inherited :)
Here's an example using flex-direction: row-reverse that cuts out the re-declarations:
.appFooter {
display: flex;
flex-flow: wrap;
justify-content: space-between;
text-align: center;
}
.appFooter ul.navigation {
margin: 0 0 48px 0;
padding: 0;
list-style: none;
}
.appFooter > * {
flex: 1 100%;
}
#media only screen and (min-width: 900px) {
.appFooter {
flex-direction: row-reverse;
}
.appFooter > * {
flex: inherit;
}
.appFooter ul.navigation li {
display: inline;
margin-right: 20px;
}
.appFooter ul.navigation li:last-child {
margin-right: 0;
}
}
<footer class="appFooter">
<ul class="navigation">
<li>Contact Us</li>
<li>Terms of Service</li>
<li>Privacy Policy</li>
</ul>
<div class="copyright">
<span>© 2018 Site</span>
</div>
</footer>
This cuts out more than half of your media query declarations, giving the exact same result.
Hope this helps! :)
You can optimize/shorten that code quite a bit, and at the same time increase its render flexibility.
By simply remove all elements but the actual links a and the copyright span, you easily control both their stacking/render order and alignment.
Initially set:
flex-direction: column, vertical direction
margin: 48px 0 0 0, top margin for copyright
With the media query:
flex-direction: row, switch to horizontal direction
order: -1, position the copyright first in order, enable it to align left
margin: 0 auto 0 0, reset top and push the links to the right by make its right margin "auto"
Stack snippet
.appFooter {
display: flex;
flex-direction: column;
align-items: center;
}
.appFooter span {
margin: 48px 0 0 0;
}
#media only screen and (min-width: 900px) {
.appFooter {
flex-direction: row;
}
.appFooter span {
order: -1;
margin: 0 auto 0 0;
}
.appFooter a + a {
margin-left: 10px;
}
}
<footer class="appFooter">
Contact Us
Terms of Service
Privacy Policy
<span>© 2018 Site</span>
</footer>
If you still want to wrap the links, just add its selector to the .appFooter {...} rule.
Stack snippet
.appFooter, .appFooter nav {
display: flex;
flex-direction: column;
align-items: center;
}
.appFooter span {
margin: 48px 0 0 0;
}
#media only screen and (min-width: 900px) {
.appFooter, .appFooter nav {
flex-direction: row;
}
.appFooter span {
order: -1;
margin: 0 auto 0 0;
}
.appFooter a + a {
margin-left: 10px;
}
}
<footer class="appFooter">
<nav>
Contact Us
Terms of Service
Privacy Policy
</nav>
<span>© 2018 Site</span>
</footer>
Related
I have an ul element (one of three on the page) that I don't want to be shown on mobile devices. I'm trying to apply display: none; to it but it doesn't work.
The code isn't mine, I just used a template from the internet, so go easy on me, plz
Html part
<section id="banner" class="major">
<div class="inner">
<header class="major">
<h1>This the title</h1>
</header>
<div class="content">
<p> and some text here </p>
<ul class="actions">
<li>A button</li>
</ul>
</div>
</div>
</section>
What I have in CSS
ul.actions {
display: -moz-flex;
display: -webkit-flex;
display: -ms-flex;
display: flex;
cursor: default;
list-style: none;
margin-left: -1em;
padding-left: 0;
}
ul.actions li {
padding: 0 0 0 1em;
vertical-align: middle;
}
ul.actions.special {
-moz-justify-content: center;
-webkit-justify-content: center;
-ms-justify-content: center;
justify-content: center;
width: 100%;
margin-left: 0;
}
ul.actions.special li:first-child {
padding-left: 0;
}
ul.actions.stacked {
-moz-flex-direction: column;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
margin-left: 0;
}
ul.actions.stacked li {
padding: 1.3em 0 0 0;
}
ul.actions.stacked li:first-child {
padding-top: 0;
}
ul.actions.fit {
width: calc(100% + 1em);
}
ul.actions.fit li {
-moz-flex-grow: 1;
-webkit-flex-grow: 1;
-ms-flex-grow: 1;
flex-grow: 1;
-moz-flex-shrink: 1;
-webkit-flex-shrink: 1;
-ms-flex-shrink: 1;
flex-shrink: 1;
width: 100%;
}
ul.actions.fit li > * {
width: 100%;
}
ul.actions.fit.stacked {
width: 100%;
}
ul.actions are used for different buttons throughout the page: one that I showed you in the html part and two other buttons to fill out a form.
So, I'm adding this bit of code but it just won't work
#media screen and (max-width: 480px) {
#banner .actions {
display: none;
}
}
You have used this code in wrong CSS file.
Please use right approch:
Your mentioned URL have this CSS
https://html5up.net/uploads/demos/forty/assets/css/main.css
The media query #media screen and (max-width: 480px) is mentioned in this css. Please go there
Please use this bit of code in this media query
#banner .actions {
display: none;
}
The code seems to work fine, I am not sure if you opened the page on the mentioned browsers before doing some code editing. In that case, clearing the caches could help.
That code works fine.
If this is not work fine on you site It seems like any other declaration overriding this declarations.
When an important rule is used on a style declaration, this declaration overrides any other declarations.
Please use CSS rule with !important and check after Clear your cache.
#media screen and (max-width: 480px) {
#banner .actions {
display: none !important;
}
}
Hi I'm trying to make my nav_list class is not responsive i have used the following code, but when I test on browser it isn't responsive gets cut from view.
/*queries*/
#media only screen and (max-width: 1200px) {
.nav_list {
width: 100%;
padding: 0 2%;
}
}
/*css*/
.top_nav {
display: flex;
}
.nav_list {
display: inline-flex;
list-style: none;
margin-right: 150px;
}
<header>
<nav>
<ul class="nav_list">
<li class="nav_list_item"><a>Sign in</a></li>
<li class="nav_list_item"><a>What is Shi</a></li>
<li class="nav_list_item"><a>Sign up</a></li>
</ul>
</nav>
</header>
What you need is media query and flex-direction: column.
For example, if overflow occur in 768px, then you would need to change your flex direction from row (default) to column view.
.nav_list {
display: flex;
}
#media (max-width: 767.98px) {
.nav_list {
flex-direction: column;
}
}
Or a smarter way flex-wrap:
.nav_list {
display: flex;
flex-wrap: wrap;
}
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 }
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;
}
}
I have the following code for flexbox and it works perfectly in Chrome and Firefox, however it's not effective in Safari(any version). I have tried specific prefixes but couldn't achieve the same simple order. What should I modify/add for Safari so it will work normal like Chrome and Firefox?
JSFiddle
.container {
display: -webkit-flexbox;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
flex-direction: row;
-webkit-flex-direction: row;
list-style:none;
}
li {
border:10px salmon solid;
padding: 25px 15px;
text-align: center;
font-size: 1.2em;
background: white;
-webkit-flex-grow: 3;
flex-grow: 3;
}
<ul class="container">
<li>Alabama</li>
<li>California</li>
<li>Florida</li>
<li>Idaho</li>
<li>Kansas</li>
<li>Nevada</li>
<li>New York</li>
</ul>
Safari 5 (the latest windows version of safari) only supports flexbox with the old syntax and with the -webkit- prefix. (display:-webkit-box)
http://caniuse.com/#search=flexbox
.container {
display: -webkit-box; /*safari*/
display: -ms-flexbox;
display: flex;
flex-flow: row wrap;
-webkit-flex-flow: row wrap; /* Safari 6.1+ */
justify-content: space-around;
flex-direction: row;
-webkit-flex-direction: row;
list-style:none;
}
li {
border:10px salmon solid;
padding: 25px 15px;
text-align: center;
font-size: 1.2em;
background: white;
flex-grow: 3;
}
<ul class="container">
<li>Alabama</li>
<li>California</li>
<li>Florida</li>
<li>Idaho</li>
<li>Kansas</li>
<li>Nevada</li>
<li>New York</li>
</ul>
Safari 5 does also not support wrapping, so the responsiveness won't be there. safari 6.1+ does support this property using the -webkit- prefix, but it's only available for mac.
If you really want the responsiveness, you could try to make a grid with media queries and floats. here's an example i made:
https://jsfiddle.net/pvLsw09u/2/
.container {
list-style:none;
width:100%;
overflow: hidden;
padding:0;
background: salmon; /*same color as border. just to look better*/
}
.container li {
box-sizing:border-box;
border:10px salmon solid;
padding: 25px 15px;
text-align: center;
font-size: 1.2em;
background: white;
width:100%;
float:left;
margin:0;
}
/*media queries*/
#media (min-width: 480px)
{
.container li {
width: 50%;
}
}
#media (min-width: 640px)
{
.container li {
width: 33%;
}
}
#media (min-width: 768px)
{
.container li {
width: 25%;
}
}
#media (min-width: 1024px)
{
.container li {
width: 20%;
}
}
#media (min-width: 1280px)
{
.container li {
width: 15%;
}
}
#media (min-width: 1824px)
{
.container li {
width: 10%;
}
}
/*etc...*/
<ul class="container">
<li>Alabama</li>
<li>California</li>
<li>Florida</li>
<li>Idaho</li>
<li>Kansas</li>
<li>Nevada</li>
<li>New York</li>
<li>Alabama</li>
<li>California</li>
<li>Florida</li>
<li>Idaho</li>
<li>Kansas</li>
<li>Nevada</li>
<li>New York</li>
</ul>
It's not as perfect as flexbox, but it's a good replacement.
The best thing you can do, is combine flexbox and the responsive grid using modernizr.
you make your styles with flexbox, but when modernizr detects .no-flexbox, you can use the responsive grid as a fix (or use the grid, and when modernizr detects .flexbox, use flexbox. you can choose)
http://jsfiddle.net/msf67Lum/
.container {
list-style:none;
width:100%;
overflow: hidden;
padding:0;
background: salmon; /*same color as border. just to look better*/
/*flexbox*/
display: -ms-flexbox;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
flex-direction: row;
-webkit-flex-direction: row;
}
.container li {
box-sizing:border-box;
border:10px salmon solid;
padding: 25px 15px;
text-align: center;
font-size: 1.2em;
background: white;
margin:0;
flex-grow: 3;
}
.no-flexbox .container li
{
width:100%;
float:left;
}
/*media queries*/
#media (min-width: 480px)
{
.no-flexbox .container li {
width: 50%;
}
}
#media (min-width: 640px)
{
.no-flexbox .container li {
width: 33%;
}
}
#media (min-width: 768px)
{
.no-flexbox .container li {
width: 25%;
}
}
#media (min-width: 1024px)
{
.no-flexbox .container li {
width: 20%;
}
}
#media (min-width: 1280px)
{
.no-flexbox .container li {
width: 15%;
}
}
#media (min-width: 1824px)
{
.no-flexbox .container li {
width: 10%;
}
}
/*etc...*/
<!--make sure to include modernizr!-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
<ul class="container">
<li>Alabama</li>
<li>California</li>
<li>Florida</li>
<li>Idaho</li>
<li>Kansas</li>
<li>Nevada</li>
<li>New York</li>
<li>Alabama</li>
<li>California</li>
<li>Florida</li>
<li>Idaho</li>
<li>Kansas</li>
<li>Nevada</li>
<li>New York</li>
</ul>
this example works with flexbox when it's supported, but in safari (or when not supported) it's done with the grid
You need to add a webkit prefix for display:
display: -webkit-flex;
https://css-tricks.com/using-flexbox/
http://caniuse.com/#feat=flexbox