The hierarchy of CSS in Vue components

I've developed a customized CSS framework to streamline the design process across all my internal projects. The central file is structured as shown below:

  @import "~normalize.css/normalize.css";
  @import "_variables.scss";
  @import "_mixins.scss";
  @import "_elements.scss";
  @import "_styledElements.scss";
  @import "_aux.scss";
  @import "_base.scss";
  @import "_composedElements.scss";
  @import "_layouts.scss";

The normalize.css file contains specific button styling rules, such as:

  button,
  input,
  optgroup,
  select,
  textarea {
    font-family: sans-serif; /* 1 */
    font-size: 100%; /* 1 */
    line-height: 1.15; /* 1 */
    margin: 0; /* 2 */
  }

In the _elements.scss file, I have established the default button style:

button {
      font-size: 1em;
      height: 40px;
      -webkit-appearance: none;
      background-color: $mainColor;
      color: $geophy-white;
      text-transform: uppercase;
      outline-color: $uiColor;
      border: none;
      padding: 2px 12px;
      cursor: pointer;
      border-radius: 2px;
      font-family: $condensed;

      &:hover {
          background-color: darken($mainColor, 20%);
          color: $geophy-white;
      }

      &--disable {
          opacity: 0.8;
          &:hover {
          }
      }
}

Everything functions smoothly: I compile my project, import the main SCSS file, and voila! However, I encounter an issue when the HTML element is situated within an external Vue component, causing normalize.css to override my custom library styles. Here's an illustration of the problem:

CSS in External Vue Component:

CSS in Internal Vue Component:

How can this be rectified?

Answer №1

The issue lies in the conflicting styles, not Vue.js specifically.

To resolve this, I suggest assigning a unique class to the button inside the Vue component and styling it accordingly. By keeping your base styles separate from the in-component styles, you can ensure that they only apply where needed.

To avoid clashes between class names in different components, consider using the scoped attribute in your <style> tag.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When hovering over one div, both it and another div should be displayed simultaneously. The second div should continue to be displayed even when hovered over

I am looking to keep another div displayed when hovering over it - in this example, it says "hello." The div I want to remain visible is not a child element of the main div where I initiate the hover event. document.write("<base href=\"" + docum ...

What steps should I take to insert a divider in a dropdown menu?

I am attempting to enhance my dropdown menu by incorporating a separator using the following code: <li class='divider'></li> Below is my HTML code with the separator included: <ul class="dropdown-menu dropdown-menu-right" id="ul ...

Angular renders HTML elements

Greetings! I am currently experimenting with using AngularJS for the frontend and Wordpress as the backend of my project. To load content from Wordpress into Angular, I am utilizing a JSON wordpress plugin along with making $http requests. The content th ...

Position items at the center of the grid in material UI

I am having difficulty centering the grid items of material UI. I have tried using the justifyItems and alignItems props as mentioned in the documentation, but I'm still unable to achieve the desired result. Can anyone provide some guidance? Below is ...

What strategies can I use to dynamically update the .active class in jquery so it doesn't only target the initial one?

Utilizing bootstrap for tab-fade functionality has been successful so far. However, I am facing an issue when trying to select multiple active classes instead of just one. My current JQuery code only changes the text in the first element with the "active" ...

Is there a way to achieve a double background color effect in CSS while ensuring that the text is centered within the second background color element?

How can I achieve a layout similar to the one shown in this image: ul menu li tags Should I use two tags for each element? Here is an example: <ul class="menu"> <div class="outside"><li class="inside"><a href="#">Firefox</a ...

Froala text area is unexpectedly visible when I attempt to cover it with a partially see-through mask

The website I'm currently developing features a semi-transparent overlay that dims the screen with a light-colored message displayed on top. This overlay and message are shown whenever there is a background process running. Everything was running smoo ...

When I click on "Submit", it redirects me to the PHP page

I've followed a tutorial on creating custom PHP contact forms at this link: and made some modifications to the form for my specific requirements. However, when I click the Submit button on the form, it redirects me to the PHP page. What I actually w ...

"Utilize Bootstrap's responsive 3-column grid layout for a sleek design, complete with hidden col-12 elements below each grid

Looking to create a 100% width column below an element within a column of a bootstrap grid. For better understanding, here is what I'm aiming for: https://i.stack.imgur.com/pl1Fi.png On selecting one of the images (from 1 to x), a hidden div with di ...

Why do certain full screen websites not align with my screen width properly?

When I use my 1920px wide screen and inspect the HTML tag with Chrome DevTools on websites like Firebase or Facebook Messenger, I notice a difference: Despite my screen width being 1920px, these websites appear fullscreen with an HTML tag width of 1440px. ...

Managing Radio Button Conditions in Vue.js

Is there a way to set conditions for radio buttons in Vue.js? I am trying to make the radio buttons dependent on a specific object that I select from the above codes. The "select" method works well and setting a value based on an object I choose using :val ...

I'm curious about how I can selectively utilize vuex-persistedstate with Nuxt for certain stores only

Check out this library: https://github.com/robinvdvleuten/vuex-persistedstate. I have customized the provided plugin file for Nuxt. import createPersistedState from 'vuex-persistedstate' export default ({ store }) => { createPersistedState ...

Guide on how to dynamically add AJAX JSON array response to an HTML table

Hey! I need some advice on how to dynamically append a JSON Array response to an HTML table after submitting a form using AJAX. Here's the scenario: This is my form : <form id="myForm" method="POST"> <input type=" ...

Ways to Fix the React Hydration Issue in Next.js

I have been encountering an issue with the error message "Hydration failed because the initial UI does not match what was rendered on the server." I am unsure of what is causing this error and would appreciate any insights or suggestions from others who ma ...

CSS3 :target and display:none inconsistency problem in Internet Explorer and Firefox

Creating a one-page site with navigation tabs that toggle between hidden divs using :target CSS3 selector is my current project. To ensure the first div is visible upon page load, I implemented this line of code: document.location.href = "#div1"; The HTM ...

Issue with iview-ui: Unable to make admin panel full height. The iView example suggests using min-height: 200px

Is there a reason why the iView admin template can only scale to a preset height? Can someone assist me in setting the height of the admin to fill the entire browser (100vh or 100%)? Many thanks in advance. The example on the site displays a predefined m ...

What is the significance of an empty href attribute in the HTML <base> tag?

If my website URL is: http://example.com/path/to/dir/index.html And I decide to proxy this page through: http://proxyserver.com/path/to/dir/index.html. I would like all relative URLs on the page to be resolved by proxyserver.com instead of example.com. Wh ...

Using Vuetify to display icons within a V-data-table component

Having just started learning Vue and Vuetify, I am facing a challenge with displaying a table containing some data. My goal is to have a delete icon in the last column that triggers an action when clicked. However, I am struggling to show the icon while al ...

Adding inline SVGs to a Nuxt 3 Vite project: A step-by-step guide

Hey there, I've been struggling with importing inline SVGs into my Nuxt3 Vite project. Any advice would be greatly appreciated. I discovered that using <img src="~/assets/images/icons/push-icon-chatops.svg" /> works, but I actually ne ...

Vue fails to trigger a rerender of the list, even when the set function is utilized

I created a list where each item can be removed, and this is how my code looked: Snippet <template v-for="(timeFrame, index) in form.timeFrames"> <el-form-item > ...