What is the reason for the absence of a shorthand property in CSS that combines width and height measurements?

Have you ever thought about using a shorter property for specifying an item's width and height in CSS?
Currently, we have to write:

selector {width: 100px; height: 250px;}

While this is already pretty fast, wouldn't it be even quicker if we could do something like this instead?

selector {dimension: 100px 250px;}

Is there a CSS pre-processor out there that can make this happen? It seems like it would save a lot of time when dealing with multiple width and height declarations.


Think about the CSS Grid properties for instance:

grid-template-columns: 100px 100px;
grid-template-rows: 100px 100px;  

This could be simplified to:

grid-template: 100px 100px / 100px 100px;

Answer №1

In short: The CSS Working Group has not yet reached a consensus on a name for the shorthand property.


While the concept is not new and has been suggested several times in the past, there is currently a proposal [css-sizing] Adding a 'size' shorthand for 'width'/'height' from the CSSWG (CSS Working Group).

Many aspects have been debated, but some key questions remain unanswered. Here are a few examples:

What should it be called?

Some of the suggestions for names include:

  • size: might conflict with the @page's size-property
  • dimensions: possibly too lengthy or difficult to remember
  • box-size: could be confused with box-sizing

How will it function?

Should it be:

<box-size>: <width> <height>?

… or more closely related to properties like padding or margin:

<box-size>: <height> <width>?

Additionally, should it support an option to maintain aspect ratio?

Which vendors will adopt it?

  • Which companies will back the proposal and implement the syntax?
  • Will it improve the user experience enough to encourage widespread adoption?

The CSSWG recently mentioned the possibility of a shorthand notation in their Teleconference Minutes on 2017-08-16:

The group agreed that a shorthand for ‘width’/’height’ would be beneficial, but decided against using ‘size’ as the name. However, they were unable to come up with an alternative at the time.


That said, you can utilize a CSS pre-processor to simplify your workflow. For example, here's a LESS mixin I use:

.size(@a: null, @b: null) {
    & when not (null = @a) {
        width: @a;
    }

    & when (null = @b) {
        height: @a;
    }

    & when not (null = @b) {
        height: @b;
    }
}

It can be used as follows:

.size(100%, 50%);

width: 100%;
height: 50%;

… and it also accommodates square elements:

.size(100%);

width: 100%;
height: 100%;

Answer №2

While it may seem like a beneficial concept, current official guidelines do not permit its implementation.

That concludes the update for now.

Answer №3

In my humble opinion, I suggest implementing a new shorthand code size for defining the height and width of a selected element.

For example:

size: <height> <width>;
size: 420px 297px;
size: 100% 50%;
size: 15rem 5em;

Furthermore, it is recommended to update the previous use of the size property with @page to now be labeled as page-size.

For reference, check out the following source: MDN Web Docs

/* Keyword values for scalable size */
page-size: auto;
page-size: portrait;
page-size: landscape;

/* <length> values */
/* 1 value: height = width */
page-size: 6in;

/* 2 values: width then height */
page-size: 4in 6in;

/* Keyword values for absolute size */
page-size: A4;
page-size: B5;
page-size: JIS-B4;
page-size: letter;

/* Mixing size and orientation */
page-size: A4 portrait;

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

Enhancing OpenSeadragon images with custom overlays and managing error messages

Currently, I am experimenting with the Basic Single-Row Tile Source Collection example using the same configurations and tile sources outlined in the example. However, I am encountering difficulties in adding overlays to the first and second images as int ...

Is it possible to implement a feature in Angular and Bootstrap where the toggle menu can be closed by clicking anywhere on the page, rather than just the toggle button

I'm working on an Angular project where I've implemented a navbar component. The navbar is responsive and includes a toggle button that appears when the browser window is resized. This button allows users to hide or display the menus. One issue ...

Can CSS be used to consistently position content in a specific manner?

I previously inquired about a similar issue and received an excellent solution. Here is my jsbin http://jsbin.com/nuwagibayo/1/edit?html,css,output where I encountered the following scenario: The positioning of 12.50 seems slightly awkward in its current ...

Ways to Randomly Flip Divs

I have developed an application where all divs flip vertically on hover. I am looking for a way to make the flipping random without requiring a hover. Any ideas on how to achieve this? .vertical.flip-container { position: relative; float: left; ma ...

Creating Custom Styles with Polymer for <content> Elements

Seeking help with styling using the ::content selector and custom CSS properties. Any insight would be appreciated! To simplify, I have a Polymer component utilizing a content tag that will always contain a paper-input: <template> <style> ...

Tips for creating a dynamic variable for an object key in jQuery to streamline your code

Utilizing the TweenMax library, I am adding styles to a div while scrolling. However, I am facing a challenge where I need different styles based on varying page resolutions. To address this issue, I implemented an if condition. Is there a way I can use a ...

Issue with sticky positioning not functioning properly with overlapping scrolling effect

When the user scrolls, I want to create an overlapping effect using the 'sticky' position and assign a new background color to each div (section). However, despite setting 'top' to 0 for the 'sticky' position, the divs still s ...

Having difficulty comprehending the syntax of Linear Gradients

Seeking assistance on understanding how background images and linear gradients function together. Currently, I am only seeing a solid color instead of the intended image. Can someone please explain how this code is supposed to work? background-image:lin ...

generate a customized synopsis for users without storing any data in the database

In order to provide a summary of the user's choices without saving them to the database, I want to display it in a modal that has already been created. Despite finding some sources online, none of them have worked for me so far. Below is my HTML: &l ...

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>& ...

Step-by-step guide on using variables to apply CSS

In our table consisting of 6 rows and 2 columns, the second column displays a Font Awesome circle followed by various values in the database field (such as Outstanding, Very Good, Good, Needs Improvement, and Adequate). I am looking to dynamically change ...

When displaying content within an iframe, toggle the visibility of div elements

I'm attempting to toggle the visibility of certain page elements based on whether the page is loaded in an iframe. <script>this.top.location !== this.location && (this.top.location = this.location);</script> The above code succes ...

Is the text alignment off?

I've been immersed in creating a website, but I encountered an issue for which I can't seem to find the solution. Check out my text alignment here (test) - it's not aligned all the way to the left of the content Here is the HTML code: < ...

Overlapping Image Arrows with CSS Styling

After receiving a design with what appeared to be a simple feature from a designer, I discovered that implementing it was more complex than expected. Specifically, I needed to create a CSS3 arrow with an image as the fill color in order to overlap the unde ...

Opacity is causing issues with hover effects in CSS

I'm facing an unusual CSS challenge. Check out this simple code snippet below that showcases the issue: <html> <head> <style> .hover { float: right; } .hover:hover { background-color: blue; ...

Unable to convert cursor to jpg format

I am facing an issue where I have a jpg file stored in the same folder as my styles.css, and I want to change the cursor on a webpage to this particular jpg file. Despite my efforts, it seems like the cursor is not changing as expected. Currently, I am us ...

CSS animation for horizontal scrolling with sticky positioning is not functioning as intended

I've designed a creative way to achieve infinite horizontal scrolling using CSS animation, and my goal is to make the first element in the list stick in place. Everything works smoothly when I utilize the left property within the @keyframes. However, ...

Create a navigation bar using CSS that is designed from an unordered list without any specific

Is it possible to create a multilevel menu using only CSS for the menu structure mentioned below? Websites like cssmenumaker.com showcase examples of menus with 2-3 level submenus by adding classes like has-submenu. Can we achieve this without adding any ...

Adjust the text placement on the toggle switch to turn it On or Off

I am looking to create a toggle switch with a small size. I found some code on Stack Overflow that I tried but it didn't work as expected. The code snippet I attempted to use is from another helpful post. My goal is to have the "on" text align to th ...

jQuery and CSS animation implemented on website performs flawlessly on all browsers, with the exception

I have recently started learning jQuery and I am facing an issue with the website linked below. It works perfectly in Firefox, Chrome, and Opera but not in Safari. <head> <meta charset="UTF-8"> <meta name="viewport" content="width=devic ...