I cannot get my vertical menu to go horizontally. Please help, as I tried to floating the ul left and right and tried inline none as well. I'm not quite sure I'm getting confused on which class I should put the inline or float on. Thank you in advance for everyone's help on this one as this one has been driving crazy, and I'm sure it is a simple solution but I just can't see it.
/* define a fixed width for the entire menu */
.horiz_nav {
width: 100px;
float: right;
}
/* reset our lists to remove bullet points and padding */
.mainmenu_horiz {
list-style: none;
padding: 0;
margin: 0;
}
.mainmenu_horiz ul {
list-style: none;
padding: 0;
margin: 0;
}
.menu_horiz li {
list-style: none;
padding: 0;
margin: 0;
display: inline-block;
float: right;
}
/* make ALL links (main and submenu) have padding and background color */
.mainmenu_horiz a {
display: block;
background-color: #8EC752;
text-decoration: none;
padding: 10px;
color: #000;
}
/* add hover behaviour */
.mainmenu_horiz a:hover {
background-color: #ABD281;
}
/* when hovering over a .mainmenu item,
display the submenu inside it.
we're changing the submenu's max-height from 0 to 200px;
*/
.mainmenu_horiz li:hover .submenu_horiz {
display: block;
max-height: 200px;
}
/*
we now overwrite the background-color for .submenu links only.
CSS reads down the page, so code at the bottom will overwrite the code at the top.
*/
.submenu_horiz a {
background-color: #999;
}
/* hover behaviour for links inside .submenu */
.submenu_horiz a:hover {
background-color: #666;
}
/* this is the initial state of all submenus.
we set it to max-height: 0, and hide the overflowed content.
*/
.submenu_horiz {
overflow: hidden;
max-height: 0;
-webkit-transition: all 0.5s ease-out;
}
<header class="header" id="header">
<div id="horiz_nav" class="horiz_nav">
<ul class="mainmenu_horiz">
<li>Home</li>
<li>Courses
<ul class="submenu_horiz">
<li>Motor Learning</li>
<li>MS II</li>
</ul>
</li>
</ul>
</div>
</header>
First, .menu_horiz li should be .mainmenu_horiz li, and then this should have float: left, not right, plus it doesn't need to be floated * and* display: inline-block - one of those two is sufficient.
.mainmenu_horiz li{
list-style: none;
padding: 0;
margin: 0;
float: left;
}
/* I have just changed your css to this one. Just check if it works for you. */
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
Related
I have written code for an HTML/CSS website sidebar, and I want to make sure that when a user hovers over an element in the sidebar, its background gets highlighted.
It just displays my normal list and the hover function does not work. What is the problem?
body {
margin: 0;
}
.sidebar {
float: right;
background-color: #F4ECFC;
/* Sidebar Background */
width: 200px;
/* Width of side bar */
height: 900px;
padding-top: 0px;
/* Padding above the header */
font-family: helvetica;
/* Font for the sidebar */
}
.sidebar a {
text-decoration: none;
/* Removes the underline from links. */
color: #0B0036;
/* Text color of the menu items */
}
.sidebar ul {
line-height: 40px;
overflow: hidden;
margin-left: 0;
padding-left: 0;
}
.sidebar ul li .listItemA {
/* list-style-type: none; */
background-color: #F4ECFC;
/* Background colour of the menu items. */
}
.sidebar ul li .listItemA a:hover {
background-color: purple;
}
.listItemA {
text-align: center;
}
<div class="sidebar">
<ul>
<li class="listItemA">Class1</li>
<li class="listItemA">Class2</li>
<li class="listItemA">Class3</li>
<li class="listItemA">Class4</li>
<li class="listItemA">Class5</li>
</ul>
</div>
One thing you want to do first is give your links display: block; which makes them take up 100% of the width available.
Then simplify the style of the link and the relative hover like so:
.sidebar ul .listItemA {
/* list-style-type: none; */
background-color: #F4ECFC;
/* Background colour of the menu items. */
}
.sidebar ul .listItemA:hover a {
background-color: purple;
}
As pointed out in a comment on your question you had a space between li and .listItemA, I just removed li to achieve the correct selector but you could've also changed that part of the selector to li.listItemA.
Really important change here is to style the link on hover of the list item:
.sidebar ul .listItemA:hover a { ... }
This tells the browser to change the background of the link when the list item is hovered.
body {
margin: 0;
}
.sidebar {
float: right;
background-color: #F4ECFC;
/* Sidebar Background */
width: 200px;
/* Width of side bar */
height: 900px;
padding-top: 0px;
/* Padding above the header */
font-family: helvetica;
/* Font for the sidebar */
}
.sidebar a {
display: block;
text-decoration: none;
/* Removes the underline from links. */
color: #0B0036;
/* Text color of the menu items */
}
.sidebar ul {
line-height: 40px;
overflow: hidden;
margin-left: 0;
padding-left: 0;
}
.sidebar ul .listItemA {
/* list-style-type: none; */
background-color: #F4ECFC;
/* Background colour of the menu items. */
}
.sidebar ul .listItemA:hover a {
background-color: purple;
}
.listItemA {
text-align: center;
}
<div class="sidebar">
<ul>
<li class="listItemA">Class1</li>
<li class="listItemA">Class2</li>
<li class="listItemA">Class3</li>
<li class="listItemA">Class4</li>
<li class="listItemA">Class5</li>
</ul>
</div>
I've been trying to solve this problem. What I want to learn is to know different ways to center the elements on navigation vertically, using semantic HTML. I want my logo on left and navigation on right.
I tried to use float on my nav element but the logo will break and will not be vertically centered. I used clearfix for that but I still can't find ways to vertically center both the logo and nav.
Will you please help me? And explain your answer please? Then if possible, can you please show me other ways of vertically centering the logo (left) and nav (right) using the exact format of my html?
Here's my code:
https://codepen.io/yortz/pen/pQdKWd
HTML
<!-- HEADER -->
<header>
<!-- LOGO -->
<img id="site-logo" src="http://lorempixel.com/190/25/" alt="Bookworm">
<nav>
<ul class="clearfix">
<li>About</li>
<li>Articles</li>
<li>News</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
CSS
* {
box-sizing: border-box;
}
/* HIGHLIGHTING NAVIGATION ELEMENTS */
header {
background-color: #ccc;
}
nav {
background-color: aqua;
}
/* CENTERING NAVIGATION */
header {
width: 100%;
}
#site-logo,
nav {
display: inline-block;
vertical-align: middle;
}
nav ul {
list-style-type: none;
padding-left: 0;
}
nav ul li {
display: inline-block;
}
nav ul li a {
border: 1px solid red;
padding: 10px 15px;
display: block;
text-decoration: none;
}
nav ul li a:hover {
background-color: red;
color: #fff;
}
/* CLEAR FLOATS */
.clearfix::after {
content: "";
display: table;
clear: both;
}
Please Help. Thank you!
I would use flexbox for the positioning in the nav
header {
display: flex;
justify-content: space-between; // pushes the logo to the left and the nav to the right
align-items: center; // center aligns children of nav vertically
}
If you want to achieve something similar without using flexbox, you can position the logo absolutely:
header {
position: relative; // with this all children can be positioned absolutely, relative to the header
text-align: right; // this aligns the nav to the right of the header
}
header > a {
position: absolute; // positions the logo absolute, relative to header
top: 50%; // aligns the logo in the middle of the relative parent
left: 0; // aligns the logo to the left edge of the relative parent
transform: translateY(-50%); // changes the coordinates of the logo, to center it vertically (y-axis)
}
nav {
text-align: left; // just used to reset the text-alignment in the nav elements
}
I would consider using a class instead of selecting the a-tag, for example <a class="logo" href="...">...</a> and then header .logo {...} in the CSS, instead of header > a {}. That is more future proof if you add more elements to the header.
Quick tip: If the logo is higher than the nav if will overflow the parent container, so you would need to modify the height of the parent to fix that. If you can guarantee, that the nav is always higher than the logo, this is not a problem for you and you can leave the height of the header untouched.
Using float left and right and giving padding to logo for vertical align center
* {
box-sizing: border-box;
}
/* HIGHLIGHTING NAVIGATION ELEMENTS */
header {
background-color: #ccc;
}
.logo{
float:left;
}
.logo img{
padding:24px 10px;
}
nav {
background-color: aqua;
float:right;
}
.clearfix{
clear:both;
}
/* CENTERING NAVIGATION */
header {
width: 100%;
}
#site-logo,
nav {
display: inline-block;
vertical-align: middle;
}
nav ul {
list-style-type: none;
padding-left: 0;
}
nav ul li {
display: inline-block;
}
nav ul li a {
border: 1px solid red;
padding: 10px 15px;
display: block;
text-decoration: none;
}
nav ul li a:hover {
background-color: red;
color: #fff;
}
/* CLEAR FLOATS */
.clearfix::after {
content: "";
display: table;
clear: both;
}
<!-- HEADER -->
<header>
<!-- LOGO -->
<img id="site-logo" src="http://lorempixel.com/190/25/" alt="Bookworm">
<nav>
<ul class="clearfix">
<li>About</li>
<li>Articles</li>
<li>News</li>
<li>Get in Touch</li>
</ul>
</nav>
<div class="clearfix"></div>
</header>
If you want to center the elements vertically, you can use align-content with display: flex.
hope this will help. I had few changes in your jfiddle link and pasted it here. just css changes.
* {
box-sizing: border-box;
}
/* HIGHLIGHTING NAVIGATION ELEMENTS */
header {
background-color: #ccc;
width:100%;
display:block;
}
nav {
}
/* CENTERING NAVIGATION */
header {
width: 100%;
}
#site-logo{
display:inline-block;
vertical-align:middle;
width:calc(20% - 2px);
}
nav {
display: inline-block;
vertical-align: middle;
position:relative;
width:calc(80% - 2px);
}
nav ul {
list-style-type: none;
padding-left: 0;
display:inline-block;
position:relative;
left:76%;
background-color: aqua;
}
nav ul li {
display: inline-block;
}
nav ul li a {
border: 1px solid red;
padding: 10px 15px;
display: block;
text-decoration: none;
}
nav ul li a:hover {
background-color: red;
color: #fff;
}
/* CLEAR FLOATS */
.clearfix::after {
content: "";
display: table;
clear: both;
}
You need to modify your code like this:
* {
box-sizing: border-box;
}
/* HIGHLIGHTING NAVIGATION ELEMENTS */
header {
background-color: #ccc;
}
nav {
background-color: aqua;
}
/* CENTERING NAVIGATION */
header {
width: 100%;
display: table;
vertical-align: middle;
}
.logo-wrapper{
display: table-cell;
vertical-align: middle;
}
#site-logo{
display: inline-block;
vertical-align: middle;
}
nav{
display: table-cell;
float: right;
}
nav ul {
list-style-type: none;
padding-left: 0;
}
nav ul li {
display: inline-block;
}
nav ul li a {
border: 1px solid red;
padding: 10px 15px;
display: block;
text-decoration: none;
}
nav ul li a:hover {
background-color: red;
color: #fff;
}
/* CLEAR FLOATS */
.clearfix::after {
content: "";
display: table;
clear: both;
}
And then edit HTML anchor tag like this:
<img id="site-logo" src="http://lorempixel.com/190/25/" alt="Bookworm">
Lots of answers already. This is mine. I'm putting the logo inside the <ul> as a li element. I'm making the <ul> a flex container and the most important: margin:auto to the right for the first list item.
nav ul li:first-child {
margin:0 auto 0 0
}
* {
box-sizing: border-box;
}
/* HIGHLIGHTING NAVIGATION ELEMENTS */
header {
background-color: #ccc;
}
nav ul {
background-color: aqua;
display:flex;
}
nav ul li a{height:47px;}
/* CENTERING NAVIGATION */
header {
width: 100%;
}
#site-logo,
nav {
vertical-align: middle;
}
nav ul {
list-style-type: none;
padding-left: 0;
}
nav ul li:first-child {
margin:0 auto 0 0
}
nav ul li a {
border: 1px solid red;
padding: 15px;
display: block;
text-decoration: none;
}
nav ul li a:hover {
background-color: red;
color: #fff;
}
nav ul li:first-child a{padding:10px}
/* CLEAR FLOATS */
.clearfix::after {
content: "";
display: table;
clear: both;
}
<!-- HEADER -->
<header>
<!-- LOGO -->
<nav>
<ul class="clearfix">
<li><img id="site-logo" src="http://lorempixel.com/190/25/" alt="Bookworm"></li>
<li>About</li>
<li>Articles</li>
<li>News</li>
<li>Get in Touch</li>
</ul>
</nav>
</header>
Problem
No space between image logo and menu although i assign margin-top:20px ?
actually i need to show full logo as you see logo cutting
logo problem
my fiddle work as below :
https://jsfiddle.net/ahmedsa/z2pmwsnr/
i need to modify fiddle to leave space between logo image and menu .
image logo
http://www.mediafire.com/view/qd5otyc1w9yv5e9/logo.png
my code
ul {
border-top: 4px solid red;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%; /* Full width */
}
li {
float: right;
}
li a {
color: white;
padding: 16px;
text-decoration: none;
}
li i{
color:white;
}
.w3ls_header_middle {
padding: 0 0;
padding-top:30px
}
.agileits_logo{
float:right;
margin-right:0em;
margin-top:20px
}
I am trying to make the top menu vertically center without assigning value like margin-top: 50px; because some of my friends say this is not the ideal approach.
/* Nav Section */
.nav {
width: 100%;
padding: 0;
margin: 0;
}
.nav-contain {
width: 1100px;
margin: 0 auto;
padding: 0;
overflow: hidden;
}
.logo {
z-index: 10;
display: inline-block;
background: #2980B9;
padding: 65px 50px 35px 45px;
font-size: 36px;
line-height: 42px;
color: #fff;
text-align: center;
}
.logo a {
color: #FFFFFF;
text-decoration: none;
}
#medical {
display: block;
text-transform: uppercase;
}
.menu {
padding: 0;
margin: 0;
float: right;
display: table-cell;
position: relative;
}
.menu a {
text-decoration: none;
color: #505050;
font-weight: bold;
}
.menu ul {
padding: 0;
margin: 0;
float: left;
top: 50%;
}
.menu ul ul {
padding: 0;
margin: 0;
}
.menu ul li {
float: left;
display: block;
margin-left: 45px;
}
.menu ul ul {
position: absolute;
left: -999px;
}
.menu ul li:hover ul {
left: auto;
}
.menu ul li ul li {
margin-left: 0;
float: none;
margin-top: 15px;
}
<div class="nav">
<div class="nav-contain">
<div class="logo">
<span id="medical">Medical</span><span id="company"> Company</span>
</div>
<!-- Logo -->
<div class="menu">
<ul>
<li>Home</li>
<li>About
<ul class="dropdown">
<li>Sample</li>
<li>Sample</li>
</ul>
</li>
<li>Gallery</li>
<li>Prices</li>
<li>Contact</li>
</ul>
</div>
<!-- Menu -->
</div>
<!-- Nav Contain -->
</div>
<!-- Nav -->
Remove float:right on .menu, and set both .logo and .menu to this:
.logo, .menu {
display: inline-block;
vertical-align: middle;
}
If you need .menu to stay on far right side, also add this:
.nav-contain {
text-align: justify;
}
.nav-contain:after{
content: '';
display: inline-block;
width: 100%;
}
How it works:
Set text-align: justify; will line up the two inner inline blocks to the left and right edges of the container.
Create an invisible 100% width element by using :after or :before pseudo-element stretching the box to occupy the entire space of the container. Otherwise inline element occupies only the space bounded by the tags that define the inline element.
One easy way to center here is to use Flexbox:
.nav-contain {
/* what is already there */
display: flex;
align-items: center;
}
Beware of browser support (check caniuse.com to see if the compatibility level is acceptable to you).
This is superior to the margin-top solution as it ensures that you won't have to manually change that 50px each time the size of the image or anything else in the navbar changes.
Try:
.menu > ul > li {
min-height:50px;
display: table;
}
.menu > ul > li > a {
display: table-cell;
vertical-align: middle;
}
http://jsfiddle.net/rawat/4h05rq2s/
Since your navbar remains the same height the whole time, I suggest you give the .nav-contain the following code:
.nav-contain {
width: 1100px;
margin: 0 auto;
line-height: 184px;
padding: 0;
overflow: hidden;
}
Note the line-height.
This will, once you smaller the available width of your device, result in a probably not so nice looking huge navigation bar. For this, I suggest media queries.
Quite difficult to explain, so I created a codepen for you to have a look at. When you hover over a navigation item and the dropdown menu appears. You'll see that some of the items don't line up correctly. I.e. the width of some items are bigger than their sibling items. I just want them all to stretch to the width of the widest list item. (confused yet?) Just have a look at the code pen..
You need to make changes to the below CSS to get them aligned.
For Instance.
#main-navigation ul li ul li {
background: none repeat scroll 0 0 #DE5D48;
width: 140px;
text-align:left;
}
WORKING DEMO
Hope this helps.
You should use display : inline-block instead of float: left in /* list item */ css and should add display : table-row; into /* sub list item */ .
#dark-blue: #31394C;
#light-gray: #E6E6E6;
#gray: #B1B2B5;
#dark-gray: #6D6D6D;
#dark-red: #d7351b;
#red: #ee2a2e;
#light-red: #f75d1f;
#dusty-red: #DE6450;
#light-dusty-red: #fdc29e;
#dark-dusty-red: #b50b03;
#sub-nav-red: #DE5D48;
#white: #ffffff;
#import url(http://fonts.googleapis.com/css?family=Rosario);
body {
background: #dark-blue;
}
#main-navigation {
/* initial list */
ul {
list-style: none;
/* list item */
li {
display: inline-block;
position: relative;
background: #dark-red;
min-width: 105px;
height: 21px;
text-align: center;
margin-right: 10px;
/* link */
a {
color: #white;
display: block;
line-height: 21px;
text-decoration: none;
font-family: 'Rosario', sans-serif;
font-size: 12px;
text-transform: uppercase;
}
a:hover {
color: #white;
background: #dark-red;
}
/* sub list */
ul {
position: absolute;
top: 100%;
left: -40px;
display: none;
border-top: 10px solid rgba(255, 255, 255, 0.0);
z-index: 99;
/* sub list item */
li {
background: #sub-nav-red;
display : table-row;
/* sub list link */
a {
padding: 0 5px 0 5px;
white-space: nowrap;
}
}
}
ul:hover {
display: block;
}
}
li:hover > ul {
display: block;
}
}
}
I suggest moving the background, size, and positioning from the li to the ul, that way the whole list will expand if one item is too big.
Try to give more min-width
#main-navigation ul li {
float: left;
position: relative;
background: #D7351B;
min-width: 140px;
in css, li element has: min-width: 105px;
change it to 155px and it will solve it.
hope that helps.
Its quite simple,
You can't use: top: 100%
You'll be wanting to update this to be top: 0 this will align it to the top of its parent.
You may need top: 11px in your exact case.