Tips for resolving text center alignment problems along with other elements when hovering

When hovering over each ul li element, a new element is added to the right side of the text. The alignment of the text is centered by default, but upon hover, the alignment changes when the new element is added. However, the issue is that the alignment should not be affected when adding a new element on hover. For reference, here is the code snippet provided: https://stackblitz.com/edit/angular-emvs2v?file=src%2Fstyles.css

home.component.html

<div style="width:50%" class="text-center box">
  <ul>
    <li [class.btn-success]="selectedIndex === 1" (mouseout)="removeIndex(1)"
        (mouseover)="setIndex(1)">Home <span class="float-right test">==></span>
    </li>
    <li [class.btn-success]="selectedIndex === 2" (mouseout)="removeIndex(2)"
        (mouseover)="setIndex(2)">Contact Us <span
      class="float-right test">==></span></li>
    <li [class.btn-success]="selectedIndex === 3" (mouseout)="removeIndex(3)"
        (mouseover)="setIndex(3)">Production<span
      class="float-right test">==></span></li>
  </ul>
</div>
<button (click)="getSelect()">Submit</button>

home.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})

export class HomeComponent implements OnInit {
  data: any;
  selectedIndex = 1;

  constructor () { }

  ngOnInit () {

  }

  setIndex (index: number) {
    this.selectedIndex = index;
  }

  removeIndex (index: number) {
    this.selectedIndex = null;
  }

  getSelect () {
    this.selectedIndex = 1;
  }
}

css

ul li {
  list-style-type: none;
}

.test {
  display: none;
}

.btn-success .test {
  display: block;
}

.text-center {
  text-align: center;
}

.float-right {
  float: light;
}

Answer №1

By utilizing display:none and display:block, you effectively remove the element from the DOM, causing nearby elements to shift position.

To address this issue, consider using opacity:0 and opacity:1 instead.

You can also leverage the :hover pseudo-class to streamline your code significantly.

ul li {
  list-style-type: none;
}

.test {
  opacity: 0;
  float: right;
}

.btn-success:hover .test {
  opacity: 1;
}
<div style="width:150px;">
  <ul>
    <li class="btn-success">Home <span class="test">==></span></li>
    <li class="btn-success">Contact Us <span class="test">==></span></li>
    <li class="btn-success" >Production<span class="test">==></span></li>
  </ul>
</div>

Answer №2

To change the positioning of a new element and remove it from the normal flow, you can use position: absolute; Update your CSS code to:

ul li{
  list-style-type: none;
}
.test{
  display: none;    
}
.btn-success .test{
  display: block;
}
.text-center{
  text-align: center;
}
li {
  position: relative;
}
.float-right{
  position: absolute;
  right: 0;
  top: 0;
}

You can view a working demo by visiting this link:

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

javascript: restrict the quantity of products

As a beginner in javascript, I am currently working on creating a simple RSS reader. However, I am facing a challenge in limiting the number of feeds to 5 items, especially since the target site may have 100 or more feeds. Here's the code snippet I&ap ...

tips for showing search outcomes with Angular

I'm currently working on a Mat-Chips Angular project and I am curious about how to display the user input results. At the moment, I have a user input field and data within the TS file. Beneath the search box, my goal is to show the number of items t ...

What is the best way to create a responsive menu in code?

I am working on creating a responsive menu. Check out cuppcomputing For the desktop version, the whole panel will be displayed when it meets the max-width: 600px (tablet and mobile size). Initially, only the title is shown, and clicking on it will reveal ...

What are some alternatives to using iframes?

Currently, I am working on a project for a client where I need to display multiple websites on a single page in a browser. To achieve this, I initially used the iframe element, but encountered an issue with the openerp frame. Despite setting up the openerp ...

"Troubleshooting Issue: Why Props in mapStateToProps Aren't Updating After Running an Action in Redux

Adding some context, I utilize Redux to manage updates in a shopping cart list. Essentially, I retrieve the list, add an item to it, and then update the entire list. However, after executing an action, my cartList does not seem to update. Despite consulti ...

Angular application does not have scrollTop set

In my HTML, I have a div identified by the name #target. <div class="col-md-10 col-xs-10"> <div class="quantity-buttons" #target> .... When referencing this div in my TypeScript code, I use the following: @ViewChild('target& ...

What could be causing my LESS files not to compile properly in grunt?

I've successfully installed npm and ran npm init. Additionally, I've installed the following packages using npm: grunt grunt-contrib-less grunt-contrib-watch jit-grunt --save-dev My Gruntfile.js configuration looks like this: module.exports = f ...

Restrict the use of Shiny's unbindAll and bindAll functions to a specific HTML element

According to the Shiny site, it is recommended that before making changes to the DOM, such as adding or removing inputs and outputs, you should inform Shiny by using unbindAll and bindAll: function modifyDom() { Shiny.unbindAll() insertI ...

Images refusing to display within the div

I am attempting to insert an image onto my website, however, I am encountering issues with the code I have written: div#planet { z-index: -10; width: 450px; height: 549px; position: absolute; background: url("https://example.com/im ...

I've been attempting to access the Marketo API, but unfortunately, I haven't had any luck

I've been attempting to make a Marketo API call, but I keep encountering this issue: net::ERR_NAME_NOT_RESOLVED Here is the code snippet I'm using for the function: var marketoAPIcallURL = 'https://182-EMG-811.mktorest.com/rest/v1/ ...

Determine whether the element is visible in at least half of the viewport

I am working on a project that involves 4 cards with images. I want the image to animate in from the left when it comes into viewport. I have finalized the CSS for this effect, but the JavaScript code always returns false even when the element is visible o ...

Arranging extensive menu sections and content containment

I am currently working on enhancing my website's navigation by creating a mega menu. However, I am facing issues with the organization of the divs containing the ul content. In the fiddle linked below, you can see that "Africa", "Asia", and "Oceania" ...

Sending form submission data with useFormik via React Router's Link component is a common task in web development. By

My goal is to transfer form data generated through the useFormik and yup hooks to another component using React Router DOM's 'Link' feature. However, I have encountered two issues in this process. Issue 1: I am struggling to pass the useFor ...

Toggle the visibility of the navigation bar in Angular based on the user

I have implemented rxjs BehaviorSubject in my login page to transmit data to auth.service.ts, enabling my app.component to access the data stored in auth.service.ts. However, I now require the app.component to detect any changes made to the data in auth.se ...

The ActivatedRoute.routeConfig object appears to be empty in an Angular 2 project built with Angular-cli

Two projects I've created using angular-cli are working perfectly fine. However, in one of them, the routeConfig is showing as null and I can't figure out what's causing this issue. Both projects have identical package.json files, so there ...

Generating Dynamic HTML Tables From JSON Data in Angular

Is it possible to generate a dynamic HTML table based on JSON data containing column and row information? { "columnNames": [ "Applicant ID(id|Optional)", "Roll No", "Applicant Name", "Password", "CA ID", ...

While using Ckeditor, typing or deleting text will cause an input checkbox to become unchecked and its value to be altered

Whenever I try to make any changes or use the backspace key in Ckeditor, the checkbox gets unchecked and the value resets to 0. Despite my efforts, I have been unable to find a solution to this issue. How can I resolve this problem? CKEDITOR.replace( &ap ...

Fixing the issue of scrollbars not working in existing divs while creating new jscrollpane divs in jQuery

Utilizing the jquery.uploadfile.min.js plugin to upload multiple files results in creating a div of jscrollpane class each time a file is uploaded. However, there seems to be an issue where only the scrollbars in the last created div are functioning proper ...

What is the best way to structure an XML document?

After successfully cURLing the JSS Casper API in PHP and receiving a response in XML, I noticed that in the browser, the XML is displayed without any whitespace, all on one line. Here's an example of the XML data: <accounts> <users> ...

Tips for handling the response after a view has been submitted on Slack using Bolt.js

Hey there! I added a simple shortcut to retrieve data from an input, but I'm facing an issue after submitting the view. The response payload is not received and the view doesn't update to give feedback to the user. Check out my code below: const ...