What could be the reason for the text-decoration not functioning as expected in my code?

When I hover over 'list item 2' in my code, the hyperlink is not underlined until then. This behavior is expected and desired.

The issue arises when attempting to add multiple hyperlinks to the drop-down links as shown in 'list item 1', where both hyperlinks are already underlined before hovering over them.

I had to enclose link 1 & 2 for 'list item 1' within a <br> tag because without it they would overlap each other. Introducing the <br> tag affects 'list item 2' by pushing it down.

To clarify, I want 'list item 1' and 'list item 2' to be at the same level and none of the links should be underlined until hovered over.

Despite searching online, issues with 'text-decoration: none' remain common, but I couldn't find a solution that addresses my specific problem.

In the presented example, I have left the href attributes blank as their destinations are irrelevant here.

ul {
  list-style-type: none;
}

a:hover {
  text-decoration: underline;
}

.dropdown {
  display: inline-block;
  width: 100px;
  height: 25px;
  background-color: black;
  color: white;
  padding: 20px;
  text-align: center;
  font-size: 20px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  padding: 12px 16px;
  text-decoration: none;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<ul>
  <div class="dropdown">
    <li>list item 1</li>
    <div class="dropdown-content">
      <a href="" target="_blank">link 1</a><br>
      <a href="" target="_blank">link 2</a>
    </div>
  </div>

  <div class="dropdown">
    <li>list item 2</li>
    <a class="dropdown-content" href="" target="_blank">link 1</a>
  </div>

Answer №1

To properly style the a element, you should wrap it within an additional container and adjust the CSS as shown below:

I have also fixed the HTML structure error by ensuring that only li elements are direct children of ul

ul {
  list-style-type: none;
}

.dropdown {
  display: inline-block;
  width: 100px;
  height: 25px;
  background-color: black;
  color: white;
  padding: 20px;
  text-align: center;
  font-size: 20px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  padding: 12px 16px;
}

.dropdown-content a {
  text-decoration: none;
}

.dropdown-content a:hover {
  text-decoration: underline;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<ul>
  <li class="dropdown">list item 1
    <div class="dropdown-content">
      <a href="" target="_blank">link 1</a><br>
      <a href="" target="_blank">link 2</a>
    </div>
  </li>

  <li class="dropdown">list item 2
    <div class="dropdown-content">
      <a href="" target="_blank">link 1</a>
    </div>
  </li>
</ul>

Answer №2

You must make adjustments to the CSS by adding styles for .dropdown-content a and moving a:hover to the end.

.dropdown-content a{
  text-decoration: none ;
  }

a:hover {
  text-decoration: underline ;
}

<!DOCTYPE html>

<html>
<head>
<style>
  ul {
  list-style-type:none;
}



.dropdown {
  display: inline-block;
  width:100px;
  height:25px;
  background-color: black;
  color: white;
  padding: 20px;
  text-align: center;
  font-size: 20px;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  padding: 12px 16px;
  text-decoration: none ;
 }
 
 .dropdown-content a{
  text-decoration: none ;
  }
  
  a:hover {
  text-decoration: underline ;
}
 

.dropdown:hover .dropdown-content {display: block;}
</style>
</head>
<body>


<ul>
<div class="dropdown">
  <li>list item 1</li>
    <div class="dropdown-content">
    <a href="" target="_blank">link 1</a><br>
    <a href="" target="_blank">link 2</a>
    </div>
</div>

<div class="dropdown">
  <li>list item 2</li>
  <a class="dropdown-content" href="" target="_blank">link 1</a>
</div>

 </body>
</html>

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

What is the reason behind IE's decision to reduce the size of password boxes compared to text

Take a look at the uncomplicated form right below. You'll notice that it consists of a text box positioned above a password box. In older versions of Internet Explorer like 7 and 8, as well as possibly in other browsers, you might observe that the tex ...

What is the proper file format for a form action in CSS?

What I Currently Have: In my Index.html file, there are a total of 4 form elements including text fields and a dropdown. Upon submission by the user, the data is processed in confirm.html using a separate JavaScript file for formatting before being displa ...

Sharing CSS styles among multiple single-page applications (SPAs) developed using the React

I am currently working on multiple micro SPAs that exist independently within an Express environment. I am facing a challenge with importing a global CSS file that is located outside of the apps, as it is not being recognized. The use of @import url(asset ...

What is the best way to incorporate CSS into this HTML document?

Content has been omitted. It is crucial to review documentation in advance in order to reduce repetitive inquiries. Check out the Webpack v2 documentation. ...

Leveraging this jQuery code across multiple div elements

As a newcomer to jQuery, I am currently experimenting with a test script. My goal is to create an effect where when an image is clicked, a div slides down displaying more information similar to the new google images effect. The issue with the current scri ...

Switch between light and dark mode on a webpage using HTML

I am looking to create a script using JavaScript, HTML, and CSS that can toggle between dark mode and night mode. Unfortunately, I am unsure of how to proceed with this task. https://i.sstatic.net/xA3Gq.png https://i.sstatic.net/v5vq5.png My goal is to ...

Dynamic selection of elements using a parameterized variable in JQuery

Having some trouble with the following line of code: alert($('input' + ' #' + $id_bici).attr('value')); Here is the corresponding HTML code snippet: <input type="hidden" id="id_bici1" name="id_bici1" value="9"> ...

Using CSS animations to animate a div while creating a subtle pause between the two

I am working on creating a notification bar that will be displayed and hidden using CSS animation. My challenge is finding the right delay between the two animations. HTML <div id="notification" class="alert" role="alert"></div> JS $(' ...

When attempting to call XML, it fails to load and returns an undefined error

I'm encountering an error when trying to load my content: Members.html:9 Uncaught ReferenceError: loadXMLDoc is not defined at HTMLButtonElement.onclick Here is my HTML code: <p><button onclick="loadXMLDoc()"> Load Table </ ...

What exceeds the size of the HTML tag?

I noticed that the height of my section with id "the-view-port" is set to 100vh in my page like this: <html> <body> <section id="the-view-port"> However, I am experiencing an issue in Chrome (Version 73.0.3683.86) where there seem ...

Obtain the title of the text generated by clicking the button using a script function

I am working on a piece of code that generates a text box within a button's onclick function. My goal is to retrieve the name value of each text box using PHP. <script language="javascript"> var i = 1; function changeIt() ...

Form confirmation popup with JQuery displaying input values upon submission click

Could anyone assist me with creating a form that displays a popup after submission, showing the entered values for confirmation along with edit and submit buttons? I have found several examples online, but none are exactly what I need. Your help would be ...

Modifying the color of the error icon in Quasar's q-input component: a step-by-step guide

https://i.stack.imgur.com/4MN60.png Is it possible to modify the color of the '!' icon? ...

Illuminated container with shading

I'm struggling to style a box with a unique glow effect and shadow at the bottom. I've hit a roadblock in creating this particular effect. The glow effect I am aiming for is shown here: https://i.stack.imgur.com/i2va8.png My attempts to create ...

PHP/HTML Contact Form that sends messages to a designated email address depending on the option selected in

UPDATED: I recently created a contact form using HTML/PHP. I want to enhance user experience by allowing them to choose a specific department from a dropdown menu and have their email routed directly to that department's designated email address. My ...

Is there a way to ensure that my if statement functions properly within this script?

Yesterday, I sought advice on my question, but everyone recommended using the BeautifulSoup library. Unfortunately, I am restricted from utilizing any external libraries for this class, although I have managed to make some progress. The goal of my code is ...

Trigger an input file click automatically using vanilla JavaScript with an AJAX request in Google Chrome

Currently, my webapp operates in a way that every button click triggers an ajax request to the server. The server then validates and sends JavaScript code back to the client. Unfortunately, the framework I am using does not allow me to incorporate jQuery a ...

Checkbox customization that shifts position when clicked

I seem to be missing something as I can't seem to find a solution for this particular issue. I have implemented custom checkboxes on a list of sentences. Initially, they appear fine, but once you check the first checkbox, it shifts its position. I am ...

Unable to select a checkbox in an ASP.NET Core MVC view

In the process of developing an ASP.NET Core 3.1 MVC application, I am faced with a task to implement a dynamic checkbox menu for selecting fruits and storing the choices in another data source. However, encountering an issue where I'm unable to sele ...

What is the technique for choosing the parent's ID with jQuery?

When a user clicks on any team, I need to retrieve the parent's parent ID. For example, if someone clicks on a Premier League Team, I want to store the ID 2 in a variable called league_id. Here are my attempts so far: Using Closest Method $('u ...