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:

https://i.sstatic.net/XrEi6.png

CSS in Internal Vue Component:

https://i.sstatic.net/ktHUR.png

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

Develop a dynamic webpage using ASP.NET for enhanced interactivity

Currently, I am working with asp.net 2.0 and C#. I have a unique teacher-student dynamic in my platform, where I envision students receiving personalized pop-ups created by their respective teachers upon login. My challenge lies in providing teachers wit ...

I possess an image sourced from the backend, and I have added boxes on top of it. However, I am struggling to accurately place these boxes on the specific points that I desire

I'm struggling to scale the image to fit my container's width and height in order to properly position specific points. Unfortunately, I've been unable to achieve this so far. Check out my code below: <div class="img-magnifier-cont ...

Struggling to iterate through the response.data as intended

Assume that the response.data I have is: { "data": { "person1": [ { "name": .... "age": xx } ], "person2": [ { ...

Tips to prevent browser from freezing while creating a large number of HTML elements

I am currently utilizing Selection.js to develop a customizable grid on my website. To make this work effectively, I need a specific number of div elements to establish the selectable area. In my scenario, I generate all the divs using a for loop and then ...

Icons that are clickable in ionic

I have successfully created a list card with 2 icons in each row. However, I am facing a challenge in making these icons clickable without disrupting the layout of the list. I attempted to add an ng-click to the icon element, but it did not work as expecte ...

Using JQuery to implement custom validation on a text field is an effective way

How can I validate a text field using Regex and create my own validation rule for starting with "com."? Can you provide me with an example of how to do this? I want the text field to only accept values that start with com. <input id="appPackage" name=" ...

Issues with forms displaying as raw text within Laravel 5

I recently entered the realm of Laravel. While following a tutorial on OpenClassrooms, I encountered some challenges as the tutorial was for Laravel 4 and I am using Laravel 5. After struggling to adapt my controllers and resolve namespace dependency erro ...

Is there a way to eliminate the shadow effect appearing on product photos in BIG CARTEL?

While using the Luna theme on my Big Cartel store, I noticed that the products are hard to see on mobile devices because of the box shadow effect. I found a solution on a Big Cartel help page recommending to add a specific code to the CSS, but when I tried ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

What is the best way to create some distance between a div element and the bottom of the body?

Hello, I am attempting to center a div within my body. The div has a minimum height of 400px, but its size can expand based on the content it holds. When I add more content to the div, it grows accordingly, but it ends up touching the bottom of the div. I ...

What steps should I take to ensure that the content within a div also goes full screen when I expand the div?

Is there a way to make a flash game go full screen along with its container div? I have a button that expands the div to full screen, but the flash game inside remains stuck in the top left corner of the screen. How can I ensure that the flash game expands ...

Troubleshooting Problem with Bootstrap CSS Menu Box Format

I'm having trouble creating a simple menu for my Bootstrap site. What I want to achieve is something like this: This is what I have accomplished so far: I've attempted to write the CSS code but it's not working as expected. Below is the ...

Mastering HTML Tag Styling within Visual Studio

I'm looking for assistance on how to automatically format HTML tags using the Ctrl + K, Ctrl + D shortcut. For example: <label id="myLabelId">Hello</label> should be reformatted to: <label id="myLabelId"> Hello </label> ...

Preloading Vue dynamic routes for optimal performance

Currently dealing with a challenge on my Vue project and could use some help. I'm looking to prerender specific dynamic routes when I navigate to a particular route within the application. On the /works route, there's a list of items displa ...

Angular material input featuring an additional component

Trying to integrate an additional text element next to an input using angular-material. The goal is to replicate the functionality of bootstrap's .input-group-addon: The closest approach achieved so far can be seen in this example: <md-content la ...

We encountered a problem while trying to create the route "/": Minification of HTML failed in Nuxt js

After successfully developing a Nuxt app that runs perfectly with "npm run dev," I encountered an error when generating the site using "npx nuxt generate." Despite my efforts, I cannot locate the source of the error. Any assistance would be greatly appre ...

The controller is referencing $rootScope which has not been properly defined

Here is my understanding of the controller concept. Whenever I launch the application, an error message appears saying "$rootScope is not defined." Can someone provide assistance in identifying the issue? var webadmin = angular.module('PcPortal' ...

Tips for ensuring that jQuery runs in the correct order within Angular 2

As a beginner with Angular 2 and asynchronous loading, I have encountered some difficulties in finding the right approach for running jQuery scripts in the correct sequence within an Angular 2 project. Despite exploring various resources such as: How to u ...

Switch from flexbox layout to begin on the next column within the same row

I am attempting to create a CSS layout where, after a specific element like :nth-child(6), the elements will break and form a separate column within the same row. The parent element should split into 2 columns after every 6th element in the same row, then ...

unable to properly verify if the value is above 0

$zombieNumber = $_POST['zombie']; if ($zombieNumber > 0){ echo "The number is greater than 0"; } It appears that even if the value of $_POST is -1, the script still considers it greater than 0. Is there a way to handle values under 0? Attemp ...