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

Show an SVG overlay when hovering over an image

Is there a way to create a hexagon shape around an image when it is hovered over using CSS only, even if the image itself has a circular border-radius of 50%? ...

Text encoded in UTF-8 emerges from the surrounding frame

As someone who is relatively new to frontend development, I recently created a blog using a template that utilizes Next.js, Tailwind, and React. However, when I input UTF-8 text in Korean, the text overflows outside of the parent frame on mobile devices. ...

Unable to utilize SASS variables in Angular from a global file, even though other styles are functioning correctly

After implementing the SASS code in my component's style, it functions correctly. $test-color: pink; div{ background-color: $test-color; } However, when I transfer the definition to styles.scss, the desired outcome is not achieved. I have attempted u ...

Is there a way to display x-overflow on a table instead of the window?

Check out the table at the following link: In my CSS, I set a min-width and overflow-x property for the table. However, on the mobile template, the overflow-x does not work properly. I want the template to display correctly with the header and footer in ...

I'm working on a CSS project and my goal is to ensure that all the items are perfectly aligned in a

I have been working on a ReactJS code and I'm struggling to achieve the desired result. Specifically, I am using Material UI tabs and I want them to be aligned in line with an icon. The goal is to have the tabs and the ArrowBackIcon in perfect alignme ...

Make sure all the table rows have the same height to completely fill the table regardless of the number of rows

Looking for a solution to create a table with a fixed height of 280px, whether it has 3 or 4 rows. Is there a method to adjust the row heights based on the number of rows in the table, so that they fill up the table's height? To clarify, when the ta ...

What is the best way to position a title and subheading alongside an image?

I am currently working on developing a blog using php and html to enhance my coding skills. On my website, I have a special category where I display posts, the number of which can vary. Each post is composed of a title, a subheading, and a placeholder div ...

I need either XPATH or CSS to target a specific checkbox within a list of checkboxes wrapped in span tags

I'm trying to find the XPATH or CSS locator for a checkbox in an HTML span tag that has a label with specific text, like "Allow gender mismatch". I can locate the label using XPATH, but I'm not sure how to target the input tag so I can click on t ...

Include a tooltip on every image by utilizing the title attribute

<br><img title="Cool as ninja!" src="/images/profile2.png" width="300"></br> <br> <strong>Who's this pretty girl ninja!?</strong> <br><img title="Cool dude bro!" src="/images/profile5.png"></br> & ...

Creating a line break in HTML using Bootstrap

Reference the source provided, specifically the red alert option. Here is an example: <div class="alert alert-dismissible alert-danger"> <button type="button" class="close" data-dismiss="alert">&times;</button> <strong> ...

The runtime is experiencing difficulty in loading the Bootstrap files

File Structure in eclipse The issue is that the CSS files are not loading at runtime, causing me to lose the design I created. Below is my code for the login page. When clicking on the links for the CSS files, they open in the Eclipse IDE instead of load ...

Positioning the box in the middle of pages

I have a website page at the following link: However, the content is not centered on the screen. Here are the codes I'm using: <div class="section_inner"> <div class="container"> <div class="row"> <?ph ...

Designing websites using elements that float to the right in a responsive manner

Responsive design often uses percentage width and absolute positioning to adjust to different screen sizes on various devices. What if we explore the use of the float right CSS style, which is not as commonly used but offers high cross-browser compatibilit ...

Learn how to design a customized loading screen using CSS with Phonegap

Currently working in Android with Phonegap / Cordova, I am faced with the challenge of optimizing page loading time due to numerous DOM objects being created. To address this issue, I am attempting to implement a "Loading..." screen to prevent users from b ...

CSS ISSUE: Issue with hidden overflow causing white space next to border in nested divs

When working with a div inside another div and setting overflow to hidden, you may notice a whitespace issue around the border when zooming in and out on Chrome. Firefox handles this situation better, showing white space only in the corners of the border. ...

The menu content has a 50% chance of appearing when the open menu button is clicked

Hey there, I'm currently facing an issue with a menu I created that slides open to the bottom when a button is clicked. The problem I'm encountering is that the content of my menu only appears about half of the time... I'm not quite sure how ...

Disappearing Bootstrap 3 Dropdown Issue Caused by Tab Click

There is an issue with the drop-down menu I created: When I click on tabs like ALL or FILM, it closes all elements. To reopen, I have to click again on the button PRODUCT.dropdown-toggle. The code example looks like this: var App = function () { ...

break-word property not functioning correctly within pre tag

Each row in my dataTable triggers a modal to appear: <div id="verifyModal" class="modal"> <div class="modal-content"> <h2>Verify # <span id="verify-modal-id"></span></h2> <pre><code class="" id="verif ...

When the anchor tag is clicked, I'm displaying the DIV for Customer Testimonials, however, the expansion of its height is not

One way to display customer testimonials is by using flexslider to create a horizontal scroll. The testimonials are hidden and only shown when the "view all testimonials" link is clicked, which can be seen in this JSFiddle example: Working : Demo JQuery ...

Utilize page variables to activate audio clips upon clicking

Can someone assist me with a script that activates an audio clip when five icons on a page are clicked? The image of the icons can be found below: https://i.stack.imgur.com/mBC6o.png Here is the HTML code: <div class="slide overlay-container" id="int ...