How to change CSS property before element is created?


Tags: javascript,html,css

Problem :

I have to modify the div's css before the window.onload, and body.onload.

<html>
    <head>
        <style type="text/css">
            html, body {
                width: 100%;
                height: 100%;
                padding: 0px;
                margin: 0px;
            }
            div {
                margin-left: auto;
                margin-right: auto;
            }
        <script>
            HTMLDivElement.prototype.style.marginTop = (window.innerHeight/2 - HTMLDivElement.scaleHeight/2) + "px";
        </script>
    </head>

    <body>
        <div>
        foo<br/>bar
        </div>
    </body>
</html>


Solution :

I don't know if that works in all browsers but you could append a new style-node inside your head section like this:

var marginTop = ...;
var node = document.createElement("style");
node.setAttribute("rel", "stylesheet");
node.innerHTML = "div { margin-top: " + marginTop + "px; }";
document.head.appendChild(node);

Your head element must obviously exist for this to work. I know it's ugly and I can't recommend using this but I think it's what you wanted.

UPDATE: I found another way, which in my opinion is better because of the API you can use:

// insert a stylesheet if you don't have one
if (document.styleSheets.length == 0) {
    document.head.appendChild(document.createElement("style"));
}

var marginTop = ...;
var rule = "div { margin-top: " + marginTop + "px; }";
document.styleSheets[0].insertRule(rule);

For more information you may visit http://www.w3.org/TR/DOM-Level-2-Style/css.html Again I don't know if all browsers support this.


    CSS Howto..

    How to use “Wedding text ” in Css font family?

    How to create live event for css

    JavaFX: How not to fill background outside the rounded borders of a Label within a ScrollingPane

    How do I center HTML objects on an MVC 4.0 Web API using Razor application?

    How to make a “” looks like a link with CSS?

    How to prevent mobile browser micro-shrinking HTML block level elements?

    Tricky: In CSS and HTML, how to make a sub DIV go under the container if the container has a z-index of 0?

    How can I restrict this CSS container to only containing the div its declared in?

    HTML5 Modal Dialog is showing shortly on page load, how can i stop this?

    How to addClass with css3 Animation?

    How to achieve a two column centered layout, with the main column expandable on wider screens?

    How to add css to jquery backstretch?

    How to rotate image used as list type in unordered list

    How to create new lines with less space HTML/CSS

    How to give viewers custom CSS option

    3x3 CSS grid: how to make center div the only one with a fixed height/width?

    CSS: How do I assign one class to multiple elements?

    How do I make sure my text stays on same line when I add a 'space' inside
  • element?
  • How to apply custom css to split post into multiple page

    How to position css sprite play button within video image

    How to get the CSS style from Inspect Element

    show two texts for one mouseOver event

    How to local storage expand/collapse CSS settings

    How to make long Bootstrap dropdown menus vertically scrollable?

    how to make an empty span collapse

    How to handle caching of GWT theme CSS files

    How do I change the color of Semantic UI icons?

    My YouTube video wont show in iframe

    How to remove some css properties using regular expression?

    How to Mask Images with Dynamic Sizes to Shapes?