Adjust the image's placement and reduce its size as the screen size decreases, without using any media queries

Essentially, I encountered an issue where I had a table row with 2 cells - the left cell containing text and the right one containing an image. As the screen size decreased, I needed the image to move below the text. After some investigation, I delved into the workings of the display and flex properties to address this problem.

In the table row properties, I included

display: flex;
flex-wrap: wrap;

and for each cell, I added flex: 1 1 10ch;, as demonstrated on this resource, providing a clear explanation.

Now, my aim is for the image to shrink when it moves below the text as the screen size decreases. The fixed size of the image does not adjust proportionally with percentages or using max-width. Additionally, I attempted vertical alignment but noticed no change.

<div>
  <table style="width: 100%;">
    <tbody>
      <tr style="display: flex; flex-wrap: wrap;">
        <td style="text-align: left; vertical-align: middle; padding: 0 3% 0 0; flex: 3 1 50ch;">
          <p>Some random text. Typically, there's more content here.</p>
        </td>
        <td align="justify" style="padding: 0.5%; flex: 1 1 10ch;"><img src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg" width="500"  /></td>
      </tr>
    </tbody>
  </table>
</div>

I'm also struggling with the vertical alignment of the text. Is there a method to reposition it to the center?

Answer №1

To adjust the size of an image to fit its container, you can use the object-fit property:

img{
 object-fit: cover;
 width: 100%;
 height: 100%;
}

Answer №2

Avoid using ch as units in this context, as it can lead to confusion and is not always compatible with variable width fonts. When working with images, the situation can be exacerbated. It's recommended to use em, rem, or even px. In this case, I opted for the latter.

The flexibility of Flex goes beyond what the example demonstrated. Check out the comments in the code snippet below for more insight.

.tbl {
  width: 100%;
}

.row {
  display: flex;
  flex-wrap: wrap;
}

.first {
  text-align: left;
  vertical-align: middle;
  padding: 0 3% 0 0;
  flex: 3 1 auto; /* Adjusts size dynamically based on content */
}

.second {
  padding: 0.5%;
  flex: 0 1 500px; /* Fixed basis at 500px, only shrinks when necessary */
}

.second img {
  width: 100%; /* Image spans the full width of its container */
}
<div>
  <table class="tbl">
    <tbody>
      <tr class="row">
        <td class="first">
          <p>Some random text. Typically longer than shown here.</p>
        </td>
        <td align="justify" class="second"><img src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg" /></td>
      </tr>
    </tbody>
  </table>
</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

Experiencing difficulties with JavaScript/jQuery when encountering the 'use strict' error

I have been working on a small function to change the background of an HTML file, but I keep getting the 'Missing 'use strict' statement' error message from JSLint despite trying multiple solutions. Can anyone suggest how to resolve th ...

Juggling PHP scripts within javascript

Can you help me with a question I have? I am working on multiple JS scripts, such as this one: <script> $('#mapveto1').click(function() { $('#mapveto1').addClass('banned'); $('#map1').text('cobble ...

Delaying UI interactivity until the document is fully loaded

I'm currently developing a web application that must be compatible with Internet Explorer 8 (yes, you read it right, compatible with the HELL). The issue I'm facing is with uploading a file which is later processed by PHP code and then refreshes ...

Change the absolute positioning of a div element to be applied globally

When trying to get the position of an element using jQuery's .offset(), I then set the div to have a position:absolute; and adjust its left and top based on the information obtained from offset(). This method works well when all the element's pa ...

Error with the jQuery scrolling plugin

Currently, the script is set up this way: jQuery(document).ready(function(){ var windheight = jQuery(window).height(); var windTop = jQuery(window).scrollTop(); var windbottom = windheight + windTop ; jQuery.fn.revealOnScroll = function(){ return this.e ...

Creating a Dynamic Clear Button for a Text Area in Angular

Working on my Angular application, I have implemented a form with a textarea element. My goal is to incorporate a clear button inside the textarea element that should: Appear only when the textarea is focused Disappear when the textarea is out of focus ( ...

Utilizing the CSS Top and Left attributes along with Jquery animations

There are two divisions on a page, namely Left_Div and Right_Div. The images in the Right_Div are positioned using CSS properties such as Top and Left. The Left_Div acts as a left panel, and upon clicking a button, both Left_Div and Right_Div animate to sl ...

The page only appears properly after clearing the cache

My floating list menu is behaving oddly in Firefox. It only displays correctly after clearing the cache, but as soon as I refresh the page, the menu breaks again. The links inside the list elements appear wider than the text, even though I haven't se ...

The information I input into this form is failing to be stored in the WAMP database

After implementing this PHP code, there are no error messages being displayed when I submit the HTML form. However, the data is not being added to the WAMP table. <?php error_reporting(E_ALL); ini_set('display_errors', 1); //$user_name = "roo ...

Steps for developing a floating image transition using CSS and React

Looking to achieve a floating image transition using CSS and React, similar to the Divi theme header images. Check out the example below: enter image description here For more information, visit: ...

Is it possible to adjust the CSS code linked to the HTML tag based on the specific webpage being viewed?

I am facing an issue with the homepage of my website, which uses Scrollmagic.js for smooth scrolling. In order for the sticky footer CSS to work properly on all other pages, the HTML tag needs to have a height of 100%. However, if I add this height value t ...

Adjust the state based on the event in ReactJs

I have the following code snippet in a React JS application: const items = [ { value: "A", label: "A" }, { value: "B", label: "B" }, { value: "C", label: "C" }, { value: "D", label: "D" }, { value: " ...

The implementation of subnavbars within a navigation bar using HTML and CSS

I am looking to align the Login button with the settings icon on the same line above the links in my navbar. Is there a way to accomplish this? Check out this fiddle that demonstrates my issue: FIDDLE Here is the code I have written: <body> < ...

Explicitly employ undefined constants deliberately

So I am working on a project called WingStyle: https://github.com/IngwiePhoenix/WingStyle. It is still in the development phase, and I'm looking for ways to improve its functionality. One thing I want to address is the use of quotes. For example, whe ...

Using PHP variables in JavaScript to access getElementById

I have multiple forms displayed on a single PHP page. They all follow a similar structure: <form id="test_form_1" action="test_submit.php" method="post" name="test_form"> <label>This is Question #1:</label> <p> &l ...

Establish a seamless UDP connection using JavaScript and HTML5

Is it feasible to establish a direct two-way connection with a UDP server using javascript/HTML5 (without node.js)? While WebRTC is an option, my understanding is that it does not support sending datagrams to a specific server. I am primarily focused on c ...

Ensure that the link's color remains constant and remove any styling of text-decoration

Attempting to create a customized header. My goal is to change the color and remove text decoration in the navbar. Below is my code snippet: ReactJS: import React from 'react'; import './Header.css'; function Header() { return ( ...

Tips for determining the full width of a webpage, not solely the width of the window

While we can easily determine the width of the window using $(window).width(); This only gives us the width of the window itself, not accounting for any overflowing content. When I refer to overflowing, I mean elements that extend beyond the visible view ...

Set the display property of all child elements within the DIV to none

CSS <div class="container"> <span></span> <input type="text"> </div> JavaScript function hideElements(){ let container = document.querySelector(".container"); let elements = ...

Building a Table Using HTML and CSS

I'm currently tackling this school assignment and have hit a roadblock with two issues. The HTML Validation error I'm encountering is regarding a table row that is 5 columns wide, exceeding the established count by the first row (4). From line 7 ...