Enforce the text inside the child div to perfectly fit within the parent container

I have a challenge with two div elements in my HTML code as shown below:

<div id="parent">
    <div id="child">
        <p class="child-text"> ..... </p>
        <a class="child-link"> ..... </a>
        <p class="child-text"> ..... </p>
    </div>
</div>

#parent{ height: 400px; width:100px}

How can I make the contents of the #child div to fit vertically within the parent div? I am searching for a solution that will automatically resize the content of the child div based on its ID. I tried using jQuery.fittext, but it only works with the child-text class and not the entire child div. It seems like it doesn't have the intelligence to recognize the inner elements within the child div and adjust them accordingly.

Are there any other alternatives available for this task?

Answer №1

One possible solution could be implementing the following code snippet:

var fontSize = 10;
while($('#child').height() <= $('#parent').height()){
    fontSize++;
    $('#child').css('font-size', fontSize + 'px');
}
#parent{ height: 400px; width:100px;background:red;}
#child{padding-bottom:5px;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="parent">
    <div id="child">
        <p class="child-text"> Once upon a time </p>
        <a class="child-link"> there lived a monkey</a>
        <p class="child-text"> who loved to eat oranges </p>
    </div>
</div>

The JavaScript code increases the font size of #child by 1 pixel each time its height is smaller than #parent. In the CSS, a bottom padding of 5px is applied to #child to prevent overflow.

JSFiddle

Answer №2

Include a table display property value for the #parent element and a table-cell display property value for the elements within #parent that you want to measure the height of.

#parent, #child {
  width: 100%;
  height: 100%;
  position: relative;
  display: table;
  }

.child-link, .child-text { /* Specify the ID/Class for the desired elements within #parent */
  display: inline-block;
  height: 100%;
  width: 150px;
  vertical-align: top;
  background-color: green;
  bottom: 0;
  display: table-cell;
  border-left: solid 5px white; /* For visual distinction */
  }
<div id="parent">
    <div id="child"><p class="child-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis felis nec est iaculis, id rhoncus eros efficitur. Aliquam justo felis, semper in placerat non, facilisis sed elit. </p>
      
        <a class="child-link"> Lorem </a>
        <p class="child-text">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis felis nec est iaculis, id rhoncus eros efficitur. Aliquam justo felis, semper in placerat non, facilisis sed elit.
      
       Lorem ipsum dolor sit amet...
      
       ...quisque sagittis felis nec est iaculis.</p>
    </div>
</div>

To make it responsive based on screen size, use the following approach:

@media screen and (max-width: 768px) { /* Modify as necessary */
enter code here

}

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

Calculate the worth of a specific item within an array of objects and save the outcome as a new attribute

I am attempting to calculate the value in each element and then create a new element called "Count". Rule: Count the quantity if the next element has the same "Quantity" value, then add the total to a new element named "Count". I have tried implementing th ...

Check if an object is already in an array before pushing it in: JavaScript

Summary: I am facing an issue with adding products to the cart on a webpage. There are 10 different "add to cart" buttons for 10 different products. When a user clicks on the button, the product should be added to the this.itemsInCart array if it is not a ...

Attempting to add text one letter at a time

Hey there! I'm new to JavaScript and Jquery, and I need some help. My goal is to display text letter by letter when a button is clicked using the setTimeout function. However, I seem to be encountering some issues. Any assistance would be greatly appr ...

Using JavaScript promises to handle connection pooling and query execution

I am contemplating whether this approach is on the right track or if it requires further adjustments. Should I consider promisifying my custom MySQL getConnection method as well? request: function(queryRequest) { return new Promise(function(re ...

How can I display the data successfully returned from a Laravel jQuery Ajax call in my view?

I am currently working on a view in which I utilize jQuery ajax to fetch and display data. The data is successfully returned back to the view, but I am unsure of how to properly display this new data within my view. Below is the code snippet that I am usin ...

Executing the algorithm through a Node HTTP request results in a significant increase in processing time

My current Node app plots data on an x,y dot plot graph. To achieve this, I send a GET request from the front end to my back-end node server, which then processes the request by looping through an array of data points. Using Node Canvas, it draws a canvas ...

Preserve the content in an input text field within ASP.NET

In my web application, I have an input text field that acts as a datepicker. When a button is clicked, the form data is validated, and if any condition fails, an alert is displayed to the user. <script src="https://code.jquery.com/jquery-1.12.4.js"> ...

Issues with AngularJS edit functionality for records not functioning as expected

I have implemented a feature on my page where users can add objects to an array. These objects are then displayed on the page along with links for editing each item in the array. Each added item is assigned a primary key, allowing users to edit it later e ...

How to Arrange Multiple Divs in a CSS Grid Layout without Using Floats

I've been working on structuring a webpage with multiple divs containing images in a grid layout, but not using CSS Grid. Here's an example of what I'm trying to achieve: <div class="some classes"> Some Text </div> < ...

Designing a dropdown menu using CSS

Currently, I am facing a requirement to create a menu that should resemble the image provided below. In the past, I have experience working on simple drop-down menus, but this time the specifications are a bit unique. The top menu links should change colo ...

What could be causing my select tags to appear incorrectly in Firefox and IE?

With the help of Jquery, my goal is to dynamically populate a select field when it is in focus, even if it already has a value. Once populated, I want to retain the previous value if it exists as an option in the newly populated field. Although this works ...

CSS division nested within the center float division

In my attempt to design a 3-column home layout, I have successfully styled the center, left, and right sections. However, I am facing difficulty in placing two sliders within the middle class. My goal is to have slider1 positioned on top inside the middle ...

Concentrate on implementing Cross-domain Ajax functionality in Opera browser

For a unique and interesting browsing experience, try using Opera 9.62 to explore the nuances of cross-sub-domain JavaScript calls with Ajax. To understand this better, follow these instructions by placing three simple files on appropriate domains. foo.ht ...

When the <li> element is clicked, jQuery will fade it out

Just wondering - is my code here correct? My goal is for the button 'Select' to trigger a fadeout effect, indicating that its value has been successfully added to the database. In the jQuery code snippet provided, I included the command $(' ...

Clicking within the text activates the dropdown menu, but clicking outside the text does not

My custom drop down menu is not functioning properly. When I click on the text, it successfully links to another place, but when I click beside the text, it does not link. Can you please help me identify what's wrong here? Your assistance would be gre ...

Unable to retrieve button value through ajax call

I have my code below and I am trying to retrieve the value of my submit button in the AJAX section without success. Can someone guide me on how to accomplish this task? <script src="http://localhost/ci/js/jquery.js"></script> <script type=" ...

What is the best way to prompt users to submit comments with a popup textarea box?

Within my project, I have incorporated a dropdown list: <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select subject <span class="caret"></span> </but ...

HTML/CSS border tiling (left, bottom, right)

Is there a way to implement borders using images in CSS/HTML that does not involve CSS3? I am working with three border tiles: left, right, and bottom. I want them to repeat-x/y to the left, right, and bottom of my content container. I attempted to use d ...

Is requesting transclusion in an Angular directive necessary?

An issue has cropped up below and I'm struggling to figure out the reason behind it. Any suggestions? html, <button ng-click="loadForm()">Load Directive Form</button> <div data-my-form></div> angular, app.directive(&apos ...

Empty text box

I've been attempting to clear ng-model inputs, but haven't had any success and I can't seem to figure out why. Here is what I have: <button ng-click="$ctrl.clear()"></button> And in the clear action, I have: $scope.$event = n ...