14

I have an angular component, that generates inline style attributes in the resulting HTML.

Example :

<my-component></my-component>

The HTML of that component is:

<div id="my_component" class="class_1" style="width: 40px; height: 40px;"></div>

Is there a way to override the width/height of my_component from external CSS file? I tried many selectors, I'm always getting overridden by the inline style...

!important is working, but I would like to know if there is another way than use !important.

4
  • 3
    The short answer is no. Commented Dec 19, 2018 at 15:19
  • No, there isn't Commented Dec 19, 2018 at 15:22
  • No. It looks like you have things back-to-front. You are trying to override inline css with a css file. Normally you would override a css file with inline css. Consider restructuring your css. Otherwise you will have to use !important. Commented Dec 19, 2018 at 15:22
  • you want to style it or just reset width and height? Commented Dec 19, 2018 at 15:55

4 Answers 4

12

If we think about this differently considering that you want to override width/height then yes you can do it with min-width/min-height:

#my_component {
  min-width:200px;
  min-height:200px;
  background:red;
}
<div id="my_component" class="class_1" style="width: 40px; height: 40px;"></div>

And to be more accurate you can add the max-* version to be sure you have a fixed height/width:

#my_component {
  min-width:200px;
  min-height:200px;
  max-width:200px;
  max-height:200px;
  background:red;
}
<div id="my_component" class="class_1" style="width: 40px; height: 40px;"></div>

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

1 Comment

wow, i didn't expect real good answer by asking my question, but your answer is perfect for my case! thanks
3

No.

The only way to override a CSS rule without using !important is to use a more specific selector.

No selector is more specific than the style attribute.

Comments

2

CSS Specificity works in a hierarchy

  1. !important overrides
  2. Inline Styles
  3. ID's - HTML: id="blah", CSS: #blah
  4. Classes - HTML: class="foo", CSS: .foo
  5. Elements and pseudo-elements (:before, :after).

you can read more on it here

but as others have said, in short, no.

3 Comments

Just curious, where does order fit in there?
Are you referring to the cascade?
@sebastian order as in source order? order is used to break ties if Specificity matches.
1

There used to be a bug where a selector containing 256 would override an id. There may be some upper limit to the counter and going over that would cause an override to the next value (a selector of 256 ids would override an inline style).

I think the 256 classes bug was fixed and I don't know whether this was done by increasing the necessary number of classes or putting in place something to prevent it ever happening again.

From a purely academic perspective, it may be possible to override an inline style without using !important but it would be more of a a bug exploit as opposed to part of the spec / a good idea.

Comments

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.