Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have an element which increases width by fixed increments?

Tags:

html

css

It's kinda hard to explain what I want to achieve... what I want is a container element that:

  • Sizes to it's contents
  • But only by fixed increments - for example, 150px, 300px, 450px, etc.

So if the contents are 100px wide, the container will be 150px wide. If the contents grow to 170px, the container's size jumps to 300px. Etc. The container will never be, say, 200px wide. Either 150px or 300px - whichever is the smallest that fits the contents.

Is this possible with pure HTML/CSS, or does this necessitate Javascript?

like image 486
Vilx- Avatar asked Nov 01 '22 23:11

Vilx-


2 Answers

Short answer: it won't work with CSS only.

A little explanation + workaraound

The design of media queries were not made for specific elements, but either the viewport or the screen size of the device itself. Therefore media queries simply don't know the size of a specific element.

To achieve this effect you have to go beyond pure CSS and use some JavaScript. A very useful tool for this is https://github.com/marcj/css-element-queries, which is a polyfill adding for media queries based on elements. It's also rather cross-browser friendly (works on IE8+).

like image 194
display-name-is-missing Avatar answered Nov 12 '22 15:11

display-name-is-missing


Not possible with CSS alone, but could work out a solution using JavaScript. :D

like image 30
Andrew Avatar answered Nov 12 '22 16:11

Andrew