What happens when a `calc()` width has an invalid value and is not ignored?

After coming across this question, I stumbled upon an intriguing observation.

Given that negative widths are considered illegal and should be disregarded, one would assume that style properties such as width:-200px; and width:calc(0% - 200px); would have no impact. Surprisingly, the former is indeed ignored while the latter results in a width of 0 being applied.

Take a look at this example:

div {border:1px solid; background:lime;}

#badwidth {width:-200px;}

#badcalc {width:calc(0% - 200px);}
Div with negative width:
<div id="badwidth"> </div>

Div with calculated width:
<div id="badcalc"> </div>

If the width property was truly ignored, the div would occupy its full width like the first one. However, it collapses to a width of 0 (as seen by the black border line on the left).

To provide further context, here are some additional div elements to illustrate 1) that calc(0% - 200px) alone is not problematic, and 2) they function correctly, indicating that the initial snippet is an anomaly.

div {border:1px solid; background:lime;}

#calc {margin-right:calc(0% - 200px);}
Plain div:
<div> </div>

Using calc(0% - 200px) appropriately:
<div id="calc"> </div>

So, why do regular widths and calculated widths display contrasting behaviors?
If this discrepancy existed solely in one browser, it could be dismissed as a bug. Yet, all browsers exhibit the same behavior! Therefore, there must be some factor that has eluded me. What could it be?

Answer №1

The calculation of lengths and the permissible values in a rule can vary, as you have observed. As outlined in CSS Values and Units:

While certain properties may allow negative length values, this could complicate layout and there may be specific limits to implementation. If a negative length value is allowed but cannot be supported, it should be rounded to the nearest supported value.

In this instance, the value -200 is adjusted to 0 since that is the closest supported width value.

Edit

The preceding section also notes:

Properties might restrict the length value within a particular range. If a value falls outside this range, the declaration becomes invalid and must be disregarded.

When setting width: -200px, this value exceeds the acceptable range and is therefore ignored. Yet, during evaluation with calc, -200 is calculated before the check, resulting in rounding up to an acceptable value within the range.

Update

Credit to @CBroe for referencing the spec on calc through this link:

Values are not checked against ranges at parse time within calc(), hence out-of-range numbers do not invalidate declarations. However, the evaluated value must still adhere to the context's specified range.

Given that widths less than 0px are not permitted, these three declarations are functionally equivalent:

width: calc(5px - 10px);
width: calc(-5px);
width: 0px;

It is important to note that while width: -5px does not equate to width: calc(-5px)! Out-of-range values beyond calc() are syntactically incorrect, leading to the entire declaration being discarded.

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

PHP database results placed one on top of the other

My current code snippet looks like this: while ( $row = mysqli_fetch_assoc($result) ) { echo '<div class="leaders" style="background-color:#ada30a;">' . $row['playername']; echo "<br>"; echo $row['points&a ...

Is it possible to resume CSS animation when the page is first loaded?

I'm looking for a way to ensure that my CSS animations on the elements in my header.php file continue smoothly when navigating to different pages. For example, if I have a 200-second looped animation and navigate to the "about us" page 80 seconds int ...

Modify the hue of the iron-icon upon being tapped

There's a simple example I have where my goal is to modify the color of an iron-icon when it's tapped. To achieve this, I'm utilizing iron-selector for tapping: <template> <style> :host { display: block; padding: 10 ...

Applying padding to an absolute div inside a Bootstrap 4 parent container does not function correctly

I'm trying to create a div with absolute position inside of another div with relative position using Bootstrap 4 like this: .p-r{ padding:8px; background-color:#ddd; height:100px; } .p-a{ bottom:3px; background-color:#000 } <link re ...

The landscape orientation causes the button bar to overflow by 100% of the body height

Imagine the scenario below: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> html, body { padding: 0; margin: 0; ...

Issue with continuous loader malfunction

I integrated a 3-second mini-loading animation on my website. It shows up every time I refresh the site or navigate to another page. The issue I'm facing is that once I add the loading section, it never stops (it should only last for 3 seconds). Usua ...

dynamic resizing, uniform dimensions

Is it possible to use CSS to ensure that an image is 100% the width of a fluid div without distorting the square shape? The goal is to match the height to the width for a cohesive look. Currently, I am using the following code for fluid width: .img{ max ...

What are the best methods for creating a responsive webpage with images and text in the header?

My webpage features a razor page with a header component called PageHeader.razor. I have been attempting to make it responsive, but I am facing an issue where the text and image overlap when viewed in responsive mode. What could be causing this problem? ...

Ensuring that a header one remains on a single line

Within this div, I have set the width and height to be 250px. Specifically, the h1 element inside the div has a height of 50px. Users who are updating content on my website can input any text they desire within this box. However, when the text within the ...

Deleting a <div> element solely by its unique identifier in plain HTML text without any tags can be achieved in the following way

After my previous question was closed, I realized that the provided answer did not meet my needs. I am actually looking to hide not just the HTML text Received, but the entire row containing 0 <3 Received. A simple display: none; would suffice for this ...

Achieve scrollability for right column (div) using Tailwind CSS

I am having trouble getting the content in the right column to scroll vertically. Can someone please help me identify what I'm doing wrong here? <link href="https://cdn.jsdelivr.net/npm/tailwindcss/dist/tailwind.min.css" rel="stylesheet"/> < ...

Strategies for smoothly navigating the page to a specific div every time

Currently, I am working on a form that requires submitting multiple child forms. To enhance user experience, I have incorporated jQuery functionality to display a message at the top of the page upon each submission. Now, my goal is to implement a feature w ...

Regular expression for selecting characters and numbers in jQuery using a selector

Can someone help me figure out how to select all IDs like this: r1 r2 r3 etc. and not the other IDs like helloWorld I attempted using the CSS selector with jQuery but I'm having trouble understanding how. I tried $('#r[0-9]') but it didn& ...

Making Explorer 11 wrap width with Flexbox

After implementing a simple code using flexbox, everything seemed to be working flawlessly on Firefox, Safari, Chrome, Edge, and Opera, but encountered issues with IE11. The problem arises with responsive frames containing images and text within a specific ...

What steps should I take to repair my image modal button?

Within my application, I have an image modal placed inside a header tag, along with a label. There is some space between the label and the image modal. However, when the application is run, the page appears in a fixed position, but the label message does n ...

Achieving precise column alignment in Bootstrap 4

After searching extensively on Google and various websites, I am still struggling to find a solution for vertically aligning columns to be centered. Many sources I found mention aligning horizontally, but none seem to address the issue of vertical alignmen ...

Ensure the background image completely fills the div, regardless of the image's aspect ratio

Can anyone help me figure out how to fill a div's background with an image, regardless of its aspect ratio (landscape or portrait)? I've tried different methods from using background-size: cover; to background-size: 100% 100%;, but the div doesn& ...

What is the process for setting an object's property as a href in an AJAX call?

I've got a quick query. How can I assign an object's property to the href attribute? I've indicated the line with a comment. success: function (listOfTags) { let tagData = ''; $.each(listOfTags, function (i, tag) { ...

Develop interactive div elements with the help of JavaScript/jQuery

Exploring dynamic div creation using javascript or jquery and looking for guidance. <div id="clickable"> <button class="start">Default</button> <button class="random">Randon</button> <button class="gradient"> ...

Creating a JQuery statement to conditionally change CSS values

Is there a way to determine if a div element with a CSS class of "x" has a height set to "auto"? If so, I would like a jQuery script to remove the CSS class "a" from all elements with the class "y". If not, the script can remain unchanged. Thank you. ...