diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index da2bf77e66..204c0b490e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -216,8 +216,16 @@ Optional StyleProperties::z_index() const auto value = property(CSS::PropertyID::ZIndex); if (value->has_auto()) return {}; - if (value->has_integer()) - return value->to_integer(); + if (value->has_integer()) { + // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines. + // NOTE: Casting between 32-bit float and 32-bit integer is finicky here, since INT32_MAX is not representable as a 32-bit float! + auto integer = value->to_integer(); + if (integer >= static_cast(NumericLimits::max())) + return NumericLimits::max(); + if (integer <= static_cast(NumericLimits::min())) + return NumericLimits::min(); + return static_cast(integer); + } return {}; }