Why doesn't "height: fit-content;" adjust to the size of its content?

In my project, I have a div that covers a portion of the page. I want this div to adjust its height to encompass all the objects inside it, so I set the height property to fit-content. However, despite having a header and grid within the div, its height remains at 0. I've tried changing positions, setting max-content, and adjusting line-height in the almost-header h1 element, but nothing seems to work. The same issue applies to the grid-container, where the height does not fully cover the last grid-item when padding is removed. Here is the code snippet I'm struggling with:

body{
    font: normal 100% "Sathu", sans-serif;
    background: #181818;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

.secondary{
    position: absolute;
    top: 0;
    left: 0;
    height: fit-content;
    width: 100%;
    padding: 0;
    margin: 0;
    background-color: #FFFFFF;
}

.almost-header{
    position: absolute;
    background-color: #181818;
    height: 20%;
    width: 100%;
    left: 0;
    top: 0;
    right: 0;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

.almost-header h1{
    position: absolute;
    left: 5%;
    margin: 0;
    padding: 0;

    font-size: 4em; /* 64px/16px*/
    line-height: 1;
    /*width: fit-content;*/
    /*max-width: 60%;*/

    color: #FFFFFF;
    width: 90%;
    border-bottom: #FFFFFF 1px solid;
}

.grid-container{
    box-sizing: border-box;
    position: absolute;
    padding-right: 5%;
    padding-left: 5%;
    padding-bottom: 15%;
    padding-top: 0%;
    display: grid;
    column-gap: 1%;
    row-gap: 1%;
    width: 100%;
    height: fit-content;
    margin: 0;
    top: 20%;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    border-bottom: white 1px solid;
}

.grid-item{
    width: 100%;
    height: 100%;
    transition: .5s;
}

.grid-item:hover{
    box-shadow: grey 0 5px 8px, grey 0 -5px 8px, grey 5px 0 8px, grey -5px 0 8px;
}

.grid-item-image{
    width: 100%;
    height: 85%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.grid-item-image img{
    object-fit: cover;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    flex-shrink: 0;
}

.grid-item-footer{
    box-sizing: border-box;
    padding: 5px;
    top: 0;
    margin: 0;
    height: 15%;
    background-color: #E6E6E6;
}

.grid-item-footer p{
    color: #333333;
    line-height: 1;
}

.footer-city{
    margin: 0;
    margin-bottom: 10px;
    font-style: normal;
    font-weight: 800;
    font-size: 2em;
    line-height: 38px;

    color: #333333;
    /*margin-bottom: 5px;*/
}

.footer-author-date{
    margin: 0;
    font-family: 'Barlow', sans-serif;
    font-style: normal;
    font-weight: 500;
    font-size: 1.125em;
    line-height: 22px
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>;
    <title>Title</title>
    <link rel="stylesheet" href= "styles.css">
</head>
<body>
<div class="secondary">
    <div class="almost-header">
        <h1 id="save-cities">
            Check this out:
        </h1>
    </div>
    <div class="grid-container">
        <div class="grid-item" id="grid-item0">
            <div class="grid-item-image">
                <img src="https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=3540&q=80">
            </div>
            <div class="grid-item-footer">
                <p class="footer-city">Something</p>
                <p class="footer-author-date">01.01.1971</p>
            </div>
        </div>
        <div class="grid-item" id="grid-item1">
            ...

Answer №1

Some reworking has been done here. Remember, less CSS is always better 🙂

Your height issue was caused by a 1% gap, though I can't explain the specifics of grid well enough.

* {
  box-sizing: border-box;
}

body {
  background: #181818;
}

header {
  background-color: #181818;
  min-height: 20%;
  display: grid;
  place-content: center;
}

h1 {
  font-size: 4em;
  margin: 0;
  color: white;
  border-bottom: white 1px solid;
}

main {
  padding: 5%;
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  border-bottom: white 1px solid;
}

.grid-item:hover {
  box-shadow: grey 0 5px 8px, grey 0 -5px 8px, grey 5px 0 8px, grey -5px 0 8px;
}

.grid-item img {
  display: block;
  width: 100%;
}

.grid-item footer {
  padding: 5px;
  color: #333333;
  background-color: #E6E6E6;
}

.footer-city {
  margin-bottom: .6em;
  font-weight: 800;
  font-size: 2em;
}

.footer-author-date {
  font-weight: 500;
  font-size: 1.125em;
}
<header>
  <h1 id="save-cities">
    Check this out:
  </h1>
</header>
<main>
  <article class="grid-item" id="grid-item0">
    <img src="[IMAGE_URL]">
    <footer>
      <p class="footer-city">Something</p>
      <p class="footer-author-date">01.01.1971</p>
    </footer>
  </article>
  [REPEATED STRUCTURE FOR OTHER ARTICLES]
</main>

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

Is it possible to include ternary in the "homepage" section of the package.json file?

When making API calls in production, they will be on the same domain as where my app is hosted, so it's necessary to include "homepage": "." in the package.json. However, when working locally from localhost, I need to make API calls to the production ...

Ensure that both the top row and leftmost column are fixed in a vertical header using JQuery DataTable

Check out the implementation of vertical header flow in Datatables by visiting: https://jsfiddle.net/2unr54zc/ While I have successfully fixed the columns on horizontal scroll, I'm facing difficulty in fixing the first two rows when vertically scroll ...

What is the best way to align these boxes so they fit perfectly together?

check it out here code snippets <div id="container"> <div class="box">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus ac magna non augue porttitor scelerisque ac id diam. Mauris elit velit, lobortis sed interdum at, vestibu ...

What is the best way to present sorted items on a user interface?

I have a unique Med interface containing properties like drugClass, dosage, and name. Using this interface, I created an array of drugs with different attributes. How can I filter this array by drugClass and then display the filtered data on a web page? ...

Avoid having two DIVS appear simultaneously in IE for half a second upon page load

I'm encountering an issue with my HTML and jQuery code. Here is what I have: <div id="block1"> some text </div> <div id="block2"> some more text </div> Along with the following piece of jQuery code: jQuery(document).ready(fu ...

Having a negative position while using overflow auto can cause the element to disappear from

My current issue arises when utilizing negative positioning (e.g. top:-20px) within an absolute div while adding overflow:auto to the parent div. The text becomes hidden in all browsers. Unfortunately, due to the structure of my application, it is not fe ...

Unable to assign the "innerHTML" property to a string

Currently, I am working on developing an Issue Tracker with two main functions. The first function, saveIssue(), is responsible for saving submitted issues to localStorage triggered by the submit eventListener. The second function, fetchIssues(), retriev ...

Having trouble with the tooltip not showing up on the fontawesome icon button?

I am experiencing an issue where the tooltip is not appearing for a fontawesome icon nested inside a element. <i class="fa fa-random" title="some tooltip"></i> Here is the CSS style being applied: i.fa { display: inline-block; } ...

Guide to creating a dynamic column layout with minimum height divs

I am facing an issue with the variable height of my div, causing it to position incorrectly. To demonstrate the problem, I have included a code snippet below. Here is the solution I am seeking: https://i.sstatic.net/XMAMr.png Note: Since the div has a ...

Ensuring a successful implementation of an AJAX rule via a .htaccess rule on a secure HTTPS connection

Greetings for taking the time to read this... Presently, my website features a search form that utilizes javascript to display results and organizes them into specific directories. The following is the .htaccess code responsible for this functionality: ...

What could be preventing the justify-content:space-between; from working properly?

I'm experiencing an issue where the items are staying to the left instead of spreading out properly on my website. Additionally, when using Google Icons, they won't display unless I include the base tag in the head section since the domain name i ...

What is the best way to reveal a concealed "div" when hovering the mouse over it?

Currently in the process of developing a dropdown menu to house a search engine form within a static menu bar. The desired functionality is for the search form to appear upon hovering over the "search icon." However, it seems that the submenu fails to disp ...

Using jQuery to locate the dimensions of an image and then adjusting the height and width of a div using

I'm currently working on a project where I need jQuery to determine the size of each image (approximately 8-10 images per page) within an Owl Carousel. However, every time I check in the developer tools, all I see is width: 0px, height: 0px Here&apos ...

What could be causing the .txt file in the <iframe> to download instead of being displayed?

I have implemented an <iframe> to display a text file: <div class="document-view"> <img src="img/302.GIF" /> </div> $(window).load(function () { <s:if test="extention.equalsIgnoreCase('txt')"> elemen ...

The styling of Bootstrap collided with my CSS

While working on my web development project, I encountered an issue with the navigation bar. My friend was responsible for creating the nav bar, but when I integrated his code into my website, it clashed with Bootstrap and caused display errors. I tried va ...

Issue with array merging/inserting feature not functioning as expected

Here are two arrays that I have: First Array: "workingHours": [ { "opening": "09:30", "closing": "13:30", "dayName": "sunday" }, { ...

Preventing Page Refresh when Button is Clicked in Angular

How can I prevent page reload when a button is clicked in Angular? I'm developing a quiz application in Angular where I fetch random questions and options from an API. When users select an option, the next question should appear without reloading the ...

Is it possible to divide an HTML document into separate files for structure and content?

In the world of software development, it's common knowledge that moving strings to separate files is a best practice. This makes it easier for translators and copyrighters to make modifications without having to delve into the code, and it streamlines ...

Is it possible to replicate this effect using HTML5 Canvas?

I'm looking to recreate the four shapes showcased on this website using HTML5 Canvas, while ensuring that the page stretches to 100% width and height. Is this achievable? I am relatively new to working with Canvas. Below is my current CSS code: htm ...

What is the best way to assign a unique color to two overlapping cells in an HTML table using HTML and jQuery?

I have a task to highlight a row or column when a checkbox is selected, similar to the example shown in the image below. How can I adjust the code so that the overlapping cell gets a different color instead of green or yellow? For instance, in the image b ...