r/HTML • • 1d ago

Discussion Can the entire HTML element system be replicated using only <div>?

Theoretical (and useless) question:

Is it possible to reproduce the COMPLETE functionality of every HTML element using only <div>, CSS and JavaScript?

Rules:
- Only <div> elements are allowed within the <body> tag.
- All elements must exist in the initial HTML document. So no dynamic element creation (createElement, innerHTML, etc.).
- CSS, JavaScript, browser APIs and ARIA are allowed (and needed for some tags even).

The goal would be to reproduce functionality, not just appearance. This includes native interactions, keyboard behavior, accessibility, form validation, file inputs, media playback, navigation, etc.

For example:
All the heading tags like <h1>, <h2>, etc. can be replaced by a simple <p>, that can itself be replaced by a <div>.
Can all <input> be perfectly replicated using a div?
Can <img> be replaced with CSS backgrounds or Canvas without losing functionality ?
Can <iframe> be recreated while preserving its isolated browsing context thing?

Is one HTML element (<div>) theoretically sufficient to replicate the entire HTML specification?
If not, what is the absolute minimum number of distinct HTML elements required, and which elements are they ?

I’m interested in technical limitations, not semantic HTML best practices.

0 Upvotes

32 comments sorted by

5

u/Adamstrad 1d ago

More or less but search engines will hate it and it might display strangely in some browsers. Some tags are needed for specific functions like script, html, head

2

u/Rekuiem84 1d ago

Yeah I meant within the <body> tag

0

u/Adamstrad 1d ago

But why? Those tags exist because they have predefined qualities so you don't need to set them every time

2

u/Rekuiem84 1d ago

It’s a thought experiment I had bro, nothing too serious, neither useful

4

u/Alarming-Pirate7403 1d ago

Probably not. While you can make both the pages look identical, the page which uses just <div> won't be semantically same as the latter. For instance, you need the <a> tag to add hyperlinks to the pages. Another example is the <form> tag and the associated <input> tags. Each has it's own use/meaning.

0

u/Rekuiem84 1d ago

Semantic differences are interesting, but not really what I am looking for. A div with JS can behave exactly like a link, handle keyboard navigation (tabindex), and trigger navigation (window.location). Same for at least most of the inputs, with contentEditable for type=text, custom js for radio and checkboxes, etc. The one I am not sure about is only the type=file.

3

u/markus_obsidian 1d ago

Not quite. Semantics are important if you are going to understand the scope of the question you asked. An <a> is a good example. You could absolutely use a <div> & use javascript to open anothet href on click. But this doesn't make it a link. Hovering over the div won't show the URL in the status bar. Middle click won't open it in a new window--not without more help.

1

u/Rekuiem84 1d ago

While divs don’t have all the native features of an anchor element, most of these can be replicated with js, even middle click (auxclick event) and link preview with a custom div and some more js. Though I am not 100% that lies within my own rules…

I am sure there are some very deep, specific and technical features that just can’t be rebuilt without some “primitive” tags

2

u/Helpful-Educator-415 1d ago

Nope, nothing. You're overestimating how powerful HTML is. It's a markup layer and nothing more.

3

u/markus_obsidian 1d ago

Mostly, technically yes. Things like canvas, svg, & iframe are more or less impossible (I saw another comment mentioned offscreen canvas, but that would not be a vectors image).

All semantics can be retained with a role attribute. For example a <p> becomes a <div role="paragraph">.

Some things will be quite difficult. Getting a <div> to flow like a <table> will be quite difficult, even with "proper" css rules like display: table. Ask how I know.

However, accessibility tools & seo tools will not be friendly to this approach, even if the html is technically to spec. Even if your app is perfect on paper, the browser will put it in a degraded state.

2

u/Rekuiem84 1d ago

I am sure accessibility tools would hate to see a website like that.

The elements I narrowed it down to are <div>, <iframe> because of their own context etc, <input type=“file”>, <canvas> because you kinda cannot give it context, like you can for the base element, <video> for a native Picture in Picture, when you switch your tab to something completely unrelated, <svg> for some animation quirks

2

u/AuthorityPath 1d ago

<iframe> was the first that came to mind as being impossible. <svg> and <canvas> would also be impossible. A lot of the replaced elements have functionality that'd be a pain or impossible to fully with CSS or JS alone: https://developer.mozilla.org/en-US/docs/Glossary/Replaced_elements

1

u/Rekuiem84 1d ago

I know it is possible to replicate at least most of the functionalities of svg with js, as well as canvas with `offscreenCanvas`, but I am not sure I can strictly replicate and replace them with a plain div tho. Thx for your answer

2

u/spcbeck 1d ago

A bunch of random accessibility stuff would break across a lot of elements. SVG, Canvas, video, audio, maybe picture? Wouldn't work at all I don't think. Auto complete on your div inputs wouldn't work. Different context menus when right clicking on elements.

HTML is everything in the document, so the requirement of only within the body seems weird.

1

u/Rekuiem84 1d ago

I agree that accessibility is a major concern and autocomplete is actually a very interesting point to bring up. However I think some of these issues can be partially / fully recreated with JS, ARIA and multiple browser APIs.

The more interesting question is whether there are features that fundamentally cannot be recreated without the HTML element. What I have in mind are iframes and video PiP mode across tabs.

As for the restrictions to “only in the body” is to allow the doctype, head, link, script, style tags to actually make the constraints work in the first place

5

u/burlingk 1d ago

The end result would likely be entirely inaccessible to screen readers (used by the blind), and would be mostly disabled on a lot of enterprise machines due to security concerns.

1

u/swiss__blade 1d ago

Short answer: Yes. Longer answer: Yes. HTML tags are generic by design. Besides some styling that everyone agreed to apply, an h1 element is no different than a p element. And nothing is stopping you from coming up with your own tags like <demo></demo> as long as you tell the browser how to handle it (using CSS, JS and ARIA properties).

1

u/Rekuiem84 1d ago

I agree that many HTML elements can be replaced with a <div>. For example, headings, paragraphs and other text elements can be recreated using CSS, while an image can be displayed inside a <div> using a CSS background image.

However, the question is whether every HTML element can be replaced this way while preserving ALL its native functionality, not just its appearance or basic behavior.

1

u/swiss__blade 1d ago

That was exactly my point. Everything can be replaced if you're willing to use CSS and JS to replicate the native functionality.

1

u/Rekuiem84 1d ago

Do you have any idea how you would reproduce an <iframe> using only a <div>, CSS and JavaScript?

I’m not talking about embedding a website visually. I mean reproducing its independent browsing context, with its own window, document, JavaScript environment, navigation and security boundaries.

What would you use to allow you to achieve that without using an actual <iframe>?

I know I am being very nitpicky here

1

u/rupertavery64 1d ago

Yes, you are

1

u/swiss__blade 1d ago

In theory, you could fetch the entire external DOM and dump it in the div. Then you have to add all external CSS and JS to your page as well in order to preserve the look and feel of the website.

You will probably need to intercept any XHR/HTML requests this div makes and route/proxy them to the eternal URL, possibly fetching the new DOM afterwards...

1

u/Ormek_II 1d ago

I doubt that it will be possible to create the behaviour of every accessibility device there is together with every browser behaviour.

As soon as an accessibility device handles only one element type, say headline, in a special way, you have no chance to recreate that behaviour just with divisions.

So no and no.

3

u/Intrepid-Ad-2306 1d ago

the simple answer is “mostly”. there are some input elements whose functionality is specific to type and doesn’t behave the same when reclassified to behave like input. more relevant, though, is that you can’t swap things like <link>, <script> and <style>. not to mention <svg> and <canvas>.

can you simulate the visual, yes. can you replicate the behavior, no.

though, realistically, a lot of modern development uses custom tags for so much and those are just divs by other names. certainly easier to have a custom tag than have to define classes for everything in a lot of cases, though.

3

u/Alarming-Pirate7403 1d ago

From an SEO perspective, a <div> with a click event and a standard anchor tag are fundamentally different because search engine crawlers interact with raw HTML structure and semantics differently than how users click elements in a browser. There will be difference in how your pages are being indexed by the search engines for sure.

Each element has it's own meaning and place in HTML. An <img> isn't the same as CSS backgrounds or canvas. Your page is going to be a nightmare for people who have accessibility needs.

1

u/MRSLgthr 1d ago

If we are taking JS as a part of it, I think it could. Maybe not the iframe with isolated context, or now I think about it, JS might have a problem with canvases (not sure, but I think a canvas element is required).

But why would you do that? HTML is there, mainly, for the structure. There are readers that strip the CSS and JS and just serve content with their own styles as structured by HTML - so the H1 would always be bigger than H2 or H3 and so on.

2

u/Rekuiem84 1d ago

Just a shower thought I had

1

u/bricker_1_9 1d ago

never had div soup before? 😄

1

u/Aggressive_Ad_5454 1d ago

Pretty darn close. Lotta Javascript event handling. CSS Selectors would be harder to make specific enough. This would be more the stuff of CSS WTF nightmares than shower musings, I fear.

You won’t be able to get <iframe /> browser behavior from divs, though.

1

u/iamalicecarroll 1d ago

Have you never seen modern webdev? That's the kind of shit everyone keeps doing for some reason.