CSS for a navigation menu with underlined links

I am attempting to create an underline effect when hovering over a link. However, I am facing an issue where the underline does not align perfectly with the line below it. Can someone please guide me on how to modify this CSS so that when the link is hovered over, the 3px line appears on top of the 1px line spanning across the page? Thank you.

<!DOCTYPE html>
<html>
<head>
<style>
.menu {
    font-family: "Helvetica Neue", verdana, arial; 
    position:fixed; 
    background:transparent;
    width:100%; 
    top:100px; 
    left:0; 
    height:25px;       /* decide on this as some stage */
    padding: 0;
    text-align:center;
    font-size: 12px;
    font-weight: 600;  /* decide on this as some stage */
    padding-top: 10px;  /* decide on this as some stage */
    border-bottom: 1px solid #ccc;
    border-top: 1px solid #ccc;
}

.ty-menu__items {
    position: absolute;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    width:100%;
    text-align: center;
}

.ty-menu__item {
    display: inline-block;
    padding-right: 50px;
    padding-left: 50px;
    }

a:link, a:visited {
    display: block;
    width: 100%;
    font-weight: light;
    color: #494949;
    background: transparent;
    text-align: center;
    text-decoration: none;
    text-transform: uppercase;
}

a:hover, a:active {
    background: transparent;
    border-bottom: 3px solid #494949; /* decide on this as some stage */
    color: #494949; /* decide on this as some stage */
}
</style>
</head>
<body>
<div class="menu">
<ul class="ty-menu__items">
  <li class="ty-menu__item"><a href="#home">home</a></li>
  <li class="ty-menu__item"><a href="#news">news</a></li>
  <li class="ty-menu__item"><a href="#contact">contact</a></li>
  <li class="ty-menu__item"><a href="#about">about</a></li>
</ul>
</div>
</body>

</html>

Answer №1

I made a modification by adding padding-bottom to your hover effect, aligning the 3px line on top of the intended 1px line.

a:hover, a:active {
    padding-bottom:8px;
    background: transparent;
    border-bottom: 3px solid #494949; /* final decision pending */
    color: #494949; /* final decision pending */
}

Check out the updated JS Fiddle here: http://jsfiddle.net/rqu39zcf/1/

Answer №2

I modified it to this based on my understanding

.nav {
    font-family: "Helvetica Neue", verdana, arial; 
    position:fixed; 
    background:transparent;
    width:100%; 
    top:100px; 
    left:0; 
    height:35px;       /* will determine later */
    padding: 0;
    text-align:center;
    font-size: 12px;
    font-weight: 600;  /* will determine later */
    padding-top: 10px;  /* will determine later */
    border-bottom: 1px solid #ccc;
    border-top: 1px solid #ccc;
}

.nav-menu__items {
    position: absolute;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    width:100%;
    text-align: center;
}

.nav-menu__item {
    display: inline-block;
    padding-right: 50px;
    padding-left: 50px;
    }

a:link, a:visited {
    display: block;
    width: 100%;
    font-weight: light;
    color: #494949;
    background: transparent;
    text-align: center;
    text-decoration: none;
    text-transform: uppercase;
}

a:hover, a:active {
    padding-bottom:1px;
    background: transparent;
    border-bottom: 3px solid #494949; /* will determine later */
    color: #494949; /* will determine later */
}

Specifically, I adjusted the .nav: height attribute

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

How can you add a new div directly after the parent div's content and display it in-line?

I have a banner that stretches to fill the width completely. Inside this main div, there is another smaller div measuring 30px in size positioned at the end. The nested div contains a close icon background image, and upon clicking it, the entire banner get ...

Issues with the alignment of columns using Bootstrap framework

I've created a design in Photoshop using the Bootstrap 12-column guide, but when I try to implement it using Bootstrap, the results are completely off. https://i.stack.imgur.com/vQwOt.png Here's my HTML code: <!DOCTYPE html> <html lan ...

Dynamic Data Drive Multi-Series Line Graph

I am currently working on creating a D3 line chart. I have taken the code from the block builder and adjusted it with my own data. While the code is functional, I'm facing an issue where the labels are not showing up when I hover over the line. My ma ...

Tips for correctly linking JS and CSS resources in Node.js/Express

I have a JavaScript file and a stylesheet that I am trying to link in order to use a cipher website that I created. Here is my File Path: website/ (contains app.js/html files and package json) website/public/css (contains CSS files) website/public/scri ...

No translation or compiling of SCSS/SASS to CSS available

As a beginner Sass user, I am working on mastering the SMACSS hierarchy and incorporating it into my project using Brackets. However, I have encountered an issue with mapping the main.scss file to the main.css file even though it compiles successfully via ...

What is the process of initializing divs in DataTables?

My application has a DataTable installed, but I encountered an error message stating "DataTables warning: Non-table node initialisation (DIV). For more details about this error, please visit http://datatables.net/tn/2". I'm aware that DataTables is d ...

Is there a way to implement a scrollbar that only scrolls through one specific column in an HTML table?

I need help adding a scrollbar to a specific column in an HTML table. Take a look at this scenario https://jsfiddle.net/6wpdc4tL/: https://i.stack.imgur.com/svzIg.png This table should have two scrollbars, one for the light blue SCROLL column and another ...

How can I add a black-colored name and a red-colored star to the Placeholder Star as a required field?

I'm looking to customize the placeholder text in an input field. I want the main placeholder text to be in black and have a red star indicating it's a required field. However, my attempt to set the color of the star specifically to red using `::- ...

Programmatically extracting a list of utilized CSS images from IE WebBrowser (IHTMLDocument2)

Exploring the IHTMLStyleSheetsCollection, IHTMLStyleSheet, IHTMLStyleSheetRulesCollection etc. of IHTMLDocument2 to compile a list of all styles in the current document is quite simple. Does anyone have any suggestions on how to generate a list of only th ...

What is the best way to retrieve the value of a component's HTML id tag in React?

Is there a way for me to retrieve the id of a react component from within the component itself? I need this information to perform some tasks with a third-party library. In Ember, you can access the component's id using elementId, which can be found ...

Tips on choosing JSON data to show on an HTML page

I am working with JSON data from a specific URL and I need to display only the information related to France on an HTML page. However, I am unsure of how to achieve this. Can someone please assist me? Here is my JavaScript code: // API Fetch Call const ...

There is a mismatch in the host name: please consider using a domain name (e.g., https://myname.myorg.com) instead of https://localhost

My current setup involves an HTML application that sends an AJAX request to a Qt C++ HTTP Server running on my local computer, hosted at "https://localhost:8081". However, I am facing an issue with a host name mismatch causing the AJAX request to fail. Is ...

What is the best way to transform this unfamiliar CSS element into JavaScript code?

I'm facing an issue where I want to animate a CSS image based on time constraints, but since it's in CSS, I'm unable to achieve the desired effect. The animation in question is not the sun itself, but rather a yellowish half-circle animation ...

jQuery UI Datepicker extending beyond its bounds

My Datepicker is expanding and filling the entire screen. What could be causing this issue? Here is my code: Additionally, how can I change the date format in jQuery to appear as dd-mm-yyyy? https://i.stack.imgur.com/IQzVV.png HTML: <!DOCTYPE html&g ...

Find the parent element that houses a particular child element

I am searching for a way to identify all the parent elements that have a certain child element within them. <tr> <i class="myClass"> </i> </tr> For example, I want to find all tr elements that contain an i element with the specif ...

Is there a way to manually trigger a re-render of all React components on a page generated using array.map?

Within my parent component (Game), I am rendering child components (Card) from an array. Additionally, there is a Menu component that triggers a callback to Game in order to change its state. When switching levels (via a button click on the Menu), I want a ...

BorderWoocommerce

Something odd has appeared around my shopping cart total ("WARENKORB SUMME") today. Could you please take a look at the screenshot below? Any idea how to remove it? This is for a Wordpress/WooCommerce store. I haven't made any recent changes - I su ...

What is the best approach to updating multiple rows instead of just the first row using the update button located on both sides using PHP and AJAX?

Starting fresh, so I might sound like a beginner with this question. How can I make use of the update button to update specific rows instead of just the top row? Every time I try to update, it only works for the top row. Here's the code snippet from ...

Issues arising with transferring information between components

My webpage had a header with a search engine input, a list of posts, and pagination all on one page. I made the decision to separate the header into its own component in a different Vue file. However, after making this change, searching for posts by their ...

Showcasing the Characteristics of Products in a Table on Prestashop

Dealing with PrestaShop. I have configured attributes for a product and I want to showcase them in a table format similar to this example: The table should display the Paper Size across the top, with each row representing the quantity of paper. In my ad ...