Rearrange the sequence of floating elements

Below is the code for a series of divs

div {
  float: left;
  width: 33.33%;
  height: 50px;
}

#container {
  width: 100%;
}

#title,
#image,
#descr {
  display: inline-block;
}

#title,
#descr {
  float: right;
}

@media only screen and (max-width: 480px) {
  div {
    width: 100%;
  }
}
<div id="title">This is a title</div>
<div id="image">This is an image</div>
<div id="descr">This is a description</div>

I am trying to rearrange these divs so that on desktop, the image appears first, followed by the title and then the description:

[This is an image] [This is a title] [This is a description]

However, when I try floating the title and description to the right, the order gets mixed up:

[This is an image] [This is a description] [This is a title]

It's important to note that while I can change the div order in the HTML to achieve the desired sequence, I need the title to come before the image and description on mobile devices, hence the challenge.

Note: The layout has been made responsive by floating the divs. I'm exploring alternatives without completely removing the floats.

Check out the jsfiddle here

Answer №1

To achieve this layout, I recommend using flex because it provides an easier way to align elements:

.container {
  display: flex;
  /* arrange items in a column */
  flex-direction: column;
}

.container>div {
  width: 100%;
  height: 50px;
}


/* start mobile layout and adjust for larger screens */

@media only screen and (min-width: 481px) {
  .container {
    /* arrange items in a row */
    flex-direction: row;
  }
  .container>div {
    width: 33.33%;
  }
  
  #title {
    order: 2;
  }
  #image {
    order: 1;
  }
  #descr {
    order: 3;
  }
}
<div class="container">
  <div id="title">This is a title</div>
  <div id="image">This is an image</div>
  <div id="descr">This is a description</div>
</div>

Answer №2

Ensure the image is floated while the other elements remain unfloated.

Be mindful of the spaces between inline-block when eliminating the float property.

div {
  width: 33.33%;
  height: 50px;
}

#container {
  width: 100%;
}

#title,
#image,
#descr {
  display: inline-block;
}

#image {
  float: left;
}

@media only screen and (max-width: 480px) {
  div {
    width: 100%;
  }
}
<div id="title">This is a title</div><div id="image">This is an image</div><div id="descr">This is a description</div>

Answer №3

Utilize flexbox for reordering elements without the need for floats.

.container {
  display: flex;
  justify-content: space-between;
}

#title {
  order: 2;
}

#image {
  order: 1;
}

#descr {
  order: 3;
}

@media only screen and (max-width: 480px) {
  div {
    width: 100%;
  }
}
<div class="container">
  <div id="title">This is a title</div>
  <div id="image">This is an image</div>
  <div id="descr">This is a description</div>
</div>

Answer №4

To properly align elements in HTML, it is important to adjust the order of the code. When you use float: right on two elements, the first one will be positioned at the far right, with the next one appearing to the left of it (if there is available space).

div {
  float: left;
  width: 33.33%;
  height: 50px;
}

#container {
  width: 100%;
}

#title,
#image,
#descr {
  display: inline-block;
}

#title,
#descr {
  float: right;
}

@media only screen and (max-width: 480px) {
  div {
    width: 100%;
  }
}
<div id="image">This is an image</div>
<div id="descr">This is a description</div>
<div id="title">This is a title</div>

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

Enhancing Speed and Efficiency with Now ui kit CSS Framework

Recently, I switched over to the stunning Now-UI kit which is built on Bootstrap 4.0. While Bootstrap has always been highly responsive on all devices, I am currently facing an issue with the responsiveness of the Now-UI kit on mobile devices. If you cou ...

What is the process for automatically activating the Enter key once moving to the next text input field?

Hey everyone, I am looking for a way to automatically trigger the enter button after the user switches to another HTML input type='text'. This is necessary for form validation. Below is the code snippet: <input type="text" class="form-contro ...

Display a block by using the focus() method

My objective is : To display a popin when the user clicks on a button To hide the popin when the user clicks elsewhere I previously implemented this solution successfully: Use jQuery to hide a DIV when the user clicks outside of it However, I wanted to ...

Adjust all children's height to match that of the tallest child

My nearly completed jQuery slideshow is working well except for one issue - the height of the slideshow remains fixed at the height of the first displayed slide, causing problems. View the issue here: . I have tried different jQuery solutions from StackOve ...

Adjust the list separator based on screen size fluctuations using media queries

I'm trying to figure out how to manage separators (such as a "-") between each element of a list. It's easy when it's just one line, but I'm having trouble with multiple lines. On a large screen, my site looks like this: Example cente ...

Is it possible to change the style of the current page in PHP using the ?page href method?

Currently, I am facing an issue with styling in my code. Specifically, I want to style the page numbers that are currently open or active. For example, if a page is open, I would like its corresponding number to be displayed in a different color or style. ...

Ensure that the background image adjusts appropriately to different screen sizes while maintaining its responsiveness

Currently in the process of developing a single page application using CRA. My goal is to create a hero image with an overlay at the top of the application using a background image. However, I'm facing issues with maintaining the responsiveness of the ...

Color overlay on top of image background

I'm struggling to create a striped colored background with a narrow center single color background on top for showcasing articles and photos. However, I am unable to get the colored background to display correctly. Despite trying different approaches ...

Issue with setting Y Axis intervals using ticks in d3 library

I am trying to set the Y Axis from 0 to 100 percent with intervals of 0-25-50-75-100. Despite setting ticks to 4, I am seeing Y axis intervals of 0-20-40-60-80-100 How can I adjust the Y axis to display intervals of 0-25-50-75-100? Here is the code I am ...

Utilize the grid system from Bootstrap to style HTML div elements

I am working on my angular application and need help with styling items in a Bootstrap grid based on the number of elements in an array. Here's what I'm looking to achieve: If there are 5 items in the array, I want to display the first two items ...

A gap only appears in the drop-down navigation when the page is scrolled

After finally completing my first website, I encountered a frustrating issue. When scrolling down the page, a gap appears in the navigation bar, making it impossible to access the dropdown menus. I've been struggling to fix this problem on my own. Any ...

JavaScript Tab Fade Effect

I have a tab system that utilizes JavaScript to switch tabs. I am trying to implement a fade in and out effect when switching tabs. The current JavaScript simply adds or removes the clicked tab selected by the user. I have managed to partially achieve the ...

Growing animated backdrop

My website layout consists of a centralized wrapper div containing a header, three columns with dynamic heights based on content length, and a footer. I now want to enhance the design by adding background colors that expand in width to the left and right. ...

Creating a JQuery slider that efficiently loads and displays groups of elements consecutively

Currently, I am in the process of developing an infinite horizontal tab slider using jQuery. My main objective is to optimize loading time by having the slider display only the first 10 slides upon initial page load. Subsequent slides will be loaded as the ...

Converting plain text to a JSON object using JavaScript: A step-by-step guide

Having trouble converting plain text into JSON value in Javascript. Need some assistance. Actual string obtained from a command line executed through Javascript: Net State: OFFLINE Net Err: Net Time: 0 Current Sample: 0 Sample #0 Sample Role: O ...

Can an iframe be replicated?

How can I retrieve the content of a specific webpage, such as http://google.com, and dynamically insert it into the current document while preserving styles, scripts, and other elements just like an iframe does? Additionally, I want to ensure that the sty ...

What is the best way to extract the chosen value from a dropdown menu within the $_POST array?

In my HTML code, I have a droplist using the <select> tag. How can I retrieve the selected value from the $_POST array once the user submits the form? <form action="subj_exec.php"> <?php echo $_SESSION['SESS_MEMBER ...

What is the best way to incorporate six rows of paragraphs using bootstrap?

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" con ...

tinyScroll currently disabled

Attempting to implement the tinyscrollbar plugin found at Here is the code snippet: $('#nvl2 .content').html( '<div class="scrollbar">'+ '<div class="track">'+ ...

Ensure that a select input, dynamically generated, is marked as 'required' only when other inputs have been filled out

Displayed below is a snapshot of a page I'm developing that allows users to input details about various rooms in a property. Users have the ability to 'add' multiple rooms, triggering javascript/jQuery to dynamically create additional input ...