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?
3
Answers
Because you defined two different blocks.
The
<p>
element is a block-level element and should contain only inline-level elements, not other block-level elements like<div>
. When defining a class, its settings will affect only the defined block-level element, not the blocks that comes after it.Every HTML tag has its own default settings that fit its purpose.
Breaking it into two p tags would work.
This is because
tag is a block-level element used to represent a paragraph of text. It should only contain inline elements such as text or other inline elements like span, a, strong etc.
Or try using a span tag or any inline element for that matter
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. Adiv
however is a block container and as such not a container for flow-text unlike aspan
.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:
As such you will realize why the second line is no longer influenced by the CSS styling.
Solution
You stated in the comments:
And this point I have to say, that a div is not a suitable container either. The correct way is to use
figure
andfigcaption
.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: