Is there a way to effectively overwrite CSS styles when utilizing @extend with another class in SCSS?

Here is some SCSS code I am working with:

.a {
    height: 100px;
    width: 100px;
}

.b {
    @extend .a;
    height: 200px;
}

After compiling, the CSS appears as follows:

.a, .b {
    height: 100px;
    width: 100px;
}

.b {
    height: 200px;
}

When applying this style to a <div class="a b">, the height remains at 100px instead of 200px. How can I get the CSS to recognize and implement the height of 200px in this scenario?

Answer №1

Which CSS selector will take precedence in the following scenarios, as a general rule for any CSS file?

The height will be set to 200px for <div class="a b">.

.a {
    height: 100px;
    width: 100px;
}   
.b {
    @extend .a;
    height: 200px;
}

The height will be set to 100px for <div class="a b">.

.b {
    @extend .a;
    height: 200px;
}
.a {
    height: 100px;
    width: 100px;
}

The height will always be set to 200px for <div class="a b">, regardless of order.

.a {
    height: 100px;
    width: 100px;
    &.b {
       height: 200px;
    }
}

Answer №2

This feature already operates in the exact manner you desire.

Ensure that the .b class is placed above your .a, .b, as the most recent style declared will take precedence.

To learn more about cascading order

Finally, prioritize based on the sequence of declarations: when two statements possess equal priority, origin, and specificity, the latter one mentioned prevails. Declarations within imported style sheets are deemed superior to those in the style sheet itself.

.a, .b {
    height: 100px;
    width: 100px;
}

.b {
    height: 200px;
}

/* ignore */
div{
  background-color: red;
}
body{
  display: inline-flex;
}
<div class="a"></div>
<span>|</span>
<div class="b"></div>
<span>|</span>
<div class="a b"></div>

Answer №3

If I understand your question correctly, it can't be overwritten in the same way

.x, .y {
    height: 150px;
    width: 300px;
}

Because this will assign width: 300px; to both .x and .y, but if you want to assign width: 300px; to only .y, then the code you already provided does that perfectly.

Answer №4

One option is to utilize a wrapper element

.a {
    height: 100px;
    width: 100px;
}

.b {
    @extend .a;
}

.wrapper .b {
    height: 200px;
}

For the HTML structure, you can nest the elements as follows:

<div class='wrapper'><div class='b'> XXX </div></div>

To ensure specific styling takes precedence, consider using !important.

.a {
    height: 100px;
    width: 100px;
}

.b {
    @extend .a;
    height: 200px !important;
}

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

Issue with the Styled Components Color Picker display

For the past 6 months, I have been using VSCode with React and Styled Components without any issues. However, recently I encountered a problem where the color picker would not show up when using CSS properties related to color. Usually, a quick reload or r ...

What could be the reason for the absence of the CSS destination folder being generated by

My colleague has the exact same code and can run gulp without any issues, but I'm struggling to compile the css files using Gulp. Gulp isn't creating the css destination file or the css files themselves. However, it works perfectly fine for build ...

Is it not possible to change node labels in D3.js after clicking on a node?

After being inspired by this link, I successfully created a basic network visualization app using D3.js. The app reads node names from a textarea on an HTML page and then constructs a network where all nodes are interconnected. All the relevant code can ...

Streamlining Tailwind CSS styles in your React/Next.js components for maximum efficiency

Since implementing Tailwind CSS in my React/Next.js project, I've noticed a decrease in maintainability compared to using traditional CSS or styled-components. Previously, I could easily consolidate style changes, but now I find myself duplicating cla ...

Play framework fails to apply style attributes correctly

When working with play-framework 2.3.x, I am in the process of creating a form and would like to show/hide it based on a model attribute. Typically, setting the style attribute manually using style = "display: none" and style = "display: block" works fine. ...

Guide to centering Embed horizontally with Bootstrap 5

My goal is to create a centralized input with an image positioned above it. I have been following the guidelines provided in the position documentation, but the image still does not align horizontally: https://i.sstatic.net/awWxL.png Here's the code ...

Easy Steps to Align a Rotated Table Header

I am looking to rotate the header table using CSS while keeping all text together. Here is my CSS code: th.rotate { height: 100px; white-space: nowrap; } th.rotate>div { transform: rotate(-90deg); } Here is how I have applied this CSS: ...

jQuery's show/hide functionality allows for the dynamic resizing of images,

I am experiencing an issue with a Joomla template that has a custom jQuery menu. When I hover over the map with my mouse, the overlay appears slightly larger than expected. This problem seems to be occurring in Firefox and IE 11, leading me to believe it ...

Adjust the dimensions of input tag depending on the length of content in Angular

Is there a way to dynamically set the size of the input tag in my HTML based on the length of the ng-model value? I attempted this approach: <span>Node Path: <input for="nodeName" type="text" ng-model="nodePath" size="{{nodePath.length()}}"ng- ...

Styling and theming in PrimeNG

Seeking advice on how to customize the appearance of the background for the primeNG panel component. I attempted to override the styling using the specific names in my scss file for the component, but it did not work. I also tried inline with <p-panel ...

What is the best way to vertically align two divs on the left and right?

Having trouble aligning my dropdown list vertically with my textarea on the left. It seems like they are clashing together for some reason. UPDATE: Thank you for the assistance, I was able to make the necessary adjustments. JSP: .ExeDropdown{ float: ...

CSS code to create a single content bar flanked by two sidebars

I have been working on a beginner-friendly webpage and have managed to style it using CSS to some extent. My goal is to have the content centered with a white background, flanked by two sidebars styled in blue. To work on this page, please visit: The ...

Tips for toggling the visibility of a flexbox-styled popup using jQuery

I am trying to implement a popup on my website and I want to use flexbox styling for it. I have the necessary scss mixins for flexbox properties, however, I encountered an issue. The problem arises when I try to hide the popup using display: none, as the ...

"Interactive bootstrap tabs nested within a dropdown menu that automatically close upon being clicked

Hey, I've got a dropdown with tabs inside. When I click on certain tabs within it, they close but the content inside the tabs that I click on changes, so it's functioning properly. However, the issue is that it closes when I want it to stay open ...

Centering an Image of Unspecified Dimensions Both Vertically and Horizontally with Hover Effect on Anchor

To achieve both horizontal and vertical centering of an image with unknown dimensions, I need to use max-width and max-height to constrain it within a container size of 101x84. Additionally, I want the image to display an icon in the middle when hovered ov ...

Using jQuery to toggle the menu

I've been brainstorming ways to add a toggle menu on my website and finally stumbled upon a solution that works great! http://jsfiddle.net/hhcsz5cr/ <div> <h1><button class="button" data-circle="travel"> <i cl ...

Changing animation during mouse hover off

I've been experimenting with using 'translate3d' and 'transition' on hover to animate a div into view. While the animation works smoothly when hovering over the element, I'm having trouble getting it to transition back out of ...

What is the best way to achieve a perfect rounded div using Bootstrap 4?

While browsing through the Bootstrap documentation and searching on stackoverflow, I came across a solution to create circular images using border-radius set at 50%. However, when I tried implementing it with my slightly oversized image, it ended up lookin ...

Is there a child missing? If so, add a class

I need to add the class span.toggle if the parent element of li does not have a child ul element. click here to view on codepen Snippet of HTML: <html lang="en"> <head> <meta charset="UTF-8> <title>No Title</title>& ...

How is the height and width of the header determined?

When examining the theme by clicking "inspect element" on the green portion (<header> ), you will notice that the height and width properties have pixel values that change dynamically as the window is resized. So, where do these values originate fr ...