Ways to prevent blank class from occupying space until filled with Javascript

I've been working on a checkout calculator using JavaScript, and here's how I'm inputting the data:

<p class="car-rent-class-info h6 pb-1"></p>
<p class="car-rent-pickupdropoff-info h6 pb-1"></p>
<p class="car-rent-coverage-info h6 pb-1"></p>
<p class="car-rent-age-info h6 pb-1"></p>
<p class="car-rent-dropfee-info h6 pb-1"></p>
<p class="car-rent-adddriver-info h6 pb-1"></p>
<p class="car-rent-roadside-info h6 pb-1"></p>
<p class="car-rent-afterhours-info h6 pb-1"></p>
<p class="car-rent-tax-info h6 pb-1">Tax Included</p>
<h2 class="car-rent-price-total h1 t600"></h2>

Here is my script:

carpricetotal = $( ".car-rent-price-total" );
carpricetotal.html("$"+generateBillMath(DaysBetween(start_date,end_date),dayprice, weeklyprice, monthlyprice, car_class, pickup_loc, dropoff_loc, coveragec));

The problem I'm facing is that when not all fields are filled, there can be unsightly gaps as shown in this screenshot:

https://i.stack.imgur.com/l8aRt.png

Any suggestions on how to avoid this uneven vertical spacing?

Answer №1

The additional space could be a result of the margin set for <p> tags.

To address this issue, you can utilize the :empty CSS pseudo-selector to eliminate the margin when there is no content present. For more information, check out this resource.

.pb {
  padding-bottom: 1em;
}

.pb:empty,
p:empty {
  margin: 0 !important;
  padding: 0 !important;
}
<p></p>

<div class="pb"></div>

Hello

Answer №2

To hide an element in CSS, you can set the class to

display: none;

This prevents any space from being allocated for the element. When you want to display it again after adding content, you can use jQuery like this:

carpricetotal = $( ".car-rent-price-total" );
carpricetotal.html("$"+generateBillMath(DaysBetween(start_date,end_date),dayprice, weeklyprice, monthlyprice, car_class, pickup_loc, dropoff_loc, coveragec));
carpricetotal.show();

Answer №3

If your corresponding field isn't filled out, you might notice empty HTML elements with default block-level styles that include top and bottom margin.

To conceal these empty elements, consider adding a specific class to each one. Alternatively, if jQuery is part of your toolkit (which seems likely), you can utilize the hide function like so:

// Use this code snippet for any paragraph or header lacking input data
$( ".car-rent-price-total" ).hide()

Answer №4

It appears that you are utilizing Bootstrap, as evidenced by the classes being used. The <p> element by default adds margin on top and bottom.

To adjust this, you can incorporate the m{side}-{size} notation, with size ranging from 0 to 5. This information can be found in the Bootstrap 4 documentation

For {side}:

  • t - top
  • b - bottom
  • l - left
  • r - right
  • x - top and bottom
  • y - left and right

For {size}:

  • 0 - 0rem
  • 1 - 0.25rem
  • 2 - 0.5rem
  • 3 - 1rem
  • 4 - 1.5rem
  • 5 - 3rem

You can update your code by adding mx-0 to eliminate the spacing between paragraphs like so:

<p class="car-rent-class-info h6 pb-1 mx-0"></p>
<p class="car-rent-pickupdropoff-info h6 pb-1 mx-0"></p>
<p class="car-rent-coverage-info h6 pb-1 mx-0"></p>
<p class="car-rent-age-info h6 pb-1 mx-0"></p>
<p class="car-rent-dropfee-info h6 pb-1 mx-0 "></p>
<p class="car-rent-adddriver-info h6 pb-1 mx-0"></p>
<p class="car-rent-roadside-info h6 pb-1 mx-0"></p>
<p class="car-rent-afterhours-info h6 pb-1 mx-0"></p>
<p class="car-rent-tax-info h6 pb-1 mx-0">Tax Included</p>
<h2 class="car-rent-price-total h1 t600 mx-0"></h2>

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

Tips for incorporating a visible HTML comment in JSX code?

Currently implementing ReactJS and facing a requirement to insert visible HTML comments (visible in the html source) within JSX. Unsure of the correct approach to achieve this. A third party vendor necessitates this comment for processing purposes on the ...

Modifying CSS stylesheet does not alter the background color

body { background-color: bisque; } <!DOCTYPE html> <html> <head> <title>Reading</title> </head> <body> <h1> <button><a href="index.html">Home</a></button> & ...

Creating UL tabs using just HTML and CSSHere's a step-by-step guide

Looking for help to accomplish this task. I already have the styling, but I want something to happen when I click on the tabs. Specifically, I want the div with the tab class names to show and hide upon clicking the tabs. Currently, nothing happens when I ...

Determining if the device is connected to the internet

Is there a way to create a unique code using HTML, JavaScript, or jQuery that executes a random action when the website detects that the device is offline? ...

Ensure that the browser is instructed to utilize a designated Accept field within the HTTP request

Can a web browser be directed to use a specific mime-type in an HTML request? For example, can we achieve this with the following code: <a type="application/pdf" href="https://url.com/">PDF</a> or <a type="text/html ...

Tips for implementing a CSS loader exclusively on a particular section of your content

Is it possible to apply a loader image to a specific section of content on a webpage? All the tutorials I've come across for loader images focus on applying them to entire webpages. However, I am looking to implement a simple loader only to a specifi ...

How can I use JavaScript to disable a table row and then save the selected option in a MySQL database?

I have a PHP snippet that dynamically displays table rows. Each row contains a radio button with "Yes" and "No" options. I have implemented a JS function where, upon choosing an option, a pop-up box is displayed. If the user selects the "Yes" option in t ...

Using the onClick function to set attributes in React JS

I am currently working with 2 functions: addBookmark() and removeBookmark(). There is a variable called IsBookmarked that can be either true or false. In my JSX code related to bookmarks, it looks like this: {props.singleCompany.IsBookmarked ? ( ...

Incorporating HTML5 & CSS3: Leveraging various Pseudo classes and the translateX/Y properties

As a graphic designer dabbling in HTML5, I have to admit that coding is not my strong suit. Despite that, I've been able to troubleshoot most of my issues thus far. However, I've hit a bit of a snag. The problem arises when clicking on a thumbna ...

Step-by-step guide on building a mat-table with nested attributes as individual rows

Here is the data structure I am working with: const families = [ { name: "Alice", children: [ { name: "Sophia" }, { name: "Liam" ...

What is the process of compiling HTML code?

In controller, I bind the same data like $scope.name = "<div>geetha teko</div>", this name will now be bound to the HTML like {{name}}. When I view it in the browser, it prints as "<div>geetha tek0</div>". Is there a way to only di ...

having trouble with developing a dropdown menu using jquery

I'm currently creating a drop-down menu for my website and here is the code I'm using: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html dir="ltr"> <head> <met ...

What is causing the CORS error with JQUERY and JQM?

My HTML5 page is set up without any jQuery and has no CORS error, as shown below: <!DOCTYPE html> <HTML> <button type="button" onClick="getBase64FromImageUrl()">Click Me!</button> <img id="preview1" crossorigin="anonymous" src=" ...

What is the best way to adjust a <div> element so that it stays centered on the webpage while also keeping its size attributes intact?

"How To" Can you help me center a single element within a "div" on my webpage without changing its current size attributes? Here is the page I need assistance with: I would like the "Interior Painting" element under the "Residential Services" header to ...

error being triggered due to relative positioning

On my webpage, I have a set of buttons located at the bottom. When I first open the page, all the buttons are displayed properly. However, sometimes the first three buttons become hidden mysteriously and the layout changes. I am puzzled as to what could be ...

Issue with Bootstrap side navbar not collapsing when clicked on a link

Currently, I'm working on creating a website for a friend. While I used to have some experience with coding in the past, it has been a while and I am a bit rusty. This time around, I decided to use bootstrap for the project. However, I'm struggli ...

Enhancing User Experience with CSS

Is there a way to create a link within a div and change the hover color to indicate it's a link? The challenge is that my div contains both an image and text in separate divs. div { :hover { background-color: light cyan; } Currently, when I hov ...

Having trouble with the alignment of the product grid on Woocommerce with the Wootique theme?

Hey there, I've been facing an issue with the woocommerce product display grid layout. The items keep getting misaligned, with one on a line and the others right next to each other. I attempted to fix it by adding some custom CSS code: .woocommerce ...

Adding a code for divs into CSS caused the navigation bar to be completely obliterated

Initially, my menu was perfectly styled with the following CSS code in my css file: ul.Menu { list-style-type: none; margin: 10px; overflow: hidden; background-color: #ffffff; border-style: solid; border-color: #e9055c; border-width: 2px; font-weight: bol ...

Tips for Implementing Multi-Level/Nested in Ajax Requests

I am currently developing a user-friendly web application with intuitive multi-level options that loads new content without the need to refresh the entire page. This is how my structure looks: /index.php /schools.html /colleges.html Within schools.html, ...