Replaced Elements in HTML: Myths and Realities

Share this article

Replaced Element Myths

Some HTML elements have always caused headaches for front-end developers everywhere, like iframes, applets, embedded objects or form controls. One could never have complete control over their display, because they look different from one browser to another, from one OS to another.

Replaced Element Myths

Countless libraries and frameworks created replacements, from jQueryUI to Bootstrap, and many more. But have you ever wondered why those elements behave the way they do? Some voices (including a few well-known websites) will point their finger to a fairly obscure definition from W3C, namely the one concerning replaced elements. But are they 100% right? This is the quest we will chase together today.

What are Replaced Elements?

Any good quest must start with research. Therefore let’s have a look at what the official specs say a replaced element is:

An element whose content is outside the scope of the CSS formatting model, such as an image, embedded document, or applet. For example, the content of the HTML IMG element is often replaced by the image that its “src” attribute designates. Replaced elements often have intrinsic dimensions: an intrinsic width, an intrinsic height, and an intrinsic ratio. For example, a bitmap image has an intrinsic width and an intrinsic height specified in absolute units (from which the intrinsic ratio can obviously be determined). On the other hand, other documents may not have any intrinsic dimensions (for example, a blank HTML document).

Source: 3. Conformance: Requirements and Recommendations > 3.1 – Definitions

So far, so good – we got a general description of what a replaced element is. Before we go deeper into this spec section, let’s make a small detour and clarify the “intrinsic dimensions” part.

What are Intrinsic Dimensions?

The best definition available now for intrinsic dimensions comes from the CSS Image Values and Replaced Content Module Level 3 document:

The term intrinsic dimensions refers to the set of the intrinsic height, intrinsic width, and intrinsic aspect ratio (the ratio between the width and height), each of which may or may not exist for a given object. These intrinsic dimensions represent a preferred or natural size of the object itself; that is, they are not a function of the context in which the object is used. CSS does not define how the intrinsic dimensions are found in general.

Here are some examples:

  • A normal image has all three dimensions: width, height and aspect ratio.
  • An SVG image can have only the aspect ratio. Its scaling nature means that the width and height are not restricted to a single value.
  • An empty HTML page, inserted via an iframe element has no dimensions at all.

The way these properties are tied to one another means that there can be no object with only two dimensions. Knowing two of them automatically determines the third one.

Overall this means that such an object, when placed into the page, will provide these intrinsic dimensions to the document so that they can be rendered properly. This information will be useful later, but for now, let’s return to the main path.

Replaced Elements in the Real World

For a full description of the replaced elements we need to go to a different resource, namely the Rendering section of the HTML Living Standard document. As one goes deeper into the specs, it’s easy to see how this topic can be confusing. This is because some HTML elements act as replaced elements all the time, while other do it only in specific circumstances.

Fortunately the Subsection 14.4 gives us all the information we need to properly understand each case.

Embedded content

The first category of replaced elements is embedded content. This includes any element that imports another resource into the document, or content from another vocabulary that is inserted into the document. These external resources, by their own nature, have the intrinsic dimensions that match the requirements of the definition.

The main elements in this category are embed, iframe, and video. These elements are always treated as replaced elements because they always import external content into your document.

Things are a bit more complicated with elements that fall into this category only in special circumstances:

  • applet – Treated as a replaced element when it represents a plugin, otherwise it’s treated as an ordinary element.
  • audio – Treated as a replaced element only when it is “exposing a user interface element”. Will render about one line high, as wide as is necessary to expose the user agent’s user interface features.
  • object – Treated as a replaced element when it represents an image, plugin, or nested browsing context (similar to an iframe).
  • canvas – Treated as a replaced element when it represents embedded content. That is, it contains the element’s bitmap, if any, or else a transparent black bitmap with the same intrinsic dimensions as the element.

These are the only cases where the elements above are treated as replaced elements (which covers almost all cases where these elements are used). For full details on this, please see WHATWG’s rendering rules for embedded content.

Images

Next on our list are images. In the vast majority of cases (when the image is displayed properly), the img element is treated as a replaced element with the intrinsic dimensions of the image. This category also includes the input elements with a type="image" attribute.

Things get a bit more complicated when, for various reasons, the image is not rendered on the page. A missing image that the browser expects it will render eventually has to be treated as a replaced element whose content is the text that the element represents (optionally an icon can also be added). The <input type="image"> will be displayed as a normal button instead. The complete set of rules and conditions can be reviewed in the spec.

Default Size of Replaced Elements

This is where the information we gathered about intrinsic dimensions becomes useful. We have established already that the width and height properties of a replaced element come from the intrinsic dimensions of the content they bring into the page. If these dimensions can’t be calculated (like in the case of an iframe loading a blank HTML page), the browser must fall back to the default rules, as specified in Visual formatting model details. The set of rules itself is fairly large and complicated, but we are only going to focus on the “nothing else works” ones:

Otherwise, if ‘width’ has a computed value of ‘auto’, but none of the conditions above are met, then the used value of ‘width’ becomes 300px. If 300px is too wide to fit the device, UAs should use the width of the largest rectangle that has a 2:1 ratio and fits the device instead.

Source: 10.3 Calculating widths and margins

Otherwise, if ‘height’ has a computed value of ‘auto’, but none of the conditions above are met, then the used value of ‘height’ must be set to the height of the largest rectangle that has a 2:1 ratio, has a height not greater than 150px, and has a width not greater than the device width.

Source: 10.6 Calculating heights and margins

We can summarise all this in three basic rules:

  • if the object has explicit width, height and ratio values, use them;
  • if the object only has ratio, use auto for both width and height while maintaining the said ratio;
  • if none of these dimensions are available:
    • use width: 300px; height: 150px when the viewport is larger than 300px
    • use “auto” for both width and height and a ratio of 2:1 when the viewport is smaller than 300px;

Here is an example illustrating the situations described above. We have a bitmap image, an SVG image, and an iframe. As you can see, the image is displayed at its normal size, the SVG keeps the aspect ratio but takes all available space while the iframe defaults to a maximum of 300×150. If we narrow down the viewport, the iframe keeps at all times an aspect ratio of 2:1.

See the Pen Replaced Elements Intrinsic Dimensions by SitePoint (@SitePoint) on CodePen.

Rounding things up

We now have enough information to put together the complete list of the replaced elements:

  • embedded content: embed, iframe, and video are always treated as replaced elements
  • embedded content: applet, audio, object and canvas are treated as replaced elements in specific cases
  • images: img and input type="image" are treated as replaced elements when loaded properly or when the browser expects it will render eventually

What About the Other Types of Form Controls?

As I mentioned at the outset, there is a certain level of misconception that other types of form controls are replaced elements too. After all, if you add no styles and no attributes, these elements are also rendered with a default width and height. That would satisfy the requirements of the definition, wouldn’t it?

Actually that is not true. Let’s consult again the Rendering section of the HTML living standard. Here we discover that 14.3.11 Form controls is a subsection of 14.3 Non-replaced elements. What most people consider intrinsic dimensions actually comes from the following line:

Each kind of form control is also described in the Widgets section, which describes the look and feel of the control.

Source: 14.3.11 Form controls

And here is the reason why form controls look so different from one browser to the next and from one OS to another:

The elements defined in this section can be rendered in a variety of manners, within the guidelines provided below. User agents are encouraged to set the ‘appearance’ CSS property appropriately to achieve platform-native appearances for widgets, and are expected to implement any relevant animations, etc, that are appropriate for the platform.

Source: 14.5 Widgets > 14.5.1 Introduction

The term “platform-native appearances” explains why form elements are rendered in such a different way from one browser to another and across different operating systems. It comes down to each browser to define their own interpretation of the way these widgets must look.

Conclusion

Looking back, it’s understandable how replaced elements and form controls got mixed up. Diving deep into both the HTML and CSS specs was necessary in order to unearth the truth. They are different categories of HTML elements, with <input type="image"> being the only form control that is a replaced element.

And here we are at the end of our quest. The results might not be exactly what we expected, but I think they show a clearer picture of a not-so-well-known area of HTML.

Frequently Asked Questions about HTML Replaced Elements

What exactly are replaced elements in HTML?

Replaced elements in HTML are elements whose appearance and dimensions are defined by an external resource. They are called “replaced” because their content is replaced by these resources. Examples of replaced elements include images, objects, videos, and embedded documents. The actual content of these elements is not determined by the HTML document itself but by an external file.

How do replaced elements differ from non-replaced elements?

The main difference between replaced and non-replaced elements lies in how their content is determined. For non-replaced elements, the content is determined by the HTML document itself. For replaced elements, the content is determined by an external resource. This means that the appearance and dimensions of replaced elements can be different from what is specified in the HTML document.

Can I control the size of replaced elements?

Yes, you can control the size of replaced elements using CSS. You can specify the width and height of the element using the ‘width’ and ‘height’ properties. However, keep in mind that the aspect ratio of the external resource will be maintained unless you specify both the width and height.

Are there any limitations to using replaced elements?

While replaced elements offer a lot of flexibility, they also come with some limitations. For instance, you cannot change the content of a replaced element using CSS or JavaScript. Also, some CSS properties do not apply to replaced elements, such as ‘vertical-align’ and ‘line-height’.

What are some common uses of replaced elements?

Replaced elements are commonly used for embedding external resources into a webpage. This includes images, videos, audio files, and other multimedia content. They can also be used for embedding documents, such as PDFs, and for creating interactive content, such as games and quizzes.

How do I specify the source of a replaced element?

The source of a replaced element is specified using the ‘src’ attribute. The value of this attribute is the URL of the external resource. For example, to embed an image, you would use the ‘img’ element with the ‘src’ attribute set to the URL of the image.

Can replaced elements affect the performance of my webpage?

Yes, replaced elements can affect the performance of your webpage. Because the content of these elements is loaded from an external resource, they can increase the load time of your webpage. To minimize this impact, you should optimize the size of your external resources and consider using lazy loading techniques.

Are there any accessibility considerations for using replaced elements?

Yes, when using replaced elements, it’s important to consider accessibility. For example, for images, you should always include an ‘alt’ attribute that describes the content of the image. This helps screen readers understand the content, making your webpage more accessible to users with visual impairments.

Can I use CSS with replaced elements?

Yes, you can use CSS with replaced elements. However, not all CSS properties apply to replaced elements. For example, you can use the ‘width’ and ‘height’ properties to control the size of the element, but you cannot use the ‘content’ property to change the content of the element.

Are there any best practices for using replaced elements?

When using replaced elements, it’s important to optimize the size of your external resources to minimize the impact on your webpage’s load time. Also, always include an ‘alt’ attribute for images to improve accessibility. And remember, while you can control the size of replaced elements with CSS, you should maintain the aspect ratio of the external resource to prevent distortion.

Adrian SanduAdrian Sandu
View Author

Adrian is a UX Developer, creator, and speaker living in Iasi, Romania. He believes happiness is the true measure of success and he wants to help other developers achieve their dreams. In the off time, he loves playing video games and tinker with custom PC builds.

embedded contentLouisLreplaced elements
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week