2

I have drop-down in vue component and class of that element is "form-control" and it has it's own styles. I need to override the styles of that class. for that I have done coding as follows (in vue component),

<style scoped>
.form-control{
  border-radius: 50px !important;
  color: #823F98 !important;
  border: 1px solid #3FA294 !important;
}
</style>

but this one didn't work for me. so, how to override it?

Thank you!

2
  • Your vue component has the other vue component inside? If that's the case, "scoped" styles will not leak into child components. Commented Nov 27, 2020 at 6:08
  • yeah. the dropdown is another vue component and I am using that component in another vue component. so, is there any way to ovrride the styles? Commented Nov 27, 2020 at 6:11

3 Answers 3

3

You should remove scoped. If you leave scoped it will not apply on other components including those you import.

Or move that css in app.css or app.scss.

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

2 Comments

I have tried removing the scoped and it works for me. is it affect other parts of the project??
It affects all elements using that .form-control class
2

Using unscoped style can be very dangerous especially general class names like form-control.

I think it's a better way to use deep styles in your parent component:

<style scoped>
>>>.form-control{
  border-radius: 50px !important;
  color: #823F98 !important;
  border: 1px solid #3FA294 !important;
}
</style>

but if you can refactor your child component and add a props like formControlStyle with your CSS styles would be the best solution to avoid side effects. You can add a default value to this prop that is your styles in your child component.

Comments

1

Use Deep Selector, ie, :deep(.whatever-style-name) you want to override in your current component

<style scoped>
:deep(.form-control) {
  border-radius: 50px;
  color: #823F98;
  border: 1px solid #3FA294;
}
</style>

by doing this, you can remove the need to use '!important' in every line of css codes that needs overiding.

Refer here in the Vue official docs for more info.

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.