What is the best way to have a <div> of a specific size accommodate a variable-size <p>

Looking for a solution using purely CSS3 for this issue.

The challenge lies in having a variable-height <p> within a fixed-size <div>, and making sure that the <p> element fills up the remaining space within the div. While setting the background-color of both elements to match is one way, it's not feasible due to special effects associated with each div.

My previous attempts:

  • Tried giving the <p> element a height of 100% and the <div> element an overflow of auto. However, this resulted in excessive white space and scrollbars on every div in the HTML view.
  • Experimented with matching the background-colors of both elements, but encountered issues when applying a hover effect - it didn't cover the entire div as the screenshot demonstrates.
  • Also tried assigning the same :hover block to both div and p, but the transition effect wasn't applied uniformly across the div.

All my attempts felt like "hacks", and I'm seeking a cleaner way to achieve the desired outcome of filling the div with the p element.

Here is a snapshot of the latest design iteration.

Below is the corresponding CSS code snippet.

.RadioEpisodeItemContainer
{
    display: inline-block;
    vertical-align: top;
    width: 20em;
    height: 12em;
    overflow: hidden;
    border: 2px solid white;
    border-radius: 10px;
    margin: 0.5em;
}

.RadioEpisodeItem
{
    border-radius: 8px;
    background-color: tan;
    margin: 0em;
    padding: 1em;
}

Here's a sample section of the generated HTML:

<div class="RadioEpisodeItemContainer">
    <p class="RadioEpisodeItem">
        <strong>Date: </strong>5/02/2009<strong> – Episode: </strong>Intro<br />
        <a href="#" class="mp3_track" data-location="../Radio_Show_Files/INTRO.mp3" title="Episode Intro: About SpecialNeeds Lifestyles">
            Play</a><br />
        <strong>Description: </strong>About SpecialNeeds Lifestyles</p>
</div>
<!-- more similar divs follow -->

In conclusion, any suggestions on how to make the p element fill the available space in the div?

Answer №1

To easily adjust the height of your paragraph styling, simply include height: 100% in the class definition for RadioEpisodeItem.

body {
  background-color: #4094cf;
}
.RadioEpisodeItemContainer {
  display: inline-block;
  vertical-align: top;
  width: 20em;
  height: 12em;
  overflow: hidden;
  border: 2px solid black;
  border-radius: 10px;
  margin: 0.5em;
}
.RadioEpisodeItem {
  height: 100%;
  border-radius: 8px;
  background-color: tan;
  margin: 0em;
  padding: 1em;
}
<div class="RadioEpisodeItemContainer">
  <p class="RadioEpisodeItem">
    <strong>Date: </strong>5/02/2009<strong> – Episode: </strong>Intro
    <br />
    <a href="#" class="mp3_track" data-location="../Radio_Show_Files/INTRO.mp3" title="Episode Intro: About SpecialNeeds Lifestyles">
            Play</a>
    <br />
    <strong>Description: </strong>About SpecialNeeds Lifestyles</p>
</div>
<div class="RadioEpisodeItemContainer">
  <p class="RadioEpisodeItem">
    <strong>Date: </strong>3/15/2012<strong> – Episode: </strong>098
    <br />
    <a href="#" class="mp3_track" data-location="../Radio_Show_Files/Show0107.mp3" title="Episode 098: Hospice. Guest: Susan Howard (Carondelet Hospice) and Corinne Spalding">
            Play</a>
    <br />
    <strong>Description: </strong>Hospice. Guest: Susan Howard (Carondelet Hospice) and Corinne Spalding</p>
</div>
<div class="RadioEpisodeItemContainer">
  <p class="RadioEpisodeItem">
    <strong>Date: </strong>2/19/2012<strong> – Episode: </strong>097
    <br />
    <a href="#" class="mp3_track" data-location="../Radio_Show_Files/Show0106.mp3" title="Episode 097: Handi-Dogs. Guest:Veronica">
            Play</a>
    <br />
    <strong>Description: </strong>Handi-Dogs. Guest:Veronica</p>
</div>
<div class="RadioEpisodeItemContainer">
  <p class="RadioEpisodeItem">
    <strong>Date: </strong>2/12/2012<strong> – Episode: </strong>096
    <br />
    <a href="#" class="mp3_track" data-location="../Radio_Show_Files/Show0105.mp3" title="Episode 096: Guest: Mary Mallone">
            Play</a>
    <br />
    <strong>Description: </strong>Guest: Mary Mallone</p>
</div>
<div class="RadioEpisodeItemContainer">
  <p class="RadioEpisodeItem">
    <strong>Date: </strong>2/05/2012<strong> – Episode: </strong>095
    <br />
    <a href="#" class="mp3_track" data-location="../Radio_Show_Files/Show0104.mp3" title="Episode 095: Mastering a Healthy Self Image. Guest: Darrel Knock">
            Play</a>
    <br />
    <strong>Description: </strong>Mastering a Healthy Self Image. Guest: Darrel Knock</p>
</div>
<div class="RadioEpisodeItemContainer">
  <p class="RadioEpisodeItem">
    <strong>Date: </strong>1/22/2012<strong> – Episode: </strong>094
    <br />
    <a href="#" class="mp3_track" data-location="../Radio_Show_Files/Show0103.mp3" title="Episode 094: The Special Needs Store. Guest: Kelly Savage">
            Play</a>
    <br />
    <strong>Description: </strong>The Special Needs Store. Guest: Kelly Savage</p>
</div>
<div class="RadioEpisodeItemContainer">
  <p class="RadioEpisodeItem">
    <strong>Date: </strong>1/15/2012<strong> – Episode: </strong>093
    <br />
    <a href="#" class="mp3_track" data-location="../Radio_Show_Files/Show0102.mp3" title="Episode 093: Music therapy for people with autism. Guest: Jackie Burger">
            Play</a>
    <br />
    <strong>Description: </strong>Music therapy for people with autism. Guest: Jackie Burger
  </p>
</div>

Answer №2

To make it function properly, simply include display: block; height: 100%; in the .RadioEpisodeItem.

Check out the JSFiddle for a preview: Preview

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

Can I safely apply my CSS class to an element already containing a Bootstrap row or col class?

It has been on my mind for a while that mixing bootstrap rows and cols with personal CSS on the same element could lead to unexpected outcomes. Despite numerous attempts at searching using different phrases, I have not found a clear answer to my question. ...

navigation menu 'selective emphasis' feature

I have created a JQuery script that will highlight the 'About', 'My Projects', or 'Contact Me' text on the navigation bar when the corresponding section of the page is in view. To achieve this, I am using a scroll() event list ...

Guide to iterating over user instances in Django

How can I display all of the user's items instead of just one? my_items.html is where I want to showcase them. {% extends 'base.html' %} {% block content%} <main role="main"> <div class="container> <!-- Exa ...

Learn the method of setting the 'was-validated' class on a form in order to display validation feedback messages dynamically using Angular 5 following form submission

I have set up a template-based form in Angular that utilizes Bootstrap (v4) for styling. My goal is to display validation messages upon form submission. Here is an example of my form: <form [ngClass]="{'was-validated': wasValidated}"> & ...

What is the proper way to implement a divider in HTML5?

Looking for advice on how to effectively use dividers positioned at the center of a page? Specifically, I am interested in having two dividers: one measuring 200px wide and another measuring 500px wide, both with a 5px top margin. These dividers should be ...

Adjusting the width of a Material UI drawer: Tips and tricks

Currently, I am working on implementing the Material UI drawer and anchoring it to the top of my page. It is functioning correctly, however, I am encountering an issue where I am unable to adjust the width of the drawer. Despite trying to update the width ...

How can a table row be connected to a different frame?

I am currently incorporating jQuery into my project and I would like to have the table row link redirect to a different target frame. Here is the HTML for the table row: <tr data-href="home.html" target="content"><td><h3><center>H ...

Utilizing jQuery to retrieve multiple values from a select box through the onchange event

I have two select boxes with JavaScript on them. When I use filter1, my URL changes to www.url.com/&filter[]=6827 and it works perfectly. The selected option also works without any problems. However, I want a new select box like filter2 with multiple f ...

Tips for vertically centering elements in the header using flexbox

Within the header of my website, I have structured 3 separate divs - one for a logo, one for a search input, and one for a cart link. My goal is to align these 3 .col-md-4 divs in a vertically centered position. While these divs currently have an align-it ...

HTML form field for JavaScript input

Can someone guide me on how to implement the following code snippet in order to redirect the page using window.location.href if the input contains an item from a JavaScript array? Here is the current code I have: var myArray = ["1", "2&q ...

Eliminate extra space beside the dark column on the right side in Bootstrap 4

For some time now, I've been struggling with an issue related to the left sidebar. When the sidebar is absent, the white space on the right disappears. I've tried various suggestions from others but none seem to solve the problem. I'm unsure ...

Passing parameters with JQuery onclick event and appending them to elements

Trying to add varying amounts of text to a list and make each item clickable with its own value being sent to a function. Struggling with a 'Object' is not defined at HTMLAnchorElement.onclick error, where object refers to the name of the object ...

Incorporating docsify CSS styling into Markdown within the VScode environment

When it comes to building my blog, I've decided to go with docsify and manage my markdown files using VScode. I wanted a preview of my blog, and after noticing that <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@4/lib/t ...

Adding Jquery content to HTML templates using Jinja2: A Simple Guide

Looking at my HTML code Base.html <title> {% block title %}{% endblock %} </title> </head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="collapse na ...

A guide on incorporating jQuery alert messages into Angular 2

Whenever I submit a form by clicking on the "send message" button, I want to display an Alert message using jQuery. However, currently, I have to double click for the alert message to appear. How can I make it so that the alert message is shown with just o ...

An Overview of Node.js Application Structure

Guten Tag! My name is Alex, and I am from Germany. I apologize for any mistakes in my English as it is not my first language. I have recently started learning programming with Node.js after creating some simple static HTML and CSS websites. As a beginner, ...

Challenges associated with Bootstrap toggle switch

I am having some difficulties with using bootstrap switch. I have tried the examples provided on , but regardless of the classes I apply, the buttons remain the same small size and the text for ON and OFF is barely visible. I can't seem to figure out ...

Is it possible to utilize CSS files from locations other than the assets folder in Python's Plotly Dash? Alternatively, is there a way to have

Hey everyone, I've been working on a pretty extensive project and organizing the CSS files has become quite messy. My project is divided into numerous folders such as: -- assets # folder -- components # folder -- Header # folder -- Buttons.py ...

Numerous translateX Functions in JavaScript (Webkit)

Trying to apply a .style.webkitTransform = 'translateX(10px)'; on an element and then adding another .style.webkitTransform = 'translateX(10px)'; to achieve a total of 20px is not feasible. The element will still remain at 10px. Is the ...

Turn off dynamically created buttons using jQuery

Whenever a user selects a checkbox in a table row and saves it, a new container is created to store the data in a specific format for that particular row. Note: The data displayed in the table is sourced from JSON. Additionally, users have the option to ...