Ensuring the container height remains consistent with fluctuating content dimensions

Imagine a container with content, where the container's width is fixed but its height adjusts based on its inner content.

Initially, when the content causes the container to be a specific height, the challenge arises in shrinking the inner elements without changing the container's height.

In the code snippet provided, the objective is to retain the original height of the red box while decreasing the font size of the inner content.

function adjustSize() {
  document.querySelector('.box').classList.add('active');
}
.container {
  border: 1px solid red;
  width: 30%;
}

.box {
  background: lime;
  transition: font-size 1s linear;
}

.box.active {
  font-size: 5px;
}
<div class="container">
  <p class="box">This text changes size and affects the container height.</p>
</div>

<button onclick="adjustSize()">Adjust text size</button>

Currently, both the red-box and inner content shrink proportionally. However, the desired outcome is for the red-box to maintain its initial height.

While imposing a fixed height on the container (red box) could solve this challenge, the goal is to have the height dynamically determined by the initial inner contents.

Is there a CSS solution to preserve the container's initial height while modifying the child element's size? The attempt at using a wrapper element was made, yet further guidance is sought.

Answer №1

My objective is to keep the red box's initial height constant while decreasing the font size of its inner content.

An alternative approach could be utilizing transform: scale(…) instead of adjusting the font-size.

By scaling the entire element, as opposed to just reducing the font size, the transition is usually smoother and the element retains its original space - preventing the container from shrinking in height.

Answer №2

Obtain the height of the container and adjust it before modifying the font style. Ensure that the margins are also properly set.

JavaScript:

function changeSize() {
    var container = document.querySelector('.container');
    var height = container.offsetHeight;
  container.style.height = height + "px";
  document.querySelector('.box').classList.add('active');
}

CSS:

.container {
  border: 1px solid red;
  width: 30%;
}

.box {
  background: lime;
  transition: font-size 1s linear;
  margin: 10px;
}

.box.active {
  font-size: 5px;
}

Example: https://jsfiddle.net/ufgo5prv/

Answer №3

Utilize the min-height property to define the initial height. Adjust the height dynamically through script.

var innerContent = document.getElementsByClassName("box");
console.log(innerContent[0].clientHeight);

var container = document.getElementsByClassName("container"); 
console.log(container);
container[0].style.minHeight = innerContent[0].clientHeight + 40 + "px";

function changeSize() {
  document.querySelector('.box').classList.add('active');

}
.container {
  border: 1px solid red;
  width: 30%; 
}

.box {
  background: lime;
  transition: font-size 1s linear;
}

.box.active {
  font-size: 5px;
}
<div class="container">
  <p class="box">This is some text which changes size and causes the container to change height.This is some text which changes size and causes the container to change height.</p>
</div>

<button onclick="changeSize()">Change text size</button>

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

Change in navigation bar appearance on scroll down

I am attempting to create a fixed navigation bar by customizing the Bootstrap CSS file. The goal is to change the navbar's color and make it stick to the top when scrolling down. Although I followed the instructions in this article, the JS code I add ...

Change the css code to override the color of the background material

Hey there, I'm facing an issue with overriding the background color of a material element. I've tried several methods but can't seem to get it to change. After inspecting the element on the web browser, I found the CSS code that controls th ...

Custom Bootstrap 3 Panel Organization

I'm intrigued by the layout shown in the image attached. My attempts to recreate it using a combination of columns and rows have been unsuccessful. Could it be that I'm going about this the wrong way? ...

Should the value exceed the designated width of the combobox, it should be displayed across multiple lines

Having an issue where, when I set the width of the combobox and the value inside it is longer than expected, the full value isn't displayed. I am considering displaying the value on multiple lines to ensure the entire content is visible. Unfortunatel ...

Exploring Angular's Animation Feature for HTML Element Movement

I'm currently using @angular/animations module to animate HTML objects and I want to move an object when a button is clicked. For example: * The object initially has top: 800px, left: 800px * Clicking the 'W' button should move the obje ...

Preventing div contents from shrinking with changing screen sizes

I am currently working on creating a portfolio website similar to this one. Check out the Website Here Typically, when the screen size is reduced to less than half of the full width, the content of a website tends to be hidden rather than shrinking. Howe ...

What is the best way to utilize *ngFor for looping in Angular 6 in order to display three images simultaneously?

I have successfully added 3 images on a single slide. How can I implement *ngFor to dynamically load data? <carousel> <slide> <img src="../../assets/wts1.png" alt="first slide" style="display: inline-blo ...

Send HTML table information from view to Controller action in ASP.NET Core by converting it to a list

Encountering issues with null values in the controller action method, preventing table data rows from being looped through. Model: class: public class Generation { public int Generation1Count { get; set; } public int Generation1TotalS ...

"Running experiments with Google Ads and Google Analytics scripts results in a blank page being displayed

Currently, I am working on a plain HTML file and conducting tests to ensure that the Google Ads and Analytics scripts are functioning correctly before implementing them on other pages. A colleague who has an AdSense account provided me with the script code ...

What steps can I take to update this HTML document and implement the DOM 2 Event Model?

I'm struggling with converting my document from the DOM 0 Event model to the DOM 2 Event model standards. I've tried getting help from different tutors on Chegg, but no one seems to have a solution for me. Hoping someone here can assist me!:) P. ...

Text fades into view from the bottom after a line break using CSS and JavaScript

I'm looking to create a unique fade reveal text effect for a multi-line H1. The goal is for each line of the text to fade up from its own row, rather than simply revealing from the bottom. Here's an example I've already developed, but it cu ...

I am having trouble properly positioning an icon next to its corresponding text within a menu item

I'm a newcomer to the world of HTML5 + CSS3 and I'm having trouble with aligning menus, text, and icons within the menu. Here's the issue: Each line of the menu consists of an icon and a text description. The problem is that they are too clo ...

What is the method for utilizing the variable from express in HTML?

Currently, I am working with Express and have the following code: app.get('/', requiresAuth(), (req, res) => res.sendFile('/db_auth_site/index.html', { title: 'Hey', message: 'Hello there!' }) ); I am trying ...

What is the source of these additional pixels that appear when percentages are used for defining height?

I have been working on implementing a sticky footer that I've successfully used in the past for various websites. You can find the link to the original method here: However, this time I am facing a challenge as I want to make the footer more responsi ...

Is it practical to extract the page type/name from the CSS of the selected tab?

My website has multiple tabs on a single page and I want to integrate Google Analytics code. However, since all the tabs are part of the same page, I need a way to track which tab the user is interacting with. Would using CSS to check for the selected tab ...

The iPhone display scaling issue is causing difficulty viewing the entire website

Currently experiencing difficulties with a page that lacks sufficient content, resulting in a smaller height. As a result, the iPhone is scaling the page and cutting off the full menu bar (960px wide). I attempted to set a minimum height using a media quer ...

Transforming data from a particular csv file and embedding it into an html document

I am currently working on a feature that allows users to select a specific CSV file and display it as an HTML table on the webpage with the click of a button. The process involves: Selecting the desired file from a dropdown menu to determine the CSV fi ...

What is the best way to add content to a box once it has been hovered over?

Looking to create a sticky note-style design, but struggling with displaying the content inside the box only when hovered. In my code example below, can you help me achieve this effect? body { margin: 45px; } .box { display:inline-block; backgrou ...

"Customizing the website's font: A step-by-step guide to replacing the default Roboto font with a custom font

Despite my efforts to globally override the font, I am still encountering instances where the Roboto font is not being replaced, particularly on MUI select and autocomplete components. import { createTheme } from '@material-ui/core/styles'; // A ...

Move the focus to the previous input field by removing the key

I have created a phone number input with 10 fields that automatically skip to the next field when you fill in each number. However, I would like to be able to go back to the previous field if I make a mistake and need to delete a digit. How can I achieve ...