CSS Tips: Defining Pseudo-Element Sizes

Currently, I am trying to adjust the width of the :before pseudo-element in order to match it with the dimensions of the element itself, specifically an anchor tag. This is part of my goal to create an overlay that completely covers the entire element. As an example, let's consider Google's web page:

a.gb_g::before {
    background-color: rgba(255, 0, 0, 0.2);
    position: absolute;
    content: 'hello';
    display: block;
    position: absolute;
}
<div class="gb_h gb_i">
      <a class="gb_g" data-pid="23" href="https://mail.google.com/mail/?tab=wm&amp;ogbl">Gmail</a>
</div>

However, the current outcome looks like this:

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

I have tried setting width: 100%, but encountered container overflows. Is there a way to perfectly align the pseudo-element's width with that of the element itself (in this case, the 'Gmail'/'Images' link)? The desired result is to have the overlay fully covering each anchor tag, without modifying any CSS properties of the element directly.

Essentially, I am dealing with a fixed web page layout where I cannot control the existing styling. I want to highlight certain parts of the web page, including specific links, without impacting the overall layout significantly. That's why I'm exploring a solution based solely on pseudo-elements.

Answer №1

Are you seeing results with this approach? I found that adding "position: relative" to the element and setting "width: 100%" for the pseudo element resolved the issue for me.

a.gb_g::before {
    background-color: rgba(255, 0, 0, 0.2);
    position: absolute;
    content: 'hello';
    display: block;
    position: absolute;
    width: 100%;
}

a.gb_g{
    position: relative;
}
<div class="gb_h gb_i">
      <a class="gb_g" data-pid="23" href="https://mail.google.com/mail/?tab=wm&amp;ogbl">Gmail</a>
</div>

Answer №2

Perhaps this solution will meet your needs: By adding "position: relative" to the actual element and setting "width: 100%" for ::before, you can achieve the desired effect. Utilize "overflow: hidden" and "white-space: nowrap" to effectively contain the pseudo element.

a.gb_g {
    position: relative;
}
a.gb_g::before {
    background-color: rgba(255, 0, 0, 0.2);
    position: absolute;
    content: 'overlay text long';
    color: white;
    display: block;
    width: 100%;
    overflow: hidden;
    white-space: nowrap
}
<div class="gb_h gb_i">
  <a class="gb_g" data-pid="23">Original Text</a> Next
</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

What is the best method to retrieve duplicate fields from a database in HTML5?

this.db.transaction(function (tx) { tx.executeSql('SELECT tsk.*, cont.firstName, cont.lastName ,cont1.firstName, cont1.lastName, list.listId FROM tbl_tasks tsk, tbl_contacts cont,tbl_contactequests cont1, tbl_lists list WHE ...

An error occurred when executing Selenium through Python due to an invalid selector issue

While attempting to save a document, I encountered an issue. Using Firefox and the Firefox IDE, the following target worked perfectly: css=div.ui-dialog-buttonset button:contains('Yes, ') However, when trying to implement it in Python with the ...

Tips for retrieving the current value of an input using AJAX

Currently, I am in the process of developing an e-commerce website using Laravel. One issue I am facing is capturing the new value of an input when it changes. This input is located on the cart page where users have the option to adjust the quantity of a p ...

occupying half of the screen

Having trouble displaying two videos in an ionic grid. My goal is to have both videos take up the entire screen, with each occupying 50% height and 100% width. However, when I try to achieve this, the ion-row only takes up 50% of the screen and the video i ...

Issues with ng-click functionality not activating on <li> HTML elements

I've been attempting to add ng-click functionality to my list, but it's not working as expected. I've tried adding the ng-repeat directive and also without it on li elements. Here is the snippet of HTML code: <ul class="nav nav-tabs"&g ...

Using Jinja2 to iterate through a dictionary while having the ability to choose which key-value pair to access

I'm attempting to generate an HTML table from data received on the web as a dictionary app.py: client = boto3.client('ec2') vpc_ids = client.describe_vpcs() for i in vpc_ids.get('Vpcs'): for tag in i.get('Tags'): ...

What is the importance of chaining selectors in order to utilize flexbox effectively?

Check out this code snippet: .flex-container { border: solid 1px purple; display: flex; } .flex-container div { border: solid 1px red; padding: 20px; margin: 20px; height: 100px; flex: 1 1 0%; } .flex-container ...

Utilizing the require pattern to integrate JavaScript functionality into HTML

Have: project |- consume_script.js |- index.html Need index.html to be like: <html> <head> </head> <body> <script src="consume_script.js"></script> </body> </html> Issue: consume_script ...

An effective way to emphasize the full area of a list item when hovering over it

Is there a way to modify the hover behavior of a list item in Fiddle? I'd like the black highlight to cover the entire area of the list item, without leaving any white space on top or bottom. Currently, it only covers the center area of the list item. ...

Python code for clicking a button using Selenium

I'm having trouble closing popup windows in Selenium with Python. The button labeled "joyride-close-tip" is functioning correctly and closes the window, but the other two buttons are not working. What could be causing this issue? Even though I copied ...

Conceal the <p> element when the user interacts with the internal href

After creating this document using JQuery, whenever the user clicks on different internal links, a div with id = "change" is loaded which effectively "erases" the content. My challenge at the moment is that while images are successfully deleted, text rema ...

Tips for getting rid of the excess space surrounding an image within a div tag

Placing images inside bootstrap card divs within col divs is my current challenge <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="row"& ...

How can I make sure to consider the scrollbar when using viewport width units?

I've been working on developing a carousel similar to Netflix, but I'm facing an issue with responsiveness. I've been using a codepen example as a reference: Check out the example here The problem lies in the hardcoded width and height use ...

Tips on aligning objects within div elements

I am currently in the process of learning how to create websites. Below is the body tag of my website: <body> <h1 id="title">Title</h1> <div id="loginContainer"> <img id="usernameIcon" src="assets/images/usern ...

In the world of HTML5, you can find the <main> nestled within the <

Plot: As I was creating a coupons website, I came across the issue of incorporating non-unique content into the <main><article> ..here..</article></main>. Problem: Despite w3schools' statement : The content inside the eleme ...

Tips for setting the textfield value in JSP using Javascript

I am in need of a way to create a random number that will be displayed in the textfield once the user clicks on the "Generate" button. Can someone guide me on how to assign the value of the "output variable" to the "randomNum" textfield? Sample HTML code: ...

Using jQuery to enable scrolling and dynamically changing classes

I'm currently working on enhancing my website with a feature that changes the opacity of the first section to 0.4 when a user scrolls more than 400 pixels down the page. I attempted the following code without success: if($("html, body").offset().top ...

How to create a slideToggle effect for nested ul and li elements

Initially, everything was working fine until I put it through the W3C validator. After fixing the errors, now it's not functioning as expected. I have created a jsfiddle The setup involves nested ul elements being used as dropdowns, with headers tha ...

What is the best way to utilize nav and main-nav in CSS?

Working with HTML and CSS <nav class="nav main-nav"> </nav> Styling with CSS main-nav li{ Padding:0 5% } I am a beginner in HTML and CSS and facing an issue. I noticed that when using main-nav in CSS, I'm not getting the de ...

The Spotify List items generated from stored localStorage information are showing as empty

I'm currently developing an app for Spotify and I am facing some challenges while trying to create a views.List object using data stored in our database. Initially, I make a POST request to retrieve the required information and store it in local stora ...