How can I apply styling to a hyperlink that links to a specific anchor in HTML?

I’m currently dealing with anchor links positioned at the top of my page that navigate to scroll-out infoboxes located at the bottom. When a user clicks on one of these anchors, it directs them to the corresponding box below; however, I also want these boxes to automatically expand upon clicking.

Is there a solution to this issue?

The opening and closing of these boxes are controlled by the following CSS properties:

Closed state:

.collapsible-factuur-content {
    max-height: 0px;
    overflow: hidden;
    transition: all 0.4s ease-out;
}

Opened state:

.toggle:checked+h3+.collapsible-factuur-content, .toggle:checked+h5+.collapsible-factuur-content {
    max-height: 300vh;
    margin-top: 1em;
    margin-bottom: 2em;
    padding-left: 15px;
    padding-top: 15px;
    padding-bottom: 15px;
}

To summarize, the anchor should link to the box below #section-details-general and apply the specified CSS values for the opened state mentioned above.

#overlay {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        background: #99999945;
        width: 100%;
        height: 100%;
        opacity: 0.8;
        z-index: 100;
        transition: all ease-in-out .2s;
    }

    #popup {
        display: none;
        position: absolute;
        top: 25%;
        background: #fff;
        width: 80%;
        margin-left: 10%;
        z-index: 200;
        border-left: 4px solid #def2a6;
        box-shadow: 0 2px 4px rgb(0 0 0 / 16%);
        border-radius: 4px;
    }

    #popupclose {
        float: right;
        padding: 2px;
        cursor: pointer;
    }

    .popupcontent {
        padding: 10px;
    }

    .popupcontent p {
        margin-left: 20px;
        margin-top: 10px;
        color: #586874;
    }

    #button {
        cursor: pointer;
    }

    .textdetails ul {
        list-style-type: disc;
    }

    .textdetails section {
        padding-top: 0;
        padding-bottom: 20px;
    }



    .textdetails label {
        cursor: pointer;
        transition: all 0.1s ease-in;
        display: flex;
        align-items: center;
    }

    .textdetails label.gp-product {
        width: 100%;
        height: 80px;
        background: #def2a6 0 0 no-repeat padding-box;
        box-shadow: 0 2px 4px rgb(0 0 0 / 16%);
        border-radius: 4px;
        display: flex;
        align-items: center;
        position: relative;
        cursor: pointer;
        text-align: left;
        font-family: Rubik;
        font-weight: 700;
        color: #005e75;
        padding: 16px 0 16px 72px;
        line-height: 20px;
    }

    .textdetails label.gp-product i {
        font-size: 32px;
        margin-left: -45px;
        margin-right: 50px;
        width: 30px;
        display: flex;
        justify-content: center;
    }

    .textdetails label.gp-product:after {
        width: 24px;
        height: 24px;
        display: flex;
        justify-content: flex-end;
        flex-grow: 1;
        font-size: 24px;
        color: #005e75;
        content: "";
        font-family: Font Awesome\ 5 Pro;
        font-weight: 900;
        transform: rotate(0deg);
        transition: all .3s ease-in-out;
        right: 30px;
        position: absolute;
    }

    .textdetails label span {
        flex: 1;
        margin-left: 10px;
    }




    .textdetails input[type="checkbox"] {
        display: none;
    }

    .collapsible-factuur-content {
        max-height: 0px;
        overflow: hidden;
        transition: all 0.4s ease-out;
    }

    .toggle:checked+h3+.collapsible-factuur-content,
    .toggle:checked+h5+.collapsible-factuur-content {
        max-height: 300vh;
        margin-top: 1em;
        margin-bottom: 2em;
        padding-left: 15px;
        padding-top: 15px;
        padding-bottom: 15px;
    }
<p style="margin-bottom: 500px;">
    <a class="button btn--tertiary" href="#section-details-general">Link to the block</a><br>
  <mark>👆this is an anchor to the block below, which should also be opened up upon clicking.</mark><br><br><br>
    <mark>In the actual enviroment there is quite a big space in between these two blocks (with images, text etc)</mark>

</p>






<section class="textdetails" style="margin-bottom: 250px;">

    <!-- Details general -->
    <section id=" section-details-general"><input class="toggle" id="collapsible-details-general" type="checkbox"
            data-hj-masked="">
        <h3>
            <label class="gp-product" for="collapsible-details-general">
                <i class="fas fa-info-circle"></i>
                <h4>Open me</h4>
            </label>
        </h3>



        <div class="collapsible-factuur-content" id="details-general">
            <p><mark>More info here</mark></p>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras luctus nunc ac arcu venenatis finibus id
                consectetur odio. Nam eget mauris efficitur, laoreet augue vel, efficitur arcu. Nullam sed arcu suscipit
                massa sodales ultrices vitae sodales odio. Quisque nisi orci, rutrum sit amet porta malesuada, commodo
                at mi. Aliquam molestie purus in ullamcorper rutrum. Nullam mollis dui sed arcu bibendum lobortis vitae
                in quam. Phasellus faucibus odio tellus, a placerat nunc malesuada sit amet. Morbi ornare sagittis
                rhoncus. Proin sagittis, nunc eget porttitor placerat, ipsum tortor elementum mi, sed mollis libero
                magna efficitur velit. Donec sed ultrices enim. </p>
        </div>
    </section>


</section>

Answer №1

For a CSS-only solution utilizing the :target pseudo-class to select an element based on its id matching the URL fragment is recommended:

#overlay {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  background: #99999945;
  width: 100%;
  height: 100%;
  opacity: 0.8;
  z-index: 100;
  transition: all ease-in-out .2s;
}

#popup {
  display: none;
  position: absolute;
  top: 25%;
  background: #fff;
  width: 80%;
  margin-left: 10%;
  z-index: 200;
  border-left: 4px solid #def2a6;
  box-shadow: 0 2px 4px rgb(0 0 0 / 16%);
  border-radius: 4px;
}

...
...(remaining CSS code)
...

.toggle:checked+h3+.collapsible-factuur-content,
.toggle:checked+h5+.collapsible-factuur-content,
:target .collapsible-factuur-content {
  max-height: 300vh;
  margin-top: 1em;
  margin-bottom: 2em;
  padding-left: 15px;
  padding-top: 15px;
  padding-bottom: 15px;
}
... ...(remaining HTML code) ...

If not using CSS, you can implement JavaScript to automatically check the checkbox to open up the panel upon click event as shown below:

document.body.addEventListener('click', ({ target }) => {
   const link = target.matches('a[href^="#"]') ? target : target.closest('a[href^="#"]');
   if (link) {
     const toggle = document.querySelector(`${link.hash} [type=checkbox]`);
     if (toggle) {
       toggle.checked = true;
     }
   }
 });
... ...(remaining CSS and HTML code) ...

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

Utilizing ReactRouter via CDN without needing npm

As someone new to React, I have been practicing by directly incorporating the react.js and react-dom.js files along with browser.min.js for JavaScript transformation into my HTML page. Now, I am looking to utilize react-router and have added the ReactRoute ...

Reasons behind the lack of proper styling in my p-calendar from PrimeNG

Currently deep diving into Angular for a project and I decided to incorporate the <p-calendar> component from primeng. After installing the necessary packages, the versions were: "primeicons": "^2.0.0", "primeng": "^5.2.7", Initially everything was ...

Guide on assigning Django variable to an HTML element's id

Currently, I'm populating numerous elements from a Python dictionary in my code snippet. The dictionary structure resembles something like {"id", "content", "id2": "content2"}. This data is then passed to an HTM ...

When you hover over nested HTML-lists in a webpage, make the tree's long text lines expand gracefully using a combination of HTML, CSS

In my Angular 4 application, I have a left div that displays a tree of items as recursive HTML lists. When there is a long text, it gets cut off by the border of the div and a scrollbar appears. I want to have the text expand beyond the border and show in ...

Is there a way to create a layout with three columns of divs, where the middle column is shifted, that automatically collapses into two columns with the second one shifted when the window size is reduced?

For the about page of my website, I have a grid layout consisting of three columns. The challenge is to offset the center column by 50%. When the window size is reduced beyond a certain point, I want the third column to disappear and its images to be distr ...

Troubleshooting a Node.js server issue with a two-dimensional array

I am currently facing an issue with submitting a form that contains two-dimensional array fields on a post request in node.js. The problem lies in the fact that the server side is receiving a one-dimensional array with all the values combined. Below is an ...

Enhance Your Page with a Widget Using Bootstrap

I am looking to incorporate a basic text box on the side of my webpage. https://i.sstatic.net/qzwIh.png Currently, I have a table on the page. Upon adding the widget, I need the table to reduce its width. ...

Enhancing the Alignment of a Bootstrap Navbar with CSS

Having trouble centering the listed items (excluding the image and title) in the middle of the navbar. Tried various solutions with no luck! Any suggestions on how to tackle this issue? <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a ...

Cross Browser Compatibility for CSS Page Scrolling to the Right and Handling White Space/Margins

Currently in the process of developing a WordPress Theme at . Struggling with achieving equal left and right margins around the center container, resulting in the page displaying white space and allowing unnecessary scrolling. Seeking advice from anyone wh ...

React - A guide on accessing the scroll position of a div using JavaScript

Within my side navigation bar, there is a title and other information. The title is fixed in place (sticky) and the sidebar has a height of 100vh. I am looking to add a box-shadow effect to the title div (sticky-title) only when the user scrolls down past ...

Is it possible to track the status of an audio download within Howler.js?

Currently, I am utilizing howler.js to incorporate audio playback on my website. However, since I am obtaining the audio files through direct links, the loading times vary based on the length of each audio track. Is there a method available to track the pr ...

Add a .handlebars or .hbs file to an HTML document

When visiting the emberjs.com homepage, you will find an example of a todo list using Ember.js and Handlebars.js. Within the todo list, there is an extension for a .hbs file. I am curious - what exactly is a .hbs file? And how can I include a .hbs script ...

Transform an html content into a Java statement using "out.println" function

Is there a way to transform an HTML string into a Java "out.println" statement using Java? For example: <h1>Hello world</h1><p style="background-color:red">hello</p> can be converted to out.println("<h1>Hello world</h1& ...

influence the order in which dom elements are loaded when the site is loaded

I am seeking advice on how to control the loading sequence of elements in my website. The issue I am facing involves a curtain effect (a <div> with a background-image) overlaying my site, which triggers an animation function to remove it once the pag ...

I'm having trouble getting my PrimeNG Calendar component to line up correctly

::ng-deep p-calendar.calendar-control > span > input.p-inputtext { border: 1px solid black; background: #eeeeee; text-align: center !important; } The content is not properly centered. <p-calendar dateFormat="mm/dd/yy&qu ...

What is the best way to retrieve the value of a JavaScript variable and display it within an HTML label tag

In my JavaScript code, I'm attempting to retrieve location coordinates using the onchange event and then display those coordinates in a label. I've tried alerting the coordinates successfully, but I'm struggling to update the label using doc ...

Developing a Customized Filtering Mechanism in Angular 8

I have some experience working in web development, but I am relatively new to Angular. My current project involves creating a simple filter for a table's column based on user input. However, I'm facing an issue where typing in a single letter fil ...

I am attempting to generate a QR code using the Google Chart API. I am looking to input text and transform it into a QR code

Attempting to generate a QR code using the Google Chart API. Taking text as input and converting it to a QR code. Struggling with the code provided, not an expert in this area. Any assistance would be greatly appreciated. Thank you. <!DOCTYPE html> ...

What is the best way to develop an expanding line animation from this starting point?

I have a vision for an animation where, upon hovering over an icon, it enlarges, increases in opacity, and reveals some accompanying text. The visual effect I am aiming for involves two lines of color expanding horizontally from the center outwards before ...

Problem aligning ul within div element

I'm having trouble aligning my ul element in the center of a div. I tried using the margin: 0 auto method, but it's not working as expected. I've always wondered if setting the width of the ul within a div is necessary for using margin: 0 au ...