Button to close CSS overlay scrolls to the top of the page

I have managed to create a CSS-only solution where clicking on an anchor displays an overlay image on top of a div. Everything works smoothly except for one issue: when the close button is clicked, it scrolls to the top of the page instead of closing and staying in the current position on the page. I attempted to add a div ID to the href but then the overlay wouldn't close. Is there a way to achieve this using only CSS? Any assistance would be greatly appreciated. Thank you.

.popup .close {
          position: absolute;
          top: 70px;
          right: 30px;
          transition: all 200ms;
          font-size: 30px;
          font-weight: bold;
          text-decoration: none;
          color: #333;
        }
        
.popup .close:hover {
  color: #06D85F;
}

.popup .content {
  max-height: 30%;
  overflow: auto;
}
<div id="popup1" class="overlay">
  <div class="popup">
    <a class="close" href="#">&times;</a>
    <div class="content">
      <img src="./assets/map-overlay.png">
    </div>
  </div>
</div>

Answer №1

Previously, I have tried to prevent the automatic scrolling to the top of the page when clicking on a link. One method that worked for me was using href="#!" instead of just href="#".

Answer №2

Replace the href="#" with href="#!"

.popup .close {
  position: absolute;
  top: 70px;
  right: 30px;
  transition: all 200ms;
  font-size: 30px;
  font-weight: bold;
  text-decoration: none;
  color: #333;
}

.popup .close:hover {
  color: #06D85F;
}

.popup .content {
  max-height: 30%;
  overflow: auto;
}
<div id="popup1" class="overlay">
  <div class="popup">
  <!-- Update anchor's href attribute from "#" to "#!"-->
    <a class="close" href="#!">&times;</a>
    <div class="content">
      <img src="./assets/map-overlay.png">
    </div>
  </div>
</div>

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

Preserving State in React Router Through Page Reload

I recently set up a react router in order to display my Navbar on all routes except the login page. To achieve this, I created a Layout component that ensures users are redirected back to the Login page if they are not authenticated. Currently, I'm st ...

What is the best way to verify an array of objects within an asynchronous function?

I am struggling with validating an array of objects being sent to the server. It seems that my current code is not working properly when the array is empty or if the objects within it are invalid. I'm confused about why it's not functioning as ex ...

What is the best way to include the file name and size as query parameters in Node.js?

To retrieve an image from the folder, a query needs to be passed containing the filename and dimensions like this: localhost:3000/images?filename=myImage&width=100&height=100 The initial objective is to fetch images from the designated folder, res ...

Is there a way to direct the embedded YouTube video on my HTML website to open in the YouTube application instead?

My website has embedded Youtube videos, but when viewing on a mobile browser they open in the default player. I want them to open in the Youtube application instead, as my videos are 360 degrees and do not work properly in the default browser player. ...

Adjust the dimensions of a button in Jquery Mobile using a designated class

I am in the process of creating a Mobile App using Jquery Mobile, I'm aiming for a result like this: Unfortunately, what I end up with is this: I prefer not to utilize .ui-btn in my CSS, as I have already specified custom-btn. Here's the code s ...

Creating links to other partials in AngularJS is a common task that can be achieved with

Creating a simple single page application with multiple partial pages in MVC5 has been a challenge. I have set up route configurations and controllers, but unfortunately, the links are not working as expected. When clicking on a link, instead of navigating ...

What is the best way to display the full image within the container?

The dimensions of the image are 6024x4024. I have tried hiding the overflow, but for some reason, I am unable to display the entire image within the container. Instead, it appears that the image is being zoomed in on. .section1{ background-image: ur ...

designing an uncomplicated button that incorporates the column size feature of Bootstrap

My concept involves crafting a minimalist green button with no text or border, just a sleek green shape that would automatically match the size of col-md-12 from Bootstrap. While I've come across similar inquiries, my limited understanding is hinderin ...

Is the "Angular" approach to creating responsive templates? Should matchMedia be used in the 'controller'?

I am currently working on the following: http://jsbin.com/EDovILI/1/edit Essentially, I am inserting an event listener into the controller. It doesn't feel like the most efficient way to do it, but I'm not sure how to abstract it out. The tem ...

Changes to a key value are not reflected in the array of objects

When making changes to input fields within an array of records that include Date and Text fields in a table, the data is not updating as expected. I am encountering issues where changing the Date input results in undefined Text values, and vice versa. My g ...

Tips for creating a flexible video layout using HTML

I've been trying to figure out how to make a video responsive in HTML with CSS, but no matter what I do, it just doesn't seem to work. Can someone please provide some guidance? Here is the HTML code that I am using: <div class="container ...

`Problem with the layout of the form on the website`

Experiencing some challenges with the container design for a job sheet project. As a beginner, I have been learning on the fly and following online tutorials to create functional forms. However, this particular form is not rendering correctly when viewed o ...

Div not centered after translation by 50% on the Y-axis

My HTML and body have a height of 100vh. Inside my body, I have a square that is 100px by 100px. When I apply top: 50% to the div, it moves down 50%. But when I apply translateY(50%) to the div, it does not move down 50%. Why is this? Please note that y ...

Difficulty with Angular JS` ng-repeat functionality

I attempted to create a sample similar to the ones found on https://angularjs.org/ Here is the JS fiddle I put together. Could someone provide assistance? What seems to be the issue? The HTML code is as follows: <h2> To Do </h2> <div ng- ...

Utilize the $scope variable as a JSON object for interacting with the $http service

Is there a way to extract all the data from Angular's scope service without creating an additional object? I am trying to send everything on the scope to a server using $http.post, but it seems that $scope is a circular structure, resulting in "Conver ...

Centering Tabs in React-Bootstrap

By default, Bootstrap-react's tabs align to the left. <Tabs defaultActiveKey={2} id="uncontrolled-tab-example"> <Tab eventKey={1} title="Tab 1"> Tab 1 content </Tab> <Tab eventKey={2} title="Tab ...

Utilize vue-resource for updating information in the database

Within my Vue component, I have the following data setup: data() { return { revenueChart: '', limit: 12, labels: '', datasets: '' } }, In addition, there is a method that utilizes vue- ...

Errors encountered while starting Angular due to issues in package.json configuration

Summary: Encountered an error while using 'Angular' for the first time, indicating tsc was not found in the package.json file. Details: As a beginner with Angular, I followed an example from a book and attempted to start it with np ...

Iterating through records with a set of three columns in every row

I need to divide each row into 3 columns image : https://i.sstatic.net/4phtb.jpg <div id="list-product"> <div class="row"> @foreach($products as $product) <div class="col-lg-4 ...

Interacting between Angular controllers and directives while maintaining separation of concerns

In my AngularJS project, I've implemented a directive within a module named ThreeViewer that displays WebGL scenes based on the specified source file. The code snippet below outlines the basic functionality: angular.module('ThreeViewer', [] ...