Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Changing the Styles of HTML Elements | DOM and HTML Manipulation
HTML & JavaScript Interactivity for Beginners
course content

Conteúdo do Curso

HTML & JavaScript Interactivity for Beginners

HTML & JavaScript Interactivity for Beginners

1. DOM and HTML Manipulation
2. DOM Event Handling and Forms
3. JavaScript HTML5 APIs
4. Beginner Projects with HTML + JavaScript

bookChanging the Styles of HTML Elements

In the last chapter, you learned how to find an HTML and change its content. Likewise, you can change the styles of the HTML. We'll look at several ways.

Change with Style Property

You can change the CSS style properties using the style attribute of HTML Elements. Let's dive into an example right away. In the below code, the Javascript changes the font color of the paragraph tags to blue.

html

index

css

index

js

index

copy

Note that you can access the above element either with the getElementById or querySelector. So through the style attribute, you can change the value of any CSS property.

Change with Events

In the real world, most web applications change their styles after the occurrence of an event. So in this example, you'll discover how to hide an element using one button click and then make it visible again with another button click. This is possible with the help of the visibility property.

Also, at this stage, do not pay much attention to the event part, as there'll be a separate lesson on it later. For now, assume that onclick detects the click event of the button where you can write JavaScript code.

html

index

css

index

js

index

copy

Changing Multiple CSS Properties at Once

You may arise with a scenario where you would have to change more than one style. Then from what you've learned so far, you can repeat the style property with different values in each line.

However, it is not a best practice for coding as it needs to be readable. So then, you can create a JavaScript variable and store all the style values. Then when the button is clicked, you can associate the click event handler to the function to assign the variable mentioned above which is called template literals. Finally, you can assign it to the cssText property.

html

index

css

index

js

index

copy
1. Which of the following pieces of code changes the font color of the element <h2 id = 'sub-heading'>I'm heading</h2> to yellow?
2. Which of the following pieces of code changes the background color to pink, font size to 45px, and font color of the element <p id = 'p1'>some paragraph</p> to white at once on the onclick event handler of the button?
Which of the following pieces of code changes the font color of the element <h2 id = 'sub-heading'>I'm heading</h2> to yellow?

Which of the following pieces of code changes the font color of the element

I'm heading

to yellow?

Selecione algumas respostas corretas

Which of the following pieces of code changes the background color to pink, font size to 45px, and font color of the element <p id = 'p1'>some paragraph</p> to white at once on the onclick event handler of the button?

Which of the following pieces of code changes the background color to pink, font size to 45px, and font color of the element

some paragraph

to white at once on the onclick event handler of the button?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
some-alt