How can I make my drop-down menu more mobile-friendly? - html
I am using the the following HTML and CSS code for devices with a max-width of 480px.
How can I:
Add the word "Menu" before the links?
Display only "Menu"?
Click on "Menu" to reveal links and then sub-links?
Make "Menu", links and sublinks 100% of device width?
Please note, my knowledge of web design is limited to basic HTML and CSS. And this is my first go at designing a website.
Any help or advice would be greatly appreciated.
#menu {
position: relative;
top: 0;
margin: 0;
padding: 0;
width: 100%;
height: 3rem;
font-size: 0.8rem;
z-index: 999;
}
#menu ul {
position: absolute;
margin: 0;
padding: 0;
height: 100%;
width: 100%;
list-style-type: none;
background-color: #0000ff;
color: #ffffff;
}
#menu ul > li {
display: block;
float: left;
height: auto;
width: 20%;
overflow: hidden;
}
#menu li > a {
display: block;
height: 100%;
width: 100%;
line-height: 3rem;
text-align: center;
text-decoration: none;
background-color: #0000ff;
color: #ffffff;
}
#menu li > span {
display: block;
height: 100%;
width: 100%;
line-height: 3rem;
text-align: center;
text-decoration: none;
background-color: #0000ff;
color: #ffffff;
}
#menu ul ul {
display: none;
position: static;
height: auto;
width: 100%;
margin: 0;
}
#menu ul ul li {
position: relative;
float: none;
display: block;
height: 100%;
width: 100%;
}
#menu ul ul li > a {
height: 100%;
width: 100%;
text-align: center;
}
#menu li:hover > a {
color: #0000ff;
background-color: #ffffff;
}
#menu li:hover > span {
color: #0000ff;
background-color: #ffffff;
}
#menu li:hover > ul {
display: block;
}
<nav id="menu">
<ul>
<li>HOME</li>
<li><span>ABOUT</span>
<ul>
<li>History</li>
<li>Champions</li>
<li>Presidents</li>
<li>Committee</li>
<li>Rules</li>
</ul>
</li>
<li><span>BOOK</span>
<ul>
<li>Functions</li>
<li>Suite</li>
<li>Lounge</li>
<li>Bar</li>
</ul>
</li>
<li><span>COMPS</span>
<ul>
<li>Fixtures</li>
<li>Gents</li>
<li>Ladies</li>
<li>Winners</li>
</ul>
</li>
<li><span>CONTACT</span>
<ul>
<li>Details</li>
<li>Join</li>
<li>Map</li>
</ul>
</li>
</ul>
</nav>
Please check following example
/********** Start RESET **********
AUTHOR: Osvaldas Valutis, www.osvaldas.info
*/
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
*behavior: url( '/theme/js/boxsizing.htc' );
-webkit-tap-highlight-color: rgba( 0, 0, 0, 0 );
-webkit-overflow-scrolling: touch;
}
html, body, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, font, img, ins, kbd, q, s, samp, small, strike, strong, sup, sub, tt, var, b, u, i, center, ul, ol, li, dl, dt, dd, table, tr, th, td, thead, tbody, tfoot, caption, form, fieldset, legend, input, textarea, select, label, applet, object, iframe, audio, video, canvas, article, aside, canvas, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary {
font-size: 100%;
font-family: inherit;
font-weight: inherit;
font-style: inherit;
line-height: inherit;
vertical-align: baseline;
border: 0;
outline: 0;
padding: 0;
margin: 0;
}
html {
font-size: 100%;
-webkit-text-size-adjust: 100%;
-moz-text-size-adjust: 100%;
-ms-text-size-adjust: 100%;
-o-text-size-adjust: 100%;
text-size-adjust: 100%;
}
body {
font-family: Helvetica, Arial, sans-serif;
line-height: 1;
color: #000;
background-color: #fff;
}
article, aside, canvas, details, figcaption, figure, footer, header, hgroup, menu, nav, section, summary {
display: block;
}
audio, canvas, video {
display: inline-block;
*display: inline;
*zoom: 1;
}
audio:not([controls]) {
display: none;
}
canvas {
-ms-touch-action: double-tap-zoom;
}
[hidden] {
display: none;
}
a {
text-decoration: underline;
color: #000;
}
a:hover {
text-decoration: none;
}
small {
font-size: 75%;
}
big {
font-size: 125%;
}
em {
font-style: italic;
}
strong {
font-weight: bold;
}
ins {
text-decoration: none;
}
del {
text-decoration: line-through;
}
sup, sub {
font-size: 75%;
line-height: 0;
}
sup {
vertical-align: super;
}
sub {
vertical-align: sub;
}
pre {
display: block;
overflow: auto;
}
code {
font-family: 'Courier New', Courier, monospace;
}
ul, ol {
list-style: none;
}
table {
table-layout: auto;
border-collapse: separate;
border-spacing: 0;
empty-cells: hide;
}
img {
vertical-align: middle;
-ms-interpolation-mode: bicubic;
}
svg:not(:root) {
overflow: hidden;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after, q:before, q:after {
content: '';
content: none;
}
input, textarea {
-webkit-appearance: none;
}
button, input, select, textarea {
vertical-align: baseline;
*vertical-align: middle;
-webkit-border-radius: 0;
}
button, input {
line-height: normal;
*overflow: visible;
}
table button, table input {
*overflow: auto;
}
button, input[type='button'], input[type='reset'], input[type='submit'] {
cursor: pointer;
-webkit-appearance: button;
}
textarea {
overflow: auto;
vertical-align: top;
}
input[type='search']::-webkit-search-decoration, input[type='search']::-webkit-search-cancel-button, input[type='search']::-webkit-search-results-button, input[type='search']::-webkit-search-results-decoration {
display: none;
}
input[type='search'] {
-webkit-appearance: textfield;
}
input[type='radio'] {
-webkit-appearance: radio;
}
input[type='checkbox'] {
-webkit-appearance: checkbox;
}
input::-ms-clear {
display: none;
}
input[type='password']::-ms-reveal {
display: none;
}
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
* html .clearfix {
zoom: 1;
}
*:first-child+html .clearfix {
zoom: 1;
}
.cleaner {
height: 0;
line-height: 0;
clear: both;
}
/********** End RESET **********/
html {
font-size: 100%;
}
body {
font-family: 'Noticia Text', serif;
font-weight: 400;
-webkit-font-smoothing: antialiased;
background-color: #f7efeb;
padding: 1.25em; /* 20 */
}
body, a, a:hover {
color: #111;
}
a {
-webkit-transition: color .3s ease;
-moz-transition: color .3s ease;
-ms-transition: color .3s ease;
-o-transition: color .3s ease;
transition: color .3s ease;
}
a, a:hover {
text-decoration: none;
}
#nav {
width: 60em; /* 1000 */
font-family: 'Open Sans', sans-serif;
font-weight: 400;
position: absolute;
top: 25%;
left: 50%;
margin-left: -30em; /* 30 480 */
}
#nav > a {
display: none;
}
#nav li {
position: relative;
}
#nav li a {
color: #fff;
display: block;
}
#nav li a:active {
background-color: #c00 !important;
}
#nav span:after {
width: 0;
height: 0;
border: 0.313em solid transparent; /* 5 */
border-bottom: none;
border-top-color: #efa585;
content: '';
vertical-align: middle;
display: inline-block;
position: relative;
right: -0.313em; /* 5 */
}
/* first level */
#nav > ul {
height: 3.75em; /* 60 */
background-color: #e15a1f;
}
#nav > ul > li {
width: 25%;
height: 100%;
float: left;
}
#nav > ul > li > a {
height: 100%;
font-size: 1.5em; /* 24 */
line-height: 2.5em; /* 60 (24) */
text-align: center;
}
#nav > ul > li:not( :last-child ) > a {
border-right: 1px solid #cc470d;
}
#nav > ul > li:hover > a, #nav > ul:not( :hover ) > li.active > a {
background-color: #cc470d;
}
/* second level */
#nav li ul {
background-color: #cc470d;
display: none;
position: absolute;
top: 100%;
}
#nav li:hover ul {
display: block;
left: 0;
right: 0;
}
#nav li:not( :first-child ):hover ul {
left: -1px;
}
#nav li ul a {
font-size: 1.25em; /* 20 */
border-top: 1px solid #e15a1f;
padding: 0.75em; /* 15 (20) */
}
#nav li ul li a:hover, #nav li ul:not( :hover ) li.active a {
background-color: #e15a1f;
}
#media only screen and ( max-width: 62.5em ) /* 1000 */ {
#nav {
width: 100%;
position: static;
margin: 0;
}
}
#media only screen and ( max-width: 40em ) /* 640 */ {
html {
font-size: 75%; /* 12 */
}
#nav {
position: relative;
top: auto;
left: auto;
}
#nav > a {
width: 3.125em; /* 50 */
height: 3.125em; /* 50 */
text-align: left;
text-indent: -9999px;
background-color: #e15a1f;
position: relative;
}
#nav > a:before, #nav > a:after {
position: absolute;
border: 2px solid #fff;
top: 35%;
left: 25%;
right: 25%;
content: '';
}
#nav > a:after {
top: 60%;
}
#nav:not( :target ) > a:first-of-type, #nav:target > a:last-of-type {
display: block;
}
/* first level */
#nav > ul {
height: auto;
display: none;
position: absolute;
left: 0;
right: 0;
}
#nav:target > ul {
display: block;
}
#nav > ul > li {
width: 100%;
float: none;
}
#nav > ul > li > a {
height: auto;
text-align: left;
padding: 0 0.833em; /* 20 (24) */
}
#nav > ul > li:not( :last-child ) > a {
border-right: none;
border-bottom: 1px solid #cc470d;
}
/* second level */
#nav li ul {
position: static;
padding: 1.25em; /* 20 */
padding-top: 0;
}
}
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<nav id="nav" role="navigation"> Show navigation Hide navigation
<ul class="clearfix">
<li>Home</li>
<li> <span>Blog</span>
<ul>
<li>Design</li>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
</li>
<li> <span>Work</span>
<ul>
<li>Web Design</li>
<li>Typography</li>
<li>Front-End</li>
</ul>
</li>
<li>About</li>
</ul>
</nav>
Related
How to align navbar to center
I want to know how to align navbar contents to center , here is the code that i am using . Actually i don't know what is the problem with the code any help would be greatly appreciated. Fiddle .tg-navigationarea { width: 100%; float: left; position: relative; background: #000; } .tg-nav { z-index: 2; float: left; text-transform: uppercase; font: 400 13px/40px 'Open Sans', Arial, Helvetica, sans-serif; } .tg-navigation { width: 100%; float: left; padding: 0; line-height: inherit; } .tg-navigation ul { margin: 0; list-style: none; line-height: inherit; } .tg-navigation>ul { width: 100%; float: left; } .tg-navigation ul li { line-height: inherit; list-style-type: none; }
apply this css rule to elemnts .tg-nav { z-index: 2; float: left; width: 100%; text-align: center; font: 400 13px/40px 'Open Sans', Arial, Helvetica, sans-serif; } .tg-navigation > ul { width: auto; float: none; display: inline-block; margin: 0 auto; }
One of the ways to do this is using flexbox. Adding display: flex; justify-content: center; Will center your navbar, if you remove the width: 100% and the float: left. Updated Fiddle: https://jsfiddle.net/103kbsh6/5/
Give display:inline-block to li and text-align:center; padding:0; to ul tag. .tg-navigationarea { width: 100%; float: left; position: relative; background: #f7f7f7; } .tg-nav { z-index: 2; float: left; text-transform: uppercase; font: 400 13px/40px 'Open Sans', Arial, Helvetica, sans-serif; } .tg-navigation { width: 100%; float: left; padding: 0; line-height: inherit; } .tg-navigation ul { margin: 0; list-style: none; line-height: inherit; } .tg-navigation > ul { width: 100%; float: left; text-align:center; padding:0; } .tg-navigation ul li { line-height: inherit; list-style-type: none; } li.menu-item-has-children { position: relative; } li.menu-item-has-mega-menu { position: static; } li.menu-item-has-children > a:before, li.menu-item-has-mega-menu > a:before { top: 0; right: 10px; content: '\f107'; position: absolute; font-size: inherit; line-height: inherit; font-family: 'FontAwesome'; } .sub-menu li.menu-item-has-children > a:after { top: 0; right: 10px; content: '\f105'; position: absolute; font-size: inherit; line-height: inherit; font-family: 'FontAwesome'; } .tg-navigation ul li .sub-menu li.current-menu-item > a { color: #333; } .tg-navigation ul li .sub-menu li.current-menu-item > a:before { height: 100%; } .tg-navigation > ul > li { display: inline-block; } .tg-navigation ul li a { color: #666; display: block; padding: 0 25px; position: relative; line-height: inherit; } .tg-navigation > ul > li > a { color: #333; z-index: 2; } .tg-navigation > ul > li:last-child > .sub-menu { right: 0; left: auto; } .tg-navigation > ul > li:last-child > .sub-menu .sub-menu { right: 100%; left: auto; } <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <nav id="tg-nav" class="tg-nav"> <div id="tg-navigation" class="navbar-collapse tg-navigation"> <ul> <li> Home </li> <li> About </li> <li> Packages </li> <li> Destination </li> <li> Gallery </li> <li> Blog </li> <li> Contact us </li> </ul> </div> </nav>
.tg-navigationarea { width: 100%; float: left; position: relative; background: #000; } .tg-nav { z-index: 2; float: left; text-transform: uppercase; font: 400 13px/40px 'Open Sans', Arial, Helvetica, sans-serif; } .tg-navigation { width: 100%; float: left; padding: 0; line-height: inherit; } .tg-navigation ul { margin: 0; list-style: none; line-height: inherit; } .tg-navigation>ul { width: 100%; float: left; Text-align:center } .tg-navigation ul li { line-height: inherit; list-style-type: none; float:none; display:inline-block }
To center this list, all you need to add is: .tg-nav { width: 100%: } .tg-navigation { width: 100%; } .tg-navigation ul { display: inline-block; } Besides that, what's up with all the float: left? If you want elements to center, using float: left is what I would avoid, except of course for the list-items. I updated your FIDDLE
Drop-down-menu looks strange (pure css)
Having trouble with how the drop-down-menu looks like when the mouse hovers over it. Note that the problem only started when I converted my nav li's from fixed to fluid. (instead of specifying it in px I specify it in %) Here is my code: * { margin: 0; padding: 0; } body { font-family: arial, Helvetica, sans-serif; font-size: 100%; width: 99%; max-width: 800px; margin: 0 auto; background-color: rgba(255, 248, 228, 0.95); border: 2px solid #585858; } a:link { color: #e07400; } a:visited { color: gray; } a:hover, a:focus { font-style: italic; } header img { float: left; width: 100%; max-width: 136.078px; } .orange_header { color: #e07400; } header h1 { margin: 0; font-size: 200%; text-shadow: 2px 1px 1px black; /*text-align: center;*/ padding-left: 7em; padding-top: 0.7em; padding-bottom: 0.5em; } /*====================NAV MENU========================*/ #nav_menu a { margin: 0; } #nav_menu a.current { color: white; display: block; background-color: #a78349; } #nav_menu ul { list-style-type: none; margin: 0; padding: 0; position: relative; height: 34px; } #nav_menu ul li { float: left; /*width:165.985px;*/ /*(800-136.06)/4 - fixed layout - uncomment to see that the dropdown menu at the lessons tab looks fine*/ width: 20.748125%; /* 165.985/800*100 - here the dropdown menu looks strange...how to I fix this?*/ } #nav_menu ul li a { display: block; text-align: center; text-decoration: none; padding: 0.5em 0; margin: 0; background-color: #ab6b06; color: white; } #nav_menu ul ul { display: none; position: absolute; top: 100%; } #nav_menu ul ul li { float: none; } #nav_menu ul li:hover>ul { display: block } #nav_menu>ul::after { content: ""; clear: both; display: block; } <body> <header> <img src="images/guitarlogo2.png" alt="Guitar" height="109.93"> <h1 class="orange_header">Annemie's Guitar School</h1> </header> <nav id="nav_menu"> <ul> <li>Home</li> <li>Useful links</li> <li>Lessons <ul> <li>Sitting position</li> <li>Reading music</li> <li>Right Hand Technique</li> <li>Left Hand Technique</li> <li>Practising and Memorization</li> </ul> </li> <li>Bio</li> </ul> </nav> </body>
Change this css: #nav_menu ul li { float: left; /* width: 165.985px; */ width: 20.748125%; } to this #nav_menu > ul > li { float: left; /* width: 165.985px; */ width: 20.748125%; }
Making menu vertically centred with logo image
I'm stuck trying to align the logo in the same height with my menu. Here the html code: /* ====================== MAIN ====================== */ .main-menu { background-color: #ffffff; padding: 16px 5px; margin: 0; display: inline-block; position: relative; width: 100%; } .main-menu:before, .main-menu ul, .main-menu li, .main-menu a { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; margin: 0; padding: 0; outline: none; border: 0; list-style: none; } .main-menu a { cursor: pointer; } .main-menu, .main-menu a, .main-menu a:visited { color: #555555; } .main-menu > li { display: inline-block; vertical-align: middle; float: left; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; } .main-menu > li > a { font-weight: bold; padding: 0 10px; line-height: 39px; } .main-menu > li:hover { background-color: #8B008B; } .main-menu > li:hover > a, .main-menu > li:hover > a:visited { color: #ffffff; } .main-menu li { position: relative; cursor: default; } .main-menu li > a { text-decoration: none; display: block; } .main-menu li > ul{ z-index: 1; } .main-menu .fa { font-size: 12pt; letter-spacing: 8px; line-height: inherit; } .main-menu:after { content: ''; display: block; clear: both; } /* ====================== LOGO ====================== */ .main-menu > li.mm-logo { float: left; margin-left: 0; font-size: 0; display: inline-block; vertical-align: middle; } .main-menu > li.mm-logo a { padding: 0; line-height: 0; } .main-menu > li.mm-logo img { border: none; display: block; padding:0px; height: 118px; width: 118px; } .main-menu > li.mm-logo:hover { background: none; } /* ====================== RESPONSIVE ====================== */ #media screen and (max-width: 768px) { /* ====================== MAIN ====================== */ .main-menu[class*='mm-response'] > li { position: relative; } .main-menu[class*='mm-response'] > li > ul{ left: 0; right: 0; width: auto !important; margin-right: 0; } .main-menu[class*='mm-response'] > li > ul ul { top: 100%; margin-left: 39px !important; } /* ====================== SWITCH ====================== */ .main-menu.mm-response-switch > li { display: none; float: none; position: relative; width: 100%; } .main-menu.mm-response-switch > li + li { margin-left: 0; margin-top: 10px; } .main-menu.mm-response-switch > li.mm-logo { display: block; } .main-menu.mm-response-switch > li.mm-logo img { position: relative; z-index: 1; } .main-menu.mm-response-switch:before { font-family: FontAwesome; content: '\f0c9'; position: relative; float: right; cursor: pointer; line-height: 39px; height: 39px; padding: 0 14px; z-index: 2; } .main-menu.mm-response-switch:hover:before { opacity: 0; } .main-menu.mm-response-switch:hover > li { display: block; } /* ====================== RESPONSE MARGIN ====================== */ .main-menu.mm-response-margin > li > ul { margin-left: 39px !important; } } <nav> <ul onClick="" class="main-menu mm-response-switch"> <li class="mm-logo"> <img src="logo.png" alt="Accueil"> </li> <li> <i class="fa fa-indent"></i>Lien 1 </li> <li> <i class="fa fa-indent"></i>Lien 2 </li> <li> <i class="fa fa-indent"></i>Lien 2 </li> </ul> </nav> Don't give more importance to the responsive section, just guide me how to make all elements in the same line vertically.
Remove the floats from the li elements and they will align vertically since the li's are inline-block and you have already assigned vertical-align: middle /* ====================== MAIN ====================== */ .main-menu { background-color: #ffffff; padding: 16px 5px; margin: 0; display: inline-block; position: relative; width: 100%; } .main-menu:before, .main-menu ul, .main-menu li, .main-menu a { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; margin: 0; padding: 0; outline: none; border: 0; list-style: none; } .main-menu a { cursor: pointer; } .main-menu, .main-menu a, .main-menu a:visited { color: #555555; } .main-menu > li { display: inline-block; vertical-align: middle; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; } .main-menu > li > a { font-weight: bold; padding: 0 10px; line-height: 39px; } .main-menu > li:hover { background-color: #8B008B; } .main-menu > li:hover > a, .main-menu > li:hover > a:visited { color: #ffffff; } .main-menu li { position: relative; cursor: default; } .main-menu li > a { text-decoration: none; display: block; } .main-menu li > ul{ z-index: 1; } .main-menu .fa { font-size: 12pt; letter-spacing: 8px; line-height: inherit; } .main-menu:after { content: ''; display: block; clear: both; } /* ====================== LOGO ====================== */ .main-menu > li.mm-logo { margin-left: 0; font-size: 0; display: inline-block; vertical-align: middle; } .main-menu > li.mm-logo a { padding: 0; line-height: 0; } .main-menu > li.mm-logo img { border: none; display: block; padding:0px; height: 118px; width: 118px; } .main-menu > li.mm-logo:hover { background: none; } /* ====================== RESPONSIVE ====================== */ #media screen and (max-width: 768px) { /* ====================== MAIN ====================== */ .main-menu[class*='mm-response'] > li { position: relative; } .main-menu[class*='mm-response'] > li > ul{ left: 0; right: 0; width: auto !important; margin-right: 0; } .main-menu[class*='mm-response'] > li > ul ul { top: 100%; margin-left: 39px !important; } /* ====================== SWITCH ====================== */ .main-menu.mm-response-switch > li { display: none; float: none; position: relative; width: 100%; } .main-menu.mm-response-switch > li + li { margin-left: 0; margin-top: 10px; } .main-menu.mm-response-switch > li.mm-logo { display: block; } .main-menu.mm-response-switch > li.mm-logo img { position: relative; z-index: 1; } .main-menu.mm-response-switch:before { font-family: FontAwesome; content: '\f0c9'; position: relative; float: right; cursor: pointer; line-height: 39px; height: 39px; padding: 0 14px; z-index: 2; } .main-menu.mm-response-switch:hover:before { opacity: 0; } .main-menu.mm-response-switch:hover > li { display: block; } /* ====================== RESPONSE MARGIN ====================== */ .main-menu.mm-response-margin > li > ul { margin-left: 39px !important; } } <nav> <ul onClick="" class="main-menu mm-response-switch"> <li class="mm-logo"> <img src="logo.png" alt="Accueil"> </li> <li> <i class="fa fa-indent"></i>Lien 1 </li> <li> <i class="fa fa-indent"></i>Lien 2 </li> <li> <i class="fa fa-indent"></i>Lien 2 </li> </ul> </nav> You can also use flexbox and assign display: flex; align-items: center; to the parent ul /* ====================== MAIN ====================== */ .main-menu { background-color: #ffffff; padding: 16px 5px; margin: 0; position: relative; width: 100%; display: flex; align-items: center; } .main-menu:before, .main-menu ul, .main-menu li, .main-menu a { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; margin: 0; padding: 0; outline: none; border: 0; list-style: none; } .main-menu a { cursor: pointer; } .main-menu, .main-menu a, .main-menu a:visited { color: #555555; } .main-menu > li { display: inline-block; vertical-align: middle; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; } .main-menu > li > a { font-weight: bold; padding: 0 10px; line-height: 39px; } .main-menu > li:hover { background-color: #8B008B; } .main-menu > li:hover > a, .main-menu > li:hover > a:visited { color: #ffffff; } .main-menu li { position: relative; cursor: default; } .main-menu li > a { text-decoration: none; display: block; } .main-menu li > ul{ z-index: 1; } .main-menu .fa { font-size: 12pt; letter-spacing: 8px; line-height: inherit; } .main-menu:after { content: ''; display: block; clear: both; } /* ====================== LOGO ====================== */ .main-menu > li.mm-logo { margin-left: 0; font-size: 0; display: inline-block; vertical-align: middle; } .main-menu > li.mm-logo a { padding: 0; line-height: 0; } .main-menu > li.mm-logo img { border: none; display: block; padding:0px; height: 118px; width: 118px; } .main-menu > li.mm-logo:hover { background: none; } /* ====================== RESPONSIVE ====================== */ #media screen and (max-width: 768px) { /* ====================== MAIN ====================== */ .main-menu[class*='mm-response'] > li { position: relative; } .main-menu[class*='mm-response'] > li > ul{ left: 0; right: 0; width: auto !important; margin-right: 0; } .main-menu[class*='mm-response'] > li > ul ul { top: 100%; margin-left: 39px !important; } /* ====================== SWITCH ====================== */ .main-menu.mm-response-switch > li { display: none; float: none; position: relative; width: 100%; } .main-menu.mm-response-switch > li + li { margin-left: 0; margin-top: 10px; } .main-menu.mm-response-switch > li.mm-logo { display: block; } .main-menu.mm-response-switch > li.mm-logo img { position: relative; z-index: 1; } .main-menu.mm-response-switch:before { font-family: FontAwesome; content: '\f0c9'; position: relative; float: right; cursor: pointer; line-height: 39px; height: 39px; padding: 0 14px; z-index: 2; } .main-menu.mm-response-switch:hover:before { opacity: 0; } .main-menu.mm-response-switch:hover > li { display: block; } /* ====================== RESPONSE MARGIN ====================== */ .main-menu.mm-response-margin > li > ul { margin-left: 39px !important; } } <nav> <ul onClick="" class="main-menu mm-response-switch"> <li class="mm-logo"> <img src="logo.png" alt="Accueil"> </li> <li> <i class="fa fa-indent"></i>Lien 1 </li> <li> <i class="fa fa-indent"></i>Lien 2 </li> <li> <i class="fa fa-indent"></i>Lien 2 </li> </ul> </nav>
There are several ways to do this; One technique is to give the parent position: relative, then absolutely position the logo top: 50% then transformY(-50%) which translates in english to "Position the top of the logo in the parent 1/2 way down, then move the logo up 1/2 of its height. There is a handy flow chart for different techniques to use here. https://css-tricks.com/centering-css-complete-guide/
Try this: .main-menu{ display: flex; align-items: center; } And revert it to display:block when the screen is too small: #media screen and (max-width: 768px) .main-menu{ flex-direction: column; } If you want more info about flexbox, I suggest you this great article: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Responsive navbar problems
I recently wanted to add some media query lines to my website and it was all perfect, until I added a mobile navigation (dropdown menu). After that, my marquee somehow got behind the header (navigation) and after that, I couldn't figure out what's wrong. I don't want to move it down with padding, but rather figure out why that's, because before I added a mobile navigation everything had their specific place to stand and not one thing overlapping another thing. http://i.imgur.com/ciqMkpB.png Here is my html: <body> <div id="container"> <header> <div id="nav"> <nav class="nav-collapse"> <ul> <li><img alt="icon" src="res/img/logo2.png"></li> <li><a rel="alternate" href="#" class="current" class="nav-collapse">Home</a></li> <li>Members</li> <li>Exams</li> <li>Timetable </li> <li>About</li> <li><a class="fck" href="password.html">Private Site</a</li> <li id="title">Class Site</li> </ul> </nav> </div> </header> <div id="content"> <div id="title"> <div class="marquee"> <h3>Welcoke...</h3> </div> </div> <div id="article" text-align="center">Website was made by ŽP, JM and TM! </div> </div> Here is my main CSS Code: body { margin:0px !important; padding:0px; bottom:0px; background:#3EA6FA url('../img/11.png') no-repeat center center fixed; } #media screen and (max-width: 480px) { body { background-size: 100%; background-position:0% 58%; } } #media screen and (max-width: 900px) { body { background-size: 100%; } } body:after { opacity:4; } hr { border:2px solid #DED5CA; } a { color:white; cursor:pointer; text-decoration:none; } #bg { display:block; position:relative; } #bg:before { content:""; position:absolute; z-index:-1; opacity:0.5; top:0; left:0; bottom:0; right:0; } #nav { font-family:"Lobster"; font-size:22px; } #nav #title { color:white; font-weight:bold; margin-top:15px ; margin-right:20px; float:right; font-size:26px; } #nav a { float:left; } #nav img { float:left; height:48px; margin-top:5px; margin-left:10px; margin-right:10px; } #nav ul { list-style-type:none; margin:0; padding:0; overflow:hidden; background-color:#454443; border-bottom:3px solid #757575; } #nav li a { display:block; color:white; text-align:center; padding:14px 16px; text-decoration:none; border-bottom:3px solid #454443; } #nav li a:hover { animation-name:navlink; animation-duration:0.35s; background:#0276F2; border-bottom:3px solid #325AFA; } #nav li a.current { border-bottom:3px solid #325AFA; } #keyframes navlink { from { background-color:#3D362B; } to { background-color:#0276F2; } } #container { font-family:"Oswald", "Bree Serif"; margin:0px; } #content { margin:2%; bottom:0px; position:relative; font-size:16px; border:2px solid white; background:rgba(255, 255, 255, 0.3); text-align:center; } #content #title { margin-left:2% !important; margin-right:2% !important; color:black; font-family:"Lobster"; font-size:32px; text-align:center; border-bottom:2px dashed white; } and my new mobile navigation: body, div, h1, h2, h3, h4, h5, h6, p, blockquote, pre, dl, dt, dd, ol, ul, li, hr, fieldset, form, label, legend, th, td, article, aside, figure, footer, header, hgroup, menu, nav, section, summary, hgroup { margin: 0; padding: 0; border: 0; } a:active, a:hover { outline: 0; } #-webkit-viewport { width: device-width; } #-moz-viewport { width: device-width; } #-ms-viewport { width: device-width; } #-o-viewport { width: device-width; } #viewport { width: device-width; } /* ------------------------------------------ RESPONSIVE NAV STYLES --------------------------------------------- */ .nav-collapse ul { margin: 0; padding: 0; width: 100%; display: block; list-style: none; } .nav-collapse li { width: 100%; display: block; } .js .nav-collapse { clip: rect(0 0 0 0); max-height: 0; position: absolute; display: block; overflow: hidden; zoom: 1; } .nav-collapse.opened { max-height: 9999px; } .disable-pointer-events { pointer-events: none !important; } .nav-toggle { -webkit-tap-highlight-color: rgba(0,0,0,0); -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; } #media screen and (min-width: 40em) { .js .nav-collapse { position: relative; } .js .nav-collapse.closed { max-height: none; } .nav-toggle { display: none; } } /* ------------------------------------------ FIXED HEADER --------------------------------------------- */ /* ------------------------------------------ MASK --------------------------------------------- */ header { background: #454443; position:absolute; z-index: 3; width: 100%; left: 0; top: 0; } .mask { -webkit-transition: opacity 300ms; -moz-transition: opacity 300ms; transition: opacity 300ms; background: rgba(0,0,0, .5); visibility: hidden; position: fixed; opacity: 0; z-index: 2; bottom: 0; right: 0; left: 0; top: 0; } .android .mask { -webkit-transition: none; transition: none; } .js-nav-active .mask { visibility: visible; opacity: 1; } #media screen and (min-width: 40em) { .mask { display: none !important; opacity: 0 !important; } } /* ------------------------------------------ NAVIGATION STYLES --------------------------------------------- */ .nav-collapse, .nav-collapse ul { list-style: none; width: 100%; float: left; } #media screen and (max-width: 480px) { .nav-collapse { float: left; width: auto; } } .nav-collapse li { float: left; width: 100%; } #media screen and (min-width: 40em) { .nav-collapse li { width: auto; } } .nav-collapse a { -webkit-tap-highlight-color: rgba(0,0,0,0); border-top: 1px solid white; text-decoration: none; background: #454443; color: #fff; width: 100%; float: left; } .nav-collapse a:active, .nav-collapse .active a { background: #325AFA; } #media screen and (min-width: 40em) { .nav-collapse a { text-align: center; border-top: 0; float: left; margin: 0; } } .nav-collapse ul ul a { background: #ca3716; padding-left: 2em; } #media screen and (min-width: 40em) { .nav-collapse ul ul a { display: none; } } /* ------------------------------------------ NAV TOGGLE STYLES --------------------------------------------- */ #font-face { font-family: "responsivenav"; src:url("../icons/responsivenav.eot"); src:url("../icons/responsivenav.eot?#iefix") format("embedded-opentype"), url("../icons/responsivenav.ttf") format("truetype"), url("../icons/responsivenav.woff") format("woff"), url("../icons/responsivenav.svg#responsivenav") format("svg"); font-weight: normal; font-style: normal; } .nav-toggle { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-decoration: none; text-indent: -300px; position: relative; overflow: hidden; width: 60px; height: 55px; float: right; } .nav-toggle:before { color: #fff; /* Edit this to change the icon color */ font: normal 28px/55px "responsivenav"; /* Edit font-size (28px) to change the icon size */ text-transform: none; text-align: center; position: absolute; content: "\2261"; /* Hamburger icon */ text-indent: 0; speak: none; width: 100%; left: 0; top: 0; } .nav-toggle.active:before { font-size: 24px; content: "\78"; /* Close icon */ }
Try fixing the navbar to the top using navbar-fixed-top and then give the body a padding-top of 50px.
Standalone Bootstrap header not responsive
Since my site has multiple pages which are being designed by multiple people, we decided to create a standalone header. This is the code. While it was working initially, now after some tweaks, it's suddenly no longer responsive. As we use different CSS files, the CSS for this had to written inline, to avoid a clash with the others. Here is the code, your help in helping me figure out the issue is much appreciated! Header code (HMTL + Inline CSS): --------------- <style> body { background-color: #fff; } #nav { font-family: 'Open Sans', sans-serif; font-weight: 400; position: fixed; top: 0; right: 1%; z-index: 1030; } #nav > a { display: none; } #li-bg { border: 1px solid #fafafa; -webkit-border-radius: 6px; -moz-border-radius: 6px; border-radius: 3px; -webkit-background-clip: padding-box; -moz-background-clip: padding; background-clip: padding-box; } #nav li { position: relative; list-style:none; -webkit-border-radius: 6px; border-color: #333 1px solid; -moz-border-radius: 6px; border-radius: 3px; -webkit-background-clip: padding-box; -moz-background-clip: padding; background-clip: padding-box; background-color: transparent; } #nav li a { display: block; padding: 3px 20px; clear: both; font-weight: normal; line-height: 20px; color: #fff; white-space: nowrap; background-color: #fcfcfc; } #nav li a:active { background-color: #3498db!important; } #nav span:after { width: 0; height: 0; border: 0.313em solid transparent; /* 5 */ border-bottom: none; border-top-color: #fff; content: ''; vertical-align: middle; display: inline-block; position: relative; right: -0.313em; /* 5 */ } /* first level */ #nav > ul { height: 2.75em; /* 60 */ } #nav > ul > li { height: 100%; float: right; width: 130px; background-color: #3498db; } #nav > ul > li > a { height: 100%; font-size: .95em; /* 24 */ line-height: 2.5em; /* 60 (24) */ text-decoration:none; text-align: center; background-color:transparent; } #nav > ul > li:not( :last-child ) > a { border-right: 1px solid transparent; } #nav > ul > li:hover > a, #nav > ul:not( :hover ) > li.active > a { } /* second level */ #nav li ul { display: none; position: absolute; top: 100%; padding-top: 8px; padding-left:40px; } #nav li:hover ul { display: block; left: 0; right: 0; } #nav li:not( :first-child ):hover ul { left: -1px; } #nav li ul a { font-size: 1.25em; /* 20 */ border-top: 1px solid #fcfcfc; background-color:#3498db; color: #fff; font-size:.85em; width:100px; text-decoration: none; } #nav li ul li a:hover, #nav li ul:not( :hover ) li.active a { background-color: #2c81ba; } #media only screen and ( max-width: 62.5em ) /* 1000 */ { #nav { width: 100%; position: static; margin: 0; } } #media only screen and ( max-width: 40em ) /* 640 */ { html { font-size: 75%; /* 12 */ } #nav { position: relative; top: auto; left: auto; } #nav > a { width: 3.125em; /* 50 */ height: 3.125em; /* 50 */ text-align: left; text-indent: -9999px; background-color: #3498db; position: relative; } #nav > a:before, #nav > a:after { position: absolute; border: 2px solid #fff; top: 35%; left: 25%; right: 25%; content: ''; } #nav > a:after { top: 60%; } #nav:not( :target ) > a:first-of-type, #nav:target > a:last-of-type { display: block; } /* first level */ #nav > ul { height: auto; display: none; position: absolute; left: 0; right: 0; } #nav:target > ul { display: block; } #nav > ul > li { width: 100%; float: none; } #nav > ul > li > a { height: auto; text-align: left; padding: 0 0.833em; /* 20 (24) */ } #nav > ul > li:not( :last-child ) > a { border-right: none; border-bottom: 1px solid #2c81ba; font-size: 1em; } /* second level */ #nav li ul { position: static; padding: 1.25em; /* 20 */ padding-top: 0; } } </style> <div style="width:100%; position:fixed;background-color:#f8f8f8;top:0;left:0;z-index:1000000; height:47px; font-size:14px!important; font-family: "Open Sans", Helvetica, Arial, sans-serif;"> <div> <a href="http://www.example.com" style=" padding-left: 100px; "><img src="http://www.example.com/images/logo.png" border="0"></a> <nav id="nav" role="navigation" class="mainheader"> <ul class="clearfix"> <li><i class="fa fa-home"></i> Connect </li> [var.loggedin; if [val] == '1'; then '<li> <span><img src=[var.userpic;htmlconv=no;noerr] class=userpicimg style="width: 40px;border: #446db2 solid 1px;border-radius: 10%;"> [var.userdetails.user_firstname;noerr]</span> <ul> <li>Dashboard</li> <li>Profile</li> <li>Help</li> <li>Logout</li></ul></li>'; else '<li> Log In </li><li>Sign Up</li>';noerr] </ul> </nav> </div> </div>
Did you add viewport meta tag to your HTML? if not try adding this <meta name="viewport" content="width=device-width, initial-scale=1"> If it didn't work try adding this to your css as well. #-ms-viewport{ width: device-width; } Hope this Helps :)