Tips on positioning text around an image when it is positioned at the conclusion of a block of text

Can anyone provide guidance on achieving the positioning and text wrapping around an image using the float:right CSS property? The illustration at https://css-tricks.com/all-about-floats/ demonstrates what I am trying to accomplish.

I am looking to place an image at the end of a block of text so that it aligns its bottom with the baseline of the last line of text, seamlessly integrating into the paragraph as the text flows around it. Despite using float:right, I have only managed to create an image that extends beyond the paragraph rather than pushing up into it like shown in the example.

While there are resources explaining how to achieve this effect with float:left, I have been unable to find clear instructions for accomplishing it with float:right. Any help or insights would be greatly appreciated!

Answer №1

To achieve the specific appearance you desire, some adjustments to the markup are necessary, such as implementing a left or right float.

When using a left float on an image, the surrounding block level content will flow around it, creating the desired effect. Similarly, applying a right float would yield similar results. However, placing an image at the very end of the content without elements to flow around will not produce the intended look.

A workaround is to display the last image while there is still remaining content. An example can be found here: https://codepen.io/andrewborem/pen/PjyNyY

Highlighted Code:

div {
  max-width: 750px;
  margin: 0 auto;
  font-size: 1.125em;
}

img.left {
  float: left;
  margin: 0 0.5em 0.5em 0;
  line-height: 0;
}

img.right {
  float: right;
  margin: 0.5em 0 0.5em 0.5em;
  line-height: 0;
}
<div>
  <img class="left" src="https://placehold.it/400x300" />
  <p>Content... </p>
  <p>More content...</p>
  <p>Even more content...</p>
  <img class="right" src="https://placehold.it/400x300" />
  <p>Additional content...</p>

  <p>Further content...</p>


</div>

In scenarios where a CMS is used and two images are included in an article with only three paragraph elements, the layout may appear distorted. Additionally, if the paragraphs following the last image vary significantly in length, the display might deviate from the expected outcome.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Including onMouseUp and onMouseDown events within a JavaScript function

I am experiencing an issue with a div that contains an input image with the ID of "Area-Light". I am attempting to pass the ID of the input image to a function. Although event handlers can be directly added inside the input tag, I prefer to do it within ...

Is there a method to ensure that Text First and Image First layouts are consistently displayed in a uniform manner?

When creating a Shopify theme for a client, I encountered an issue with the layout of my liquid file. Specifically, when using the text-first layout, the image in the image-with-text div is partially hidden due to the content that follows it causing the he ...

Having issues with ToggleClass and RemoveClass functionalities not functioning properly

As a novice in Jquery and CSS/scss, I've managed to create dynamic bootstrap cards for recording players. Each card consists of a music-container with control-play and vinyl elements. I aim to have multiple bootstrap cards generated based on the data ...

What could be causing my div link to redirect to different content instead of the intended destination?

When creating a div to provide information about a course, including the price and a "Take Class" button that links to the purchase page, I encountered an issue where the link extends beyond the intended area. @import url(https://fonts.googleapis.com/cs ...

Implementing a Dialog Box for Confirmation

I want to include a "confirmation box" that will display the message "Are you sure you want to update/delete question?" when I press the "update" and/or "delete" buttons. I attempted to add onclick="return confirm('Are you sure you want to logout?& ...

What is the solution to preventing contentEditable="true" from inserting <div> instead of <p> when the return key is pressed?

I have a div <div contentEditable="true"> <h1> My headline</h1> </div> When editing the text inside the <h1> tag and pressing return, a new div is added underneath instead of the usual paragraph tag. Resulting in: < ...

Adding class names dynamically to div elements using jQuery can enhance the interactivity and styling of a webpage

I need to dynamically add a class name to multiple divs using jQuery. Specifically, I have 10 divs in mind. <div class="sec"></div> <div class="sec"></div> <div class="sec"></div> <div class="sec"></di ...

How can I find a group of child div elements with specific class attributes using Selenium locators?

I am in search of a list of child div class tags. Inspecting elements in Chrome hasn't yielded the desired results with the Xpath I've been using. There are 5 tabs displayed on the page and I need to gather all of them into a list for iteration ...

Excessive Text Overflow in HTML Input Placeholder

<input class="form-control" style="width: 15rem; height: 15rem;" v-model="formData.booking_code" placeholder="Enter Booking Code, e.g. XYBJLL" /> I am facing an issue where the placeholder text ...

What steps can be taken to adjust the alignment of a Bootstrap Dropright menu so that it opens in an upward direction instead of the standard downward

My dropdown menu is dropping towards the right correctly, but it is going downwards and getting hidden under my page. I want it to open upwards instead of downwards. I attempted to adjust the CSS, adding bottom:0 to the dropdown-menu, but unfortunately, i ...

How to position a div in the center on top of a full-width slideshow using CSS

I am facing a challenge with positioning the banner slideshow controls div in the middle of the screen on top of a full-width slideshow. The main container's width is set to 100%, which is causing issues with centering the controls div. I have attemp ...

Tips for resolving the issue of '{' "readyState":0,"status":0,"statusText":"error"}' in an Ajax GET request

I am currently working on developing an API to input data into my website, which resides in different domains. Despite my efforts to format the response correctly, I am facing issues with my ajax calls failing to retrieve any data. One of the strategies I ...

The HTML Search Bar is Misaligned

once more. I seem to be having trouble with my search bar, as it is not appearing in the position or size that I coded for it. The search bar should look like the one shown in this link: https://www.w3schools.com/howto/howto_css_searchbar.asp However, ...

Exploring the process of implementing tab-link ripple effects in Angular Material

Is it possible to apply the ripple effect to tabs in Angular elements using a similar attribute to md-ink-ripple, like we can with grid or title elements? For example, check out https://material.angularjs.org/latest/demo/tabs If there isn't a specif ...

Having trouble with Simplehtmldom's innertext function?

While working with simple_html_dom: I encountered the following code snippet: $j = ' <itemBody> <div>films - to watch (Simple Present)<br/> <textEntryInteraction responseIdentifier="RESPONSE_1"/> ...

Steps for creating scrollable boxes with uniform size using tailwindcss:1. Define a specific width and height for

<div className="overflow-x-auto p-5 gap-5"> <div className={` bg-orange-700 w-[300px] h-[300px]`}></div> <div className={` bg-orange-700 w-[300px] h-[300px]`}></div> <div className={` bg-orange-700 ...

Instructions on how to submit a form using a button while also clicking on an anchor link simultaneously

I have a form with a button to submit it. Now, I want to pass the same variables to another page in order to insert them into a database. The approach I'm considering is passing the variables using an anchor link with GET parameters. However, I' ...

What is the most effective way to transfer information from one page to another in a PhoneGap application?

I attempted to transfer data from one HTML page to another using two different methods: function reply_click(clicked_id) { window.location = "newsList.html?Title="+clicked_id; } And I also tried: function reply_click(clicked_id) { window.l ...

What is the best method for transferring HTML tables to Excel with multiple tabs?

I have created a code that exports an HTML table into an Excel file. However, I now need to export multiple HTML tables into different tabs within a single Excel file. Here is the current code: Currently, when I click "Export To Excel," the Excel file is d ...

Is there a way to conceal the scrollbar while still permitting scrolling?

I'm interested in creating a custom scrollbar, but in order to do so I need to hide the default scrollbar while still allowing scrolling functionality. I've attempted: overflow-y:hidden; but this method hides the scrollbar and prevents scrolli ...