Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Text styling breaks after a child element inside it

I have a simple <p> element with text in it and halfway through the text I want to insert an image. For some reason the text that comes after the image isn’t styled according to the stylesheet:

p {
  font-size: 20px;
}
<p>
  Text is styled according to the stylesheet.
  <div></div>
  Text loses its styling.
</p>

Why is that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your Markup is invalid! Use a Markup Valdiator to check if your projects are validly coded.

The issue is, that paragraph (<p>) is a container solely for a text block where flow-text is expected. A div however is a block container and as such not a container for flow-text unlike a span.

Modern mainstream browsers have an auto-correction feature to fix most mistakes in the code to still render the document and not completely break itself.

Your code will be corrected by browsers like this:

<p>
  Text is styled according to the stylesheet.
</p>
<div></div>
Text loses its styling.

As such you will realize why the second line is no longer influenced by the CSS styling.

Solution

You stated in the comments:

@ralph.m Yes, that’s beacause I want to add a description of the image under it.

And this point I have to say, that a div is not a suitable container either. The correct way is to use figure and figcaption.

<figure>
  <img src="https://via.placeholder.com/200.jpg">
  <figcaption>This is just a Placeholder Image</figcaption>
</figure>

However, as said above, it is not correct to include anything else than flow-text inside a paragraph. So you have to split the markup as follows:

<p>Text is styled according to the stylesheet.</p>
<figure>
  <img src="https://via.placeholder.com/200.jpg">
  <figcaption>This is just a Placeholder Image</figcaption>
</figure>
<p>More flow-text.</p>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading