What are the steps for transitioning from an HTML-table layout solution to a floating-div solution?

Whenever I need to display a list of items with differently sized images on the left and corresponding text on the right, I always face this challenge:

Click here for image link

For many years, my go-to solution has been using HTML tables like this:

<html>

  <style>

    * { margin: 0; padding: 0 }

    body {
      padding: 20px;
    } 

    #content {
      width: 600px;
      padding: 20px;
      margin-left: auto;
      margin-right: auto;
      background: green;  
    }

    .item {
      margin: 0 0 20px 0;
    }

    .itemIcon {
      float:left;
    }

    .itemBody {
      float:right;
    }

    .clear {
      clear: both;
    }

  </style>

<body>

<div id="content">

  <div class="item">
    <div class="itemIcon">
      <img src="icon.png" alt=""/>  
    </div>
    <div class="itemBody">
      <h1>Item Title</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac mollis mauris.</p> 
    </div> 
    <br class="clear"/>
  </div>

  <div class="item">  
    <div class="itemIcon">
      <img src="largeIcon.png" alt=""/>  
    </div>
    <div class="itemBody">
      <h1>Another Item</h1>
      <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p> 
    </div>    
    <br class="clear"/>
  </div> 

  <div class="item">
    <div class="itemIcon">
      <img src="icon.png" alt=""/>  
    </div>
    <div class="itemBody">
      <h1>One More Item</h1>
      <p>Sed pharetra vehicula posuere. Ut rutrum libero in arcu tincidunt bibendum.</p> 
    </div> 
    <br class="clear"/>
  </div>

</div>

</body>
</html>

Despite several attempts, whenever I try to switch to floating divs, it always resulted in failure. Here's my latest attempt:

Check out my failed attempt

What modifications do I need to make to this code to ensure that the floating div layout resembles the original table structure?

Answer №1

This method may not offer the most flexibility since you need to customize the padding for each individual DIV based on the image, but it does result in clean markup:

<style>
div.one {
   padding-left: 210px;
   overflow: auto;
}
div.one img {
    float: left;
    margin-left: -210px;
}
</style>


<div class="one">
<img src="http://www.pixeloution.com/breakdown.png" width="200" />
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum blandit orci sed turpis aliquet ac mattis sem t...
</div>

View this code in action at: http://jsbin.com/iniqu3/2

If you resize your browser window very narrow, you'll notice that the text doesn't wrap around the image as shown in your example.

Answer №2

To somewhat improve the layout, you could add a width to the <p> within itemBody (along with some padding for aesthetics):

.itemBody p { 
  width:400px;
  padding-left:20px;
}

.itemBody h1 { 
  padding-left:20px;
}

However, a drawback of this approach is that the right edges of the paragraphs may not align perfectly depending on the image size, and text might still wrap if the image is wide. If possible, dynamically resizing elements using server-side code based on the image dimensions would be a better solution. Alternatively, JavaScript could be used for resizing.

In my opinion, using tables is still the most effective way to address this issue. This method allows for dynamic width adjustment while keeping everything on the same line regardless of the image or text size. No hacks, no complications, and compatible with all browsers.

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

Using React Material UI to implement a button function that triggers a file picker window

I have a customized Material UI button component that I want to use for selecting files from my computer upon clicking. However, my attempt to achieve this by nesting the <Button></Button> within a <label> associated with an <input typ ...

Maintaining the proportions of images in different screen sizes when resizing

I apologize if this question has already been addressed, but I have been unable to find a solution that works for my specific issue. My Gallery consists of a side list of available images in one section, which when clicked changes the image source in anot ...

Ways to adjust the ngx-pagination color scheme?

I am looking to customize the background color of ngx-pagination Here is my current code: <pagination-controls class="custom-pagination" id="indicadorPaginationResults" (pageChange)="p=$event" maxSize="9" directionLinks="true" autoHide="true" previ ...

Storing Jquery result in a variable in Angular

Within my Angular component, I have a folder structure that needs to retain its previous state (which folder was open) even after reloading due to CRUD operations. https://i.sstatic.net/z44k6.png The API sends back a nested response utilized for generati ...

Scraping Websites with Php cURL

I'm trying to extract the price of a mobile phone from this website link: The price can be found within this SPAN on the page: <div class="pricing line"> <div class="prices" itemprop="offers" itemscope="" itemtype="http://schema.org ...

What is the reason for the .foo a:link, .foo a:visited {} selector taking precedence over the a:hover, a:active {} selector in CSS?

Sample code: http://jsfiddle.net/RuQNP/ <!DOCTYPE html> <html> <head> <title>Foo</title> <style type="text/css"> a:link, a:visited { color: blue; } a:hover, a:active { ...

Issues with clicking on the ion-tab icon in AngularJS are hindering the functionality

I'm currently facing a challenge with an ion-tab icon in my AngularJS project. I've been attempting to add a click action using the code below, but unfortunately, nothing is displaying as expected. HTML <ion-tab title="Share" icon="icon ion- ...

Is there a way to prevent Bootstrap 4's grid system from splitting column content when resizing the browser window?

Just delving into Bootstrap's grid system for the first time here, so bear with me if this sounds silly. My goal is to create a 3 column page that is responsive, featuring an image and a Spotify player in each column. Everything looks good on a full ...

What is the best way to eliminate the final character from a series of repeated Xs and following strings

Data consists of [one][two][three]. I want to exclude the last set of brackets and have a result like [one][two]. How can this be achieved using jQuery? ...

Header that sticks to the top of a container as you scroll through it

Many questions arise regarding sticky elements in the DOM and the various libraries available to handle them, such as jquery.pin, sticky-kit, and more. However, the issue with most of these libraries is that they only function when the entire body is scro ...

Ensuring consistency in aligning float elements

I've been struggling to create a concept design for my internship project. I aim to have a page with six clickable elements (images). When one is clicked, the others should disappear and the active one moves to the top. I managed to achieve this using ...

Learn how to incorporate a 'Select All' feature as the primary choice in DevExtreme SelectBox with Angular 15 through Html coding

We are looking to replicate the following basic HTML select in Angular 15 using a dropdown control from DevExpress known as dx-select-box <select ng-model="selectedVal" ng-change="selectedValueChanged()"> <option value=&q ...

The justify-content property aligns single-line text horizontally, but its alignment may not be consistent when the text expands over

Seeking a deeper understanding of flexbox alignment, I decided to experiment with aligning content using only flexbox properties. One puzzling observation is how "justify-content" centers items with single lines of text (flex-item-1 and flex-item-5), but n ...

Steps for creating a clickable product item card throughout

I am looking to modify my Product Card design. Currently, there is a hyperlink named Buy at the end of the card that users can click on. However, I want to make the entire card clickable with another hyperlink so users do not have to specifically click on ...

A problem arises in Next.js when CSS is not rendering properly during Server Side Rendering

After creating my next.js application using the command npx create-next-app, I realized that the styles from the imported .css files are rendering correctly on Client Side Render but not on Server Side Render. The Next.js documentation states that importe ...

The text in the TextArea is not displaying line breaks as expected

Similar Question: Issue with new line preservation in .val() method for textarea I'm facing an issue where my text area does not preserve newlines when I enter a message with multiple lines. Even after retrieving the value from the text area, the ...

Integrating Ruby code and outputting content with puts in an HTML

In my html.erb file, I have some HTML code. Within it, I included this Ruby snippet: <li <%= puts "class='active'" %>>Link</li> However, when I test the code on a Rails server, the class="active" part does not appear as expect ...

Vue Labyrinthine Design theme

Looking for some guidance from experienced developers out there! I'm currently exploring how to incorporate Mazeletter as a background feature for each page in an app project I've been working on. This is all new territory for me, so any assista ...

Is there a way to manipulate CSS to update automatically when new content is loaded via ajax?

I need help with customizing the CSS for 4 image links on my website. Each link is represented by a small image in its normal state and a larger image when hovered over. The content for each link is loaded using ajax. My question is how can I modify the C ...

What is the best way to connect an image using Flask and javascript?

I'm attempting to insert an image into a card using JavaScript, structured like this: console.log(account_name); document.getElementById("list_of_datasets").innerHTML += ` <div class="col-lg-4 d-flex align-items-stret ...