0

I have a DIV with a specific ID ("container") on every page of my site as part of the template.

#container
{
max-width:980px; min-width:980px; padding-top:6px;
}

When a button (to toggle full screen) is clicked i get what is the max supported width of the client and then do:

document.getElementById('container').setAttribute('style', 'max-width:' + width + 'px !important;');
document.getElementById('container').setAttribute('style', 'min-width:' + width + 'px !important;');

This works fine and sets the max-width to say 1400px.

My question is, can I remove this style I just added, so only the css would apply, using javascript ?

Or is my option having the same code, going back to 980?

document.getElementById('container').setAttribute('style', 'max-width:980px !important;');
document.getElementById('container').setAttribute('style', 'min-width:980px !important;');
2
  • Are pixels mandatory? Why don't you base yourself on vw units ? Why !important ? You'd be better off with CSS classes, applied conditionally based on fullscreen mode. Commented Mar 10, 2016 at 12:19
  • I would make a "fullscreen" css class which overrides the default #container styles. Then you can simply add a classname to your element when going fullscreen and remove it when not. Commented Mar 10, 2016 at 12:46

2 Answers 2

2

You can remove the entire style attribute

 document.getElementById('container').removeAttribute('style');

Or you can remove only the max-width property in the style attribute:

 document.getElementById('container').style.maxWidth = "none";

Refferences:

style

removeAttribute

Sign up to request clarification or add additional context in comments.

6 Comments

Brilliant, I was afraid something like this would also remove any css, it doesn't.
This doesn't removes CSS, this manipulates the DOM style attribute, that overrides the defined CSS styles, but don't change anything in your css files.
It removes only inline styles.
@Bonatoc why? can you explain? That's the question I guess. Downvotes are not for fun, remember. And why the other answer is the question but this not?
@Bonatoc can you come here and explain? Or you are making a troll-marathon ?
|
2

My question is, can I remove this style I just added, so only the css would apply, using javascript ?

You need to use a class here

.default-style
{
   max-width:980px; min-width:980px; padding-top:6px;
}

so while assigning the new width, remove this class first

document.getElementById('container').setAttribute('style', 'max-width:' + width + 'px;min-width:' + width + 'px;');
document.getElementById('container').classList.remove("default-style");

When you want to go back to default

document.getElementById('container').removeAttribute('style');
document.getElementById('container').classList.add('default-style');

Refer to Element.classList for adding/removing class.

6 Comments

My point precisely. But again, !important is a bad habit. max-width can be reset to default browser value if needed.
@Bonatoc Removed the !important. It is not required now. I agree that it should be used very carefully
I'm affraid why this is not downvoted by @Bonatoc . I feel like LOL
@MarcosPérezGude I am not sure about that too. Anyways, I have removed !important. Note that I'm upvoted this answer. I did yours too, because I liked the simplicity and OP liked it too
@gurvinder372 And I (OP) upvoted both of you, he was just tiny bit faster.
|

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.