Tips for maintaining a consistent paragraph height even in the absence of text

I am facing an issue with two skinny boxes in the code below. Each box contains a paragraph as a child element. The first paragraph has text while the second one does not. The problem arises when the text is removed, causing the paragraph to lose its height. I want to find a way to maintain the height of the second paragraph even without any text present.

Is there a way to retain the height of the second paragraph as if it had one line of text? (assuming font-size is defined) Can this be achieved using CSS?

.drunk-container {
   display: flex;
}

.skinny-box {
  height: 70px;
  width: 300px;
  outline: 1px solid blue;
  
  padding: 10px;
  box-sizing: border-box;
}

p {
  font-size: 1.2rem;
  color: green;
  outline: 1px solid red;
}
<div class="drunk-container">

  <div class="skinny-box">
    <p>Some text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>

Answer №1

Include an empty element within if it's void. Compatible with all font sizes

.drunk-container {
   display: flex;
  font-size: 1.2rem;
}

.skinny-box {
  width: 300px;
  outline: 1px solid blue;
  
  padding: 10px;
  box-sizing: border-box;
}

p {
  color: green;
  outline: 1px solid red;
}

p:empty::before {
  content:"";
  display:inline-block;
}
<div class="drunk-container">

  <div class="skinny-box">
    <p>Some text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>

<div class="drunk-container" style="font-size:2rem;">

  <div class="skinny-box">
    <p>Some text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>

Alternatively, you can use the following code for comprehensive support (even for multiple lines)

.drunk-container {
   display: flex;
  font-size: 1.2rem;
}

.skinny-box {
  width: 300px;
  outline: 1px solid blue;
  display:flex; /* here */
  padding: 10px;
  box-sizing: border-box;
}

p {
  color: green;
  flex-grow:1; /* and here */
  outline: 1px solid red;
}
<div class="drunk-container">

  <div class="skinny-box">
    <p>Some text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>

<div class="drunk-container" style="font-size:2rem;">

  <div class="skinny-box">
    <p>Some text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>

<div class="drunk-container" style="font-size:2rem;">

  <div class="skinny-box">
    <p>Some<br> text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>

Answer №2

One method to achieve this is by setting a min-height for the <p> element like below:

p {
    min-height: 22px;
}

.drunk-container {
   display: flex;
}

.skinny-box {
  height: 70px;
  width: 300px;
  outline: 1px solid blue;
  
  padding: 10px;
  box-sizing: border-box;
}

p {
  font-size: 1.2rem;
  color: green;
  min-height: 22px;
  outline: 1px solid red;
}
<div class="drunk-container">

  <div class="skinny-box">
    <p>Some text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>


To handle multi-line <p> elements, utilizing JavaScript can be beneficial:

const p1 = document.getElementById('p1');
const p2 = document.getElementById('p2');
p2.style.minHeight = p1.clientHeight + 'px';
.drunk-container {
   display: flex;
}

.skinny-box {
  height: 150px;
  width: 300px;
  outline: 1px solid blue;
  
  padding: 10px;
  box-sizing: border-box;
}

p {
  font-size: 1.2rem;
  color: green;
  outline: 1px solid red;
}
<div class="drunk-container">

  <div class="skinny-box">
    <p id='p1'>Some<br>mutliline<br>text</p>
  </div>

  <div class="skinny-box">
    <p id='p2'></p>
  </div>
  
</div>

Answer №3

Could you please review the code snippet below? It should work for your needs. You just need to include min-height: calc(100% - 30px); in the p tag, where the value of 30px depends on the total margin of the p tag.

For more information, you can visit this link: https://jsfiddle.net/yudizsolutions/qax3egj5/2/

.drunk-container {
   display: flex;
}

.skinny-box {
  height: 70px;
  width: 300px;
  outline: 1px solid blue;
  
  padding: 10px;
  box-sizing: border-box;
}

p {
  font-size: 1.2rem;
  color: green;
  outline: 1px solid red;
  margin: 15px 0;
  min-height: calc(100% - 30px);
}
<div class="drunk-container">

  <div class="skinny-box">
    <p>Some text ... </p>
  </div>

  <div class="skinny-box">
    <p></p>
  </div>
  
</div>

Answer №4

To add a minimum height to your paragraph using CSS, you can simply include the following code snippet: min-height: 21px;. Take a look at the example below and see if it resolves your issue.

.container {
   display: flex;
}

.box {
  height: 70px;
  width: 300px;
  outline: 1px solid blue;
  
  padding: 10px;
  box-sizing: border-box;
}

p {
  font-size: 1.2rem;
  color: green;
  outline: 1px solid red;
  min-height: 21px;
}
<div class="container">

  <div class="box">
    <p>Lorem ipsum...</p>
  </div>

  <div class="box">
    <p></p>
  </div>
  
</div>

Answer №5

Why not try incorporating placeholder text into the second paragraph and matching its color to blend in with the background?

.creative-box {
   display: flex;
}

.slim-container {
  height: 70px;
  width: 300px;
  outline: 1px solid blue;
  
  padding: 10px;
  box-sizing: border-box;
}

p {
  font-size: 1.2rem;
  color: green;
  outline: 1px solid red;
}
<div class="creative-box">

  <div class="slim-container">
    <p>Some creative content...</p>
  </div>

  <div class="slim-container">
    <p style="color: white;">!</p>
  </div>
  
</div>

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

Utilizing the text field feature within Twitter Bootstrap 3.0 in-line styling

I am attempting to create a horizontal form with an inline text field split into two sections: one on the left and one on the right. The left side will contain horizontal text fields, some of which are inline, while the right side will have a mix of inline ...

Having trouble with Jquery's .mouseover() method? Let's catch the solution!

I'm currently working on a jQuery homework assignment and I've been attempting to enhance the mouseover feature, but unfortunately, it's not functioning as expected. $("button").mouseover(function() { $(this).css({ left: (Math.rando ...

What are the best practices for preventing duplicate IDs in JavaScript-based web applications?

On my heavily JavaScript-based webpage, I have sections that consist of lists of HTML elements categorized as lists A and B. When the first item in list A (A1) is clicked, an action is triggered on the first item in list B (B1). To simplify the process, e ...

Setting the height of a box-inner div to full height in Bootstrap 4: A guide

Assistance Needed: I have encountered an issue with my design while using bootstrap-4. The box-inner div works perfectly on desktop, adjusting its height according to the content entered. However, on a real iPad, the columns are not of equal height as sho ...

Unable to assign the value of "textcontent" to null property

There is an issue that occurs specifically with this code snippet, generating the error message: Cannot set property 'textcontent' of null for this line: document.getElementById("days").textContent = d; function countdown() { var now = new ...

Eliminate the caret icon from the bootstrap dropdown menu

In the process of creating my personal portfolio using Bootstrap, I've encountered an issue with the navigation dropdown. There is a caret visible at that I'd like to address. While the caret next to "Portfolio" appears fine, clicking on it rev ...

Pre-loading custom fonts in Next.js for a smoother user experience

I am currently utilizing next.js. My objective is to ensure that the fonts are loaded before any content is displayed on the screen. I attempted to achieve this by including them in the Head component within the _.document file using the rel="prelo ...

How to align label and input field horizontally in Bootstrap 4

When designing a form in Bootstrap 4, there are specific requirements that need to be met: 1) The screen needs to be split into two parts. (ID & Age one part, Name & College another part.) To achieve this, I am utilizing class="container", is th ...

Using CSS to cut out a triangle shape from an image

Our designer has a vision for this: However, I'm struggling to find a suitable method for creating the triangle crop effect: Implementing an :after element that spans the entire width and utilizes border tricks to form a triangle shape Creating a l ...

Efficiently adjust the width of a child div to automatically fit within

I stumbled upon this fascinating jsfiddle on this website that almost completely addresses my concern, solving up to 90% of it. Check out the JSFiddle Nevertheless, I am keen to incorporate margins within the inner divs. Despite attempting to modify the ...

How about blending $_GET[''] with a text?

Hey there, I'm facing an issue with the following code snippet: <?php include('pages/{$_GET["page"]}.txt'); ?> Unfortunately, it's not working as expected. The code is embedded in a functional website but it renders the div empt ...

Align the child element to the baseline and have it aligned to the right within an `<li>` list item

I am interested in aligning the price of coffee to the right end of the coffee name. For example, the price 1.80 should be in line with Americano and 10.00 should be in line with Macchiato. https://i.sstatic.net/unm26.jpg ul { list-style: none; pad ...

The HTML head JavaScript files are not applicable to dynamic Bootstrap modals

I have broken down my PHP files into smaller parts, for example, tillbody.php contains everything from the HTML tag to the body tag, followed by navbar.php and so on. I am including these files in index.php. Here is the code for a better understanding: &l ...

How can JavaScript help determine the performance of a device's CPU and GPU?

(Let's consider this question in the context of web development) Recently, I've been working on developing a web app interface using three.js and implementing a fallback option between WebGL and Canvas renderer for desktop browsers. However, I& ...

What is the best way to transform one svg into a different svg using Anime.js?

I'm facing an issue where two of my SVGs have the same number of points, but when I try to play the animation, they seem to be too close together. The animation suddenly jumps and creates a weird shape before transitioning from the first SVG to the se ...

"The PHP script was executed despite having a MIME type of "text/html", which is not an appropriate JavaScript MIME type" error

I'm encountering an issue while trying to add data to an Oracle database using PHP from a Bootstrap form. When attempting to run the HTML file in Mozilla Firefox, the following error is displayed: The script from “http://localhost/CustomerPartInAs ...

Navigating the art of employing different fonts for a website

I am looking to incorporate the trainway font into my website, but I'm concerned that the user may not have it installed on their device. Is there a way to showcase text in a specific font without requiring the user to download and install it? Thank ...

What is the best way to position a table caption at the top of a table?

I need help setting up a calendar within a table structure, and I'm struggling to add a caption at the top of the table. Unfortunately, I can't modify the current table structure. I want it to resemble the example shown below: https://i.sstatic. ...

Populating a Textarea Using Javascript Triggered by a Click Event

Currently, I am working on a project that requires a status update. I want to create an onclick event for the select box so that the textarea is only displayed if the user selects "pending" as the status. echo " <select onchange=\"window.location= ...

Exploring the static files in Django

I am facing issues organizing my static directory and linking css files through templates in html pages using django. The error message "Not Found: /CSS/bootstrap.min.css" keeps popping up. I believe the problem lies in how the directory is configured in ...