0

I have the following javascript, using jQuery:

$('html').css('font-size','10px');

I would like to do the same thing without using jQuery.

I've tried document.getElementsByTagName('html').style.fontSize but it didn't work.

It seems like it should be easy, but because HTML is part of every page about javascript & css, googling an answer turns up too many unrelated results.

1
  • It is the same question but the selected answer is not present on the other page. Commented Feb 18, 2015 at 17:33

2 Answers 2

5

Try this instead. getElementsByTagName returns a list of items. You need to reference the first item.

document.getElementsByTagName('html')[0].style.fontSize = '10px';
Sign up to request clarification or add additional context in comments.

1 Comment

This answer might be more robust (I don't know), but I try to avoid accessing arrays whenever possible... there's no particular speed hit this time because it gets executed only once, but it feels logically messy to get something from an array if I can get it directly.
2

You can use

var htmlElement = document.documentElement;

to select the html element. In order to set styles go on as you allready did:

htmlElement.style.fontSize = '10px';

3 Comments

Why is this downvoted? This works just fine.
I actually combined both to: document.documentElement.style.fontSize = 10px';
@AndrewSwift That will do it as well. Usually I prefer to set css classes from javascript instead of manipulatiing style properties directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.