Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector based on width?

Is there a CSS rule that would match elements based on their width? If not, would it be interesting to propose that in the standard?

One could do something like code:max-width(200px) { white-space: nowrap; } to have all small code tag force line break.

I know I could use some js.

like image 483
Robert Vanden Eynde Avatar asked Mar 01 '18 18:03

Robert Vanden Eynde


People also ask

How do I set auto width in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

How is CSS width calculated?

The width property in CSS specifies the width of the element's content area. This “content” area is the portion inside the padding, border, and margin of an element (the box model). In the example above, elements that have a class name of . wrap will be 80% as wide as their parent element.

What does width auto do in CSS?

Width: auto When an element has auto as a value for width, it can have margin, padding, and border without becoming bigger than its parent element. The width of its content box will be the content itself with the subtraction of margin, padding, and border.

How do you override a width in CSS?

The max-width property in CSS is used to set the maximum width of a specified element. The max-width property overrides the width property, but min-width will always override max-width whether followed before or after width in your declaration.

What are the basic CSS selectors?

This page will explain the most basic CSS selectors. The element selector selects HTML elements based on the element name. The id selector uses the id attribute of an HTML element to select a specific element.

How do I select a specific class in CSS?

The CSS class Selector The class selector selects HTML elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the class name.

What is the adjacent sibling selector in CSS?

It is a very rare but necessary css selector. Add background-color to only those button elements which are direct children of .container The adjacent sibling selector selects all of the siblings that are next to each other on the same line. It contains ' + ' sign.

How to group selectors in CSS?

The grouping selector selects all the HTML elements with the same style definitions. Look at the following CSS code (the h1, h2, and p elements have the same style definitions): It will be better to group the selectors, to minimize the code. To group selectors, separate each selector with a comma.


1 Answers

Is there a CSS rule that would match elements based on their width?

No.

If not, would it be interesting to propose that in the standard?

Maybe, but not the Selectors standard, because Selectors is not intended to be tightly coupled with the styling aspect of CSS.

There were proposals to standardize something called "element queries" a few years ago, but other than a few proofs-of-concept, they seem to have mostly dissipated. Probably because they're just not feasible to spec and implement in a way that is robust (cyclic dependencies immediately come to mind).

like image 63
BoltClock Avatar answered Sep 29 '22 10:09

BoltClock