Achieving auto-height for two elements with CSS within a single block

I am attempting to combine two blocks into one fixed-height block in order to achieve the following design:

------------------------

UL (starts with height=0),

expands when elements are added until maximum height is reached

scroll bar should appear after reaching max height

------------------------

DIV (initial height set to 100% of parent)

decreases in height until minimum height is reached

------------------------

The HTML structure for this layout is as follows:

<div style="height:100px">
  <ul style="max-height:70px;height:auto;overflow:auto"></ul>
  <div style="min-height:30px;height:auto">
    <span>TEST CONTENT</span>
  </div>
</div>

Answer №1

If you're aiming for a clean solution, CSS alone won't cut it. I recommend incorporating some jQuery to dynamically adjust the heights of elements based on their content. By comparing the heights and setting them accordingly, you can achieve the desired effect seamlessly.

Answer №2

I'm uncertain about the clarity of the properties of this DIV. Please note that this is not a complete answer, as it's too lengthy for a comment.

<div id="container">
  <div id="list">
    <ul></ul>
  </div>
  <div id="content">
    <span>TEST CONTENT</span>
  </div>
</div>

#container {
    height: 100px;
    background: grey;
}
#list {
    max-height: 70px;
    overflow: auto;
    background: #ddf;
}
#content {
    min-height: 30px;
    height: auto;
    background: #fdf;
}

// For testing
setInterval(function(){
    $('ul').append('<li>Test</li>');
},3000);

http://jsfiddle.net/V8yuN/

If you aim for the initial full height of DIV#content to shrink as the DIV#list UL expands, what purpose does DIV#content serve in your design? It's worth mentioning that I placed the UL inside a DIV.

The linked fiddle demonstrates a scenario resembling your description (pushing DIV#content to the bottom). The question remains - how crucial is the height attribute of DIV#content to your layout?

EDIT

In case you set #container to overflow: hidden and define #content's height: 100%, it might give an impression of the #container shrinking.

#container {
    height: 100px;
    background: grey;
    overflow: hidden;
}
#list {
    max-height: 70px;
    overflow: auto;
    background: #ddf;
}
#content {
    height: 100%;
    background: #fdf;
}

http://jsfiddle.net/V8yuN/2

However, the above adjustment may affect your design negatively if the content within #content needs to be displayed dynamically.

EDIT 2

The code below accomplishes all elements except the vertical-align property for the text inside #content:

HTML

<div id="container">
  <div id="push">
    <div id="list">
      <ul></ul>
    </div>
    <div id="content">
      <div class="border-top"></div>
      <div id="content-inner">
        <span>TEST CONTENT</span>
      </div>
    </div>
  </div>
  <div class="border-bottom"></div>
</div>

CSS

#container {
    height: 100px;
    background: grey;
}
#push {
    height: 95px;
    overflow: hidden;
}
#list {
    max-height: 70px;
    overflow: auto;
    background: #ddf;
}
#content-inner {
    min-height: 100px;
    background: #dfd;
    margin: 0;
    border-left: 5px solid #fdf;
    border-right: 5px solid #fdf;
}
.border-top {
    background: #fdf;
    border-radius: 5px 5px 0 0;
    height: 5px;
}
.border-bottom {
    background: #fdf;
    border-radius: 0 0 5px 5px;
    height: 5px;
}

http://jsfiddle.net/V8yuN/6/

Answer №3

Imagine your HTML code is structured like this:

<div id="wrap">
    <div id="top">
    </div>
    <div id="bottom">
    </div>
</div>

In this scenario, your CSS might resemble the following example, where #wrap has a specified height and #bottom has a minimum height set. Pay attention to the height 100% !important.

#wrap{
    height: 400px;
    background: #ccc;
}
#top{
    //height: 200px; for testing 
    background: #f0f;
}
#bottom{
    height: 100% !important;
    min-height: 200px;
    overflow: scroll;
    background: #000;
}

Does this align with what you were looking for?

It would be beneficial if you could share the work you've already completed.

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

Enable the set body to have certain sections that are scrollable by setting overflow=hidden

I am currently working on a website that features the following structure: <body> <section></section> <section></section> ... </body> To prevent scroll bars on the body, I have set the CSS property body{overflow:hidden ...

"Utilize Tailwind to effortlessly place a single item at the center

Looking for a way to align a row of items with one item centered in the div and the others positioned on either side while maintaining their original order? Check out this code snippet: <div className="flex mx-auto justify-center items-center" ...

Unusual behavior encountered while attempting to reset input text using AngularJS

I'm encountering an issue with what I assumed would be a straightforward scenario. Here is the form in question: <form class="form-inline" role="form"> <div class="form-group"> <input type="text" ng-model="customerInput" size="80 ...

How can HTML5 be used to easily sync offline data with an online database?

Is there a straightforward method for creating an html5 app that can sync user changes made offline to the server? Imagine visiting a wiki site on your iPad, downloading the entire datastore to local html5 storage, then going offline and making edits as i ...

Angular 8 allows for the creation of custom checkboxes with unique content contained within the checkbox itself, enabling multiple selection

I'm in need of a custom checkbox with content inside that allows for multiple selections, similar to the example below: https://i.sstatic.net/CbNXv.png <div class="row"> <div class="form-group"> <input type ...

Swap out the variables in your function with the values selected from the dropdown menu

I've recently started delving into writing JS functions and I'm facing a challenge with the following scenario: On an HTML page, I want to change a variable within a lodash function based on the value of a dropdown and display the result in a HT ...

What is the best way to organize large amounts of data into an array?

I am currently working on developing a unique version of wordle using javascript and html. In order to do this, I require a comprehensive list of all possible wordle words stored in an array format. Although I have the words listed within a document contai ...

Show SVG in its ViewBox dimensions

Currently, I am utilizing the img-Tag to showcase SVG images that have been uploaded by users onto my Amazon S3 storage. <img src="http://testbucket.s3.amazonaws.com/mysvg.svg" /> An issue arises once the image is displayed as it does not retain i ...

What is the best method for obtaining a modified image (img) source (src) on the server side?

Having some trouble with a concept in ASP.Net that's causing me quite the headache. I am fairly new to ASP.Net and struggling with something that seems much easier in PHP. I created an img element with an empty src attribute : <img runat="server" ...

What's causing the excessive amount of white space in my image?

Whenever I include a picture of myself on the page, it creates excessive white space at the bottom. I'm not sure why this happens, but removing the image eliminates the white space. I've attempted adjusting the margins and padding, as well as se ...

Position the Button in Line with the Input Box

I've been diving into web development recently and I've spent hours trying to solve this issue. My goal is to align my button with the input sections rather than the labels, but all the solutions I found on Stack Overflow haven't worked for ...

Utilizing a Stylesheet for a Single Element

Is there a way to create a footer and include it using the PHP include function from an external file, but ensure that the stylesheet within it only affects the footer and not the entire page? I typically link stylesheets in the head tag, which applies t ...

Crafting a unique datalist from an XML file with the help of jQuery

<mjesta> <mjesto>Zagreb</mjesto> <mjesto>Split</mjesto> <mjesto>Ploce</mjesto> <mjesto>Pula</mjesto> <mjesto>Pazin</mjesto> <mjesto>Daruvar</mjesto> <mjesto>Bjelovar</mj ...

Modifying Copyright Feature in Footer

I am attempting to implement this function within my dark-colored footer: import Typography from '@material-ui/core/Typography'; function Copyright() { return ( <Typography variant="body2" color="textSecondary" align="center"> ...

Tips for achieving vertically centered text wrapping around a floating image

Describing it in words is proving to be difficult, so let me just illustrate it for you. In the following images, the horizontal lines represent the "text", the small box symbolizes the image, and the larger box encompasses the entire webpage. My goal is ...

Choose a group of radio buttons inside a container when a button is clicked

I have a bunch of containers containing radio buttons. The goal is for the container of a selected radio button to appear different: input[type="radio"]:checked+.container { border: 5px solid red; } <div class="container"> <input class="btn ...

Is it possible to utilize formatted strings in Python selenium to locate elements based on different categories such as xpath, css, etc?

An interesting revelation just hit me and it's quite unsettling since my entire program relies on locating elements by CSS selector using formatted strings. Let's consider this example: stop_iteration_for_foh = driver.find_element_by_css_selecto ...

Do I have to implement a column grid for creating responsive websites?

I'm feeling a bit uncertain about the necessity of a column grid for creating a responsive website. For instance, I plan to kick off with the Bones WordPress theme. My intention is to utilize the SCSS setup provided in this theme. The grid it offers i ...

Create a page turning effect in React that simulates scrolling a certain amount at a time

Is there a way to disable default scrolling and have the page automatically scroll to the next item? For example, if I have element2 below element1, how can I set it up so that when I scroll down once, the page scrolls directly to the position of element2 ...

Fundamental CSS Selector

I'm encountering an issue with CSS selectors while using Foundation 5: I am having trouble selecting the desired element, which seems strange to me. The specific problem I am facing is related to changing the background-color of the "Right Button Act ...