Sass: Overcoming the challenge of the IE limitation on 4095 selectors per stylesheet

Hey there! Are you working on a Rails project with Sass & Compass? If you're using the Rails Asset Pipeline, check out this question.

We're in the process of developing a large application with multiple use cases and various individually styled pages, catering to different contexts. This means we have a lot of style information to manage.

In our latest project section, we've encountered Internet Explorer's limit of 4095 selectors per stylesheet. Need proof of this limitation? Check out this link.

Now, why not just split the application stylesheet into several smaller ones by design?

The issue is that mixins and selector inheritance won't work across multiple Sass files (not partials), correct?

While the quality of our stylesheets is high, we can't eliminate the excess number of selectors. And it doesn't seem like reducing the amount of selectors should be our top optimization priority. The Sass core team recommends using selector inheritance over mixins when possible to reduce CSS file size, even though it may result in an increase in the number of selectors.

So, what are my options?

I'm considering writing a script that generates additional CSS files, breaking down the main application.css file. These extra files would only be loaded in IE to avoid multiple requests in modern browsers. I'd need a basic CSS parser to divide the application.css file after reaching 4095 selectors at a valid point. And I'd also require a Compass compile-after hook so developers don't need to manually create the IE files for testing purposes.

If you have any better ideas, please share them!

Thanks, Christian

Answer №1

Using mixins across multiple files is a common practice, but it's not feasible for @extend to work in the same way. The purpose of @extend is to create a single rule that shouldn't be duplicated across different files, so splitting files isn't an option.

To address this issue, I developed a splitter which can be found here: https://gist.github.com/1131536

Following the integration of these changes into Sass and Compass through this pull request and this one, you can automate the creation of additional stylesheets for IE by adding the following hook to your Rails config/compass.rb:

on_stylesheet_saved do |filename|
  if File.exists?(filename)
    CssSplitter.split(filename)
  end
end

Update:

The aforementioned CssSplitter has now been released as a gem, which can be accessed here: https://github.com/zweilove/css_splitter

Answer №2

In situations where it is not feasible to decrease the amount of selectors being used, dividing the CSS file appears to be the only viable option.

It seems like your plan for accomplishing this is on the right track, even though it may require some complexity in execution.

Answer №3

If you're looking for a simple solution, check out . Here is how to make it work:

  • First, download and install node.js
  • Next, run npm install bless -g in your terminal
  • Finally, use the command blessc source.css output.css

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

Enhance the dimensions of images on Tumblr

Is there a way to customize the width of a photo in HTML? I am having trouble changing the size of a photo set on Tumblr to be larger. I have tried researching it, but I don't understand where to place maxwidth:600px or if this is even the correct app ...

How to perfectly align content on a webpage

Can someone help me understand why my webpage is off-centered as it scales up? I have set a minimum width of 1000px, but it still appears to be centered at 30%-70%. Any insights on this issue would be greatly appreciated. <!DOCTYPE html PUBLIC "-//W3 ...

Changing the image source when clicking on it and removing it can be done by using JavaScript

How can I add a class 'selected' to ".swiper-slide" when it is clicked? I also want to change the image src when svg-img1, 2, or 3 is clicked. However, I need the image to revert back to its default src when another swiper-slide is clicked. ...

Tips for aligning a div containing an image in the center of another div

Trying to perfectly center an image inside a div both vertically and horizontally using the display inline-block property. HTML <div class="center-wrapper"> <div class="image-holder"> <img src="picture.jpeg" alt="Centered Image"> ...

Having trouble getting transitionProperty to work in the style object of your ReactJS CSS?

In my current setup, I have been utilizing css transitions triggered by property updates to manage animations within my react components and everything has been functioning smoothly up until now. However, I am encountering an issue where I only want to tr ...

Does Internet Explorer 10 support the FormData object?

I've developed a JavaScript app that enables me to upload images asynchronously, and it works seamlessly in most browsers. However, the infamous Internet Explorer is causing some trouble... To address this issue, I devised a workaround for IE9- versi ...

Aligning the text in the middle of a button

Currently delving into Front-End development and in the process of coding my maiden site using bootstrap. Encountered a roadblock on something seemingly simple. How can I center text within a button? Here's the button, the default one utilizing an " ...

What is the best approach for scaling @material-ui Skeleton in a grid row with variable heights?

I am working on creating a grid of Avatar images with a transition state where I want to show a skeleton representation of the image. To achieve this, I am using @material-ui/lab/Skeleton. The issue I'm facing is that since my images are set to autos ...

Troubleshooting issue: Chrome bug causing JavaScript full height intro section to have incorrect padding and display issues

Trying to create a full-height intro section using basic JavaScript markup has been quite the challenge. Here's a more detailed explanation: I have a parent DIV element that is supposed to adjust to the full height of the viewport. Unfortunately, t ...

Issues with border/padding and overlay in Packery Grid Layout

After spending some time working on a grid layout using the metafizzy isotope packery mode, I have encountered a few issues. To illustrate my problem, I have provided a link to a codepen below: http://codepen.io/anon/pen/EgKdpL Although I am satisfied wi ...

Aligning text and lists using Bootstrap on both desktop and mobile devices

I want to format a text with a list similar to the images provided https://i.stack.imgur.com/6giDH.png for desktop and tablet view, but for mobile display I would like it to look like this https://i.stack.imgur.com/7zSXi.png This is my current approach ...

How to add icons to HTML select options using Angular

Looking for a way to enhance my component that displays a list of accounts with not only the account number, but also the currency represented by an icon or image of a country flag. Here is my current component setup: <select name="drpAccounts" class= ...

ERB failing to interpret Ruby script

Hello, my index.html.erb file looks like this: <h1>Bookmarks</h1> t <% @books.each do |b| %> <div class="book"> e <div class="row"> s <div class="col-md-1"> t <p class="rank-this ...

Hover Effects for CSS Info Boxes

I have a code snippet below for an info box I am working on. How can I adjust it so that when I hover over it, the green background fills the entire height instead of just 3/4 of the height? Any guidance on what may be causing this issue would be greatly ...

How can you transfer data from a jQuery function to a designated div element?

I'm struggling to transfer data from a function to a specific div, but I can't seem to make it work. I'm in the process of creating a gallery viewer and all I want is to pass the counter variable, which I use to display images, and the total ...

What advantages and disadvantages come with choosing between using vh and vw for responsive text?

My main inquiry is centered around the various techniques for creating responsive text. Is there a particular method that stands out as the best option for general use, or do different methods serve distinct purposes? ...

Returning to a page that has been scrolled down and contains dynamic content loaded via ajax

Currently, my page utilizes ajax to load dynamic content. However, I have noticed a discrepancy in how Firefox and Internet Explorer handle the scrolling position when navigating back using the browser's back button. Firefox retains the scrolled posit ...

Steps to eliminate the select all checkbox from mui data grid header

Is there a way to remove the Select All checkbox that appears at the top of the checkbox data column in my table? checkboxSelection The checkboxSelection feature adds checkboxes for every row, including the Select All checkbox in the header. How can I ...

Steps for Adding a class or Id to an Ext.Msg.alert box

Is there a way to customize the style of a specific Ext alert box without affecting all alert boxes? Can someone please explain how to assign a class or ID to an Ext.Msg.alert box? Ext.Msg.alert('Status', 'Changes saved successfully.' ...

Modifying button appearance upon clicking using JavaScript

JavaScript: const buttons = document.getElementsByClassName("togglebtn"); for (let i = 0; i < buttons.length; i++) { buttons[i].onclick = function () { this.classList.toggle("active"); } } html: <md-butt ...