Having a problem with the hover effect on the navbar component

Whenever I hover over the individual links, I notice that the color change doesn't extend all the way up. I have a feeling there is a more efficient way to achieve this than my current approach. Any assistance would be greatly appreciated!

HTML:

<ul class="nav nav-blue">
    <li><a href="" class="nav-hdr">Nigel Silva</a></li>
    <li><a href="" class="el-b-1">Home</a></li>
    <li><a href="" class="el-b-2">About</a></li>
    <li><a href="" class="el-b-3">Work</a></li>
    <li><a href="" class="el-b-4">Clients</a></li>
    <li><a href="" class="el-b-5">Contact</a></li>
</ul>

CSS:

.nav-hdr {
  color: #FFF;
  font-size: 1.5em;
  position: relative;
  left: -27%;
  top: 5%;
}
.nav{
  list-style:none;
  margin:0 auto;
  padding:0;
  text-align:right;
  text-decoration: none;
  height: 55px;
}
.nav li{
  display:inline;
}
.nav a{
  display:inline-block;
  padding-right:15px;
  color: #000;
  margin-top: auto;
  -webkit-transition-duration: 0.2s;
  -moz-transition-duration: 0.2s;
  transition-duration: 0.2s;
}
.nav a:hover, .nav a:focus {
  color: #FFF;  
}
.nav-blue {
  background-color: #00aeef;
  margin: auto;
  width: 50%;
}
.nav-blue a {
  color: #FFF;
}
.el-b-1:hover, .el-b-1:focus, .el-b-2:hover, .el-b-2:focus, .el-b-3:hover, .el-b-3:focus,
.el-b-4:hover, .el-b-4:focus, .el-b-5:hover, .el-b-5:focus {
  background-color: #47cdff;
  margin-top: 8.5px;
  margin-bottom: 8.5px;
}
.el-b-1:active, .el-b-2:active, .el-b-3:active, .el-b-4:active, .el-b-5:active {
  background-color: #0b8aba;
}

Answer №1

To enhance the alignment, apply vertical-align: top; to the .navbar a class and delete margin-top: 8.5px;

Answer №2

If you're looking for a quick solution, try implementing the code snippet below:

Simply include this in your .css file:

.nav li:hover{
    background:#47cdff;
    padding-top:7px;
    padding-bottom:31px;
}

Make sure to delete the following from your existing stylesheets:

.el-b-1:hover, .el-b-1:focus, .el-b-2:hover, .el-b-2:focus, .el-b-3:hover, .el-b-3:focus,
.el-b-4:hover, .el-b-4:focus, .el-b-5:hover, .el-b-5:focus {
  background-color: #47cdff;
  margin-top: 8.5px;
  margin-bottom: 8.5px;
}

There are several ways to achieve the same result, but this is one of the most efficient methods.

Answer №3

One interesting approach involves utilizing display: table and display: table-cell.

  • The navigation is enclosed within a <header> styled with display: table

  • The first link in the navigation is taken out, placed before it, and assigned display: table-cell along with the navigation. This setup ensures they both remain on the same line.

  • The links inside the navigation are set to display: inline-block and are centered using line-height: 3em. This line height dictates the overall height of the header section.

  • Upon reaching a small screen size, the @media query CSS triggers the transformation of menu items into a dropdown list.

  • When in collapsed mode, the selector a.menu:hover + .menu-items targets the menu-item division directly following the menu button.

Demo

  1. Click "Run code snippet" and view the demo in full screen mode.

  2. Resize the window width to observe the menu transitioning into a dropdown format.

* {
  margin: 0;
  padding: 0;
}
header {
  background-color: #00aeef;
  margin: 0 auto;
  width: 50%;
  display: table;
}
.nav-hdr {
  color: #FFF;
  font-size: 1.5em;
  display: table-cell;
  vertical-align: middle;
  padding: 0 10px;
  white-space: nowrap;
  text-decoration: none;
}
.nav {
  text-align: right;
  text-decoration: none;
  display: table-cell;
  padding: 0 0 0 1em;
  vertical-align: top;
  position: relative;
  min-width: 300px;
  /* Prevent shrinking too small */
  background-color: #00aeef;
}
.nav a {
  display: inline-block;
  line-height: 3em;
  padding: 0 0.5em;
  cursor: pointer;
  text-decoration: none;
  -webkit-transition-duration: 0.2s;
  -moz-transition-duration: 0.2s;
  transition-duration: 0.2s;
  color: #FFF;
  white-space: nowrap;
}
.nav a:last-child {
  padding-right: 1em;
  /* Last child gets a little extra padding on the right */
}
.nav a:hover,
.nav-hdr:hover {
  background-color: #0b8aba;
}
.nav a:active,
.nav-hdr:active {
  background-color: #47cdff;
}

a.menu {
  display: none;
}

@media screen and (max-width: 450px) {
  .nav {
    min-width: 0;
    position: relative;
  }
  .nav .menu-items {
    display: none;
  }
  a.menu {
    display: inline-block;
  }
  a.menu:hover + .menu-items,
  .menu-items:hover {
    display: block;
    position: absolute;
    top: 3em;
    right: 0;
    background: #000;
  }
  .menu-items a {
    display: block;
    text-align: left;
  }
}
<header>
  <a href="" class="nav-hdr">Nigel Silva</a>
  <nav class="nav">
    <a class="menu">Menu (hover me)</a>

    <div class="menu-items">
      <a href="" class="el-b-1">Home</a><a href="" class="el-b-2">About</a><a href="" class="el-b-3">Work</a><a href="" class="el-b-4">Clients</a><a href="" class="el-b-5">Contact</a>
    </div>
  </nav>
</header>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to eliminate the sticky class as you scroll down?

Check out this jQuery code I wrote to remove the sticky class while scrolling down: $(window).scroll(function (e) { if ($('.main_form_wrapper').length != 0) { var window_scroll = $(window).scrollTop(); console.log(window_scro ...

Is there a way to identify if the parent page has completed loading while within an iframe?

Is there a way to detect properties or events within the iframe that can help determine this? The src of the iframe and the parent page belong to different domains. ...

Disabling a button until all textfield fields are completed: A Step-by-Step Guide

My modal contains a table with the following code structure. <div> <Table> <tbody> {props.data.map((p) => <> <tr> <th> STC </th> ...

CSS, Assistance in incorporating a sub menu into a drop-down menu

I'm looking to enhance my drop down menu by adding a sub menu. Specifically, I would like to create a sub menu for Item 3 in the HTML below. How can I achieve this? Thank you, Below is my CSS code: .nav_menu { width:100%; background-color:# ...

Looking to adjust the background-image size of a table cell (td) upon clicking a specific

I have a website where I would like to display logos of different games with links attached to them. I have managed to adjust the size of the image on hover, but now I am looking for a way to keep the size bigger after clicking on the link. Is there a simp ...

What is the best way to place a child on top of a different parent in the stack?

There is a collection of figure tags, each with an expandable figcaption. Inside each figure, there is a clickable div that reveals the figcaption when clicked. However, the visible figcaption only overlaps the figure it belongs to and not others. Is there ...

Responses were buried beneath the inquiries on the frequently asked questions page

My FAQs page is in pure HTML format. The questions are styled with the css class .pageSubtitle, and the answers have two classes: .p1 and .p2. Here's an example: <p class="pageSubtitle">Which Award should I apply for?</p> <p class="p1" ...

Dynamically indicate the destination for AJAX success feedback across various forms

Here are a couple of inquiries: Is there a way to incorporate a second form on the same page and dynamically handle each form based on which submit button is clicked? Let's say I have several forms on one page. How can I alter the output destina ...

Element remains hidden until the developer console is activated

On my website, I've noticed that certain LayerSlider elements are remaining invisible until: The window is resized I disable the Bookmarks bar in Chrome (quite strange, right?) I switch on the Chrome debugger tools This issue is not exclusive to Ch ...

Add the AJAX response to a div element when the submit button is pressed

Working on a WordPress project involving an AJAX call, I need to return the result of a row to a div with the ID #hero. The challenge is that since the result is inside a for loop and each row has its own hero div, I want the result to be returned to the c ...

It is not possible to invoke a function within the AJAX success method

When trying to display an error message in my notify (toast) div using jQuery in Ajax success, I am encountering difficulties. Despite decoding the response from the URL properly, only .show() and .hide() functions seem to work. Although I have used conso ...

Unable to manipulate JQuery lightSlider slides using element index

I've been working on a new page design at this link: The code is still a work in progress, so bear with me as I test out some functions and scripts. At the end of the first section, there are 4 logos that, when clicked, will trigger a modal to pop u ...

Is it possible to use WHILE loops twice in PHP?

Just starting out with PHP and hoping for some assistance. I have a MySQL database table with columns for "id", "img", "link", and "desc". My goal is to echo all of this data in the following format: <div id="featured"> <a target='_b ...

CSS or jQuery: Which is Better for Hiding/Showing a Div Within Another Div?

Show only class-A at the top of the page while hiding all other classes (x,x,c). Hide only class-A while showing all other classes (x,x,c). Is it possible to achieve this? <div class="x"> <div class="y"> <div class="z"&g ...

Superimpose above the tbody

I am attempting to implement a loading overlay specifically on top of the tbody element. My current method involves adding a tr as the last child of tbody and positioning it using position: absolute, while setting the parent tbody with position: relative. ...

Mastering the technique of showcasing landscape using CSS3

I'm working on an HTML and CSS3 report with multiple columns. I am trying to print it in landscape orientation using HTML and CSS3. I attempted rotating the body and table, but only half of the table is visible while the other half is cut off or hidde ...

Can you provide me with instructions on how to change the background image?

Having trouble with the CSS code I wrote for a background-image. It's not showing up when I set it in my CSS stylesheet. I tested it and found that it only works when written in the body... <body background="Tokyo.png"> ...but not in the styl ...

Avoid having Masonry load all divs simultaneously

I have experience using jQuery Masonry on Tumblr, but now I want to incorporate it into my portfolio website for displaying my photography. However, I'm struggling to find a solution that allows sets of images to load in as the user scrolls to the bot ...

Using the .slider class on concealed div elements

Explaining this may be a bit tricky, but here we go... I have multiple hidden divs that switch with each other when a link is clicked using $(document).ready(function(){ $('a').click(function () { var divname= this.name; $("#"+divname ...

Interactive website powered by a dynamic database system and featuring HTML pages

I am currently working on a project involving a cutting-edge dynamic database driven website where users can engage by posting, commenting, liking, and more. I have been contemplating the idea of using HTML pages instead of PHP pages. Essentially, wh ...