Resizing images next to lengthy text on mobile screens

My text and image keep behaving oddly when viewed in a smaller window or on mobile. The image resizes unpredictably based on the length of the text, as shown in my preview. I attempted to use flex wrap to contain the longer text, but it only pushed the image below the text. Am I overlooking something simple here? Thank you in advance for any assistance.

.content-title {
  font-family: "Helvetica Neue", sans-serif;
  color: black;
}

.icon-img {
  background-image: url(https://cdn2.oceansbridge.com/2017/09/16181826/A-hermit-in-a-ruin.-1650-1668-Jan-Adriaensz.-van-Staveren-oil-painting.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
  border-radius: 50%;
  width: 3.4em;
  height: 3.4em;
}

.content-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
<div class="content-container">
  <div class="box">
    <div class="content-heading">
      <h3 class="content-title">Sample of longer longer longer longer longer longer longer longer longer longer longer longer longer text</h3>
      <div class="icon-img"></div>
    </div>
  </div>
</div>

<div class="content-container">
  <div class="box">
    <div class="content-heading">
      <h3 class="content-title">Sample of shorter text</h3>
      <div class="icon-img"></div>
    </div>
  </div>
</div>

Answer №1

To set the width of .content-title to be a percentage minus the img width, you can use the calc function in CSS. Here is an example:

width: calc(100% - 3.4em);

.content-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.content-title {
  font-family: "Helvetica Neue", sans-serif;
  color: black;
  width: calc(100% - 3.4em);
}

.icon-img {
  background-image: url(https://cdn2.oceansbridge.com/2017/09/16181826/A-hermit-in-a-ruin.-1650-1668-Jan-Adriaensz.-van-Staveren-oil-painting.jpg);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
  border-radius: 50%;
  width: 3.4em;
  height: 3.4em;
}
<div class="content-container">
  <div class="box">
    <div class="content-heading">
      <h3 class="content-title">Sample of longer longer longer longer longer longer longer longer longer longer longer longer longer text</h3>
      <div class="icon-img"></div>
    </div>
  </div>
</div>

<div class="content-container">
  <div class="box">
    <div class="content-heading">
      <h3 class="content-title">Sample of shorter text</h3>
      <div class="icon-img"></div>
    </div>
  </div>
</div>




Check out these helpful resources:


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

The content inside the inner div does not stretch to match the height of its parent element

I want to address a specific issue I am facing with my div structure. Although it may seem similar to other questions on StackOverflow, none of the existing solutions have worked for me. The problem is that I have two child divs inside a parent div. The h ...

Cut off text inside an HTML anchor element using an ellipsis, while ensuring the image remains at the end - all without the need for JavaScript

How can I truncate link text on a header with an ellipsis but keep the image at the end as a glyphicon? And all of this without using JS. Here's my code: <!DOCTYPE html> <html> <body> <ul> <li> <a href="# ...

What could be the reason for the malfunction of the Bootstrap panel toggle feature in a ReactJS production build

In my ReactJS development, I have successfully added Bootstrap panel toggle functionality. However, I encountered an issue when deploying the React build code - the panel is not expanding. After investigating, I realized that the problem lies in using hr ...

The main div on my website appears completely chaotic when viewed in IE versions 7 and 8

On my website, the main div appears floated left on some pages and right on others. In certain pages, it seems to be scattered all over in IE 7 and 8 - I didn't even bother checking in IE6. However, oddly enough, it displays perfectly in IE 6. I need ...

Utilizing Capture Groups in CSS File for Regex Search and Replace

When it comes to extracting critical styles from a CSS file wrapped in a @critical rule, I run into some issues: @critical { .foo { ... } @media bar { ... } } The command I'm using for extraction involves sed search ...

When printing a PDF, a div that is set to 100vh only takes up

After setting the div to be 100vh, I realized that when printing it only takes up half of the page. Adding media print did not make a difference, and even trying to set the height to 100% instead didn't work for PDFs. html, body { height: 100%; ...

What is the best way to automatically clear an input field after a specified delay?

On my landing page, I have a single input field where users can enter their email address. Upon successful entry... success: function(result){ console.log(result.status); if(result.status == true) { $('input').attr("style", "color:green" ...

Page not reaching the very top when scrolled

Having a puzzling issue that's got me stumped. Currently working on my professor's website at jurgec.net. The problem is specific to the mobile version of the site. Whenever clicking on a link like "I am an undergraduate student," scrolling down ...

Creating personalized styles for my React components with the help of styled-components

Exploring the power of styled-components for custom styling on child components has been an interesting journey for me. For instance, I recently crafted a unique card component named myCard. Take a look at how it's structured: import React from "rea ...

``How can I apply styling to a child component in React within a regular HTML element?

I'm currently learning React and faced a unique situation while working on projects. Here's the scenario - I have two React child components nested under a standard HTML element. If I need to target and style each individual React child element ...

Ways to condense a text by using dots in the middle portion of it

How can I dynamically shorten the text within a container that varies in width? The goal is to replace the strings between the first and last words with dots so that it fits within the container. For example, Sydney - ... - Quito. It should only replace wh ...

Having trouble with Drag & Drop on Safari iOS? It seems that it won't drag or respond to drop on desktop or iPad

As I work on coding a webpage designed for viewing on an iPad, I have encountered an issue with Safari/Webkit's drag and drop functionality. Despite copying Safari's example code exactly, the drag and drop feature refuses to work as expected. Th ...

increasing the SQL integer value with padding

Is it possible to apply padding to certain INT return values retrieved from an SQL database using CSS within a div tag? I need specific numbers to have padding-left: 20px; while other specific numbers should have padding-right: 20px;. This presents a styl ...

Styling Based on Conditions in Angular

Exploring the unique function of conditional formatting in Microsoft Excel, where color bars are utilized to represent percentages in comparison to the highest value. https://i.sstatic.net/nTGCJ.png Is there a way to replicate this functionality using HT ...

"Entering a text message will prompt the appearance of the on-screen keyboard in capital

Our website is optimized for use on Android tablets. I have added the following CSS to the input's class: text-transform: uppercase; While this successfully displays typed letters in uppercase, it does not change the appearance of the on-screen keyb ...

What is the process for implementing a third-party component in my web application?

After some experimentation, I've discovered that it's necessary to include the link to the css files in the header and then mention the link to the js files before the closing tag. However, I encountered difficulties when trying to use a compone ...

Are Bootstrap Input groups inconsistent?

Hey there! I've been working on the sign-in example, but I seem to have hit a roadblock. In my local setup, the top image is what I see in my browser after running the code, while the desired layout that I found on the Bootstrap site is the one below ...

What is the most efficient method for implementing uniform rounded corners in HTML/CSS across different web browsers?

Is there a simple way to achieve cross-browser compatibility without a lot of tedious work? I'm looking for a solution that works effortlessly on IE7+, FF2+, and Chrome, without resorting to using tables which may be outdated. Is there a middle ground ...

Is the Site Header displayed depending on the scroll position and direction of scrolling?

On my website, I have a header that I want to hide when the user scrolls down 100px and show again when they scroll up 50px. I attempted to write a script for this functionality, but it doesn't seem to be working as expected. CSS /* This CSS rule w ...

Steps for converting SCSS to CSS using node-sass

I have a main.scss file with numerous imports from other .scss files. Whenever I make changes to any of the *.scss files, the master.css file is automatically generated. I rely solely on NPM for my build process and do not use Gulp or Grunt! It's imp ...