Basically I wanted to create a media query which would make my nav bar disappear and change into a menu icon... however it still won't show. Does anyone have a solution? Here's my code:
<header>
<div id="logo"></div>
<nav>
<ul>
<li>Home</li>
<li>Latest Videos</li>
<li>About Me</li>
</ul>
</nav>
</header>
my CSS:
nav {
float: right;
padding: 35px 20px 20px 0px;
}
#menu-icon{
display: hidden;
width: 40px;
height: 40px;
background-image: url(http://www.w3newbie.com/wp-content/uploads/icon.png) center;
}
a:hover#menu-icon {
border-radius: 4px 4px 0 0;
}
ul {
list-style: none;
}
nav ul li {
font-size: 25px;
display: inline-block;
float: left;
padding: 20px;
}
.current {
color: #fff;
text-decoration: underline;
}
and finally my media query:
#media screen and (max-width: 478) {
body {
position: absolute;
}
}
#media screen and (max-width: 478) {
header {
position: absolute;
}
#menu-icon {
display: inline-block;
}
nav ul, nav:active ul {
display: none;
position: absolute;
padding: 20px;
background-color: #405580;
border: 1px solid #fff;
right: 20px;
top: 60px;
width: 50%;
border-radius: 2px 0 2px 2px;
}
nav li {
text-align: center;
width:: 100%;
padding: 10px 0;
margin: 0;
}
nav:hover ul {
display: block;
}
}
According to the docs, looks like you need to add the px after 478:
#media screen and (max-width: 478px) {
body {
position: absolute;
}
}
#media screen and (max-width: 478px) {
First off display: hidden; isnt a thing. You are looking for display:none.
Additionally you can use visibility: hidden; or visibility: visible;
If an object is set to display:none. And then you want to reveal it again at a specific screen width. You need to change the display value of that object in your media query.
display:block; or display:inline-block; or even in some cases display:inline;
For example:
#menu-icon{
display: none;
width: 40px;
height: 40px;
background-image: url(http://www.w3newbie.com/wp-content/uploads/icon.png) center;
}
#media screen and (max-width: 478px) {
#menu-icon{
display: block;
}
}
Related
I'm following this guide for a responsive navigation bar that works for mobile and for web
https://www.youtube.com/watch?v=D-h8L5hgW-w&t=7225s
Sorry in advance if its a very easy solution.
Im struggling to center my navigation bar. Right now its placed left side.
<body>
<!-- Navigation bar-->
<div class="navbar">
<img id="mobile-cta" class="mobile-menu" src="/images/open.svg" alt="Open Navigation">
<nav>
<img id="mobile-exit" class="mobile-menu-exit" src="../images/close.svg" alt="Close Navigation">
<ul class="primary-nav">
<li class="current">Forside</> </li>
<li>Portfolio</li>
<li>Kontakt</li>
</ul>
</nav>
</div>
<body>
That looks like this
Picture of navbar
Picture of mobile version, this one is totally fine
And this is my whole CSS for the navbar.
/* Code for Navigation bar */
.navbar {
background: white;
padding: 1em;
position: sticky;
top: 0px;
width: 100%;
/* Hides the navigation menu when on mobile */
nav {
display: none;
}
.mobile-menu {
cursor: pointer;
}
/* Defines distance from logo and if the list should have dots etc. */
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
}
nav.menu-btn {
display: block;
}
/* Mobile menu navigation */
nav {
position: fixed;
z-index: 999;
width: 66%;
right: 0;
top: 0;
background: black;
height: 100vh;
padding: 1em;
ul.primary-nav {
margin-top: 5em;
}
li {
// Mobile menu text
a {
color: white;
text-decoration: none;
display: block;
padding: .5em;
font-size: 1.5em;
text-align: right;
&:hover {
color: #1e73be;
}
}
}
}
a {
display: block;
position: relative;
padding: 0em 0;
}
// Controls the text after animation
a::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 0.2em;
background-color: #1e73be;
opacity: 0;
transition: opacity 300ms, transform 300ms;
}
li a::after {
opacity: 1;
transform: scale(0);
transform-origin: center;
}
li a:hover::after,
li a:focus::after {
transform:scale(1);
font-weight: bold;
}
.mobile-menu-exit {
float: right;
margin: .5em;
cursor: pointer;
}
// Controls when to switch from mobile to website view.
#media only screen and (min-width: 768px) {
.mobile-menu,
.mobile-menu-exit {
display: none;
}
.navbar nav {
display: flex;
justify-content: space-between;
background: none;
position: unset;
height: auto;
width: 100%;
padding: 0;
ul {
display: flex;
text-align: center;
}
a {
color: black;
font-size: 1em;
padding: .1em 1em;
}
ul.primary-nav {
margin: 0;
}
// CSS for the current tab in navbar for web
li.current a {
color: #1e73be;
}
li a {
color: black;
border: 3px;
font-weight: bold;
border-radius: 5em;
margin-top: 0;
&:hover {
color: #1e73be;
}
}
}
}
On your min-width media query on .navbar nav use justify-content: center; instead of space-between so that your ul is centered. Then set another media query instructing the same width but max-width and then change it back to justify-content: space-between; to position your logo.
I've added a fiddle since you are using SCSS.
I am trying to create a dynamic nav bar. By default, I want the nav bar to display vertically in a div which spans the entire height of the page, but when resized, I want the bar to become horizontal. Here's a picture of what I am trying to achieve:
I have tried to mould my approach based off W3Schools suggestions, by adding display:block; to my ul but it doesn't change anything. My understanding of media queries isn't the best, but I have also tried to alter the width and height of the left div (dark grey):
#media screen and (max-width: 200px) {
.nav-container{
width: 100%;
height: 200px;
background-color: #333;
border-bottom: 0.5px solid #333;
}
}
What is the best approach to achieve this?
Edit:
html,
body {
height: 100%;
background-color: #fff;
}
body {
background-color: #fff;
text-align: center;
margin: 0;
font-family: "Helvetica Neue", Arial, sans-serif;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
display: block;
}
.site-wrapper {
display: table;
width: 100%;
height: 100%;
min-height: 100%;
-webkit-box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5);
box-shadow: inset 0 0 5rem rgba(0, 0, 0, .5);
}
* {
margin: 0;
padding: 0;
}
.site-wrapper {
display: flex;
flex-wrap: wrap;
}
.nav-container {
height: 100%;
width: 200px;
background-color: #333;
display: flex;
justify-content: center;
align-items: center;
}
.logo-holder {
position: absolute;
top: 0;
text-align: center;
}
.logo-holder img {
height: auto;
}
#navigation-div {
margin-top: -300px;
width: 100%;
}
.nav-ul li a {
display: block;
}
.nav-link {
width: 100%;
display: block;
text-align: left;
color: #fff;
text-decoration: none;
margin-left: 0px;
padding-left: 15px;
}
.nav-link:hover {
background-color: #fff;
color: #333;
}
.nav ul {
width: 100%;
padding-left: 0px;
}
.nav ul li {
list-style-type: none;
width: 100%;
}
.nav ul li a {
text-align: left;
padding: 5px;
color: #fff;
text-decoration: none;
margin-left: 15px;
}
.nav li:hover {
background-color: #fff;
}
.nav li:hover a {
color: #333;
}
#media screen and (max-width: 540px) {
.nav-container {
width: 100%;
height: 160px;
background-color: #333;
border-bottom: 0.5px solid #333;
}
.nav-container nav,
.nav-container nav ul,
.nav-container nav ul li,
.logo-holder {
display: inline;
}
.logo-holder {
position: absolute;
left: 0;
}
#navigation-div {
float: left;
}
}
<div class="site-wrapper">
<div class="nav-container">
<div class="logo-holder">
<img src="#" alt="Logo" />
</div>
<div id="navigation-div">
<nav class="nav left-nav-bar">
<ul class="nav-ul">
<a class="nav-link active" href="">
<li>Home</li>
</a>
<a class="nav-link" href="">
<li>Blog</li>
</a>
<a class="nav-link" href="">
<li>Store</li>
</a>
<a class="nav-link" href="">
<li>Contact</li>
</a>
</ul>
</nav>
</div>
</div>
</div>
Firstly, your media query might not be called at all because the max width is pretty small at 200px.
Inside your media query you'll need to set the display property of the navigation elements and the logo to inline, so that they display side-by-side. (You could also consider using flexbox for this) You'll also need to reset some of the properties that are defined higher up, like the line-height of the h3 logo element.
#media screen and (max-width: 480px) {
.nav-container{
width: 100%;
height: auto;
background-color: #333;
border-bottom: 0.5px solid #333;
}
.nav-container nav,
.nav-container nav ul,
.nav-container nav ul li,
.logo-holder,
.logo-holder h3 {
display: inline;
}
.logo-holder h3 {
line-height: 1;
}
}
I was attempting to build a responsive nav using flexbox. When the screen is smaller than 744px, I wanted a toggle button to appear, the main nav to have a max-height of 0, and then on click, have the nav display in block. Fairly typical stuff.
However, I'm used to doing this just with floats and I'm running into several problems:
I don't understand how to drop the UL below the nav without pushing the nav logo and toggle up;
The UL with the LI doesn't seem to be responding to the max-height trick.
If anyone can provide some assistance or point me in the direction of tutorial that would be great.
* {
margin: 0;
padding: 0;
}
body {
font-family: 'open-sans', 'sans-serif';
font-size: 17px;
color: #444;
}
.navText {
font-size: 14px;
}
nav {
height: 100%;
width: 100%;
background-color: white;
}
.nav-fixedWidth {
//border: 1px solid;
min-height: 120px;
width: 960px;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
}
.mainNav {
list-style: none;
display: flex;
}
.mainNav li {
margin-right: 60px;
padding: 10px;
//border: 1px solid;
}
.mainNav li:nth-child(5){
margin-right: 10px;
}
.mainNav li a {
text-decoration: none;
color: #444;
display: block;
}
.mainNav li a:hover {
color: #9d9d9d;
}
.logo {
height: 60px;
width: 60px;
background-color: #ccc;
}
.toggle {
height: 60px;
width: 60px;
background-color: #ccc;
display: none;
}
#media screen and (max-width: 960px) {
.nav-fixedWidth
{
width: 95vw;
}
}
#media screen and (max-width: 744px) {
.nav-fixedWidth
{
flex-wrap:wrap;
}
.toggle
{
display: block;
}
}
<nav>
<div class="nav-fixedWidth">
<div class="logo"></div>
<div class="toggle"></div>
<ul class="mainNav">
<li class="navText">Webinars</li>
<li class="navText">e-Books</li>
<li class="navText">Blog</li>
<li class="navText">e-Course</li>
<li class="navText">Search</li>
</ul>
</div>
</nav>
I know this might be a bit late for your particular need, but you might want to take a look at this solution by Chris Coiyer
https://codepen.io/chriscoyier/pen/GJRXYE
html {
background: #666;
}
body {
width: 60%;
margin: 0 auto;
background: white;
}
.nav {
position: relative;
ul {
display: flex;
height: 3rem;
overflow: hidden;
flex-wrap: wrap;
list-style: none;
padding: 0;
width: 80%;
}
li {
a {
display: block;
padding: 1rem 0.5rem;
text-decoration: none;
white-space: nowrap;
}
}
&.open {
ul {
height: auto;
display: block;
}
}
}
.x {
position: absolute;
top: 0.75rem;
right: 0.75rem;
cursor: pointer;
}
This solution does require a small amount of JavaScript to toggle the menu.
Hope it helps :-)
I want my .logo to be in the middle for the small screens, and on the left for all of the other screens. The .logo stays on the left until the screen gets big and then moves a little to the right, but I would like it to stay into the middle until the screen gets to the breakpoint for bigger screens. I don't know a whole lot about flexbox, but I was hoping that y'all would be able to tell me how to do this!
Here is my HTML:
<!DOCTYPE html>
<html>
<head>
<title>Mad Men Software | Sometimes Crazy is Just What We Need</title>
<link rel="stylesheet" href="/main.css">
<link rel="stylesheet" href="css/normalize.min.css">
</head>
<body>
<header class="header">
<h1 class="logo">Logo</h1>
<ul class="nav">
<li>Projects</li>
<li>About</li>
<li>Contact</li>
</ul>
</header>
</body>
</html>
and here is my css:
/******************************
Flexbox Layout
*******************************/
.header, .nav {
display: flex;
flex-direction: column;
}
.header {
justify-content: space-between;
}
.nav {
flex: 1;
justify-content: space-around;
}
#media all and (min-width: 670px) {
.header, .nav {
flex-direction: row;
}
}
#media all and (min-width: 1030px) {
.nav {
flex: none;
}
}
/******************************
Additional Styling
*******************************/
body {
margin: 0;
font-family: Helvetica;
background: #5fcf80;
}
.header {
padding: 10px 0;
margin: 0 auto;
}
.logo {
background: url('/HTML/MadMenTransparent/MadMenOfficialLogo.png') center center no-repeat;
width: 150px;
min-height: 70px;
background-size: contain;
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
}
.nav {
list-style: none;
}
.nav li {
margin: 12px 0 12px 28px;
}
.nav li a {
text-decoration: none;
color: #fff;
font-size: 12px;
text-transform: uppercase;
}
.nav li a:hover {
color: rgba(255,255,255,0.7);
}
.nav li:last-child a {
background: rgba(255,255,255,0.3);
border-radius: 2px;
transition: 200ms ease-in-out;
padding: 8px 16px 7px;
}
.nav li:last-child a:hover {
background: rgba(255,255,255,0.5);
color: #fff;
}
#media all and (min-width: 1030px) {
.header {
width: 1030px;
min-width: 768px;
}
}
/*************************
Clearfix
*************************/
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
You can add a breakpoint at ur css like this:
#media only screen and (min-device-width: 700px) and (max-device-width: 1024px) {
header {
position: relative;
}
header ul.nav {
margin-top: 150px;
}
.logo {
position: absolute;
left: 50%;
margin-left: -75px;
}
}
I'm not sure how else to describe this: When a scroll bar appears and I scroll to the right due to overflow, my nav tag is resized to a shorter field. In any case, pictures are better:
In the second image, it is obvious that the field has shortened in length. I'm not sure why this is happening. Here's my code:
<link rel="stylesheet" type="text/css" href="../CSS/Style2.css"/>
<body>
<script type="text/javascript" src="../JS/jquery.js"></script>
<script type="text/javascript" src="../JS/respond.min.js"></script>
<script src="../JS/jquery.min.js"></script>
<script type="text/javascript" src="../JS/menu_dropdown.js"></script>
<nav class="clearfix">
<ul class="clearfix">
<li>Home</li>
<li>Territory</li>
<li>Publishers</li>
<li>Special Campaign</li>
<li>Management</li>
<li>About</li>
</ul>
Menu
The CSS:
#charset "utf-8";
/* CSS Document */
html {
font-size: 100%;
-webkit-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
height: 100%;
background: url(../Images/bg.jpg)
}
body {
height: 100%;
overflow-y: hidden;
padding: 0;
margin: 0;
}
nav {
height: 40px;
width: 100%;
background: #455868;
font-size: 11pt;
font-family: 'PT Sans', Arial, sans-serif;
font-weight: bold;
position: relative;
border-bottom: 2px solid #283744;
}
nav ul {
padding: 0;
margin: 0 auto;
width: 900px;
height: 40px;
}
nav li {
display: inline;
float: left;
}
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
nav a {
color: #fff;
display: inline-block;
width: 150px;
text-align: center;
text-decoration: none;
line-height: 40px;
text-shadow: 1px 1px 0px #283744;
}
nav li a {
border-right: 1px solid #576979;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
nav li:last-child a {
border-right: 0;
}
nav a:hover, nav a:active {
background-color: #8c99a4;
}
nav a#pull {
display: none;
}
#media screen and (max-width: 600px) {
nav {
height: auto;
}
nav ul {
width: 100%;
display: block;
height: auto;
}
nav li {
width: 50%;
float: left;
position: relative;
}
nav li a {
border-bottom: 1px solid #576979;
border-right: 1px solid #576979;
}
nav a {
text-align: left;
width: 100%;
text-indent: 25px;
}
}
#media only screen and (max-width : 480px) {
nav {
border-bottom: 0;
}
nav ul {
display: none;
height: auto;
}
nav a#pull {
display: block;
background-color: #283744;
width: 100%;
position: relative;
}
nav a#pull:after {
content:"";
background: url(../Images/nav-icon.png) no-repeat;
width: 30px;
height: 30px;
display: inline-block;
position: absolute;
right: 15px;
top: 10px;
}
}
#media only screen and (max-width : 320px) {
nav li {
display: block;
float: none;
width: 100%;
}
nav li a {
border-bottom: 1px solid #576979;
}
}
Any insight is appreciated.
Your nav width is 100%! it's only going to be 100% width of the body. If you scroll to the right, your nav doesn't get any bigger. The text is just overflowing out of it. I'd suggest setting the backgrounds of each nav item to a solid color.
EDIT: an odd fix: Change your width: 100% from the nav to min-width: 100%. That (and the fact that nav is a block element) is what is limiting the width. In order to make sure it flows to the right, put display: inline-block in its place.
yeah this is an annoying.
the problem with setting a width to 100% is that zooming or different screen sizes can render it differently depending on other css rules.
you should set a min-width to whatever you expect the usual width to be.
for me a min-width:960px usually does the trick but it looks like your using a 900px value.
so add min-width:900px to your nav {} style rules. or whatever width youre using.
nav {
height: 40px;
width: 100%;
background: #455868;
font-size: 11pt;
font-family: 'PT Sans', Arial, sans-serif;
font-weight: bold;
position: relative;
border-bottom: 2px solid #283744;
min-width:900px;
}
Find your nav a css style and replace this css and done.
DEMO http://jsfiddle.net/bC63a/
NEW
nav a {
color: #fff;
background: #455868;
border-bottom: 2px solid #283744;
display: inline-block;
width: 150px;
text-align: center;
text-decoration: none;
line-height: 40px;
text-shadow: 1px 1px 0px #283744;
}
OLD
nav a {
color: #fff;
display: inline-block;
width: 150px;
text-align: center;
text-decoration: none;
line-height: 40px;
text-shadow: 1px 1px 0px #283744;
}