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;
}
}
Related
I have made a responsive navbar with html, css and basic js, that changes according to the width of the screen.
Although the code works perfectly and the navbar is responsive while resizing the window (that i preview my code to), when I test it with Google Chrome Inspect to see how it looks on other devices (phones, tablet, etc) it doesn't work, even though the width is smaller than the max-width i have set:
#media only screen and (max-width: 600px) {
.toggle-button {
display: flex;
}
.navbar-links {
display: none;
width: 100%;
}
.navbar {
flex-direction: column;
align-items: flex-start;
}
.navbar-links ul {
flex-direction: column;
width: 100%;
}
.navbar-links li {
text-align: center;
}
.navbar-links li a {
padding: 0.75rem 1rem;
}
.navbar-links.active {
display: flex;
}
}
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>
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 was wondering if there was a way to change the position of a navigation bar in mobile view by only using CSS3. For example instead of having the navigation bar underneath the top container in mobile view, I would like to have it fixed above the top container. I am using a wordpress theme, Thanks in advanced.
The website is www.capcar.com.au
You may try "transform" property with media rule.
Add css :
#media only screen and (max-width: 767px) {
.navigation_menu {
background: none repeat scroll 0 0 #ffffff;
position: absolute;
top: 0;
width: 100%;
}
.header_section {
background-color: #ffffff;
color: #99ca00;
padding-top: 51px;
width: 100%;
}
}
Flexbox can solve this issue pretty easily. Just add display: flex and flex-direction to #wrapper. Then, inside of a media query, add order: 0 to .header_section and order: -1 to .navigation_menu. This will switch those two around, but leave everything else in the order it was before.
You should be able to just add this to your css:
#wrapper {
display: flex;
flex-direction: column;
}
#media only screen and (max-width:640px) {
.header_section {
order: 0;
}
.navigation_menu {
order: -1;
}
}
var el = document.getElementById("toggle");
el.addEventListener("click", function(){
var menu = document.getElementById('menu');
menu.classList.toggle("show")
}, false);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#wrapper {
display: flex;
flex-direction: column;
}
header,
nav ul {
display: flex;
max-width: 800px;
width: 100%;
margin: 0 auto;
justify-content: space-between;
align-items: flex-end;
padding: 1rem 2rem;
}
header {
margin-bottom: 1rem;
}
#logo {
width: 40%;
}
.awards {
display: flex;
align-items: flex-end;
width: 40%
}
#award1 {
width: 25%
}
#award2 {
width: 75%;
}
nav {
background: linear-gradient(to bottom, #e3f5ab 0%, #99ca00 100%);
display: flex;
flex-direction: column;
min-height: 3rem;
}
#toggle {
display: none;
align-self: flex-end;
background: transparent;
border: none;
width: 2rem;
height: 2rem;
margin: .5rem 1rem;
outline: none;
cursor: pointer;
}
nav ul {
list-style: none;
padding: 0 2rem;
}
li {
text-transform: uppercase;
font-family: 'Open Sans', sans-serif;
width: 20%;
text-align: center;
}
li a {
display: block;
height: 3rem;
line-height: 3rem;
width: 100%;
text-decoration: none;
color: #02006d;
}
li:hover {
background-color: #ffffff;
}
.caret {
font-size: .6rem;
}
#media screen and (max-width:700px) {
#logo {
width: 70%;
display: block;
margin: 0 auto;
}
#award1 {
display: none;
}
#award2 {
display: none;
}
header {
order: 0;
display: block;
}
nav {
order: -1;
}
#toggle {
display: block;
}
#toggle img {
width: 100%;
}
nav ul {
display: none;
}
nav ul.show {
display: flex;
flex-direction: column;
align-items: center;
}
nav ul li {
width: 100vw;
}
}
<div id="wrapper">
<header>
<img id="logo" src="http://www.capcar.com.au/wp/wp-content/uploads/2015/05/LOGO-FOR-WEB23.png" />
<div class="awards">
<img id="award1" src="http://www.capcar.com.au/wp/wp-content/uploads/2015/05/HIA_Logo1.png" alt="" />
<img id="award2" src="http://www.capcar.com.au/wp/wp-content/uploads/2015/05/MBA_Logo.png" alt="" />
</div>
</header>
<nav>
<button id="toggle"><img src="http://www.tax-consulting-group-san-diego.com/images/taxes-menu-icon.png" alt="" /></button>
<ul id="menu">
<li>Home</li>
<li>About Us</li>
<li>Projects <span class="caret">▼</span></li>
<li>Testimonials</li>
<li>Contact Us</li>
</ul>
</nav>
</div>
A simple clean demo, and a demo with your site (codepen doesn't like your site, so the header looks weird, but I can't get all the css to load properly—hence the simple demo).
For more info on flexbox here's a snippet from css-tricks and the support tables.
Hope this helps!
Try using the flex property to build your menus. I have great success with this feature and it allows for a number of simple order methods via CSS and media queries. Try CSS-Tricks.com as a reference