What is the <meter> element in HTML?
The <meter> element in HTML represents a scalar measurement within a specified range or a fractional value. <meter> can help a user gauge the level of an entity, e.g., disk usage, battery percentage, etc.
It is important to note that the <meter> element should not be used to indicate progress (HTML provides a <progress> element for this purpose). Similar to most HTML elements, the <meter> element can be styled using CSS.
Attributes
The <meter> element includes the following attributes:
max: The maximum value of the measured range.min: The minimum value of the measured range; this value must be less than the value of themaxattribute. Theminvalue is by default.value: A required attribute that specifies the current numeric value; this numeric value must be within the range specified by theminandmaxattributes. Thevalueattribute will contain if unspecified or malformed.low: The upper bound for what is considered a “low” value in the measured range. The value oflowmust be greater thanmin, and less than bothmaxandhighif specified. If unspecified, the value oflowis set equal tomin.high: The lower bound for what is considered a “high” value in the measured range. The value ofhighmust be less thanmax, and greater than bothminandlowif specified. If unspecified, the value ofhighis set equal to the value ofmax.form: Specifies which<form>element the<meter>element is associated with.optimum: Specifies the optimal value for the measured range.
The <meter> element also includes global attributes and supports all the usual events supported by HTML elements.
Example
The code below shows how the <meter> element works in HTML:
Explanation
The code above initializes identical <meter> elements that differ only in their value attribute to represent a low and high battery level.
Each <meter> element is associated with a corresponding <label> element. You can do this by ensuring that the id attribute of the <meter> element matches the for attribute of the <label> element it is meant to associate with. For example, the <meter> element in line is associated with the <label> element in line because the id and for attributes both contain the string “low_battery.”
Since the min and max attributes are specified as and respectively, the value attribute of these <meter> elements can take any numerical value in this range. Since the value of low is set to , values between and are considered “low” for the specified range. Similarly, values between and are considered “high” because the high attribute is set to .
The value of the <meter> element in line is , which falls in the range of “low” values. Therefore, the color of the <meter> element indicates a low battery level. In contrast, the value of the <meter> element in line is , which falls in the range of the “high” values, and so the color of the <meter> element indicates a high battery level.
Note: For further details regarding the
<meter>element, you can check the documentation.
Free Resources