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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With