Expand the element to accommodate the content, ensuring it does not surpass the boundaries of its parent

I am looking to create an element called .errorMessages that can grow to accommodate content (with a div set to overflow-y: auto to display a scrollbar), while still remaining within the boundaries of its parent container.

The structure of the HTML is as follows:

<div class="modalBox"> <!-- this size adjusts based on window -->
  <div class="content">
    <table class="vtable" style="width: 350px;">
      <!-- this should expand, but not surpass the content width -->

      ...
    </table>
    <div class="errorMessages flex">
        <!-- this should adjust in size, but not exceed the content area -->
      <div>
        <ul>
          <li>an error message</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
        </ul>
      </div>
    </div>
  </div>
  <div><input type="button" value="Close" class="button blue" />
  </div>
</div>

Preview images: https://i.sstatic.net/keQpb.png https://i.sstatic.net/0c9ft.png

Answer №1

Set a specific height in rem for your .content class. Then assign desired heights in percentage to your .vtable and .errorMessages classes.

<div class="modalBox"> <!-- adjusts based on window size -->
  <div class="content" style="height: 10rem">
    <div class="vtable" style="width: 350px; height: 50%; overflow-y: auto;">
      <!-- this should expand, but not exceed the content's height -->
      <div>
        <ul>
          <li>an error message</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
        </ul>
      </div>
      ...
    </div>
    <div class="errorMessages flex" style="width: 350px; height: 50%; overflow-y: auto;">
        <!-- this should expand, but not exceed the content's height -->
      <div>
        <ul>
          <li>an error message</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
          <li>another error message...</li>
        </ul>
      </div>
    </div>
  </div>
  <div><input type="button" value="Close" class="button blue" />
  </div>
</div>

Also, ensure to use appropriate HTML elements, like the table element, when developing your code.

Answer №2

Currently, I have come up with a simple workaround to address this issue. However, I cannot consider it as a definitive solution because it does not effectively resolve the initial problem. My main objective is to determine if there is a way to achieve this without relying on JavaScript.

The approach I am currently implementing involves setting the entire .content section to overflow-y: auto. Meanwhile, I refrain from making any adjustments to .errorMessages, allowing it to expand according to the content. By placing the close button outside of .content, it remains visible. I then establish the height of .content by using calc(100% - var(--close-button-height)).

Although this method is not perfect, it serves its purpose adequately and maintains a satisfactory visual appearance.

<style type="text/css">
  .border {
      border: 1px solid red;
  }

  .modalBox {
      border-color: blue;
      height: 90vh;

  }

  .modalBox .content {
      background-color: #fffeec;
      border-color: #09c900;
      height: calc(100% - 48px);
      /*max-height: calc(100% - 40px);*/
      /*margin-bottom: 40px;*/
      overflow-y: auto;
  }

  .modalBox .content .table {
      border-color: gray;
  }
  .modalBox .errorMessages {
      margin-top: 10px;
      border-color: red;
      background-color: #ffecec;
      height: fit-content;
  }
  .modalBox .errorMessages .errorsContent {
      min-height: 50px;
  }
  .modalBox .buttons {
      background-color: rgba(0, 181, 173, 0.1);
      height: 42px;
      box-sizing: border-box;
      padding: 10px;
      text-align: center;

  }

  </style>

<div class="modalBox border">

  <div class="content border">
<div class="table border">
  content<br />
  table<br />
  placeholder<br />
</div>
<div class="errorMessages border">
  <div class="errorsContent">
  <ul>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
    <li>error1</li>
  </ul>
  </div>
</div>
  </div>
  <div class="buttons">
<input type="button" value=" Close " />
  </div>
</div>

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

Images in the background that adapt to the screen size

Hello, I am a beginner in React and looking for assistance with my home.js code: I am attempting to use an image as a background for my page, but I am struggling to make it adjust to the window size. I have tried using properties like background-repeat: n ...

Difficulty in smoothly changing background image using JQuery

Update After some troubleshooting, I realized that the background image was indeed fading out. The issue was that the background image was being removed before the fade effect could be seen. By removing .css('background',''), I was abl ...

A guide to extracting text from HTML elements with puppeteer

This particular query has most likely been asked numerous times, but despite my extensive search, none of the solutions have proven effective in my case. Here is the Div snippet I am currently dealing with: <div class="dataTables_info" id=&qu ...

Steps for retrieving the image URL after clicking on an image within a list of images

I have a list of images in the index file shown below: <div id="image-list" <ul> <li data-id='1'> <img src='image/001.jpg' alt=''> </li> <li data- ...

Google Chrome's number input type trims its rendering

When using the number input type in Chrome, the appearance of the input boxes is different, with the numbers appearing bottom-trimmed. Is there a way to fix this issue without adjusting the size of the input controls? Any assistance would be greatly appre ...

Explore a webpage for links, navigate through them, and retrieve pages that do not contain any links using R programming

In the process of updating my rNOMADS package to include all models from the NOMADS website, I found myself faced with the challenge of not knowing the depth or breadth of the html directory tree beforehand. To solve this issue, I decided to create a simpl ...

How to Execute a Java Method Using a JavaScript Function?

I've been doing some research, but haven't come across a straightforward answer to my question. Imagine I have an HTML page with embedded Javascript code, as well as a java.class file located in a package named somePackage.someSubPackage.*; Is t ...

Bootstrap 4 image gallery with thumbnails sizing

I'm facing an issue with my responsive image gallery that has only 6 thumbnails. Everything looks great on large and medium devices, but in mobile view, the thumbnails appear too small. Adding padding doesn't work as it breaks the side margins al ...

Do not exceed 3 elements in a row when using Bootstrap's col-auto class

col-auto is excellent. It effortlessly arranges items by size in a row, but I need it to strictly limit the number of items in each row to a maximum of 3. Each row should only contain between 1-3 items. <link rel="stylesheet" href="https://cdn.jsdeli ...

"Add some pizzazz to your website with Animate.css - Animate your

I am looking to create a dynamic effect where three words slide in and out individually. The first word should slide in and out, then the second word, and so on. How can I make this happen? Here's what I've tried: HTML: <p class="fp-animate ...

Is it not possible to check the server control checkbox in ASP.NET?

When attempting to implement a toggle button in my project, I encountered an issue where a server control checkbox cannot be checked. However, when using an HTML control, the checkbox can be checked. What could I be overlooking? <div class="material- ...

Encountering a Django Channels issue while loading the JavaScript wrapper

After successfully running the server, the following appeared in my cmd prompt: System check identified no issues (0 silenced). January 21, 2020 - 17:43:42 Django version 3.0.2, using settings 'crm.settings' Starting ASGI/Channels version 2.4.0 ...

Centering text using CSS Bootstrap

Hey everyone, I've been attempting to center my text using Bootstrap, but for some reason it's not working. Can someone help me figure out what's going on? <footer class="row p-1 mt-3 bg-primary text-white"> <p class= ...

all elements with equal height within a flexbox positioned at the bottom

Check out this HTML code snippet I have for constructing a grid using Bootstrap 4: <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOh ...

Divide HTML content while keeping inner tags intact

Currently, I am developing an online store. In my code, there is a requirement to display attributes and descriptions for multiple products on a single page. The attributes are displayed in a table format, while the description can include simple text as w ...

Changing the color of text in one div by hovering over a child div will not affect the

When hovering over the child div with class .box-container, the color of another child div with class .carousel-buttons does not change. .carousel-slide { position: relative; width: inherit; .carousel-buttons { position: absolute; z-index: ...

Beginner attempting to comprehend Safari CSS font-size test outcomes

Currently, I am delving into the realm of CSS. To test my skills, I am making edits to an HTML file and previewing the outcome in Safari V11.0. My objective is to present an online version of a traditional printed report, complete with fixed columns and a ...

I am having trouble with the browser detection not accurately identifying the browser I am currently using

Currently, I am working on a JavaScript code that will simply display the name of the browser being used to view the web page. Despite me using Safari, none of the navigators seem to recognize it as the current browser. The information displayed in my brow ...

Applying absolute positioning to the router-outlet element in Angular2 using CSS

I am working on a website with the following basic layout: <nav> Nav content here </nav> <router-outlet></router-outlet> I have a component that is being displayed in the router outlet, but I want it to appear on top of ...

"Encountering an Internal Server Error while trying to submit the

Seeking Assistance: I am experiencing an error when attempting to send a contact form. https://i.sstatic.net/7sK58.jpg The Visual Basic code causing the issue: <% mail_to = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail=" ...