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

Maximizing the potential of NestJS apps with Docker

I am working on a NestJS project that consists of multiple apps structured as follows: my-project: -- apps; --- app-one ---- src ---- tsconfig.app.json --- app-two ---- src ---- tsconfig.app.json -- libs -- package.json -- etc... Within this project, I ha ...

jQuery Suckerfish with disabled animations

Currently, I am incorporating jQuery and the Suckfish plugin for my menu. However, I have a specific requirement - I do not want any animation to occur when the submenu is displayed. Although I have set the speed to 0, there still seems to be a slight del ...

Why is my React build's index.html coming up empty?

I've been working on a React app using Snowpack, and everything seems to be in order. The build process completes successfully, but when I try to open the index.html file from the build folder, the page appears blank. To temporarily resolve this issu ...

Differences in accessing the previous state between React's useCallback and useState's setState(prevState)

It has come to my attention that useCallback functions recreate themselves when their dependencies change, acting as a sort of wrapper for memoizing functions. This can be particularly useful for ensuring access to the most up-to-date state in useEffect ca ...

Make a <div> element that has a fixed size, but allows for scrolling text inside

I have tried searching the forum for a solution to my problem, but haven't found one yet. I am trying to create two "divs" with fixed height and internal scrolling. However, whenever I add text inside one of them, its height increases accordingly. Wha ...

Is there a way to achieve the same zooming and panning effects on an element using CSS transform instead of jQuery's

If you click on the following link, you will see a jsFiddle that I have created to demonstrate my question: http://jsfiddle.net/VbCcA/3/ In my project, I have developed a function that allows for panning and zooming to a specific element. This function i ...

Assigning index values to child rows in AngularJS: a step by step guide

One of my requirements involves assigning index values to child rows. The structure includes group rows with child rows underneath. Currently, I am using ng-repeat along with $index for the children as shown below: HTML code: <table ng-repeat="nod ...

Combining Vue.js with Laravel Blade

I've encountered an issue while trying to implement a Basic Vue script within my Laravel blade template. The error message I am getting reads: app.js:32753 [Vue warn]: Property or method "message" is not defined on the instance but referenc ...

Adding ngrx action class to reducer registration

Looking to transition my ngrx actions from createAction to a class-based approach, but encountering an error in the declaration of the action within the associated reducer: export enum ActionTypes { LOAD_PRODUCTS_FROM_API = '[Products] Load Products ...

Next.js: Generating static sites only at runtime due to getStaticProps having no data during the build phase, skipping build time generation

I am looking to customize the application for individual customers, with a separate database for each customer (potentially on-premise). This means that I do not have access to any data during the build phase, such as in a CI/CD process, which I could use ...

Error: The function 'nivoSlider' is not defined for the object [object Object]

Trying to integrate a nivo slider into my WordPress website, but encountering a strange exception: Uncaught TypeError: Object [object Object] has no method 'nivoSlider' The website URL is: (It's in Hebrew, so you may not understand) - th ...

Dynamic Content Sorting with JavaScript and jQuery

I am presenting various courses that are available, and I am utilizing JavaScript/jQuery to display or hide them based on element classes. You can view the page here. Currently, the script conceals all content on the page and then reveals items that matc ...

The dynamically generated Jquery selectmenu is failing to trigger

I am in need of assistance with a script I am working on that involves dynamically creating a jquery selectmenu. The issue I am facing is that when a selection is made on the first selectmenu, which is created within the HTML itself, it triggers an alert. ...

The AJAX request to the REST service is failing to trigger the success function in the AJAX call

Struggling with some issues related to AJAX, specifically when calling a Java REST server that I've developed. While I am relatively new to AJAX, I have put in quite a bit of effort searching for solutions. The problem arises when making a call from a ...

Printing using *ngFor will display items in an ascending order

When attempting to display an object in markup, I am running into the issue of *ng printing it in ascending order instead of maintaining the original order. Ideally, I would like the elements to be printed as they are. You can view my code on StackBlitz ...

Incorporate fresh Google sites into different web pages using iFrame integration

Wishing you a fantastic day! I am currently working on embedding a brand new Google site into another webpage. I attempted to use an iframe for this purpose, but unfortunately it did not work as expected. Here is the code snippet: <iframe width="1280 ...

Transforming a traditional HTML/CSS/JS website into a cutting-edge React application using Tailwind CSS styling

I have a unique website template complete with HTML, CSS, and all necessary assets. Now, I am looking to develop a React JS application using this template. Firstly, I am wondering: Since I typically use Tailwind CSS for my other projects, would it be mo ...

Error in thread : TagNameUnexpectedException

I encountered an issue while trying to find a dropdown using Select: An error occurred: Exception in thread "main" org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input" Even when attempting to use ...

The grid-column-end attribute is creating a gap in the layout

For the task at hand, my goal is to showcase the final 3 elements of a container in a horizontal alignment. In this setup, the first two items are allotted fixed space while the third element expands to fill the remaining columns. To accomplish this layou ...

What could be the reason that a MUI component does not disappear when the display is set to none in the sx prop?

I'm attempting to construct a responsive side drawer using MUI's Drawer component in React, specifically leveraging MUI version 4.12.1. In the example provided on the mui.com website, they utilize the sx prop and pass an object with different di ...