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

Unable to assign to 'disabled' as it is not recognized as a valid attribute for 'app-button'

How to link the disabled property with my button component? I attempted to add 'disabled' to the HTML file where it should be recognized as an input in the button component (similar to how color and font color are recognized as inputs) ... but ...

What is the method to dynamically modify the value of location.href using vanilla javascript?

I have a button that looks like this: <button type="button" class="play-now-button" onclick="location.href='www.yahoo.com'">Play Now</button> However, I want to change the location.href value using vanilla JavaScript. The code below ...

Managing a new browser window using Selenium

I am attempting to automate a download process on a webpage using selenium. Upon clicking a button, a blank popup window appears. After approximately 60 seconds, a link appears within the popup that I want to click on. The code snippet below outlines my ap ...

Leveraging Excel VBA to manipulate the selection in a dropdown menu on a webpage

Is there a way to select a specific value using Excel VBA? <div class="drpCompanies"> <select class="selectBox homepage selectCompanies" style="padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px"> <op ...

Sketch a straight path starting from the coordinates x,y at a specified angle and length

Is there a way to draw a line in Javascript starting from a specific x/y position with a given length and angle, without having to define two separate points? I have the x/y origin, angle, and length available. The line should be placed on top of a regula ...

Tips for aligning hyperlinks in the navigation bar using CSS3 and HTML5

Struggling to center the links in the navigation bar has been a challenge. I really want those buttons smack dab in the middle of the header bar, but despite trying everything, they stubbornly remain on the left-hand side. .header { overflow: ...

Could you explain the distinction between Node.bind() and Template Binding?

Currently, I am exploring the documentation for Google Polymer and have come across two types of data binding - Node.bind() and Template Binding. Can someone please explain the distinction between Node.bind() and Template Binding to me? ...

Is there a way to mimic this type of scrolling effect?

Upon visiting this website, it is evident that the entire interface has been constructed with React. The scrolling experience on this site is exceptionally smooth, although it may feel slightly more substantial than a typical scroll. After researching onli ...

CSS3 alternative to width:calc(100% - 10px)

Currently, I am in need of an alternative solution for migrating my CSS file since some CSS3 features are not being rendered properly on QtWebKit yet. The CSS code that I am working with is as follows: .fit { width: -moz-calc(100% - 10px); width: ...

Is it possible to utilize a hyphen in the link_to attribute?

I'm encountering a dilemma in my Rails app where I need to assign a value to a custom data-* attribute for an anchor tag. The issue is that hashes can't contain hyphens, leading to difficulties with this code: <%= link_to 'Example', ...

CSS Hover Effects on Navigation Bar Not Displaying as Expected

I was aiming to make one of my Navbar tabs stand out by having a different appearance compared to the others. However, there seems to be an issue with the text color for this particular tab in the hover state - it works inconsistently across different brow ...

Leveraging CSS classes for enhancing the bootstrap grid system

Forgive me for the seemingly simple question, but here is my dilemma. Presented below is the current html code: <div class="col-xs-12 col-md-6 col-lg-4"> ...stuff </div> I am wondering how to achieve the following transformation: index ...

css boxShadow merger

Looking to create a sleek horizontal navigation bar using an unordered list with list items representing each element: To ensure the elements fit perfectly within the bar, I set the width of each at 25% and added a box-shadow for a border. Using a traditi ...

Is it possible to create a custom tag in HTML 4?

What is a custom tag in HTML (Kindly note that I am specifically talking about the differences from XHTML)? I need to include some additional text to meet StackOverflow's posting requirements. Thanks! ...

Incorporating Material-UI themes within styled components

Trying to incorporate theme spacing value in a styled component but facing issues. Initially, the style was applied to the button, but now it is not working as expected. You can view the problem here: sandbox. import React from "react"; import ...

Automatically updating quantity with the power of jQuery

I have created a spreadsheet where users can input their expenses and the total will update automatically. Initially, I have set some default numbers in my HTML which are editable for users to modify as needed. However, I am facing an issue with my JQuer ...

What is the best way to display content at a specific time using Angular?

Whenever a user searches something and no results are found, I display a message. However, when there is content to show, an error message pops up for about 3-4 seconds before disappearing and showing the search results. This is my HTML: <div> &l ...

Developing SAPUI5: Construct a GenericTile featuring a centrally positioned icon

I'm having trouble creating a custom tile in SAP that inherits from sap.suite.ui.commons.GenericTile and only displays an icon centered in the middle of the tile. The standard aggregations and properties are causing issues as they position the icon on ...

Select a specific element to apply a CSS selector for a button

One of the challenges I am facing involves a webpage with multiple submit buttons. My goal is to iterate through each button and click on them one by one. While I am aware that this can be achieved using xpath like this (//button[@class='submit' ...

Is it possible to establish the page state upon loading (in the context of a GET request

At present, my navigation bar is set up to utilize AJAX for loading new pages and window.pushState() in order to update the URL when the AJAX call is successful. I've come to realize that it's crucial to push the page state during GET requests s ...