Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - modify CSS on HTML tag?

The HTML tag on this page I'm working on is in a class that is giving it a top padding of 28px. I need this to go away temporarily when a button is clicked, but it doesn't appear that I can change the styling of the HTML tag itself.

Will I need to use position: relative; on the body tag or something similar? Is there really a way to assign CSS to the HTML tag that I don't know about?

@ Comments:

Sorry, I'm in a bit of a rush here. It's something to the effect of this:

<html class='pad_my_top'>
<head>

<style type='text/css'>
    .pad_my_top{
        padding-top: 28px;
    }

    body{
        background: #000000;
    }
</style>

<script type='text/javascript'>
    function its_gone(){
        // Something here to remove padding.
        alert("It's gone. Hurray!");
        // Something to put it back.
    }
</script>

</html>
<body>

<button onClick='its_gone()'>Click me to get rid of the top padding</button>

</body>
</html>

I really want it gone so I can print the page with Javascript, but I'd rather not use any 3rd party code because this is for a plugin for Wordpress and I don't want a billion dependencies. I only need to hide/re-display 3 divs and (re)change 2 styles.

like image 221
ITS Alaska Avatar asked Dec 10 '12 21:12

ITS Alaska


People also ask

Can you modify CSS with JavaScript?

With JavaScript, we are able to set CSS styles for one or multiple elements in the DOM, modify them, remove them or even change the whole stylesheet for all your page.

How do you style a HTML tag in JavaScript?

There are two other ways to set style using JavaScript. The first is to use the setAttribute() method. The second is by adding a class using the add() method of the classList property. The class you add can then be handled using external CSS styling.

Can JavaScript change the style of an HTML element?

The HTML DOM allows JavaScript to change the style of HTML elements.

Can I change CSS class in JavaScript?

The easiest way of changing a CSS class in JavaScript is by using the className method. Using this method, you can replace any existing classes already present on the HTML element with some other classes. You can specify all the new classes that you want to add as a string with space separation.


1 Answers

Use this to remove the top padding:

document.documentElement.style.paddingTop = "0";

and this to set it back:

document.documentElement.style.paddingTop = "28px";
like image 114
KatieK Avatar answered Nov 01 '22 09:11

KatieK