0

I try to append style into webpage through console tab of Chrome browser with the code like this.

$('head').append("<style>.hova {border-color: red !important;border-style: solid !important;border: 1px;}</style>");
Sy Loc
  • 64
  • 5
  • Possible duplicate of [How to append a jQuery script to head tag](https://stackoverflow.com/questions/34004667/how-to-append-a-jquery-script-to-head-tag) – Hitmands Apr 10 '18 at 11:31
  • 2
    Not every page has jQuery loaded, in which case either the brower-specific function, or a different defined function with `$` as name is used. – Kevin Drost Apr 10 '18 at 11:32
  • Possible duplicate of [How can I add a CSS class to a page with jquery?](https://stackoverflow.com/questions/12219188/how-can-i-add-a-css-class-to-a-page-head-with-jquery) – Calvin Nunes Apr 10 '18 at 11:33
  • Also some page, like Stack Overflow or JSFiddle use `iframe` to show your code snippet, so be aware you should check `iframe` head not page. – Pedram Apr 10 '18 at 11:34
  • If you append it to every page, why not just put it in your style sheet? By using js, this style will not be applied until the js has loaded and run – Pete Apr 10 '18 at 11:37
  • check if jQuery is loaded. type this in console - `typeof jQuery` should return "function". – Awol Apr 10 '18 at 11:44
  • Thanks guy but it is not duplicate because the problem is the " – Sy Loc Apr 11 '18 at 02:40
  • 2022 - just tried this in Chrome with jQuery 3.6 and it "does the right thing" - works like a charm. Just FYI – YePhIcK Aug 19 '22 at 17:15

5 Answers5

2

You have to use the native functions instead of jQuery, if you want to do it to every page you visit:

var style = document.createElement('style');
style.innerText = '.hova {border-color: red !important;border-style: solid !important;border: 1px;}';
document.head.appendChild(style);
Kevin Drost
  • 383
  • 4
  • 7
0
$( "<style>body { background: black; }</style>" ).appendTo( "head" )

use appendTo like this.

Awol
  • 169
  • 6
  • It does not work like my script because some page missing jquery. The script of Kevin Drost is the best answer. – Sy Loc Apr 13 '18 at 02:47
0

If you want your styles to be treated as an element, you need to put in an HTML element object. Since you're just putting in a string, it thinks you're doing something like adding text to a <p> element. Instead, try the following:

$("<style>.hova {border-color: red !important;border-style: solid !important;border: 1px;}</style>").appendTo("head");
0

You can do it like this and also you add multiple css classes

var h = document.getElementsByTagName('head').item(0);
var s = document.createElement("style");
s.type = "text/css"; 
s.appendChild(document.createTextNode(".hova {border-color: red !important;border-style: solid !important;border: 1px;}");
h.appendChild(s);
Hasnain Bukhari
  • 458
  • 2
  • 6
  • 23
-2

you can use this:

$('head').css({
   'color' : 'red !important',
   'width' : '30px',
   'height' : '10px'
});
oma
  • 1,804
  • 12
  • 13