Skip to content

Updated information in the article "Styles and classes" #3536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Alexandre887
Copy link
Member

Styles and classes

Text from the article:

We can't set the full style like div.style="color: red; width: 100px", because div.style is an object, and it's read-only.

Text from MDN 'HTMLElement: style property':

This property is read-only, meaning it is not possible to assign a CSSStyleDeclaration object to it. Nevertheless, it is possible to set an inline style by assigning a string directly to the style property. In this case the string is forwarded to CSSStyleDeclaration.cssText. Using style in this manner will completely overwrite all inline styles on the element.




So we can actually set the full style like div.style="color: red; width: 100px". But, if we nevertheless want to set several styles at once, it is better to set them already in cssText.

MDN says that by setting styles in this manner all existing styles will be overwritten. But this is only if we use =. If we use +=, we can add new styles to the existing ones.

When we use +=, if the element already had the styles we want to add, the values of those styles will be overwritten with the new ones (the ones we are setting)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant