Ensure that the HTML element remains fixed to the bottom of the container at all times, avoiding any clipping

My current predicament involves having a container div element, with an inner element (in this case, a picture) nested within it. I have specific requirements for the positioning of the inner element based on the height relationship with the container:

  • If the container div is taller than the inner element, the inner element should be aligned at the bottom of the container.
  • If the container div is shorter than the inner element, it should instead align to the top of the container while the overflow is clipped at the bottom.

The main challenge lies in the variable height of the inner element, and I am seeking a CSS-only solution, although utilizing JavaScript would be simpler.

Partial Resolution

In instances where the inner element's height is fixed, I have achieved the desired result using the following code (CodePen link):

.container {
  margin-top: 30px;
  margin-bottom: 30px;
  background-color: red;
  overflow: clip;
  height: 100vh;
  position: relative;
  container-type: size;
}

.inner {
  background-image: linear-gradient(green, yellow);
  height: 400px;
  width: 200px;
}

.option1 {
  position: absolute;
  @container (max-height: 400px) {
    top: 0;
  }
  @container (min-height: 400px) {
    bottom: 0;
  }
}
<div class="container">
  <div class="inner">
  </div>
</div>

I believe this could serve as a starting point for potential solutions. By accessing the current element's height in the @container queries, this issue could be easily resolved.

Answer №1

To create a layout where two elements touch two opposite sides, you can use a pseudo element with flex-direction: column for the container:

.container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: clip;
}

.container::before {
  content: '';
}

The invisible pseudo-element ::before ensures proper spacing between elements vertically. If there was visible content within it, the layout would change as shown below:

┌────────────────────────┐
│        ::before        │
├──────┬─────────────────┤
│      │                 │
│.inner│                 │
│      │                 │
└──────┘                 │
│                        │

By combining display: flex, flex-direction: column, and justify-content: space-between, you achieve the desired layout:

┌────────────────────────┐
│        ::before        │
├────────────────────────┤
│                        │
├──────┐                 │
│      │                 │
│.inner│                 │
│      │                 │
└──────┴─────────────────┘

If the container's height is smaller than its children, it will adjust by shrinking them. The space between elements collapses to zero, resulting in clipping of the inner content:

┌────────────────────────┐
│        ::before        │
├──────┬─────────────────┤
│      │                 │
│.inner│                 │
└──────┴─────────────────┘

Feel free to test this setup using the provided code example.

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

Position a div at the bottom of another div using Bootstrap

I'm facing an issue with trying to position an inner div at the bottom of an outer div using bootstrap classes. I have attempted various methods in both bootstrap and regular CSS, such as vertical-align: bottom; and setting position to absolute, but n ...

Getting the WC_Product object within the woocommerce_after_main_content function is a simple task that can be

I've been encountering an issue with a function that I have set up to output text after the main_content action hook in WordPress. Woocommerce keeps throwing a CRITICAL Uncaught Error: Call to a member function get_id() on null in .../function.php err ...

What is preventing me from using jQuery to dynamically insert HTML onto the page?

Below is the HTML code for a Bootstrap Modal dialog box: <div class="modal fade" id="rebateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> ...

Textbox value disappears after being updated

When I click on the edit link (name), the value from the database is displayed as a textbox. However, when I update another normal textbox (age), the value in the edit link textbox disappears. Strangely, if I input a new value into the edit link textbox, i ...

Load CSS stylesheet depending on Internet Explorer document mode

As I work on my website, I am facing the challenge of incorporating different versions of my style sheet based on the browser's document mode, not the browser mode. For instance, if the documentmode = ie8, I may need to load main_ie8.css, whereas for ...

Aligning an image next to a login form with the help of materializecss

https://i.sstatic.net/tki2N.png https://i.sstatic.net/4tJE0.png To view the complete code, visit http://codepen.io/anon/pen/BjqxOq I have implemented a login page using materializecss and I am looking to align an image with the login form. However, when ...

Real-time audio feed permanently stuck in Chromium-powered web browsers

Here is how the audio element appears. No interaction seems to take place with it. (Experimented on ungoogled chromium, Edge, and an android device using a default Chrome browser) Below is the HTML code for the audio element: <div> <audio co ...

Choose a shade from Three.js' color selector

Currently working on a project using A-FRAME, I am in the process of creating a menu for hololens. However, I am facing some challenges with a color picker. I have developed a color picker (link: Color picker) and my goal is to select a color and have it d ...

Guide to creating a unique link to contact a specific phone number through Telegram

Recently, I discovered a cool HTML trick that allows users to contact someone directly from a webpage using WhatsApp: <a href="https://api.whatsapp.com/send?phone={{phone_number}}" target="_blank">WhatsApp</a> Now, my curio ...

Element not being located by Selenium - TimeoutException

Currently, I am utilizing Selenium to input data into a specific field and then extract information from the results. The process of entering the data works fine, including accepting the cookie pop-up prompt, however, there seems to be an issue with locati ...

The jquery ajax user login form is malfunctioning and failing to redirect to the intended page

I am having trouble creating a login form using jQuery AJAX to retrieve user details from the "reg" table in the database. The form doesn't seem to be working as expected, and I want the user to be redirected to another page called menu.php after logg ...

What is the best way to display all values in the class when no selection is made in the drop-down menu?

I need assistance as I am facing an issue where all files are not appearing at the same time. It seems that the problem lies with the year option in my code. The year option requires at least one year to be chosen for it to work properly. I want to impleme ...

Is it possible to access a file on the client's PC without transferring the file to the server?

Is there a way to read an excel file directly from a user's PC in a web app and insert the cell values into a MySQL database cell by cell? Or should the file be uploaded to the server first before being read? (The web app is built using JSP) ...

What could be causing my Flask login function to not accept my post method?

Take a look at my code snippet: @app.route("/login", methods=["GET", "POST"]) def login(): session.clear() if request.method == "GET": return render_template("login.html") if request.method == "POST": rows = User.query.filter_b ...

Encountering a 403 error from AWS S3 bucket when trying to access a static folder path within a Django project

I have been successfully hosting static files in an S3 bucket for my Django web app using the Appwork custom admin template. The files render perfectly locally, but I am facing an issue with rendering from the S3 bucket. I suspect the problem lies in the s ...

Upon selecting a checkbox, I desire for a corresponding checkbox to also be marked

I am looking to enhance my current project by incorporating a feature that allows the user to simply check a checkbox with specific content once. For example, I have a recipes page where users can select ingredients they need for each recipe while planning ...

Why does a black model appear when loading a GLTF model with materials?

I am currently attempting to load a 3D model in glb format. Below is the code snippet: Expected Outcome: Image Current Outcome: Image var renderer = new THREE.WebGLRenderer(); renderer.setSize(1000, 1000); renderer.setPixelRatio(window.devicePixelRati ...

Trouble with bringing in the solana/web3.js library

After successfully importing the solana/web3.js package and running it within a NodeJS file, everything was working fine. However, things took a turn when attempting to connect this file with basic HTML, resulting in an error being thrown. import { Tra ...

Tips for designing a navbar specific to a profile section

I am currently working on an angular application with a navigation bar composed of anchor tags. When the user logs in, I have an ngIf directive to display my profile icon with dropdown options. However, I am facing challenges in styling it correctly. I aim ...

Switch the hover effect to be activated by clicking

Recently, I discovered an incredible plugin created by the talented developer janko. It has been a fantastic addition to my project. The only issue I've encountered is that I do not want the default hover style. Is there a way to transform it into a ...