Is it possible to utilize ::before content in order to turn a div into a clickable link?

I am currently working on customizing the appearance of a Wordpress site, and I have encountered an issue with a specific DIV element that I would like to make clickable instead of using its current content. The page-builder being used on the site is making it challenging for me to achieve this easily. Before resorting to creating custom HTML content, I am exploring the possibility of modifying the module through CSS or JavaScript in a way that allows the site owner to still manage and update the content.

For instance:

.container {
width: 100%;
  height:400px;
}

.container .module-content {
display: block;
  height:150px;
  bottom:0;
  position: absolute;
width:100%;
overflow:hidden;
transition: ease .5s;
  color:white;
  text-align:center;
}

.container:hover .module-content {
display: block;
  height:250px;
overflow:visible;
}

.container .module-content .module-title {
display: block;
font-size:30px;
  height:100px;
padding:50px 25px 0;
  margin:0;
background: -moz-linear-gradient(top, rgba(66,114,184,0) 0%, rgba(66,114,184,1) 100%);
background: linear-gradient(to bottom, rgba(66,114,184,0) 0%,rgba(66,114,184,1) 100%);
}

.container .module-content .module-title a {
  color:white;
  text-decoration:none;
  }

.container .module-content .module-description {
background-color:#4272b8;
height: 50px;
  padding:25px;
  margin:0;
  color: white;
}
<div class="container"><!-- this is a 600x400 pixel box with an image for a background -->
      <div class="module-content"><!-- this is a 100px height div positioned absolutely and at the bottom with overflow hidden, when the container is hovered, the overflow becomes visible and height is 150px -->
        <div class="module-title"><h2><a href="/about/">About</a></h2></div><!-- this heading is the current link and is always visible -->
        <div class="module-description"><p>text</p></div><!-- this text is only visible when the container is hovered -->
      </div>
     </div>

In the provided image, you can see that I am able to edit the heading text (with or without a link), paragraph text, and the container background, as the content positioning is controlled via CSS overrides.

https://i.sstatic.net/Pd34H.png

Initially, I considered making the H2 <a href></a> element a block to cover the entire container, but this caused issues with the positioning of the "About" text (shifting it to the top instead of keeping it at the bottom).

Therefore, I also thought about utilizing pseudo-elements like ::before or ::after with content to create a clickable link within the container DIV... Is this approach feasible? If not, could you suggest an alternative method to achieve my desired outcome?

Answer №1

Is this a feasible option?

If my understanding is correct, you are unable to provide links for pseudo classes.

If not, what alternative method could I use to achieve my desired outcome?

One simple approach (though not necessarily the best) would be to increase the clickable area for h2 like demonstrated here:

https://jsfiddle.net/9j516L2g/6/

.clickable-area {
   display: inline-block;     
   position: relative;    
   z-index: 1;     
   padding-bottom: 20px;     
   margin-bottom: -20px; 
}
  • Using display: inline-block sets margins
  • Position must be set to relative
  • Z-index ensures the clickable area remains on top (important to consider if using other z-index values elsewhere)
  • Padding increases the clickable area size
  • Negative margin prevents disruption to surrounding text

In the provided example, an additional 20px of clickable area is added - similar adjustments can be made for left and right areas as needed, which may suit your needs well.

Please inform me if this solution works for you.

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

How can the communication be improved between a JSP page and a Java object?

In the midst of a project involving a java back-end and web page UI, my goal is to integrate the two seamlessly. The java system functions on the server, storing rules created within the web page's interface. To achieve this, I am seeking a way for te ...

How can I maintain consistent spacing between two areas in CSS3 across all screen sizes?

My question pertains to the alignment of an advertisement (on the left as a banner) and content sections on my website. I have noticed that as the screen size increases, the distance between these two areas also increases. How can I ensure that the distanc ...

What is the process of importing a JSON data file and utilizing it within a React component?

As a beginner in React, I am struggling with the concepts of importing, exporting, and rendering components. I have a fakeData.json file within the same src/components folder as the component I want to render. Let's take a look at the structure: < ...

What is the best way to extract the value from an anchor element?

To retrieve the value from an input tag, we can use <id>.value like this: <input type="text" #solutionInput (keyup)="0"><br> <solutionDetail [solutionName]="solutionInput.value" > </solutionDetail> Here, 'solutionInput& ...

Code in jQuery or JavaScript to retrieve precise node information for the currently selected form field, text, or image on a webpage

Looking to retrieve specific information about the item that has been clicked on a web page using jquery. The clickable item could be a form element (like a checkbox, text box, or text area) or a section of text within a paragraph, div, list, or image... ...

Creating animations with an svg mask can be effective, however, it seems to only work properly

I have been experimenting with creating a transition effect using SVG masks and CSS. Initially, everything was working as intended, but after a few repetitions (usually 1 or 2, unpredictably), the transition effect would suddenly stop and become instantane ...

Creating an interactive dropdown menu in react.js

For a project I'm working on, the user can input data that is then sent to a MongoDB instance. There's also a search bar where users can retrieve the data and select a specific ID to view all corresponding document information in text fields. ht ...

I attempted to set up a Discord bot using JavaScript on Replit, but unfortunately, it seems to only respond when I mention the bot specifically, rather than to any regular

I've successfully created a python discord bot, but I'm encountering issues with the javascript one. Despite trying various solutions from StackOverflow, I'm unable to understand how to get it working. The plethora of online solutions for d ...

Fade in and out a Div with a distinct class and ID

I'm currently experiencing a minor issue with some jQuery code. Below are some divs: <div class="add" id="1">Follow</div> <div class="added" id="1">Following</div> <div class="add" id="2">Follow</div> <div clas ...

Guide to vertically aligning text within a row using Bootstrap 5

I am currently struggling to achieve vertical alignment of text in the center of a row. Despite my efforts, I have not been successful in achieving the desired vertical alignment. What steps can be taken to ensure that the text is vertically centered with ...

Adding dynamic content to CSS in Next.JS can be achieved by utilizing CSS-in-JS

Currently diving into the world of Next.JS, I've managed to grasp the concept of using getStaticProps to retrieve dynamic data from a headless CMS and integrate it into my JSX templates. However, I'm unsure about how to utilize this dynamic conte ...

In what way does ReactJS enable me to utilize constant functions before they are declared?

I'm intrigued by the concept of using a value before defining it in ReactJS. Let's explore this through an example: function CounterApp() { const [counter, setCounter] = useState(0); const increaseValueTwice = () => { increaseValue() ...

Unable to show input in Javascript HTML

Every time I try to run this code on my webpage, the buttons do not seem to respond when clicked. I am aiming to have the user input for full name, date of birth, and gender displayed in the text box when the display button is clicked. When the next butt ...

Uncaught ReferenceError: The variable in Next.JS is not defined

Within the server-side code of page.tsx, I have a client-side component called SelectType.tsx. The functionality should be as follows: When the user changes the value of the select component It triggers a function inside page.tsx The function is supposed ...

Is jest the ideal tool for testing an Angular Library?

I am currently testing an Angular 9 library using Jest. I have added the necessary dependencies for Jest and Typescript in my local library's package.json as shown below: "devDependencies": { "@types/jest": "^25.1.3", "jest": "^25.1.0", ...

Accessing an item within a JSON object using jQuery

Trying to access an element within a JSON object, part of the code is shown below: { " academy": { "business": { "E-commerce": [ I have successfully accessed the academy as the first element using the following code: $.getJSON("p ...

Adding a dynamic style programmatically to an element created using createElement in Vue: A guide

I have a canvas element that is created programmatically and I am looking to apply a reactive style to it. canvas = document.createElement('canvas') //Apply style this.$refs.parentElement.appendChild(canvas) In a typical scenario, you would u ...

Using Jquery to dynamically add an active class to a link if it matches the current URL

Is there a way to modify the code below so that only the exact current URL will have the active class added? Currently, even when the URL is http://localhost/traineval/training, it also adds the active class to http://localhost/traineval/training/all_train ...

Having difficulty erasing the existing input

I'm trying to create a form input field in JavaScript that generates a specified number of additional inputs based on user input. The issue I'm facing is that while the code works and adds more input fields, it does not delete the previously gene ...

My program is able to perform calculations even in the absence of user input

I have been encountering an issue with my PHP code that is used for performing calculations. The code is designed to take inputs from textboxes and calculate the total based on those values. However, when I click the button intended for calculating the tot ...