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>
Related
I can't seem to center the navigation bar buttons. Is there a way to do this in the css file? I have tried centring but it hasn't worked.
HTML
<div class="navbar">
Home
News
Contact
</div>
CSS
.navbar {
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: 1300px; /* Full width */
z-index: 99999;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
/* Links inside the navbar */
.navbar a {
display:inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
I have modified your style for ".navbar a". Hope it will work for you.
You will love flexbox - super simple, and very useful.
Flexbox requires a parent and items.
You turn flexbox on on the parent, and then the various switches are set either on the parent (as in the case of justify-content) or on the items.
Here is a great cheatsheet for Flexbox.
Here is a fantastic YouTube tutorial.
DEMO:
.navbar {
overflow: hidden;
background-color: #333;
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
z-index: 99999;
text-align: center;
width: 100vw; /* Full width */
display:flex;
justify-content:center;
border:5px solid yellow;
}
/* Links inside the navbar */
.navbar a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
border:1px solid pink;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
<div class="navbar">
Home
News
Contact
</div>
You can use
<div class="navbar">
<div style="display: inline-block;">
Home
News
Contact
</div>
</div>
If I understand you correctly, you need to align the links in the center of the navbar, for this you need to do:
CSS:
/* Links inside the navbar */
.navbar a {
/* float: left; remove this property */
display: inline-block; /* change display: block to inline-block */
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
You can see an example on: https://jsfiddle.net/4gy2japx/
.navbar {
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 */
z-index: 99999;
text-align: center;
margin: 0 auto;
}
.navbar ul {
display:inline-block;
list-style-type: none;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div class="navbar">
Home
News
Contact
</div>
</body>
</html>
You have to remove float left and add display: inline-block;
.navbar a {
float: left;
display: block;
There are several mistakes in your elements styling. Trying to align floated elements, assigning display block to linear links and defining empirical lengths when you're aiming for full lengths are some of them.
Try this instead:
html,body {
margin: 0; /* overwrite browser defaults */
}
.navbar{
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 */
z-index: 99999;
text-align: center;
}
/* Links inside the navbar */
.navbar a {
display: inline-block;
color: #f2f2f2;
padding: 14px 16px;
text-decoration: none;
}
/* Change background on mouse-over */
.navbar a:hover {
background: #ddd;
color: black;
}
<div class="navbar">
Home
News
Contact
</div>
I have written this codepen for a Top Navbar.
This Top Navbar is a <div> element containing <a> elements:
<div class="topnav">
Home
News
Contact
About
</div>
<h1>TopNav using div and a elements</h1>
The key CSS is as follows:
.topnav {
background-color: #bbb; /* gray */
line-height: 50px; /* same as height! */
}
.topnav a {
height: 50px;
margin: 0;
padding: 15px 20px;
color: black;
text-decoration: none;
}
I have vertically centered the links in the <div> element, by:
1) giving each link a height of 50px and then
2) giving the wrapping <div> the same line-height.
This works fine.
The problem is that I am unable to precisely control the padding and margin for the links.
Right now, I have given the padding for the links an arbitrary value:
padding: 15px 20px;
However, there is a small gap at the top and bottom of each link, where the Navbar background color shows through. This can be seen when you hover over a link.
When a link is moused over, I would link the link color to cover the entire NavBar. Is there any exact calculation I can make to ensure this, rather than choosing an arbitrary value for padding-top?
Secondly, there is also a gap at the sides of each link when one hovers. It can be clearly seen when one hovers over a link that is on either side of the "active" link (the green one). I would prefer this gap to be eliminated. How can I do this?
By Default a tag is inline element, you should change it display property to display: inline-block. so that you can set margin and padding.
* {
box-sizing: border-box;
}
/* Extra small devices (phones) */
body {
margin: 0;
background-color: powderblue;
}
.topnav {
background-color: #bbb; /* gray */
line-height: 50px; /* same as height! */
}
.topnav a {
margin: 0;
padding: 10px 20px;
color: black;
text-decoration: none;
display: inline-block; /* Set inline-block for a tag */
}
.topnav {
background-color: #bbb; /* gray */
line-height: 50px; /* same as height! */
}
.topnav a.active {
background-color: mediumseagreen;
color: white;
}
.topnav a:hover:not(.active) {
background-color: #f1f1f1; /* light gray */
}
<div class="topnav">
Home
News
Contact
About
</div>
<h1>TopNav using div and a elements</h1>
Use this. I gave display:block to a and display:flex to .topnav
* {
box-sizing: border-box;
}
/* Extra small devices (phones) */
body {
margin: 0;
background-color: powderblue;
}
.topnav {
background-color: #bbb; /* gray */
height:50px;
display:flex;
line-height: 50px; /* same as height! */
}
.topnav a {
display:block;
height: 50px;
margin: 0;
padding: 0px 20px;
color: black;
text-decoration: none;
}
.topnav a.active {
background-color: mediumseagreen;
color: white;
}
.topnav a:hover:not(.active) {
background-color: #f1f1f1; /* light gray */
}
<div class="topnav">
Home
News
Contact
About
</div>
<h1>TopNav using div and a elements</h1>
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>
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;
}
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
}