What is causing the issue with this pseudo class selector not functioning properly?

I've been experimenting with the :after selector to create an inside-border effect in every div I hover over. However, I'm facing an issue where the ":after" pseudo-class doesn't seem to work. Any insights on why this selector isn't functioning as expected? I'm keen on using both the :hover and :after pseudo-classes together!

Check out this EX

Thank you!

<body>
    <h1>Example Six</h1>
                
    <p>Grade: A</p>
    <p>This is the thing</p>

    <div class="w3c">
        <div id="tab16">
            <a href="#tab16">Cars</a>
            
            <div class="test">
                <div><img alt="" src="http://placehold.it/176x106/" /></div>
            </div>                    
        </div> <!-- ends tab16 -->        
        <div id="tab17">
            <a href="#tab17">Other Cars</a>
        <div>
            <div><img alt="" src="http://placehold.it/180x110/" /></div>
            <div><img alt="" src="http://placehold.it/180x110/" /></div>
        </div>
    </div> <!-- ends .w3c -->
</body>

The CSS:

@charset "utf-8";
/* CSS Document */
    
.w3c {
    min-height: 250px;
    position: relative;
    width: 554px;
}
.w3c > div {
    display: inline;
}
.w3c > div > a {
    margin-left: -1px;
    position: relative;
    left: 1px;
    text-decoration: none;
    color: #fff;
    background: #666666;
    display: block;
    float: left;
    padding: 5px 10px;
    border: 1px solid #ccc;
    border-bottom: 1px solid white;
}
.w3c > div:not(:target) > a {
    border-bottom: 0;
    background: #b0b0b0;
    /*-moz-linear-gradient(top, white, #eee); */
}   
.w3c > div:target > a {
    background: white;
    color: #b0b0b0;
}
                
.w3c > div > div {
    background: white;
    z-index: 0;
    left: 0;
    top: 30px;
    bottom: 0;
    right: 0;
    padding: 10px;
    border: 1px solid #ccc;
    width: 596px;
    height: 133px;
}   
.w3c  div  div > div {
    border: 1px solid red;
    float: left;
    width: 180px;
    height:110px;
    margin-left: 10px;
}
.w3c  div div > div:hover:after {
    border: 1px solid black;
    width:178px;
    height: 108px;
}
            
/* .w3c > div > div > a  > img:after {
    border: 2px solid black;
    width:178;
    height: 108px;
} */
.w3c > div:not(:target) > div {
    position: absolute;
}
.w3c > div:target > div {
    position: absolute;
    z-index: 1;
}

Answer №1

To make the :after pseudo-element visible, ensure that you assign content to it and set its display property to block.

Next, you should absolutely position the :after element and relatively position the parent div:

.custom div div > div {
    position: relative;
    border: 1px solid blue;
    float: left;
    width: 180px;
    height: 110px;
    margin-left: 10px;
}

.custom div div > div:hover:after {
    display: block;
    content: '';
    position: absolute;
    border: 1px solid green;
    width: 178px;
    height: 108px;
}

Answer №2

After much investigation, I have finally identified the root of the issue.

The proper use of ::after is to append content to an existing element, not to style elements that follow it.

To target specific elements or every x element, consider utilizing nth-child().

.w3c div div > div:nth-child(2):hover {
     border: 1px solid black;
}

Uncover the mechanics of how nth-child works here

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

Using a function on a singular element

I'm currently working on my portfolio and I would like to organize my projects in a menu similar to this felipestoker.com When you click on Works, it displays a menu with my projects categorized. However, when I click on a category, all of them appea ...

HTML5 allows users to choose data from a list using a ComboBox and submit the 3-digit code

I'm looking to enhance my form by including an input box where users can select airports, similar to the functionality on this website (). When typing in the Destination Input, I want users to see a list of possible values with detailed suggestions su ...

The header bar intrudes into the main content area

I am in the process of transitioning my landing page design from Bootstrap to Semantic-UI. The layout includes a fixed top navbar and main content split into two columns (3-cols and 9-cols) - the left column is reserved for a sidebar, while the right colum ...

Can the lazy load script dependent on jQuery be utilized before the jquery.js script tag in the footer?

After receiving HTML from an AJAX callback, I noticed that there is a script tag for loading code that uses jQuery. However, I consistently encounter the error of jQuery being undefined. All scripts are connected before the closing </body> tag. Is ...

Utilize Selenium in Python to choose an element

When working with the following HTML code: <div class="nav-category__col" id="category_nav_level_3" style="display: block;"> <input type="hidden" value="1" name="videogame_type" id="videogame_type"> <ul class="nav-category__list" adp ...

Inserting pictures onto Bootstrap SVGs

Currently working on a website using Bootstrap 5 and incorporating an element from BS5 templates: <div class="col" data-aos="fade-up" data-aos-delay="200"> <div class="card shadow-sm"> ...

Creating borders around Material UI grid items can be achieved by applying a border style to the

import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Paper from '@material-ui/core/Paper'; import Grid from '@material-ui/core/Grid'; const useStyles = makeStyles((theme) => ({ ...

Received a notification after submitting an HTML form

<html> <body> <form action="" method="GET> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> <?php if( $ ...

Why does the for loop assign the last iteration of jQuery onclick to all elements?

I've encountered an issue with my code that I'd like to discuss var btns = $('.gotobtn'); $('#'+btns.get(0).id).click(function() { document.querySelector('#navigator').pushPage('directions.html', myInf ...

Tips on concealing a div until the value of a specific field surpasses zero

In order to ensure that users focus on the first section of the form before proceeding, I plan to hide the last section until a specific field has a value greater than zero. Check out my HTML code below: <div class="fieldcontainer"> <label>E- ...

create a gentle appearance for an element

I am looking to replicate the visual appearance of each page on this particular website: There is something about the lighting that I really admire. I have attempted to recreate the animation, but I have been unsuccessful in my attempts. Any assistance wo ...

Ways to obtain jquery object for replaced DOM element

Is there a way to select the new content after using the replaceWith function in my plugin? I want to chain my plugin for the new content. $.fn.customPlugin = function() { this.replaceWith('<div>New Content</div>'); }; So, ideal ...

Load css and js files from a directory within Liferay

Is it possible to automatically load all CSS (or JS) files from a specific folder in Liferay 6.2? Below is an example of the liferay-portlet.xml file: <portlet> <portlet-name>Example</portlet-name> <icon>/icon.png</icon ...

Ensure that the text inside the button does not exceed its boundaries (utilizing Bootstrap v4)

My current code snippet appears below. <div class="col-xl-2 col-lg-12"> <button class="btn btn-secondary w-100" value=1>But.</button> </div> <div class="col-xl-4 col-lg-12"> <button cla ...

Attempt to create a truncated text that spans two lines, with the truncation occurring at the beginning of the text

How can I truncate text on two lines with truncation at the beginning of the text? I want it to appear like this: ... to long for this div I haven't been able to find a solution. Does anyone have any suggestions? Thanks in advance! ...

The combination of the card hover effect and the bootstrap modal creates complications

Hey there! I'm in the midst of crafting a webpage using Bootstrap and EJS. My aim is to craft a modal window that pops up when a specific button is clicked to display extra information, and upon clicking an X or "close" button, the modal should disapp ...

If an AngularJS Form Item is hidden with jQuery's hide() method, will it still be validated?

Even though we're in 2020, I'm still working with AngularJS 1.x instead of the newer version due to work requirements. Despite this limitation, I am facing a challenge with setting up a form that requires exclusive-or validation between two field ...

What are the disadvantages associated with the different methods of submitting data?

My goal is to create an Online testing platform. I have come across two different approaches to verify user-selected answers. Approach 1 <div class="qContainer" index="0"> Who holds the record for scoring 100 centuries in International cricke ...

What could be causing the empty space along the right edge of my website?

I've encountered an issue with my website where there's a white space on the right side of the page, causing horizontal scrolling. I attempted to resolve this by adding overflow: hidden to the CSS, which worked perfectly in Chrome. However, the p ...

How to change the background color of select options using JQuery

My webpage includes multiple select boxes with different options: <select name="item-0-status" id="id_item-0-status"> <option value="">---------</option> <option value="1">Online</option> <option value="2">Offline</o ...