How can I ensure that the padding of various items adjusts perfectly to the containing div?


I am attempting to create a dynamic table that adjusts automatically to new content. Instead of using fixed padding sizes, I want the padding to adjust itself to fully fit the table. To better understand, here is a simple example:

.table{
  width: 800px;
  height: 600px;
  text-align: center;
  background-color: #FBEFF2;
  position: relative;
}
#cell{
  width: 120px;
  height: 500px;
  background-color: grey;
}
#cell p{
  background-color: rgb(200,200,200);
  word-wrap: break-word;
  padding-top: 5%;
  padding-bottom: 5%;
  padding-left: 4px;
  padding-right: 4px;
}
.explanation {
  position: absolute;
  top: 100px;
  width: 200px;
  left: 180px; 
}
<div class="table">
  <div id="cell">
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties</p>
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties Big enough to fit 3 lines</p>
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties</p>
  </div>
</div>
<p class="explanation">I want the padding of the "p" elements to automatically fit to the bottom of #cell. If I add another "p" element to #cell, the padding of all the other "p's" should responsively shrink until all "p" elements fit again...<br />Possible?<br />


Yes, I am specifically focused on adjusting the padding! I have already set up the table, but it gets messy every time I add new content to the rows. How can I make the padding of the text fit the rows automatically without fixed sizes?

I appreciate any help!

Answer №1

Utilizing Flexbox for layout

.table {
  width: 180px;
  height: 300px;
  text-align: center;
  background-color: #FBEFF2;
  position: relative;
  display: inline-block;
  /* for demonstration */
  vertical-align: top;
  /* for demonstration */
}
.cell {
  display: flex;
  flex-direction: column;
  width: 120px;
  height: 500px;
  background-color: grey;
}
.cell p {
  background-color: rgb(200, 200, 200);
  word-wrap: break-word;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  margin: 2px;
}
<div class="table">
  <div class="cell">
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties</p>
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties Big enough to accommodate 3 lines</p>
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties</p>
  </div>
</div>

<div class="table">
  <div class="cell">
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties</p>
    <p>Addresses</p>
    <p>ALKIS</p>
    <p>Properties Big enough to accommodate 3 lines</p>
  </div>
</div>

Answer №2

It seems like you're looking to adjust the spacing between elements, not the padding. Have you considered using the flexbox method? I've included a link to a fiddle for reference (just keep in mind that you may need to include fallbacks for older browsers using prefixes like display: -webkit-flex;) Check out the fiddle here

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

Whenever I include "border:0" in the styling of my hr element, a significant number of hr elements fail to appear on the screen

I have been experimenting with using hr elements to divide sections of my web page. Here is a snippet of the CSS code I am using: hr{ content: " "; display: block; height: .1px; width: 95%; margin:15px; background-color: #dfdfdf; ...

Css animations not functioning properly in Chrome

I've been working on this animation project, here is the link: http://codepen.io/Zeaklous/pen/dIomg It seems to work perfectly fine on Firefox but not on Chrome. Here is the code snippet: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/ ...

What is the best way to conceal all lists except for the one that has been chosen?

Looking to enhance my sortable list with jQuery UI - currently it's functional, but I'd like to incorporate a dropdown menu for each item to designate the active one when clicked. Is there a way to modify the code so that upon selection, only th ...

Position the button at the bottom of a div

I am currently utilizing Bootstrap 4.5, however, the following is just a brief demonstration of my desired outcome. Within the dimensions of a fixed-width and height card, I aim to have the content occupy all remaining space while ensuring the button stay ...

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

What is the right way to efficiently rotate a View in React Native?

Here is my code snippet: <View style={{borderColor:"red",borderWidth:4,flex:1,transform: [{ rotate:'90deg' }]}}> <Text>Hi</Text> </View> The result can be seen here: https://i.sstatic.net/C9ryl.jpg Unf ...

What could be causing render_template to fail when attempting to update the same parameters more than once?

Lately, I've dived into the world of Flask, MongoDB, and ElasticSearch. So far, my MongoDB and ElasticSearch setups are running smoothly. However, I've encountered an issue with generating a list of dictionaries and displaying them on my HTML we ...

Using CSS Flex to style text that has been transformed

I'm attempting to achieve a specific date display effect, but I'm running into some difficulties. Despite using flex and text transform, I can't seem to get rid of the extra width on the right side of the year. Here's my current outcom ...

I am unable to view the input appearing inside the input box on my React/HTML application

I have been developing a React app for building portfolios, but I am facing an issue where I cannot see any text that I type into my input boxes. Can someone please help me troubleshoot this issue? Any assistance would be greatly appreciated. I have made ...

When attempting to use the jsonify method on a variable within an html file, the output is not displayed

Is it possible to store "jsonify" in a variable? I find the pure json format of "return jsonify(data)" unappealing, so I am looking for a way to enhance it using an HTML template with proper indentation. Here's the python code I have: from flask impo ...

Error when the class is not found + Application

I am attempting to create my debut Applet named MemoSaver2.java In addition, I have crafted the following html: <html> <object classid="java:MemoSaver2.class" type="application/x-java-applet" archive="MemoSaver2.jar" he ...

"Applying CSS styles to highlighted text using jQuery - how can I do it

Struggling to style selected text with CSS? Here's what I've tried so far without success in Firefox. $(document).keyup(function(){ savedRange = selection.getRangeAt(0); $(savedRange).wrap('<span style="color:red"></span>' ...

Unable to modify the border radius of the carousel indicators in Bootstrap 5

Currently, I am working with the carousel component in Bootstrap 5.1. I have encountered an issue where I am unable to add border-radius to the carousel indicators, despite trying various methods. Even though the inspector shows that the indicators (HTML ...

Adjust all children's height to match that of the tallest child

My nearly completed jQuery slideshow is working well except for one issue - the height of the slideshow remains fixed at the height of the first displayed slide, causing problems. View the issue here: . I have tried different jQuery solutions from StackOve ...

"Enhance the Navbar with a pop of color by changing

https://i.sstatic.net/dHKsV.pngMy Navbar is already defined and working perfectly, but I want to add a full-page background to the Navbar links. Currently, on mobile devices, the link backgrounds do not cover the entire page horizontally and instead look l ...

Tips on transferring information from AngularJS to a remote server using ngResource

Looking for guidance on utilizing the ngResource module? To send data to a server. To retrieve data from a server. I am attempting to send data to a Solr server. Utilizing AngularJS API. Using npm package manager to install packages to my application. Th ...

Leveraging the ng-hide property of one controller to adjust the ng-style attribute of another controller through a centralized global controller

Seeking assistance with accessing the boolean value of ng-hide from one controller in order to alter CSS properties of another controller utilizing a global controller. Here is the jsfiddle link: https://jsfiddle.net/dqmtLxnt/ HTML <div ng-controller= ...

What are some effective methods to completely restrict cursor movement within a contenteditable div, regardless of any text insertion through JavaScript?

Recently, I encountered the following code snippet: document.getElementById("myDiv").addEventListener("keydown", function (e){ if (e.keyCode == 8) { this.innerHTML += "&#10240;".repeat(4); e.preventDefault(); } //moves cursor } ...

Establishing the REM value for all CSS properties

My goal is to implement the use of rem units throughout my website. However, relying on the standard rem-to-px conversion rate doesn't feel very intuitive: 10px = 0.625rem 12px = 0.75rem 14px = 0.875rem 16px = 1rem (base) 18px = 1.125rem 20px = 1.25r ...

I experienced issues with the brackets software freezing up while using the Bootstrap min CSS file

Recently, I've been using brackets as my text editor and have run into an issue with the bootstrap.min CSS file. For some reason, it keeps freezing whenever I try to load it in brackets. Oddly enough, HTML files work perfectly fine, but ...