Ensuring proper alignment of images and text in HTML when resizing

Trying to show an image and text side by side, I'm using the following code:

div.out {
  background-color: silver;
  border: 1px solid black;
  padding: 10px;
  display: flex;
}

div.right {
  order: 1;
  background-color: white;
  border: 1px solid black;
  padding: 10px;
  margin-left: 10px;
}
<div class="out">
  <img src="https://i.sstatic.net/asXsj.jpg" width="40%" height="60%">
  <div class="right">
    A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over a hundred species and thousands of cultivars. They form a group of plants that can be erect shrubs, climbing or trailing with
    stems that are often armed with sharp prickles. Flowers vary in size and shape and are usually large and showy, in colours ranging from white through yellows and reds. Most species are native to Asia, with smaller numbers native to Europe, North America,
    and northwestern Africa. Species, cultivars and hybrids are all widely grown for their beauty and often are fragrant. Roses have acquired cultural significance in many societies. Rose plants range in size from compact, miniature roses, to climbers
    that can reach seven meters in height. Different species hybridize easily, and this has been used in the development of the wide range of garden roses.
  </div>
</div>

When resizing the window horizontally, both the image and text adjust accordingly. However, I want to implement one additional feature. If the image's size is reduced below, say 250x250 pixels, I'd like the text to move below it instead of shrinking further. For instance:

https://i.sstatic.net/cSzLu.png

Is there a way to achieve this? Thank you

Answer №1

Depending on the screen size, the flexbox may wrap or remain in a single line. It is important to specify how the browser should handle the viewport.

<meta name="viewport" content="width=device-width, initial-scale=1.0">

div.flex-container {
  display: flex;
  background-color: silver;
  border: 1px solid black;
  padding: 10px;
}

div.right-panel {
  order: 1;
  background-color: white;
  border: 1px solid black;
  padding: 10px;
  margin-left: 10px;
}

@media screen and (max-width: 640px) {
  .flex-container {
    flex-wrap: wrap;
  }
}
<div class="flex-container">
  <img src="https://example.com/image.jpg" width="40%" height="60%">
  <div class="right-panel">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed quis lectus nec est efficitur ultricies et vel quam. Phasellus eget sapien at nunc tempor aliquam. Duis egestas magna id libero maximus sodales. Quisque elementum urna lacus, in rutrum velit mollis ac. 
  </div>
</div>

Answer №2

To target specific screen sizes, implement media queries as shown below:

@media(max-width:768px){
  .container img { max-width:100%; }
  .sidebar { width:100%; float:left; }
}

Answer №3

Perhaps this illustration could provide some guidance for you.

.example {
  clear: both;
}
.example img {
  max-width: 350px;
  float:left;
  margin-right: 10px;
}
.example p {
  float:none;
}

.example1 {
  clear: both;
}
.example1 div {
  display:inline-block;
}
.example1 img {
  max-width: 350px;
  dispaly:inline-block;
  margin-right: 10px;
}
.example1 p {
    dispaly:inline-block;
    max-width: 350px;
}
<div class="example">
  <img src="http://f9view.com/wp-content/uploads/2013/09/What-is-Beauty-of-Nature-Description-Pictures-3.jpg" alt="">
  <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </p>
</div>
<br><hr>
<div class="example1">
 <div>
  <img src="http://f9view.com/wp-content/uploads/2013/09/What-is-Beauty-of-Nature-Description-Pictures-3.jpg" alt="">
 </div>
 <div>
    <p>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry.
  </p>

 </div>

Answer №4

I'm attempting to showcase an image along with some text positioned to the right of it. Here's the code snippet I'm using:

div.out {
  background-color: silver;
  border: 1px solid black;
  padding: 10px;
  display: flex;
}

div.right {
  position: relative;
  order: 1;
  background-color: white;
  border: 1px solid black;
  padding: 10px;
  margin-left: 10px;
}
<div class="out">
  <div id="wrapper">
    <img src="https://i.sstatic.net/asXsj.jpg" width="40%" height="60%">
    <ul id="list"></ul>
    <div id="similar" class="right">
      A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or the flower it bears. There are over a hundred species and thousands of cultivars. They form a group of plants that can be erect shrubs, climbing or trailing with
      stems that are often armed with sharp prickles. Flowers vary in size and shape and are usually large and showy, in colours ranging from white through yellows and reds. Most species are native to Asia, with smaller numbers native to Europe, North
      America, and northwestern Africa. Species, cultivars and hybrids are all widely grown for their beauty and often are fragrant. Roses have acquired cultural significance in many societies. Rose plants range in size from compact, miniature roses,
      to climbers that can reach seven meters in height. Different species hybridize easily, and this has been used in the development of the wide range of garden roses.
    </div>
  </div>
</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

Arranging divs within list items in a single row

My goal is to create a dropdown menu within an <li> element of <ul>. Here's what I have so far: HTML: <ul class="navigationTable"> <li>1</li> <li>2</li> <li><div id="comittee"> <div id= ...

jQuery - delete a single word that is case-sensitive

Is there a way to eliminate a specific case-sensitive word from a fully loaded webpage? The word in question is "Posts" and it appears within a div called #pd_top_rated_holder that is generated by Javascript. The Javascript code is sourced externally, so ...

Is it possible to utilize the Wicket:id for generating CSS?

Can the wicket:id attribute of an element or component be used for CSS styling instead of the "class" attribute? For example: .tooltipster-arrow span, .column-shifter { display: block; width: 0; height: 0; position: absolute; } Let&apos ...

Is there still a need to preload dynamic images in the background?

Imagine you have HTML code that includes an image with a placeholder: <img class="lazy-load" src="http://via.placeholder.com/150/?text=placeholder" data-src="http://via.placeholder.com/150/?text=real%20image"/> At some stage, you want to implem ...

Utilizing $.each to dynamically add data to a jQuery Mobile listview

Trying to implement a data reading mechanism using ajax resulted in unexpected html tag generation for <ul> <li>. The code snippet is as follows: (function( $, undefined ) { $(document).on("pagecreate", ".jqm-demos", function(){ ...

Why does using rgba color with an alpha value cause my div to become see-through?

I am currently attempting to assign a background color to a specific style of pop-up, but whenever I use a value that includes an alpha channel, it ends up being transparent. This is the CSS code I have been using: --color: 255, 144, 106, 0.18; background ...

Troubleshooting problem with ion-input in Ionic 3 regarding the keyboard issue

Issue with Keyboard Overlaying Button In my ionic3 app, I am facing an issue where the keyboard overlaps the "Registrarse" button when it is open, as shown in the image. The button is positioned at the bottom of the ion-content using CSS rules such as pos ...

Preventing Click Events from Firing During Drag in JavaScript

I have implemented a code for dragging containers left or right, which is functioning properly. Users can also click on the "thumb". However, I am facing an issue where a click event occurs even after dragging. I want to ensure that only either drag or cli ...

Swapping out the current div with the outcome from jQuery

I'm attempting to retrieve the most recent data for a div and replace it. The ajax query is fetching the entire HTML page, from which I am locating the right div. Now I want to update the current right div with the new content. $.ajax( { ...

The ckeditor vanishes upon refreshing the div element

I have created two pages named openclosediv.php and content.php. The openclosediv.php page contains a list of records and a button that can show/hide the div, bringing in the content from content.php. However, I am facing an issue where the CKEditor in the ...

Managing the load more feature with a Selenium script using Python

As a newcomer to Python, I am experimenting with automating clicks on the "load more" button found in the comment section of Instagram using Selenium and Python. Despite being able to click once successfully, the button disappears after the first click. ...

Retrieving a value from an array at random to assign to a different variable

I have different options for a specific variable depending on the scenario --> var lowSpeed = Math.random() * (45 - 30) + 30; var mediumSpeed = Math.random() * (60 - 45) + 45; var highSpeed = Math.random() * (80 - 60) + 45; var highwaySpeed = Math.rando ...

Variability in Focus Behavior while Opening a URL in a New Tab with window.open()

Here is a code snippet I have been using to open a URL in a new tab: window.open(urlToOpen, '_blank', 'noopener noreferrer'); The issue I am experiencing is that when this code is executed for the first time, it opens the URL in a new ...

What is the best way to animate my logo using JavaScript so that it scales smoothly just like an image logo

I dedicated a significant amount of time to create a unique and eye-catching logo animation for my website! The logo animation I designed perfectly matches the size of the logo image and can be seamlessly integrated into the site. The issue arises when th ...

What is the best method for rounding a decimal number to two decimal places?

Here is the JavaScript code I am using: $("input[name='AuthorizedAmount'], input[name='LessLaborToDate']").change(function () { var sum = parseFloat($("input[name='AuthorizedAmount']").val()).toFixed( ...

Set iframe scroll to match the height of the user's screen resolution

Can the height of an iframe scroll be adjusted to match the size of the screen (browser)? Essentially replacing the default browser scroll with the iframe scroll. Here's what I'm looking to achieve: Currently, my browser scroll is disabled: < ...

Mobile Mode Issue with Bootstrap 4 Navbar Example

Currently, I am exploring the Bootstrap material and trying to understand how to integrate their content with a jinja2 file that I am preparing for a django project. Upon attempting to copy and paste their example code (specifically this example http://get ...

Issue: Troubile in fetching CSS file from CDN and using local copy as fallback, What is the solution?

I previously inquired about this issue, however, I did not receive a satisfactory solution. Therefore, here I am asking again for your assistance. I am attempting to retrieve my CSS from an external CDN service, such as http://cdn.example.com/. This scrip ...

Using ReactJS to toggle a div upon clicking

I am facing an issue with toggling a div by clicking on text within a toggle bar. I am in the process of recreating a WordPress website template in React JS that was originally built using bootstrap. You can view the original template here. So far, I have ...

"Step-by-step guide for incorporating a right-to-left (RTL) Bootstrap

Is there a way to make my bootstrap navbar right-to-left (RTL)? I want the logo to be on the right and the links to flow from right to left. I've tried various tricks but none of them seem to work. Here's the code I currently have: <nav class ...