Real estate for a targeted audience

1. Overview

  1. I have a list of selectors that should always apply certain properties.
  2. Some selectors require additional properties to be added.

I'm struggling to find a way to achieve this without repeating code.


2. Minimal CSS Example (MCVE)

2.1. Expected CSS Output

.KiraFirst,
.KiraSecond,
.KiraThird {
  color: red;
}
.KiraSecond {
  background-color: yellow;
}

In the example above, .KiraSecond is repeated twice. Is there a way to achieve the expected behavior without this repetition?

2.2. Using Stylus

  • View live demo on stylus-lang.com
.KiraFirst
.KiraSecond
.KiraThird
    color red

.KiraSecond
    background-color yellow

This stylus code compiles to the expected CSS output, but the use of .KiraSecond is duplicated.

I'm unsure of how to avoid this duplication. For instance, the following syntax did not produce the expected CSS:

.KiraFirst
.KiraSecond
    background-color yellow
.KiraThird
    color red

The result was:

.KiraFirst,
.KiraSecond {
  background-color: #ff0;
}
.KiraThird {
  color: #f00;
}

3. Resources I've Tried

  1. Stylus official documentation, specifically the Selectors section
  2. Stack Overflow questions related to Stylus
  3. Stylus GitHub issues

Answer №1

One approach could be to use a generic class for all elements, such as .Kira. Then, if you need to apply specific styles to certain elements, you can create additional classes like .KiraSecond or use pseudo selectors like .Kira:nth-child(2).

For example:

.Kira {
  color: red;
}
.KiraSecond {
  background-color: yellow;
}

Alternatively:

.Kira {
  color: red;
}
.Kira:nth-child(2) {
 background-color: yellow;
}

Answer №2

It's best not to? and you should avoid it. Repeating that selector in such cases is actually a good practice.

  • You are not duplicating the same property/value for multiple classes
  • You can easily override specific properties
  • You can easily modify/change behavior for specific classes

For example:

.class1, .class2, .class3 {
  color: red;
  background: yellow;
  border: 1px solid;
}

.class1:hover {
  color: blue;
}

.class2 {
  border: 2px dotted;
}

.class3 {
  color: pink;
}

What would be a poor practice in this case (without repeating selectors)

.class1 {
  color: blue;
  background: yellow;
  border: 1px solid;
}

.class2 {
  color: red;
  background: yellow;
  border: 2px dotted;
}

.class3 {
  color: pink;
  background: yellow;
  border: 1px solid;
}

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

Incorporate concealed image into HTML

When I perform actions with a couple of images, I encounter delays because the browser needs to load the images. For example, if I use $('body').append(''); everything works smoothly without any delays. However, when I try using style= ...

The initial transition in offcanvas on bootstrap 5 is not appearing when a placement is dynamically added

I am currently working on triggering an Offcanvas with JS and making the placement configurable. The issue arises when attempting to dynamically set the offcanvas-end class to the offcanvas element, as it does not transition smoothly the first time it is t ...

How to ensure a div within an anchor tag occupies the full width in HTML and CSS?

In my code, I am working on creating multiple small boxes with images and centered text inside. The goal is to have these boxes clickable, where clicking the image will take you to a specific link. On desktop, I want a hover effect that darkens the image b ...

Ensure that the select boxes are aligned in a horizontal manner

I am currently attempting to adjust the alignment of select boxes to reduce the staggered appearance. While not all need to be perfectly aligned, it would be ideal to have those that are close enough in alignment. I prefer using HTML to accomplish this tas ...

Tips for aligning one item in the center and another item on the right with MUI v5

My goal is to center 3 navigation tabs in the middle of my page and have a dropdown on the far right for sorting. I managed to get the dropdown on the far right, but I'm having trouble perfectly centering the 3 navigation tabs inside the <Box> & ...

Having trouble with loading base.css in React tutorial?

Struggling with the React tutorial here. Managed to get the server/API up and running thanks to stackoverflow, but now I'm facing an odd issue - base.css won't load. The base.css file is sitting right where it should be in the css folder, and we ...

Discrepancy in inner div presentation between Internet Explorer and Chrome

In my coding dilemma, the "outerDIV" contains an "innerDIV", with Chrome displaying the "innerDIV" at 491px while IE shows it as 425px (matching outerDIV). This discrepancy causes issues in viewing child elements within "innerDIV": Chrome shows the first t ...

Stable Banner with Automatic Scroll to Sections

I have implemented a script that allows for smooth scrolling to different sections within a webpage when clicking on links in the top navigation menu. In the HTML, I've assigned IDs to each section (section1, section2, section3, etc.) and linked these ...

Having trouble with ion-item click not functioning in Ionic 3?

Working on my Ionic 3 project, I have encountered an issue with the ion-item click listener. The scenario is that I am adding text over a canvas and then attempting to change the style of the text (such as font-family, font-size, bold, and italic). The UI ...

I noticed a random gap between the elements with no discernible explanation

<div id="colorscheme"> </div> <div id="content"> <div id="display_saved"> TEXT TEXT TEXT </div> Here is the HTML structure related to a particular document issue. CSS: #colorscheme{ width:25%; display:inline-blo ...

Is your CSS failing to synchronize with HTML?

Greetings from MyWebsite.html! <DOCTYPE !html> <html> <head> <title>My Website</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- CSS --> <link rel="stylesheet" href="MyWebsite ...

Unable to vertically align images in the carousel

Greetings! I am currently working on my website www.acigaiabeta.esy.es and I am facing an issue with aligning the images of the carousel to the center vertically. Despite trying various solutions, such as following tips from this resource, the images remai ...

Parent div is positioned absolutely with overflow set to hidden

I am facing a unique requirement where the parent div has an absolute position and the child div also has an absolute position. Inside the parent div, I have set overflow: hidden to hide the extra width of the child div. However, this setup is not working ...

The arrangement of elements varies based on the type of display

I'm still learning HTML and CSS, and I've noticed that the layout of my elements changes depending on the screen size. Everything looks good on my 24" monitor, but some elements overlap on my 15" laptop screen. What could be causing this issue, ...

What's the deal with this occurrence? The width of the HTML window seems

Exploring different html markup and layouts, I stumbled upon this JSFiddle: http://jsfiddle.net/vLJAq/15/. I made some modifications to it, but one thing that puzzles me is why the green box disappears when the window width is reduced? Here's the cod ...

Content positioned beside an image with the help of Bootstrap 4 framework

How can I align text to the right of an image, like a table layout? I experimented with grid layout using <div class="span2"> and <div class="span10"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bo ...

Display the HTML/CSS layout following the JavaScript window.open action

Encountering issues with printing output in an opened window using JavaScript. I'm creating a new document through window.open and including CDN links to Bootstrap files. While everything appears fine in the opened window, when attempting to print (XP ...

Tips for adjusting the position of overflowing text on a website using CSS in real-time

I'm currently working on an Angular application and I'd like to customize the styling so that any text exceeding 128 characters is not displayed. Ideally, if the text exceeds 128 characters, it should move to the left; otherwise, it should remain ...

What is the formula for determining the grid width when an item exceeds the column size?

I am working with a grid that matches the size of an image, which can be adjusted to be both smaller and larger in size. Within this grid, I have 6 items. Each item is wider than the columns in the grid. In order to achieve the desired width for the gri ...

Ensuring Equal Padding on Both Sides of a Div by Setting its Width

In search of a div that houses multiple inner divs with equal padding on the left and right edges. This is crucial for maintaining a fluid layout where the distance between the inner divs and outer div border remains consistent even when the window size ch ...