Sushma
Sushma

Reputation: 45

how to add css styling inside javascript?

Actually i am using iframe element in html page but it is inside tag which means javascript. so I want to add css to that iframe element, how can I do that? Can someone help please

Upvotes: 1

Views: 57

Answers (2)

Emir Tanır
Emir Tanır

Reputation: 42

yourElement.style.attribute = value

for example

var button = document.getElementById("btn")
button.style.color = "red"
button.style.marginTop = 0;

when writing css in javascript you must use camel case css for example type marginTop not type the margin-top

Upvotes: 0

Michael
Michael

Reputation: 1872

Styles from the parent will not be applied to the document within the iframe. The best solution is to add the stylesheet to the document in the iframe. Either via javascript or creating a template page to load in via src.
Something like:

var head = doc.head
var link = doc.createElement('link')
link.type = 'text/css'
link.rel = 'stylesheet'
link.href = ...src...
head.appendChild(link)
This is an example: link

Upvotes: 2

Related Questions