Adjust the height of a CSS div to automatically fit the space between two other divs

I am trying to adjust the height of my table (with class "body") so it automatically fits the remaining space between the header div and the footer div. All three divs are enclosed within a fixed and centered position on the screen.


Update:

JSFiddle with Flex OR JSFiddle w\o Flex

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
@import url('https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i&subset=greek,latin-ext');
    .dp-container {
        ... 
        // CSS code continues
            });

</script>

Note: I would like the body div to adjust its size according to the window dimensions.

Answer №1

To properly view this code, it is recommended to fullscreen it or open it in the editor as the preview may not display correctly. This will help you understand the structure better.

Unnecessary code has been removed for clarity and focus on the main content.

If you want to learn more about flexbox, check out this helpful guide: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

The key CSS property to initiate a flex container is display: flex; It establishes a flex context for its direct children.

flex-direction row is set by default, displaying items from left to right.

flex-wrap By default, flex items attempt to fit on one line, so setting it to wrap allows them to wrap onto new lines when necessary.

To space the items inside the flexbox, use .flexbox item with a width of 28% or any division that fits between a fourth and a third of the total width minus margins. This will create rows of three.

Alignment:

justify-content determines alignment along the main axis.

align-items sets the default layout of flex items on the cross axis of each line.

align-content aligns the lines within the flex container.

flex-start (default): items are positioned towards the start line

flex-end: items are positioned towards the end line

center: items are centered along the line

space-between: items are evenly distributed in the line; first item at the start line, last item at the end line

space-around: items are equally distributed with space around them. Note that visually the spaces aren't equal due to varying individual spacing.

Quote from CSS Tricks

main {
  display: flex;
  align-items: center;
  padding: 1%;
  background: #ff0000;
}

header span {
  display: flex;
  padding: 1%;
  margin: auto;
}

.nav {
  background: #008000;
  color: #fff;
  text-align: center;
}

.flexbox {
  display: flex;
  justify-content: space-between;
  text-align: center;
  height: auto;
  padding: 1%;
  flex-wrap: wrap;
}

.flexbox item {
  width: 30%;
  padding: 1% 2%;
  background: #00ffff;
  margin: 1% 0;
  align-self: center;
}

.footer {
  background-color: purple;
  padding: 2%;
  display: flex;
  justify-content: space-around;
  text-align: center;
}

.footer item {
  padding: 1% 2%;
  background: #eee;
  border-radius: 5px;
  width: 30%;
  font-weight: bold;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />

<body>
  <main>
    <header class="nav">
      <span>&lt; Prev / Next &gt;</span>
    </header>
    <div class="flexbox">
      <item>td-1</item>
      <item>td-2</item>
      <item>td-3</item>
      <item>td-1</item>
      <item>td-2</item>
      <item>td-3</item>
      <item>td-1</item>
      <item>td-2</item>
      <item>td-3</item>
    </div>
    <div class="footer">
      <item>Open</item>
      <item>Clear</item>
      <item>Close</item>
    </div>
  </main>
</body>

Answer №2

Feeling up for a challenge, I decided to test my skills by creating a custom datepicker using code similar to what you would find in your typical fiddle. However, I'll spare you the nitty-gritty details for now!

It may seem like an unnecessary exercise when you can simply use...

<input type="date"> 

...and let HTML5 handle the rest by providing a built-in datepicker that you can customize.

If you're interested, you can learn more about date inputs here.

Despite the simplicity of the default option, it was still enjoyable to experiment with my own creation.

A Creative Twist on CSS Flexbox Date Picker

$(document).ready(() => {
  $('.flexbox.day').hide();
  $('dates.date').hide();
  $("switchButton").click(function() {
    $('.flexbox.day').slideToggle();
    $('.flexbox.month').slideToggle();
    $('dates.date').slideToggle();
  });
});
...

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

Utilizing JQuery to parse and manipulate JSON data retrieved from PHP servers

Apologies if this is a basic question, but I've been struggling with it all day. I have managed to work with Jquery and CakePHP effectively (not sure if the latter matters in this case), and now I want to return a variable from a CakePHP function to J ...

A fresh inquiry: jQuery custom plugin callback

Many individuals inquire about plug-ins and callbacks, and after reading numerous posts on the topic, I have managed to create a simple hide/show accordion type plugin for FAQs. While I am pleased with its functionality, as I continue to learn, some aspe ...

Is it possible to animate CSS Grid components?

Is it possible to animate a re-ordered set of elements in an array that are arranged using a CSS Grid layout? Check out this quick boilerplate ...

Creating uniform line lengths with a ruler utilizing Fabric.js

I have a div that scrolls horizontally and contains a ruler div and a canvas where I draw horizontal lines of varying lengths. When drawing the lines, I want to ensure they are accurately measured against the ruler using JavaScript and CSS: var canvas = ...

Capture user input from an HTML form and save it as key-value pairs in a JSON object, which can accommodate multiple input

I'm trying to figure out how to collect input from an HTML form and save it into a JSON object for AJAX use. The traditional method of $('#id').val(); won't work in this case because there are multiple fields, as shown below. Here' ...

Issue with deactivating attribute through class name element retrieval

There are multiple input tags in this scenario: <input type="checkbox" class="check" disabled id="identifier"> as well as: <input type="checkbox" class="check" disabled> The goal is to remov ...

Ways to conceal the contents of a page behind a transparent background div while scrolling

I created a Plunkr with the following markup: <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <nav class="navbar navbar-fixed-top"> <div class="container-fluid"& ...

dealing with a situation where the call to the getElementsByTagname method returns null

Here is a snippet of JavaScript code that I am working with: <script language="JavaScript" type="text/javascript"> var sendReq = getXmlHttpRequestObject(); var receiveReq = getXmlHttpRequestObject(); var lastMessage = 0; var mTimer; ...

Is Nextjs the best choice for developing the frontend code exclusively?

When deciding whether to use Next.js or plain React for my front-end development, should I consider that a back-end already exists? If I am not planning to write a new back-end, which option would be better suited for the project? ...

Displaying a progress bar while fetching data in Vue: A step-by-step guide

I am working on developing a progress bar using vue js and bootstrap for my desktop application. Within the template, I have the code that will generate the necessary markup: <div class="container-fluid p-0 vh-100" v-if="isLoading&quo ...

Find the intersection of objects near the Raycaster in Three.js

Having trouble getting the objects that are considered 'near' the mouse using the following method: raycaster.near = 10; raycaster.far = 100; var intersects = raycaster.intersectObjects( scene.children ); if ( intersects.length > 0 ) { ...

What sets v-on:event apart from this.$on(event, handler) in VueJs?

I've been delving into Vue.js event handling and I believe developers can utilize this.$on('event', handler) in their JavaScript files to handle the 'event'. An interesting example demonstrates this concept. <div id="maina ...

Creating a custom button to perfectly fit the contours of its content with Font Awesome 5

Can the shape of a button be altered to match its content? Specifically, I am using the "fa-trash-alt" icon and have a button with hover effects that I only want to apply to the icon itself rather than the entire button. Button: <button type='but ...

Unable to show PHP results in HTML

I've tried many solutions from different articles, but none seem to work for me. I would greatly appreciate any assistance in displaying the Full_Name field within an HTML element on my webpage. Below is the content of the PHP file: {"Test_Info": { ...

Disabling the @import cache feature in LessCSS Assetic

I'm facing a minor issue that is robbing me of precious time. Currently, I am using the assetic plugin with the lesscss filter in my symfony2.1 project. The problem lies in Assetic not detecting changes in imported files when using the @import functi ...

The lookAt function in threeJS seems to be pointing in the opposite direction due to inherited rotations

I'm attempting to align this._mesh.skeleton.bones[ 5 ] (located at the bend in the green object extending from the hand) with the yellow helper line. _FindBubble() { const handPosition2world = new THREE.Vector3(); this._anchor[&apo ...

Customize your Bootstrap panel with a user-friendly wysiwyg editor

I'm trying to adjust the height of a wysiwyg editor housed within a panel to be 200px. How can I achieve this? <div class="row"> <div class="col-xs-12 col-md-12 col-lg-8"> <panel heading="Product Des ...

Create a function that outputs an array containing objects as its elements

Struggling with this one, I've hit my head against all corners but something seems to be missing. Details I'm working on a small JavaScript page that retrieves a Facebook photo ID, calculates the total number of people who shared it, identifies ...

Retrieve a value through an AJAX request when a button is clicked, even if the process is within a loop

$(document).ready(function() { $(".actdeact").on({ click: function() { var id = $('.getfromhere').text(); alert(id); {% for x in mylist %} <span class="getfromhere">{{x.id}}</span> < ...

What is the best way to choose an HTML element based on the attributes of its child nodes in CSS?

Take this scenario for instance: <tr> <td align="center">123</td> <td class="someclass">abc</td> </tr> My goal is to target all <tr> elements that contain a <td> with the class attribute set to someclass. ...