I have an element that I want to increase in width as its parent decreases in width
The equation should look something like:
width:calc(150 + 500 / 100%);
But at least in Chrome it says the property is invalid whenever I try to divide by percent width.
Is this possible? (Alternatives to calc() are acceptable)
EDIT
I added spaces (didn't realize about that). Tried it with a variety of units, no luck yet.
Fiddle
<div style="width:100%;position:relative;">
<div style="width:calc(150px + (500 / 100%));position:absolute;top:0;left:0;">This one should get bigger as the page gets smaller</div>
</div>
Thought process:
Fixed width (150px) plus 500 divided by the current parent width.
So if the parent is 500px:
150 + 500/500 -> 150 + 1 = 151
Parent is 100px
150 + 500/100 -> 150 + 5 = 155
Parent is 20px
150 + 500/20 -> 150 + 100 = 250
Solution is so simple it's mind-boggling. Move the 500px into the first part and subtract the width.
width:calc(650px - 100%);
Gets wider as its parent gets narrower.
Updated fiddle
It seems like you've found your solution already, but I'll answer specifically why your original code wasn't working:
I'll start with the syntax for a product equation (when you divide) in calc():
<calc-product> = <calc-value> [ '*' <calc-value> | '/' <number> ]
The spec for the calc() property syntax is a bit more complicated than it sounds. When dividing in calc() as you're doing here, the right side must be a number. You cannot use "unit-ed" values:
Number values are denoted by <number>, and represent real numbers, possibly with a fractional component.
When written literally, a number is either an integer, or zero or more decimal digits followed by a dot (.) followed by one or more decimal digits and optionally an exponent composed of "e" or "E" and an integer. It corresponds to the production in the CSS Syntax Module. As with integers, the first character of a number may be immediately preceded by - or + to indicate the number’s sign.
That is to say, you cannot divide something by a percentage value like 100%.
calc() need spaces between the operators ( but just in + and -), but you are missing the units, which could be px, em,rem etc..., so would something like this:
width:calc(150px + (500px / 100%))
This is would be invalid because as explained by #TylerH, you can't divide by un-ed values(px, %, etc).
But if was possible, when you divide by 100% you are multiplying by 1, so basically you'll stay the same because 1 is the neutral value for multiplication, so this would be useless to do.
Hardly to know, because there isn't much to see in your question, and still invalid, but I'm guessing you are looking for something like this instead:
width:calc(150px + (100% / 500px))
Given your Edited question AFAIK you have to use JS to achieve this, unless you could provide a Fiddle.
Related
I'm writing a calculation for the left property of an absolutely positioned element. After reading the syntax the only thing I can think of is that I'm trying to multiply two different units but I can't find confirmation for that as I thought the first calculation would have resolved to an integer.
left: calc(1vw * ((100vw / 100) * 1.2));
I need to capture the full size of the viewport so 100vw and then divide it by 100. So if the screen is 1600px this should resolve to 16, then multiply by 1.2 so now it is 19.2 and finally multiply by 1vw to convert it to 19.2vw. The issue I can't confirm is whether the first calculation resolves to an integer (16) or a measurement (16px). If the former then I have no idea why this isn't working. If the latter, how do I get around this?
See MDN on calc:
Multiplication. At least one of the arguments must be a number.
Your expression is trying to multiply 1vw by another vw amount and hence is not valid.
I've been trying to do a complicated calculation in CSS to get rid of media queries on a page, and not require too many of them. The second part of the code below with the long decimal number is me trying to use a set number to calculate the percentage of something (a padding). That number almost perfectly represents the percentage needed per pixel of a view-port, so if I had a massive view-port it could still calculate the percentage or even an extremely small one. But since this number needs to be a percent and I can't just put a percent sign at the end. I needed to get it into a percent via pixels. Meaning calculate how many pixels the browser would calculate if it was a percent. I figured out to take the actual 100 percent width of the page and divide it by 100 to get 1% of the page in pixels. I then multiply that by the number we got for the percentage of the page in the second part of the calculation, giving me the actual pixel width as if it were a percent, and as if the browser had calculated this itself. This is the calc operation I used, that is not working properly:
padding-left: calc((100% / 100) * (0.013641975308642 * 100vw));
It's not working and when we look with chrome dev tools, it tells me the object is invalid. What am I doing wrong?
The idea was to put a tooltip with value related to slider. I thought initially to accomplish that task by using css grid. CSS provides you with grid of any size, 10 or 1000 cols, doesn't matter. By utilizing grid functionality, we can align out tooltip as we wish.
What we really get is:
Thumb position is sort of unpredictable. It seems that it is being offset, and the direction of that offset is dependent on whether input value is in the left or right part of slider.
Note: default thumb behaves exactly the same way. I mean, shape of thumb is not of concern.
So, how does html calculate position of thumb based on its value?
For anyone else stumbling upon this problem, the issue is that the first and last position of thumb are offset by half the width of thumb. Because of this, all of the other positions are slightly offset to compensate for the first and last one.
One simple solution for this is to normalize the thumb position between actual first and last thumb positions which are actually 0 + halfThumb and width - halfThumb respectively.
To normalize, we can use the formula recommended in this answer.
So the left offset of absolutely positioned element in px would be:
const left = (((value - minValue) / (valueMax - valueMin)) * ((totalInputWidth - thumbHalfWidth) - thumbHalfWidth)) + thumbHalfWidth;
Where
value is actual input value.
minValue is actual minimum value for input.
maxValue is actual maximum value for input.
thumbHalfWidth is half of the width of thumb used for slider drag.
totalInputWidth is width of input element in pixels.
You have to take into consideration the size of your thumb
It is possible to achieve this using a little bit of javascript (unless you can get the range ratio which I am not aware of as of now)
Therorical
Calculate the ratio of your current value relative to the min and max of your input (between 0 and 1)
(value - el.min) / (el.max - el.min)
So its position (starting from the arrow relative to the input container) would be:
thumbSize / 2 + ratio * 100% - ration * thumbSize
Method
This might give an idea of how to implement this in javascript
const thumbSize = 10
const range = document.querySelector('input[type=range]')
const tooltip = document.querySelector('.tooltip')
range.addEventListener('input', e => {
const ratio = (range.value - range.min) / (range.max - range.min)
tooltip.style.left = `calc(${thumbSize / 10}px + ${ratio * 100} - ${ratio} * ${thumbSize}px)`
}
This above code has not been tested but the same method has been implemented
When transferring the fixed widths, margins, paddings etc. of a layout into percentage-values I use the formula: (target * context) / 100
For example: If I calculate with that formula ...
(350px / 1024px) * 100%
I get (depending on the digits of the calculator) => 34.1796875 ... %
How many digits after the dot do I have to use that I can expect my layout to work as expected?
Is there a rule of thumb at which after comma digit one shall round?
I recommend You to use calc();
You can use this function in CSS to calculate for example width of Yours divs.
So You dont have to calc this by Your own, and thinking about how many digits will be good. Just use calc(), and then CSS will be thinking about it for You.
Example of code:
div{
width: calc(350px / 1024px);
}
Viewport height usually returns an integer value; however I am now noticing it returning a value with 2 decimal points.
Why / how do viewport units get rounded?
(Additionally, I am not resizing my browser window -- yet 100vh returns 616px sometimes, other times 616.36px)
The specification merely prescribes that 1 unit of vw is:
1% of viewport’s width
This ought to be reasonably precise, as 100 of these units should fill the entire width, so rounding will cause problems here.
The CSS definition of number includes integers and decimals see v2 or v3.
In the cases of the browsers that return an integer, if the view port is not coincidentally exactly divisible by 100, and if the result of 1vw * 100 is significantly off - it is worth raising a bug with the browser vendor.
Result of browser test... note that size difference is down to browser chuff and tool size.
Firefox seems to reliably calculate to 4dp:
height: 3.06667px;
width: 6.76667px;
An old version of IE I have lying around similarly reliable, but to 2dp:
height: 3.78px;
width: 6.76px
How are you obtaining your numbers?