Utilize CSS to wrap text around an image and adjust positioning at a specific breakpoint

Having a bit of a challenge with responsive css here.

I'm working on articles where there's text on the left and an image on the right, with the text wrapping around the image. However, at a certain breakpoint I need the body copy to appear above the image.

Here's the code I've been using:

<section class="news-article-body">
    <img src="img/news-article-image.jpg" alt="" title=""/>
    <p> 
      Lorem Ipsum...
    </p>
</section

Css:

.news-article-body img {
    float: right;
}

It looks good. The image floats to the right and the text wraps nicely when necessary. But since the image tag is before the text, I can't get the image to be positioned below the text at the breakpoint.

Essentially, I need the text to wrap properly with this html (Or any other solution):

<section class="news-article-body">
    <p> 
      Lorem Ipsum...
    </p>
    <img src="img/news-article-image.jpg" alt="" title=""/>
</section

Does anyone know of a technique that could help me achieve this? I've been struggling to find a solution. Thanks

Answer №1

To achieve this layout, you can utilize the display:flex property and adjust the order of elements:

When experiencing a breakpoint, your HTML structure should look like this:

.news-article-body {
display:flex;
flex-direction:column;
}
.news-article-body img {
order:2;
}

If needed, you can also employ JavaScript and float clearing techniques for assistance: VIEW DEMO 1 or VIEW DEMO 2

Here is an example of the code implementation:

onload=function() {
  document.getElementById('myImg').style.position='absolute'; // remove img from normal flow
  var myoffsetHeight = document.getElementById('myP').offsetHeight; // retrieve height of <p>
  document.getElementById('myT').style.height=myoffsetHeight +'px'; // set floating element's height
  document.getElementById('myImg').style.position='static'; // return img to normal flow
}

Your HTML structure should resemble something like this:

<section >
  <b id="myT"></b>
  <img src="http://lorempixel.com/250/100/" id="myImg"/>
  <p id="myP">Lorem ipsum dolor sit amet...</p>
</section>

In both examples provided, don't forget to define and customize your breakpoints accordingly.

Answer №2

To effortlessly incorporate an <img> tag into the start of your content, simply add align="right" to have the text flow around it seamlessly.

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

Retrieves a single data point through an AJAX request

Currently, my goal is to fetch data from the database and display it in a structured format using HTML tables. The data needs to be distributed into different tables based on the academic year and semester they belong to. This process is similar to organiz ...

"Utilizing jQuery to achieve vertical center alignment within a div

Attempting to vertically center a div within another div that has 100% dimensions is proving to be a bit challenging. The code I have written seems to work but occasionally triggers an odd bug. This is the snippet of code in question: var window_height = ...

Is there a way to resolve the issue of the hero image appearing behind the text box without relying on media queries?

I have a hero image with a text box overlapping the hero slightly. However, when I add content below the box, it appears under the text box. While my code includes some media queries that work well, I believe there must be a way to encapsulate the hero and ...

What is the best way to eliminate the appearance of the blue square that shows up when the button is clicked on smaller screens?

When testing my project on different screen sizes in Chrome dev tools or on my phone, I noticed a blue square briefly appearing around the button when it is clicked. I attempted to remove this by setting outline: none or outline: 0 on various pseudo-classe ...

optimal scaling at the center, not the edge

In this code snippet, my goal is to create an effect where the div in the middle of the visible scroll section appears at full scale (scale(1);), while the other div elements scale down towards scale(0); as they approach the edges. I have included a debug ...

A cutting-edge JQuery UI slider brought to life using HTML5's data-* attributes and CSS class styling

I've been attempting to create multiple sliders using a shared CSS class and HTML5 data attributes, but unfortunately, I haven't had much success so far. Although I am able to retrieve some values, there are certain ones that simply aren't w ...

What is the process for taking a website project running on localhost and converting it into an Android web application using HTML, CSS, and JavaScript

Looking for recommendations on how to create an Android web application using HTML, CSS, and JavaScript. Any suggestions? ...

Overlapping CSS Menu

I've been working on creating a CSS menu and here's what I have accomplished so far: Check out my code on Js Fiddle However, I'm encountering an issue where the menu items overlap when the browser width is decreased. You can see the proble ...

What causes the value from the <input> form to be duplicated in all other inputs?

this is a section of my code: <input style="width:100px;" #quantity name="vehicle{{i}}_quantity" class="options-input" [(ngModel)]="detail.quantity" type="number" required [ngClass]="{ 'error-bottom-border': quantity.value.length==0}" placeho ...

How can I remove the arrow from a CSS selector?

I'm looking to enhance the appearance of a select element on my webpage, so I've crafted some custom CSS: .selectWebDropDown { background-color:Transparent; background: url(../Images/ddl_Normal.png) no-repeat right #FFFFFF !important; ...

Trouble with AngularJS not redirecting to home page after logging in

My current setup involves spring + angularJS, and I'm facing an issue where after authentication from the controller, I'm unable to redirect to the home page. console.log("id:"+employee.id); alert("Attempting to redirect to the home page") ...

Issues with CSS3 keyframe animations failing to function in Firefox and IE

My CSS code is functioning properly in Chrome and Safari, but not in Firefox, IE, and Opera when I create the @keyframes rotate {}. It seems that these 4 lines might be causing the issue: animation-duration: 4s; animation-timing-functio ...

How can I use a CSS file in PHP to conceal the folder structure?

main.css $theme->assign('css_file',SITE_URL.'styles.php?theme='.THEME_ID); $theme->display(TEMPLATES_PATH.'header.tpl'); header.tpl <link rel="stylesheet" href="{$css_file}"> styles.php include TEMPLATES_PATH. ...

What potential issues could arise from utilizing a numerical value as an identifier or class in an HTML4, XHTML, or HTML5 webpage?

I am hesitant about using elements with either id="12" or class="12", as the title suggests. Although it is allowed by the specifications, particularly in HTML5, have you faced any issues when doing so? In my specific scenario, I have <li> elements ...

Is it feasible to remain on the page and receive a confirmation message once the file has been successfully uploaded

After uploading and clicking Submit, the page changes without using the JavaScript below. The JavaScript is meant to ensure that the page remains unchanged. Issue While the HTML and JavaScript keep the page as desired and retrieve the radio button value, ...

Tips for shrinking the circumference of a circle

Currently, I have a circular div that has been styled using CSS animations. My goal is to keep the size of the circle consistent when it moves to the bottom, but reduce its size when it bounces back to the top. I am uncertain if this can be achieved solely ...

PHP and AJAX successfully retrieving data, but failing to redirect to new page

I am trying to pass a username and password to a PHP page using AJAX, and once the credentials are verified, I want to load the main.html page. Currently, when I submit the form, I receive a response of "true," which displays above the form where an error ...

Specify a maximum length for numerical input and permit the use of a plus sign

I am currently working on a form field where users can input a country code. I want to ensure that the user enters only 3 numbers with a + sign at the beginning. I have successfully limited the length to 3 digits, but I am facing an issue with typing the ...

Database information displayed in a text area with spaces before each entry

I have implemented a text area for members to update their about me profile. However, I am facing an issue where extra spaces appear at the beginning before the first word when they go to edit it. <textarea rows="15" cols="25" name="aboutme" id="aboutm ...

Interacting with a Bootstrap menu

I recently incorporated an example from Bootstrap, found at http://getbootstrap.com/examples/starter-template/, into my project and it worked well. However, after adding a custom left menu that appears on left-click, I noticed that the color of the Boots ...