I've created a custom twig template in which I'm placing a text field inside a

tag like so:

<p>{{ content.field_sample_text }}</p>

It renders the content of that field just fine but when I inspect it, it seems to create a new div below the

tag and places its contents in that div like so:

  Content of the field

There's an extra p tag at the end as well which is also confusing. How can I ensure that my text field renders inside the original p tag as intended so that I can style it properly?

  • Presumably your field template wraps things in an empty div? A p cannot contain a div, so what you're seeing is the browser trying to recover the invalid markup
    – Clive
    May 9 '17 at 18:12
  • That was exactly what was happening, thank you! I was using the stable theme's field.html.twig template and that makes all the content be inside divs. If you can put this down as an answer I can accept it. :)
    – Vishal Rao
    May 9 '17 at 18:26

If you are using Stable like theme you can see that the field is wrapped around a div tag. You can rewrite field.html.twig with your theme suggestions and edit the markup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.