Struggling to modify CSS properties for :before and :after pseudo-elements?

My current styling looks like this:

section.tipos .content > div:before {
    background: none repeat scroll 0 0 #DDDDDD;
    content: " ";
    height: 10px;
    left: 32%;
    position: absolute;
    top: -10px;
    width: 10px;
}

It's working perfectly, but I need to change the left property percentage when certain events occur.

I understand that I could achieve this by adding classes or inline styles. However, my boss prefers not to use unnecessary class names unless absolutely necessary.

The question is: Is it possible to modify the CSS of a pseudo selector using JavaScript? I attempted something like the code below, but it didn't work as expected.

$('section.tipos .content > div:before').css({ 'left' : 50+index*17});

Answer №1

Sorry, but it is not possible to access the :before and :after pseudo-elements in your Javascript code.

A potential workaround would be to use Javascript to change the class of the main element and then have separate CSS rules for the :before pseudo-element based on the class applied.

You could implement CSS like this:

section.tipos .content > div.newClass:before {
    ....
}

Keep in mind that setting a calculated width may be challenging with this approach. Utilizing CSS calc() function might be helpful depending on the context of the index being used and the browser support required.

Answer №2

One way to enhance the css style is by adding '%' at the end:

$('section.classes .content + div:before').css({ 'right' : 80+count*20 + '%'});

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 to use Laravel to upload images using Ajax

My database table structure for users is as follows: id (integer) firstname (string) lastName (string) email (string) login (string) password (string) image (text) I want to be able to insert an image along with the user data using Ajax and jQuery. How ...

Learn the process of directing to a view in a jQuery AJAX call with Spring MVC

Below is the ajax call I am using: $.ajax({ type: "POST", url: contextPath +"/action", cache:false, dataType: 'text', data: {Id:Id}, success: funct ...

Cutting off the final character in CKEditor for AngularJS

I have come across an issue with my web page that utilizes CKEditor for creating message edit panes. The problem arises when I try to send a message and the content retrieved from CKEditor is missing the last letter. Below is a snippet of what I believe t ...

Exploring the Power of Jasmine Testing with Ternary Conditions

Imagine a scenario where we are working with the following snippet of JavaScript code. object = _.isUndefined(object) ? '' : aDifferentObject.property; Is it possible to write a Jasmine test that covers both scenarios? Do we need to use two se ...

Challenges with saving a segment of an AJAX GET response (in JSON) as a string variable

I am facing an issue while attempting to save a section of the result obtained from a GET request through AJAX into a string variable. Essentially, my goal was to ensure that a specific function containing a GET request operation can return the outcome of ...

JavaScript: Collection of Pictures

I've recently begun my journey into HTML and JavaScript by working on a basic website that showcases four images using a for loop. However, upon viewing the website in a browser, I noticed that only the names of the images are displayed, not the actua ...

Dealing with issues of toggling visibility with jQuery when using a dropdown menu

Inside a hidden drop down div, the focus is on "DC" right now. In the image below, we are looking at sales: HTML: <td class="edit"> <select class="touch" style="display: none;"> <option value="11">Rebuying</option><option value ...

CSS Troubleshoot: Unable to Change Background of Current Menu Page Item

I've been attempting to add a small graphic using CSS to indicate the current page the viewer is on, but for some reason, it's not highlighting properly. Below is my code: HTML: <ul class="side-nav"> <a href="http://www.cieloazulsant ...

CSS table row border displaying irregularly in Chrome and Internet Explorer

I recently created a basic table with bottom row borders. Surprisingly, the borders display perfectly in Firefox but only partially in Chrome and IE 10: <div style="display:table; border-collapse: collapse; width: 100%"> <div style="display:table ...

Is there a way to arrange the cards in a row so they fill up the grid before moving on to the next row?

After retrieving data from a table in my database, I am displaying it on the screen as cards. However, the issue is that all the cards are displaying on the left side of the screen in a single column, rather than appearing in rows of 3 as desired. Below i ...

Filtering a collection using another collection in Backbone: tips and tricks

I am managing two distinct collections: Collection X includes element1, element2, element3, element4. Collection Y consists of element2, element3. For illustration purposes: var element1 = new models.ExModel({id: "1", name: "element1"}); var elemen ...

Encountering a Typescript error while attempting to iterate through Enum keys for generating JSX components

I'm really struggling with this problem. Here's a simple enum I have: export enum depositTypes { ACH = 42, Wire = 36, Check = 3, Credit = 2, } I'm trying to create option tags for a select element, like so: Object.keys(depositTyp ...

The alignment of the background images is off

I am attempting to give a container a background image and then create two div tags using the col-md-6 classes from Bootstrap. The main div's background image is of grass texture, while the two inner divs have a skeleton of a football court as their b ...

Transform an item into a map of the item's properties

I am faced with an object containing unknown key/value pairs in this format: myObj = { key_1: value_1, key_2: value_2, key_n: value_n } My goal is to transform it into a dictionary of structured objects like the one below: dictOfStructureObjec ...

Executing cssnano Using the Command Prompt

Although I'm not an expert in node JS, I have come across cssnano as a JavaScript tool for minifying CSS, which apparently does a more sophisticated job compared to the outdated YUI compressor. My only issue is that I am struggling to understand how t ...

During post-processing, the elimination of lens flares can sometimes lead to an error known as "copyTexImage2D: framebuffer is

After looking at the lens flares example here: , I'm encountering an issue with post-processing this particular scene. The blocks display correctly, but the light sources and lens flares are missing. Additionally, I am receiving several warnings in t ...

What is the best way to transfer Express.js variables to MongoDB operations?

I have been developing a blogging application using Express, EJS, and MongoDB. Feel free to check out the GitHub repository for more details. One of the features I've implemented is a simple pager for the posts within the application. Within the pos ...

Performing Jasmine unit testing on a component that relies on data from a service, which itself retrieves data from another service within an Angular 2+ application

Attempting to implement unit testing for a service using httpmock has been challenging. The service in question utilizes a method to make http get calls, but I have encountered difficulties in writing the test cases. saveservice.service.ts -- file const ...

Using AngularJS, learn how to populate array objects within a JSON object

I'm trying to load array objects from a multi-select control, then populate a model object called "name" with its name and age values. After that, I want to load an array from a select element into the model object. However, the ng-model directive on ...

What is the best way to use ajax with jquery for file uploading?

Is it possible to use jQuery and ajax to upload an image with this code or something similar? jQuery.ajax({ type: "GET", url: "/userinfo/update/", dataType: "json", data: { ...