What is the reason that CSS does not have built-in support for variables and hierarchy?

I am relatively new to UI development and I find CSS to be quite challenging to work with.

Scenario: [ Experimented with CSS & less ]

I wanted to apply specific styles within a particular div on a page.

CSS approach :

div.class1 {
    font: normal 12px arial, helvetica, sans-serif;
    color: #f30;
}
div.class1 div.class2 {
    border: 1px solid #f30;
}

Less approach :

@red: #f30;
@font-family: arial, helvetica, sans-serif;
div.class1 {
    font: normal 12px @font-family;
    color: @red;
    div.class2 {
        border: 1px solid @red;
    }
  • When writing CSS, it can lead to errors as you need to repeat #f30 & div.class2 multiple times for hierarchy and variable re-use.

My Inquiry :

  • Why does CSS seem to complicate things?
  • Is Less simply enhancing and converting to CSS without offering anything unique?
  • What is the reasoning behind CSS promoting such repetitive coding practices?

I believe CSS should adopt the simplicity of Less. Perhaps I'm missing an important aspect of why CSS is structured that way.

I initially thought this was an outdated issue, but I was surprised by the lack of solutions in CSS3.

Please guide me on how to approach CSS more effectively.

Answer №1

The evolution of CSS has been slow, with outdated user agents lingering around for far too long before becoming obsolete. It seems unlikely that we will see any significant changes in CSS for quite some time.

In addition, I believe CSS and HTML were initially designed to cater to individuals without much experience in programming languages.

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

The display of Bootstrap Cards may become distorted when incorporating J Query

I am attempting to design a dynamic HTML page that pulls data from a JSON response using an AJAX request. My goal is to display three columns in a row, but currently I am only seeing one column per row. I would greatly appreciate any guidance on what migh ...

The issue with Thymeleaf recursive function not functioning as expected

I am attempting to create a recursive list using Thymeleaf. I have a simple Java object that represents a node with a description and an array list of child nodes. However, my current HTML/Thymeleaf structure is not properly iterating through the nested le ...

Does using .stopImmediatePropagation() in the click event of a menu item have any impact on analytical tools?

Scenario I've implemented a navigation menu that loads subpages into a div using AJAX. While everything seems to be working fine, I noticed a bug where if I navigate to a subpage, then return to the main page and revisit the same subpage, it gets loa ...

Creating responsive list items using Bootstrap 4 and Flexbox: Adjusting the width of <li> elements to fit within containers

Struggling with expanding li elements to match the container width? Despite tweaking individual widths and Flexbox properties, the desired outcome remains elusive. How can the width of each li be adjusted to align with the container, mirroring the dimensio ...

Displaying server errors in an Angular componentIn this tutorial, we

As I work on creating a registration page, my focus has been on posting data to the server. I have successfully implemented client-side and server-side validation mechanisms. Managing client-side errors is straightforward using code such as *ngIf="(emailAd ...

Expanding Div Heights that Adjust to the Browser's Width

Looking to set up a responsive height for a div, almost there but facing some distortion when resizing the browser. Here is the fiddle file for reference: https://jsfiddle.net/td5n8ky9/10/ The issue lies in the bottom 2 divs - aiming for a grey bar the wi ...

PHP Ajax login form to instantly authenticate without redirecting to a new page

After attempting to implement Ajax in my login form by following two different tutorials, I am still facing the issue of error messages redirecting to another page instead of displaying on the same page. I have tried to troubleshoot and compare my code wit ...

Ways to include text underneath icons using HTML and CSS

Seeking guidance on how to display text below icons on my website. Can someone please provide instructions? <div class="bottom-bar div-only-mobile" style="position: fixed; bottom: 0; width:100%"> <a href="Dashboard"> <ion-icon name= ...

Easy steps to launch Excel application with an HTML button using a web browser

How can I create a button on an HTML page that will launch an Excel application when clicked by the user? <button class="btn btn-primary" onclick="window.open('excelApplication.url','_blank')">Launch Excel</button> ...

Tips for ensuring the Google+ JavaScript tag is W3C compliant

I have a Google+ button on my website that is functioning properly. However, when I run it through the W3C validator, an error is detected: The text content of the script element does not meet the required format: It was expecting a space, tab, newlin ...

Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images. First screenshot below shows that it works ...

Using absolute positioning on elements can result in the page zooming out

While this answer may seem obvious, I have been unable to find any similar solutions online. The problem lies with my responsive navbar, which functions perfectly on larger screens. However, on mobile devices, the entire website appears zoomed out like thi ...

Transitioning between different hues using specific fraction values

On my website, there is a variable called 'x' which represents a percentage. I am looking for a way to assign colors based on this percentage - with 0% being red and 100% being blue. For example, if 'x' is 50%, the color should be a mix ...

JavaScript Refresh Function Malfunctioning

Currently, I have a JavaScript code snippet on my website that automatically refreshes an iframe every three seconds. window.setInterval(function() { reloadIFrame() }, 3000); function reloadIFrame() { var frame = document.getElementById("if ...

Similar to using `display:flex` and `justify-content: center`, you can achieve centered

I have a group of n children within a div. I want to arrange them in a single row and center them horizontally. If I were to use flexbox, I could achieve this by: display: flex; justify-content: center; Is there a way to accomplish the same layout using ...

Decoding HTML with Python

My current project involves creating a program that can find the definition of any given word and return it. I managed to achieve this using RegEx to search for text between specific tags where the definitions are stored. However, I am now seeking a more ...

Received undefined response from Axios.get() request

While working with the code below, I encountered an issue. The axios get request from await axios.get('/products/api') is functioning properly and I can see the data in the console. However, for await axios.get('/users/api'), 'Unde ...

Using mySQLi to store query results in an array and display them in separate DIV containers

I am looking to efficiently query the content of multiple IDs (from 1 to 6) from a mySQL database using mySQLi. Once I retrieve the content, I want to display it in separate DIVs. Currently, I am using the following code which works perfectly: <?php $c ...

In Bootstrap cards, image overlays are always displayed on top of the image

I'm struggling to understand why I can't get the image in this HTML snippet to appear anywhere other than on top. Despite my attempts to adjust z-index, use !important overrides, and try other methods for hours, I haven't been successful. My ...

Create a screen divided into two separate sections using horizontal split dividers

Trying to figure out how to set a div in HTML and then have a second div take up the remaining space. It seems like it should be simple, but I'm struggling with it. I'd like to have a div with a fixed height and then have another div take up the ...