Jump to content

How do we change/modify a style property within CSS body

Recommended Posts

Do you use jQuery?

$("body").css("background-color", "red");

and if you want to change more properties create an object, but notice how the formatting of each property-value pair changes.

$("body").css({"background-color": "red", "font-size": "1.2em"});


Link to post
Share on other sites

It would be very difficult to alter css rules specifically in a css style tag between <head>...</head> tag and  impossible to alter external css file using JavaScript, and if the element you where targeting already had inline styling, that is the higher precedence over styling elsewhere (style tag or css external file).

If the element has a inline style you wish to override, you would need to overwrite the current inline styling properties.

If the element has no inline styling, and you don't wont to use inline styling, which is what I gathered from your first post, you would have to add the styling you require using a class name within the css style tag, or external css file, then use JavaScript/jQuery to add the class name to the element you want to target.

Link to post
Share on other sites
  • 2 weeks later...

dsonesuk, you are right, adding a class is a reasonable solution. However, as far as I can get from a topic description, the author wants to address a <style> tag directly.

Hypotheticaly, it must look like this:

let style = document.getElementsByTagName("style")[0];

style.innerHTML = style.innerHTML + "{any selector we want to add here}";

Link to post
Share on other sites
1 hour ago, dsonesuk said:

That's impractical, It was also mentioned adjusting a css external file, how would that work? what if the op wanted to revert back, append with the same solution, or string replace? where as, all I would have to do is change class name or remove.

agree with you, adding a class is the best solution in this case

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...