Is there a way to adjust the padding temporarily?

Important Note: Despite the misleading title of my question, it doesn't accurately reflect my actual query (sort of).

Below is the code snippet in question:

.one{
  background-color: gray;
}
.two{
  padding: 20px;
}
<div class = "one">
  <div class = "two">
    <span>something</span>
  </div>
</div>

<br>

<div class = "one">
    <span>something</span>
</div>

I am seeking guidance on how to make sure that both blocks look identical. Initially, div.two does not exist and will be added later. I want to maintain the UI without any changes when div.two is appended. How can this be achieved?

In essence, I need both elements with class div.one to appear similar at all times. Is there a way to achieve this? (it should be noted that I want them to both resemble the first block)

Answer №1

Can the use of visibility: hidden; be a substitute for adding a new element? Instead of adding, simply change to visibility: visible;

.one{
  background-color: gray;
}
.two{
  visibility: hidden;
  padding: 20px;
}

https://jsfiddle.net/Hastig/bx2kwjmw/

Alternatively, can padding be added to the parent element instead...

.one{
  background-color: gray;
  padding: 20px;
}
.two{

}

https://jsfiddle.net/Hastig/bx2kwjmw/1/

Edit, upon further experimentation, it was observed that adding padding to the parent div is not a viable solution as the height still increases when text is inserted due to the size of the text.

A new fiddle has been created to demonstrate placing invisible ghost text as a placeholder for sizing and then removing it when content is appended with a plugin.

This is just one approach, there are likely many other ways to achieve this..

https://jsfiddle.net/Hastig/bx2kwjmw/5/

$('.plugin-append-simulator').click(function() {
  var content = '<div class="two">something</div>';
  $('.one').html('').append(content);
});

// $('.one') | selects .one
// .html('') | removes ghost text
// .append(content) | appends your content
.one {
  background-color: gray;
  padding: 20px;
  color: hsla(0, 0%, 0%, 0);
}
.two {
  color: hsla(0, 0%, 0%, 1);
}


/* scaffolding. ignore this */.plugin-append-simulator { position: fixed;bottom: 0;left: 50%; transform: translateX(-50%);display: inline-flex;padding: 3px 8px 1px 8px;color: white;background-color: black;cursor: pointer;
}.plugin-append-simulator:hover { background-color: red; }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="one">ghost text</div>

<!-- ignore, scaffolding -->
<div class="plugin-append-simulator">simulate plugin append</div>

Previous version that toggles padding from parent to child..

https://jsfiddle.net/Hastig/bx2kwjmw/6/

Answer №2

Adjust the location of the padding....

.one{
  background-color: gray;
  padding: 20px; /* previous padding declaration */
}
.two{
  padding: 0; /* setting this to 0 will take precedence over any previous padding values */
}

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

Record the marker's location only once following the completion of its dragging action

Is there a way to make the console log for getPosition only happen when the marker is stopped being dragged, rather than every 0.1 seconds while it's being dragged? Similar to how .getRadius() works - it logs the radius change just once. https://i.ss ...

npm's protocol for handling callback errors

While exploring the coding style guidelines by npm, I stumbled upon a rather enigmatic suggestion: Be very careful never to ever ever throw anything. It’s worse than useless. Just send the error message back as the first argument to the callback. Thi ...

"Modifying state within a child component and utilizing the refreshed value in the parent component

I'm currently working on creating a simple header mini cart with a cart item counter in NextJS. I'm utilizing the form state value in the header component and then passing that value to the child components of the header where the numerical quant ...

After resolving a quirky syntax error in my ejs code, I can't help but ponder what caused the issue in the first place

For my personal web development project, I've been working on a blog webpage to enhance my skills. However, I encountered an unusual syntax error while modifying the code to display different navigation bars for logged in and logged out users. Here&ap ...

fluctuating random percentage in JavaScript/jQuery

I am currently faced with the challenge of selecting a random number based on a given percentage ranging from 0 to 5. 0 - 25% (25/100) 1 - 25% (25/100) 2 - 20% (20/100) 3 - 15% (15/100) 4 - 10% (10/100) 5 - 5% (5/100) However, there are instances where ...

Retrieving the headers from an ajax request

Is there a method to retrieve the complete request headers used in an AJAX call made through jQuery? ...

Troubleshooting collapsing problems in Bootstrap 5.2 using Webpack and JavaScript

I am currently developing a project utilizing HTML, SASS, and JS with Webpack 5.74.0. My goal is to implement a collapsible feature using Bootstrap 5, however, I ran into some issues while trying to make it work. To troubleshoot, I attempted to direct ...

Are there ways to implement Vue.js transitions without directly setting the height in the code?

I'm having an issue with a Vue app I created where I need to make an element expand and collapse when a button is clicked. I want the effect to be smooth and animated, but using the <transition> tag alone isn't working as expected. The prob ...

Difficulties encountered when trying to send emails on iPhone with HTML formatting and attachments

I'm attempting to send a JPG attachment along with a formatted HTML message using [picker setMessageBody:emailBody isHTML:YES]; as recommended by several forum posts. However, what I've noticed is that the resulting email's content varies d ...

Adding Information to a Material Design Card

Currently, I am delving into frontend development and honing my CSS skills. In my latest project, I have incorporated a mat card which can be viewed in the linked image https://i.sstatic.net/TEDcg.png. Below is an excerpt of my code: <div class=&quo ...

Error: Attempting to access a property of an undefined object resulting in TypeError (reading 'passport')

I am currently working on a project that requires me to display user profiles from a database using expressjs and mongoDB. However, I have encountered an issue and would appreciate any solutions offered here. Here is the code from my server: const express ...

Serialization in .NET and the weekcalendar: events that are not defined

I've been attempting to integrate the jQuery weekcalendar within a .net environment. The issue I'm encountering is that after making an ajax call to a custom webmethod that returns JSON, the weekcalendar plugin reports that events.events is undef ...

Unable to access the inner object using key-value pair in Angular when working with Firebase

Within my json object, there is an inner object labeled data, containing {count: 9, message: "9 sites synced"} as its contents - also in json format. My objective is to extract the value from message, rather than count. Provided below is the temp ...

Ways to prevent scrolling on mobile web browsers?

I am currently facing an issue with disabling scrolling on a webpage when a user opens a popup, while still allowing them to scroll within the popup itself. The popup element is defined by the following attributes: #popup { display: none; width: ...

Use Vue.js to shorten the number of decimal places and include commas in numerical values

How can I best parse a long number retrieved from a JSON using VueJS within a for loop? The JSON data is used in a v-for prop to create Li elements for a side navigation. I attempted to use vue-numeric without success since the project does not utilize ECM ...

What is the reason behind the effectiveness of this prime number verifier?

Take a look at this code snippet that effectively checks whether a number is prime: var num = parseInt(prompt("Enter a number:")); var result = "Prime"; for (var i = 2; i < num; i++) { if (num % i === 0) { result = "Not Prime"; break; } } ...

Please be aware that the website's loading speed may be slower than usual

Is it possible to notify a user that the website may take longer to load due to their slower internet connection? For example: Attention! It appears that the page is loading slowly because of your connection speed. Please be patient while the websi ...

Selecting a language by clicking on it

I recently designed a bootstrap navigation bar for my website and incorporated a language selection dropdown menu into it. <li class="nav-item dropdown"> <a class="nav-link dropright" href="#" id="navbarDropdown" role="button" data-toggle="dr ...

Show a pair of pictures every X hours using Javascript

Trying to create a code that displays different pairs of images each day, but struggling to make it select them randomly. Instead, the code currently just goes through the array one by one. I'm not great with Javascript and haven't found a way to ...

The issue with logging out feature

Operating an ASP.NET Web application, I have a Logout feature implemented in JavaScript. However, my current code closes the browser upon Logout, which is not the desired behavior. Instead, I am looking to clear cookies/session and redirect the user to the ...