The hover effect for changing the style of one element when hovering over another element is not functioning as

I've encountered an issue with styling a triangle element using the .something:hover .another{...} selector. Can anyone help me understand what might be causing this problem?

.actions {
  display: flex;
  margin-top: 20px;
  max-width: 260px;
}

.action-reg-button {
  font-size: 13px;
  font-weight: bold;
  color: black;
  box-sizing: border-box;
  padding: 45px;
  padding-top: 10px;
  padding-bottom: 10px;
  transition: 150ms;
}

.action-log-button {
  font-size: 13px;
  font-weight: bold;
  color: white;
  box-sizing: border-box;
  padding: 25px;
  padding-top: 10px;
  padding-bottom: 11px;
  background-color: #0D5E5E;
  transition: 150ms;
  cursor: pointer;
}

.triangle {
  position: relative;
  width: 40px;
  margin-top: 12px;
}

.triangle-left {
  width: 0;
  height: 0;
  border-top: 37.5px solid transparent;
  border-bottom: 0px solid transparent;
  right: -1px;
  border-right: 40px solid #0D5E5E;
  position: absolute;
  right: 0px;
  transition: 150ms;
  z-index: 20;
}

.action-reg-button:hover {
  color: #b2b2b2;
  cursor: pointer;
}

.action-log-button:hover {
  color: #ddd;
  background-color: #2e2e2e;
}

.action-log-button:hover .triange-left {
  border-right: 40px solid #2e2e2e!important;
}
<div class="actions">
  <p class="action-reg-button">Register</p>
  <div class="triangle">
    <div class="triangle-left"></div>
  </div>
  <p class="action-log-button" id="login-menu-button">Login</p>
</div>

Answer №1

Problematic Situation

The issue at hand revolves around the selector:

.action-log-button:hover .triange-left {...

which suggests a layout that does not exist:

<p class="action-log-button" id="login-menu-button">
Login <div class="triangle-left"></div>
</p>

In CSS, styles cascade in a flow, meaning if you wish to target one element but affect another unrelated element, make sure that the affected element is either nested within or follows the targeted element.

Upon inspecting the layout, it becomes apparent that hovering over .action-log-button can never impact .triangle-left due to the positioning of .action-log-button and .triangle. In essence, for .triangle-left to be affected, it needs to be inside or placed after .action-log-button.


Resolution Strategy

To rectify this situation, place the HTML of .action-log-button right before .triangle* and implement this rule:

.action-log-button:hover+.triangle .triangle-left {
  border-right-color: #2e2e2e;
}

*Although having the triangle positioned after the button may not align with your preference, please continue reading for an explanation.

This leads to the following sequence of actions:

  • hover over .action-log-button

  • proceed to + .triangle immediately after

  • navigate inside .triangle to reach .triangle-left

  • and modify its border-right-color: #2e2e2e;.

  • Note the shift from

    border-right: 40px solid #2e2e2e;

  • alongside updates in .triangle-left -- separating shorthand rulesets simplifies style complexity and reduces errors.

Finally, to address the position switch between the triangle and button, utilize the flexbox property: order By assigning numerical orders to flex-items (direct children of a flex-container), they will assume corresponding positions. Therefore, assign .action-log-button order: 3 and .triangle order: 2. While rendering on a webpage displays them correctly, the actual order remains true in the HTML structure.


Visual Presentation

Moreover, the HTML tags have been adjusted to semantically accurate types. Utilizing <p> or <div> as a button proves suboptimal. It is valid yet far from ideal.

.actions {
  display: flex;
  margin-top: 20px;
  max-width: 260px;
}

.action-reg-button {
  font-size: 13px;
  font-weight: bold;
  color: black;
  padding: 10px 45px 10px;
  transition: 0.2s;
  order: 1
}

.action-log-button {
  font-size: 13px;
  font-weight: bold;
  color: white;
  padding: 10px 25px 10px;
  background-color: #0D5E5E;
  cursor: pointer;
  transition: 0.2s;
  order: 3
}

.triangle{
  width: 40px;
  margin-top: 12px;
  order: 2
}

.triangle-left {
  width: 0;
  height: 0;
  border-top: 40px solid transparent;
  border-bottom: 0px solid transparent;
  border-right-width: 40px;
  border-right-style: solid;
  border-right-color: #0D5E5E;
  transition: 0.2s;
}

.action-reg-button:hover {
color: #b2b2b2;
cursor:pointer;
}

.action-log-button:hover+.triangle .triangle-left {
  border-right-color: #2e2e2e;
}

button {
display: block;
}

.triangle-left{
margin-top:-10px;
}
<fieldset class="actions">

  <button class="action-reg-button">Register</button>


  <button class="action-log-button">Login</button>

  <section class='triangle'>
  <div class="triangle-left"></div>
  </section>

</fieldset>

Answer №2

Unable to achieve this in a single CSS style:

.action-log-button:hover .triange-left {
  border-right: 40px solid #2e2e2e!important;
}

Initially, you need to establish your .triangle-left as the parent block for your Login button like so:

<div class="actions">
     <p class="action-reg-button">Register</p>
     <div class="triangle">
       <div class="triangle-left">
           <p class="action-log-button" id="login-menu-button">Login</p>
       </div>
     </div>
</div>

Next, design the shape of .triangle-left as desired, where the CSS would be something similar to this:

.triangle-left {
    border-bottom: 37px solid #0D5E5E;;
    border-left: 45px solid transparent;
    height: 0;
    width: 80px;
}

Subsequently, add hover effect to this block:

.triangle-left:hover {
    border-bottom: 37px solid #2e2e2e;
}

By following these steps, you will have achieved the same shape but consolidated into one block.

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

Progress Bars Styled with CSS Percentages

I need help creating a basic CSS percentage bar. To get started, visit and paste the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://w ...

Move buttons from one group to another group

I have a task to update a group of buttons with different buttons depending on the initial button chosen. Button1: when clicked, both buttons will be replaced by Button1a and Button1b Button2: when clicked, both buttons will be replaced by Button2a ...

What is the best way to adjust the height of a side-panel to match the dynamic

I'm facing an issue where my side-panel height is not expanding along with the content of the div. While specifying a height of 100% works well for mobile view, I encounter problems in desktop view as the sidebar's height remains fixed. I want th ...

reveal or conceal content on hover of mouse pointer

I am currently working on a section that functions like jQuery tabs, but instead of requiring a click to activate, I want it to work on mouse hover. The current implementation is somewhat buggy - when hovering over a specific section, it displays as expe ...

What is hindering the responsiveness of this HTML table?

I recently designed a schedule table that resizes properly in the browser, but when I tried to access it on my mobile phone, it didn't resize correctly. On my phone screen, half of Thursday was cut off due to the horizontal scroll. https://i.sstatic. ...

Tips for customizing bootstrap's fixed navbar-default to ensure that the list items align downwards:

Is it possible to customize the bootstrap fixed navbar-default so that the li elements align downward instead of at the top? If you have any corrections or custom solutions, I'd love to see them! Feel free to share your code on platforms like CodePen, ...

Styling Your Website: Embrace CSS or Stick with Tables?

My goal is to create a layout with the following features, as shown in the screenshot: The main features include: The screen is divided into 3 regions (columns); The left and right columns have fixed widths; The middle column expands based on the browse ...

Determine the height using jQuery before adding the class

I've been grappling with jQuery to accurately calculate the height of an element and then apply a specific CSS class to adjust its height. The issue I'm facing is that jQuery seems to be executing these calculations out of order, which is causing ...

Alter the appearance of the mouse cursor when hovering over input fields in HTML

I am working with a variety of HTML elements that can be moved via Drag'n'Drop. In order to indicate to the user where these elements can be dropped, I change the cursor's appearance using CSS classes applied through JavaScript. This method ...

Difficulty in adjusting the height of the popover menu that appears when using the Select Validator in Material UI

I am having trouble adjusting the height of a popover that emerges from a select validator form in Material UI. Despite attempting various methods, including adding the following CSS to the main class: '& .MuiPopover-paper': { height: &apos ...

jquery is in motion while svg paths are at a standstill, waiting to

i have been attempting to incorporate a css-animated svg into my project. initially, the animation would start automatically, which was undesirable. after some research, i discovered that by declaring it as paused and then triggering it using jQuery with $ ...

Using JavaScript to create a search bar: Can "no results found" only show after the user has completed typing their search query?

How can I ensure that the "no match" message only appears after the user has finished typing in their search query, rather than seeing it while they are still typing "De" and the list displays "Demi"? usernameInput.addEventListener("keyup",function(){ ...

When I include scroll-snap-type: y; in the body tag, the scroll-snapping feature does not function properly

Hey there! I've been trying to implement scroll-snapping on my project but unfortunately, I couldn't get it to work. I tested it out on both Chrome and Firefox, but no luck so far. Here's the code snippet I've been working with, would a ...

A guide to entering information into an input field with JavaScript for logging in successfully

https://i.stack.imgur.com/TF51Z.gif https://i.stack.imgur.com/HHsax.png https://i.stack.imgur.com/HUztt.png When attempting to input text using document.getelement('').value="" , it doesn't behave as expected. The text disappear ...

The resizing effect in jQuery causes a blinking animation

I would like to create a functionality where, when the width of .tabla_gs_gm surpasses the width of .content, .tabla_gs_gm disappears and another div appears in its place. While this is already working, there seems to be a screen flicker between the two d ...

Instructions for activating the click function for each individual looping image

When creating a blog page, I am using PHP to loop the like button images according to the total count of posts (example: Facebook like button). The issue I am facing is that while the PHP loop is working, when I click on the first post image, only the firs ...

jQuery fails to hide DIVs when jQuery.show() has been utilized in a previous event

I've always considered myself pretty proficient in jQuery, but this particular issue has me stumped. Essentially, I have a click event that should hide one DIV (a placeholder) and show two others (an input section and control buttons section). However ...

Toggle checkbox feature in Bootstrap not functioning properly when placed within ng-view

When attempting to embed a bootstrap toggle checkbox within <ng-view></ng-view>, an issue arises where a regular HTML checkbox is displayed instead of the expected bootstrap toggle. Strangely, the same checkbox functions as a bootstrap toggle w ...

Discrepancies in button padding across desktop and mobile platforms

I have created a sample button and included a jsfiddle link to showcase the code. Due to the font properties of the specific font I'm using, I had to adjust the padding values differently for the top and bottom of the text to achieve a vertically cent ...

Maintaining Order with SCSS and Compass - Is it Possible?

Does compiled SCSS using Compass preserve the order of declarations? Can Compass guarantee the order of properties (assuming it's the way Compass operates that determines this)? This is mainly important when there are multiple definitions with the s ...