Forums

WordPress puts <p> around <img />, how do you disable this? (9 posts)

  1. Spencer Hill
    Member
    Posted 2 years ago #

    Heya - so WordPress automatically puts <p> around all my <img /> when using the Visual or HTML editor. This is extremely annoying and obstructive, can someone tell me how I disable that?

    Yes "correct invalidly nested..." is unchecked. And I've tried it checked as well.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    If your image markup is on a line by itself, WP will treat it as a paragraph. One way to avoid the auto-p addition is to place your image markup inside a <div></div>.

  3. Spencer Hill
    Member
    Posted 2 years ago #

    Ah, I guess that makes sense. It's just screwing with my layout, but I think I resolved it.

    Though I'm having other markup issues. Is there a way to make sure the Visual Editor doesn't insert extra HTML at all? Like it's adding "floats" and advanced code beyond p's and h1's that really have no use or place.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Where's it adding floats? They're normally only added for images/attachments depnding upon what alignment was set when the image was inserted.

  5. Spencer Hill
    Member
    Posted 2 years ago #

    It's adding it to uploaded and placed images that are aligned either left or right using alignleft or alignright classes. And it adds other random code.

  6. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    The editor just adds the classes. The theme's CSS then defines whether those elements with classes float left, right or are aligned centrally. If you don't want the floats, edit the theme's style sheet and remove the flats from those classes.

  7. Spencer Hill
    Member
    Posted 2 years ago #

    Sorry, I should have noted that in addition to the class, it's appending an inline style that's similar to style="float: left; etc..."

  8. jjsoini
    Member
    Posted 2 years ago #

    To automatically wrap the image in <div>s instead of <p>, add a caption to the image.

    By adding a caption, WordPress autogenerates a div-wrapper (.wp-caption) around the image with desired alignment class (.alignleft, .alignright, etc).

    Now you can style the images separately without paragraph styles affecting them.

    If you don't want to show a caption for your images, you can insert a single space for caption, and/or use CSS to hide the caption with .wp-caption-text {display: none}.

    This is useful if the end-users (ie. your clients) are not comfortable with the code editor.

  9. sunstardave
    Member
    Posted 2 years ago #

    @jjsoini

    I'm having this issue as well. I tried to use the method you describe above but for some reason the images aren't floating. Well, they are in the editor but not in the front-end.

    Any ideas why this may happen? Because as you say, a lot of my clients aren't comfortable with the code editor.

    Cheers.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.