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:

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

Emails sent through an HTML form submission should always include a valid "from"

I am currently in the process of creating a feedback form that allows individuals to provide anonymous feedback. I have explored nodemailer and node-sendmail, but I have encountered an issue with both requiring a from address. While I am aware that this ca ...

What are some creative ways to animate a highlight effect on a CSS

I'm struggling to implement a sliding underline effect for my top navigation bar on hover. I've tried using transitions and keyframes, but so far it's not working as expected. My current attempt only triggers the effect on the parent elemen ...

Solving the right-to-left orientation issue in the Bootstrap navbar

I have implemented a navbar using the code below, which aligns well from the right side. However, the menu items are sorted in ltr order and I need them to be in rtl order, so that "Dropdown 1" starts from the right side of the page. Currently, it appears ...

What is the process for implementing the tooltip plugin within a modal dialog in Twitter Bootstrap?

Hello there, I am currently using the latest version of Bootstrap, v2.1.1. I am trying to implement a tooltip in my modal dialog. <!DOCTYPE html> <html lang="zh-CN"> <head> <title>Bootstrap v2.1.1</title> <meta htt ...

Is there a way to display images in Vuetify similar to the linked image below?

I am looking to measure my v-img like the one shown in the image below. https://i.stack.imgur.com/edMKr.jpg Does anyone know how I can achieve something similar to the image shown using vuetify's v-img. <div class="row"> <v ...

ways to access a designated nodal point in angularJS from a constantly updating data source

I am in need of assistance with how to open a specific node using angularJS from a dynamic data source. I have been able to toggle (hide/show) the sub nodes, but what I would like is for clicking on a specific node to only show its sub-nodes. Currently, wh ...

Tips for handling CSS loading delays with icons in OpenLayers markers

When using openlayers (v4.6.4) with font-awesome as marker icons, the icons do not display upon first load (even after clearing cache and hard reload). Instead, I see a rectangle resembling a broken character. It is only on the second load that they appear ...

Implementing dual backgrounds in CSS

I am trying to achieve a gradient background for li elements along with different image backgrounds for each individual li. Is there a way to accomplish this? List of items: <div id="right_navigation_container"> <ul> <li id ...

I am encountering unexpected issues with the functionality of img srcset and sizes

Attempting to enhance the responsiveness of images on my website has led me to discover the srcset and sizes attributes. The objective is clear: If the screen size exceeds 600px or falls below 300px, a 250x250 image should be displayed For sizes in betw ...

Creating a unique custom icon by leveraging the material UI icons - a step-by-step guide

I am looking to create an arrow graphic similar to the one shown in the image below https://i.stack.imgur.com/k9TvH.png Originally, I was able to achieve this using SVG - <svg height='24' width='12' style={{ marginBottom: '-4p ...

If you invoke revokeObjectURL, Chrome will fail to display Blob images

-----update------ After some investigation, I found that commenting out window.URL.revokeObjectURL( imgSrc ); fixed the issue in all browsers. It seems like Chrome was revoking the URL too early. I am curious to understand why this behavior occurs, and if ...

Video background is malfunctioning on Firefox and Internet Explorer

Currently, I am facing some issues with the JQuery plugin 'videoBG'. The problem is that when I view the video offline, it works perfectly in all browsers. However, once I go online, the video stops working in FireFox and IE. It seems like the sc ...

Experience unique website functionalities across Windows/Android and Apple ecosystems

Apologies for any language errors in advance. I'm facing an issue where my website appears differently on Safari or Chrome on iOS/MacOS compared to Windows 10 or Android devices. If you observe the List of Receiving Countries in the images provided: ...

Tips for dynamically styling a Styled Component with all the CSS housed in an external file

My goal is to dynamically render a Styled Component. In the past, it was simple because all styling was contained within the component itself. However, I now strive to maintain a separation of concerns by storing the CSS in an external file. While this app ...

The height of the image remains constant when its width is decreased, causing it not to resize proportionally

I am facing an issue with displaying images in a div of various resolutions. When I have a wide image with low height, such as 730x90, it does not show properly on mobile devices. The width shrinks but the height remains the same. Below is the code snipp ...

The tag's onclick function that was dynamically created was not triggering in jQuery

I created a web application using jquery mobile and ran into an issue. I am trying to call a function by clicking on a dynamically generated anchor tag, but it's not working and showing an error that the function is not defined. Any assistance on this ...

Change the background color of all cells in a Bootstrap table by hovering over a single cell

Here is the code for a bootstrap table: <body leftmargin="0" topmargin="0" bgcolor="#ffffff" marginheight="0" marginwidth="0"> <div class="container-fluid h-100"> <div class="row float-right align-items-center" style="height: 5%;"> ...

The clash of interests between jQuery and Bootstrap

I'm facing a problem with conflicting jQuery and Bootstrap files in my header.php code. Whenever I include the jQuery file before the bootstrap.js file, it causes issues with the tabs on my website where clicking on them does not navigate me to the co ...

I possess a pair of UI tabs. Whenever a button located outside the tab is clicked, I am required to activate a distinct tab

As a beginner in Javascript, I recently encountered an issue with a UI tab element that contains two tabs. My goal is to create a button that, when clicked, will scroll up and activate the second tab. <style> .tab-container { overflow-x: ...

Restrict the dimensions of the image to fit within the div

I've been struggling to resize my LinkedIn logo on my website. I've attempted various methods, like using inherit and setting max height and width to 100%, but the logo keeps displaying at full size. Do I need to use a different type of containe ...