Manually refreshing the CSS top position is necessary

While working on my webshop, I encountered a peculiar bug.

I am trying to create a cart "badge" for easy access to see the number of items in the cart. The badge is located within an a tag with two display: block spans inside. On desktop, the badge's CSS looks like this:

.count::after{
  content: "2";
  display: inline-block;
  background: #FF0000;
  border-radius: 20%;
  padding: 0 0.5em;
  transform: scale(0.7);
  color: #FFF;
  float: right;
  position: absolute;
  top: 5px;
  right: 1.5em;
}

.count::after:empty{
  display: none;
  padding: 0
}

Everything works well on desktop as the badge displays in the upper right corner. However, on mobile, the a tag's wrapper expands to full width and using the same CSS causes the badge to fly off to the side of the screen.

So, I adjusted the code for mobile devices:

@media max-width: 990px{
    ...
    .count::after{
        position: relative;
        top: -90%;
        right: 0;
    }

}

Unfortunately, the top: -90% adjustment doesn't work properly. When I toggle it off and on in the development tools, it functions perfectly. But upon refreshing, it reverts back to the bottom of the icon.

To see the issue in action, check out this JSFiddle: here

Answer №1

I completely agree with Shahil's suggestion to apply the position:relative property to the element containing the .count class, and then set the position:absolute property along with top:-5px (for example) to the .count::after pseudo-element.

Here is how you can implement it:

.d-block{
  display: block;
}
.d-inline_block{
  display: inline-block;
}
.t-center{
  text-align: center;
}
.menu{
  width:100%;
  text-align: center;
}
.count {
  position:relative;
}
.count::after{
  content: "2";
  display: inline-block;
  background: #FF0000;
  border-radius: 20%;
  padding: 0 0.5em;
  transform: scale(0.7);
  color: #FFF;
  float: right;
  position: absolute;
  top: -5px;
  right: 0;
}
<div class="col-xs-12 col-md-4 my-auto menu t-right">
                <a href="/account/login" class="d-inline_block headerLink">
                  <i class="fa fa-lg fa-user d-block t-center py-8">icon</i>
                  <span class="d-block t-center">Min konto</span>
                </a>
                <a href="/pages/kundeservice" class="d-inline_block headerLink"><i class="fa fa-lg fa-question d-block t-center py-8">icon</i><span class="d-block t-center">Kundeservice</span></a>
                <a href="/cart" class="d-inline_block headerLink count"><i class="fa fa-lg fa-shopping-cart d-block t-center py-8">icon</i><span class="kurv d-block t-center">Min kurv</span></a>
              </div>

This solution should be responsive for both desktop and mobile views without the need for @media queries.

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

An alternative to Firebug for Internet Explorer

Can anyone suggest a suitable replacement for Firebug that is compatible with IE 7 and 8? I need a tool that allows me to edit CSS/HTML in real-time, debug JavaScript code, and visualize the positions of elements on web pages. Appreciate any recommendati ...

In Firefox 50.0, the button's resizing feature causes the parent div container outline to adjust size

I am currently working on developing a responsive design with the feature of having a button that appears to "float" under a div element. My desired outcome is achieved in Opera and Chrome: https://i.sstatic.net/Vm55V.png In Firefox, the result is not as ...

Having trouble locating and interacting with the textarea element in Salesforce using Selenium Webdriver

Encountering an issue with the Selenium Webdriver where it throws an error stating that the element is not visible and cannot be interacted with when attempting to access a textarea. 1. The textarea is located within a pop-up window, which can be accessed ...

Troubleshooting a Navbar Issue in a Custom Bootstrap Template

I am a beginner in html and bootstrap, but I recently attempted to create my own bootstrap template. Initially, I tried to work with the "starter template" code from getbootstrap.com, but after launching the code and making some changes, the navigation bar ...

Creating a full width and height slider with a header menu that is responsive to all device screen sizes

Looking to create a full-width slider with a header that adjusts for all screen sizes, especially larger devices. I've been searching online but haven't found a satisfactory solution yet. If anyone has any ideas or tips on how to achieve this, pl ...

How can I evenly align each <td> in a Bootstrap table?

Looking at the image below, you can observe that the red lines indicate the uneven width of each <td>. My objective is to align each <td> evenly. How can I achieve this? CSS .stickyFooter { background: #c1c1c1; positio ...

Having trouble locating an element using Selenium?

I'm attempting to extract the price of a flight from Google Flights website using Selenium, but I am unable to locate the specific element on the page. Even after scraping the entire page, the element remains elusive. It has been suggested that it may ...

Issues with CSS z-index in negative values not functioning correctly

Help! I'm encountering a perplexing z-index issue on my website. Check out the problem here: Upon closer inspection of each step's header, you'll see that only step 3 features a ribbon while steps 1 and 2 do not (if this isn't visible, ...

Having trouble implementing an onClick event to change a button's CSS to href in Vue.js

When working with a SinglePageApp and using UIKit (UKit), I have found that I need to use a <button> tag instead of an <a> tag. Previously, the <a> tag was written like this: <a type="button" class="uk-button uk-button-link" href="#e ...

Connecting an external SVG file to its corresponding HTML code

Can I connect my SVG file to my CSS file for a line animation? I'm uncertain about including the SVG code directly in my HTML file. ...

Navigating to a new webpage element within a div class using Selenium

Struggling with selecting an element on a webpage using Selenium? Despite my experience with this website and methods like css selector and XPath, I can't seem to pinpoint the element in a troublesome section. The challenge arises when I attempt to in ...

Passing and showcasing JSON information from one webpage to another upon clicking by utilizing local storage

I've been experimenting with a way to transfer JSON data from one page to another by using local storage. For instance, when a user clicks on a specific book image, they are directed to book.html where the image and book name are displayed (retrieved ...

Is there a way to style alternate iframes using CSS?

I need to select every second iframe in the image provided. Is it possible to achieve this using nth-child? I have tried with the following code (unsuccessfully): #main iframe:nth-child(2n+2) ...

Exploring the ID search feature in JavaScript

I'm facing an issue with implementing a search feature in my project. Here is the HTML snippet: HTML: <input type="text" id="search"> <video src="smth.mp4" id="firstvid"> <video src="smth2.m ...

Automating item selection using Selenium in Python

Currently, I am in the process of developing a software program to automate web database management. However, I have encountered an issue while trying to manipulate a table where I need to arrange the items based on one of the columns by clicking on the co ...

Attempting to show an image in an alternative DIV element upon hovering with the mouse

I'm currently working on a dropdown menu with a list of links. Within the drop-down menu, I want to display different images in a separate tag based on a mouseover event. Is it possible to achieve this using CSS without relying on JavaScript? I need a ...

automatically dragging elements using jQuery UI

When implementing jquery UI draggable, I incorporate certain functionalities within the drag function. One specific task is scaling the dragging element based on its position. I am looking to automate the dragging of elements to specific coordinates (x, ...

Placement of "like" and "reblog" options within tumblr

Having issues with the button placement on my tumblr page. The Like button is in the correct position, but the Reblog button keeps appearing at the beginning of the post. On subsequent posts, it even extends outside the post block. Additionally, the text a ...

Unable to view flash elements in HTML using Django framework

Apologies for any errors in my English, I will do my best to explain clearly. I am new to working with Django and I have a html page with flash content called map.html. I would like to include this page into another page called soporte.html (which extends ...

Identify modifications in the input and at the same time update another input

I currently have two input text boxes. My goal is to synchronize the content of the second input box whenever the first input box is changed. I attempted to achieve this using this code snippet. However, I encountered an issue where the synchronization on ...