How to Dynamically Modify CSS Width Property of :before Selector Using jQuery?

Mary-Kate Olsen
Release: 2024-11-10 04:52:03
Original
792 people have browsed it

How to Dynamically Modify CSS Width Property of :before Selector Using jQuery?

Dynamically Modifying CSS Width Property of :before Selector Using jQuery

While it's not possible to directly modify the width property of :before CSS selectors using jQuery, a workaround exists. By dynamically adding a new style element to the document's head, you can achieve the desired effect. Here's how it's done:

$('head').append('<style>.column:before{width:800px !important;}</style>');
Copy after login

This code appends a new style element to the head of the document. The CSS rules within this element target only the :before selector within the .column class, setting the width property to 800px and overriding any existing width settings.

Example and Demo

Consider the following CSS rules:

.column:before {
    width: 300px;
    float: left;
    content: "";
    height: 430px;
}

.column {
    width: 500px;
    float: right;
    padding: 5px;
    overflow: hidden;
    text-align: justify;
}
Copy after login

To dynamically modify the width property of :before for .column class elements, add the following JavaScript code:

$(function() {
    $('head').append('<style>.column:before{width:800px !important;}</style>');
});
Copy after login

This will ensure that only the :before selectors within .column elements have their width modified. A live demo of this solution is available [here](DEMO_URL).

Alternative Plugins

While the above method is a straightforward solution, you can also explore external plugins that provide direct access to pseudoclass rules. However, researching and testing such plugins is recommended before using them in a live environment.

The above is the detailed content of How to Dynamically Modify CSS Width Property of :before Selector Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template