Strange behavior when writing divs in a custom renderer #760

Closed
opened 2026-01-29 14:44:55 +00:00 by claunia · 2 comments
Owner

Originally created by @minisbett on GitHub (Oct 18, 2025).

I have a custom renderer which, for demonstration purposes, writes something along the lines of this:

renderer.Write("""
  <a href="http:\\www.heise.de">
    <div style="display: flex; flex-direction: row; gap: 8px; align-items: center;">
      <div style="background: red;">Hello</div>
      <div style="background: green;">World</div>
    </div>
  </a>
  """);

However, in the rendered HTML, the very same code looks like this:
Image

It seems to

  1. Take the a-tag, and places it there, just completely empty.
  2. Takes the first div-tag in the a-tag, and places it on the root layer as well, then putting the a-tag inside, which then contains everything the div contained.

This seems very strange, and I've tried a lot, always running into this issue when using div-tags. Is this intended behavior? Something I am not aware of?

Originally created by @minisbett on GitHub (Oct 18, 2025). I have a custom renderer which, for demonstration purposes, writes something along the lines of this: ```html renderer.Write(""" <a href="http:\\www.heise.de"> <div style="display: flex; flex-direction: row; gap: 8px; align-items: center;"> <div style="background: red;">Hello</div> <div style="background: green;">World</div> </div> </a> """); ``` However, in the rendered HTML, the very same code looks like this: <img width="425" height="152" alt="Image" src="https://github.com/user-attachments/assets/8cfc37a0-4f3a-4b7d-a8b4-8c25338cd762" /> It seems to 1. Take the `a`-tag, and places it there, just completely empty. 2. Takes the first `div`-tag in the `a-tag`, and places it on the root layer as well, then putting the `a`-tag inside, which then contains everything the `div` contained. This seems very strange, and I've tried a lot, always running into this issue when using `div`-tags. Is this intended behavior? Something I am not aware of?
claunia added the invalid label 2026-01-29 14:44:55 +00:00
Author
Owner

@xoofx commented on GitHub (Oct 19, 2025):

Your problem is not related to Markdig but an invalid usage of HTML. By default, <a> tag is an inline HTML element, which cannot contain a block element like <div>. Browsers will accept whatever you throw at them, even invalid HTML like this, but they will try to arrange things with convoluted rules (sometimes browser specifics).

@xoofx commented on GitHub (Oct 19, 2025): Your problem is not related to Markdig but an invalid usage of HTML. By default, `<a>` tag is an inline HTML element, which cannot contain a block element like `<div>`. Browsers will accept whatever you throw at them, even invalid HTML like this, but they will try to arrange things with convoluted rules (sometimes browser specifics).
Author
Owner

@minisbett commented on GitHub (Oct 19, 2025):

By default, <a> tag is an inline HTML element, which cannot contain a block element like <div>.

This is only correct in HTML 4. HTML 5 onwards states:

The element "may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links)".

I'm really confused why my browser decides to do this. But yes, I was able to confirm that it is indeed not a Markdig issue.

Edit: I've dug a bit deeper, and eventually figured that the problem is that the renderer writes the HTML into a p-tag.

@minisbett commented on GitHub (Oct 19, 2025): > By default, \<a\> tag is an inline HTML element, which cannot contain a block element like \<div\>. This is only correct in HTML 4. HTML 5 onwards states: > The <a> element "may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links)". I'm really confused why my browser decides to do this. But yes, I was able to confirm that it is indeed not a Markdig issue. Edit: I've dug a bit deeper, and eventually figured that the problem is that the renderer writes the HTML into a p-tag.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#760