A method using CSS to automatically resize an image in a teaser box as text content increases, ensuring compatibility across various web

In the process of constructing a teaser element using HTML, I plan to integrate an image and a title. This is what I have in mind:

<div>
    <img src="./image.jpg" />
    <h1 contenteditable>Something</h1>
</div>

While the dimensions of the <div> are fixed, the length of the text within the <h1> may vary. It's important that the text takes precedence, causing the image to automatically resize when the text extends (without overflowing from the box). While one solution involves using position:absolute for the text to overlay on the image, this leads to cropping of the image.

A workaround utilizing CSS grid has been discovered:

div {
    width: 300px;
    height: 200px;
    background: red;
    padding: 10px;
    display: grid;
    grid-template-rows: 1fr auto;
    overflow: hidden;
}

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

h1 {
    margin-top: 10px;
    overflow: auto;
}

This method functions properly in Firefox:

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

However, it encounters issues in Chrome:

https://i.sstatic.net/6Hsom4MB.png

Attempts with Flexbox were unsuccessful as well. Although JavaScript could adjust the image's height dynamically, I'm inclined towards a responsive CSS solution.

You can access the code for this specific example through this CodePen link.

Answer №1

For a header that needs to have a fixed text content, making the element height known, you can follow this approach:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
}

div {
  width: 300px;
  height: 200px;
  background: red;
  padding: 10px;
  overflow: hidden;
}

img {
  width: 100%;
  height: calc(100% - 40px);
  /*30px height of the H1 plus 10px of the margin-top*/
  object-fit: contain;
}

h1 {
  margin-top: 10px;
  overflow: auto;
}
<div>  
  <img src="https://picsum.photos/200/300" />
  <h1 contenteditable>Blabla</h1>
</div>

The display: grid was removed to prevent interference with the object-fit property settings.

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

Explaining the precise measurements of font size in CSS

I recently started diving into the world of CSS and picked up a copy of Eric Meyer's 3rd edition of CSS: The Definitive Guide. While reading through his section on font sizes (page 107), I came across an interesting concept - that the font size deter ...

How to create a captivating image effect using CSS on hover

I am attempting to create an animated image on hover. The desired outcome is similar to the one showcased here: Check it out (scroll to view the image "Our Team") Essentially, I want a background where I can showcase information such as names and links ju ...

Preventing a webpage's CSS from affecting an iframe loading an HTML document

Whenever I load an iframe, it seems to change a bit. Do I need to apply some kind of reset or adjustment? How can I ensure that the content within the iframe looks consistent no matter what page it's loaded into? ...

What's the best way to eliminate the space between vertically aligned divs?

I've been attempting to adjust the space between div 1 and div 2, but despite adjusting margins and padding, the gap persists. When modifying properties on the parent div, the children divs end up extending beyond the parent. I realize my approach to ...

Steps to make the placeholder in an MUI TextField component move to the top of the box instead of staying within the border:

I'm working on styling a text field in MUI to achieve the look shown in the image below: https://i.sstatic.net/JHhpf.png However, my current implementation looks like this: https://i.sstatic.net/5N7hH.png Currently, when I click inside the text fi ...

Which specific HTML element triggers the initiation of ajax in Jquery?

I have a situation where various HTML elements trigger ajax requests when clicked. My goal is to determine which specific element was clicked inside the ajaxComplete event. Although I attempted to use event.target, it only returns the entire document inst ...

I have a collection of dictionaries that I need to integrate into a Django application. What is the best approach for doing this?

My data structure consists of an array of dictionaries: [{'name': 'X','class':'A'},{'name':'Y','class':'B'}] I need the HTML to display as follows: Name:X, Class:A Name:Y, C ...

Is it possible to nest a parsys within another parsys in this context?

On my webpage, I have a paragraph system (parsys) with a component that contains two inner paragraph systems. When I try to add a rich text component to each of these inner paragraph systems, the components overlap and become uneditable. Is it possible t ...

does not output any console log statements

I am attempting to showcase the values of checkboxes on the console, however, it is not working. <input type="checkbox" id="id_price" value="1" onclick="display_img()">Under £200<br> <input type="checkbox" id="id_pr ...

Container Slides Along Despite Accurate Measurements

In a recent project of mine, I utilized two containers positioned side by side. Upon clicking a button, my goal was to have the content container (essentially a false body) decrease its width while the menu container would slide in. Despite my best effort ...

Show the "Splash" picture, then switch to a newly uploaded image and show it for a set amount of time

I am in the process of developing an HTML/JavaScript page that will showcase a splash image (splash.jpg) until it gets replaced by another image file called latest.jpg. Once this latest.jpg is displayed, I want it to remain on the screen for 90 seconds bef ...

"Adjusting the size of a circle to zero in a D3 SVG using Angular 2

Trying to create a basic line graph using d3 in Angular 2 typescript. Below is the code snippet: import { Component, ViewChild, ElementRef, Input, OnInit } from '@angular/core'; import * as d3 from 'd3'; @Component({ selector: 'm ...

What is the process for customizing the color of the focused label in Material UI through styling?

Recently, I delved into the world of JSS for styling and ran into an intriguing issue. My goal was to modify the color of a label in an InputLabel component when it is in focus state. After some tinkering, I managed to achieve this using the code snippet b ...

Adjust the CSS to give <a> elements the appearance of plain text

Is it possible to style an anchor tag to appear identical to plain text? Consider the following scenario: <p> Here is some text and here is a <a class='no-link' href="http://www.example.com" >link</a></p> I have atte ...

Creating a CSS class dynamically with AngularJS: A step-by-step guide

I'm working on an Angular JS website that pulls data from an API to create a dynamic CSS class. This class will be used to format other data being displayed on the page. Is there a way for Angular JS $scope data to be generated in the Controller whil ...

Utilizing the '::marker' pseudo-element for generating Markdown-inspired headers

This code snippet is functioning correctly, however, I have a suggestion to make it more appropriate. Instead of using '::before', why not try using '::marker'? I attempted this adjustment but encountered an issue. Can anyone explain wh ...

Python: Remove tag and substitute u00a0 within a JSON document

I have a JSON file structured like this: [ { "content": ["<p style=\"text-align:justify;\"> This is content1</p>"], "title" : ["This is\u00a0title 1"] }, { "content": ["<p style=\"text-a ...

The submission of the form with the ID "myForm" using document.getElementById("myForm").submit() is

<form name="formName" id="formName" action="" method="post" autocomplete="off"> <input type="hidden" name="text1" id="text1" value='0' /> <input type="button" name ="submit" onClick="Submit()" value="submit"> ...

Utilize a selector to target and retrieve the content of the nested element

How can we target the deepest inner child using CSS selectors? Take a look at the example below: <div class='d1 view'> <div class='d2 view'> <div class='d3 view'></div> </div> ...

The POST request is coming back as null, even though it includes both the name and

Having issues with a login PHP code where the post method is returning an empty string. Here is my HTML code: <form action="iniSesion.php" method="post"> <div class="form-group"> <input type="email" name="email_" class ...