treddie
treddie

Reputation: 43

Can a JS script created in the body or head of an HTML file be used directly in CSS code?

I am new to webpage development and have a technical question. If I write a javascript function and put it in the head or body of my HTML file, can the variables in the function be used directly in the CSS code to alter things like content positioning? Or must the function be called from the CSS file(s)?

Thanks for any and all input!

Upvotes: 2

Views: 63

Answers (3)

neohex
neohex

Reputation: 7

No, you cannot call a function from CSS . JS will let you control the DOM elements.

Upvotes: 1

Adam Britto
Adam Britto

Reputation: 21

Writing JavaScript allows you to directly modify the DOM, not your CSS code. This produces the same effect you were anticipating but through different methods. If you're using a script element to write your JS you could use it to alter content positioning, but be aware of the order you load your CSS and scripts to avoid any conflicts.

Upvotes: 1

Shah Rukh Charlie
Shah Rukh Charlie

Reputation: 52

You can apply CSS on you html elements from the functions you created itself. Or please pass some code here to be more clear on in it.

General scenario can be as:

<html>
<head>
    <script>
        function useCSS(){
            $("#myElement").css("color","red");
        }
    </script>
</head>
<body>
    <p id="myElement">This has to be changed!</p>
</body>
<script>

    // function can be written anywhere in the script file or tags but ,has to be called after the element is loaded on page
    useCSS();
</script>

Upvotes: 1

Related Questions