Ensuring that a nested div adjusts responsively within a static parent container

I am working on a project that involves HTML, CSS, and JS code. Here is the structure of my code:

HTML:

<div id="blockcart-wrapper">
  <div class="blockcart cart-preview">
    <div class="header">
      <a rel="nofollow" href="#">
        <img class="cart-icon" src="https://via.placeholder.com/20x20" onclick="toggleClass()">

      </a>
    </div>
    <div class="body" id="shopping-cart-body">
      <div class="close"><a href="" onclick="toggleClass()">X</a></div>
      <ul>
      </ul>
      <div class="shopping-cart-header">CART</div>
      <div class="products-container">
        <div class="product">
          <span class="prodcut-image"><img src="https://via.placeholder.com/250x200"></span>
          <div class="product-details">
            <div class="name-header">NAME</div>
            <div class="product-quantity-details">
              <span class="quantity">QTY</span>
              <span class="color-circle"></span>
              <span class="color">COLOR</span>
            </div>
            <div class="price-open">
              <span class="product-price">XX.XX</span>
              <span class="product-link"><a href="#">öffnen</a></span>
            </div>
          </div>
        </div>

        <div class="product">
          <span class="prodcut-image"><img src="https://via.placeholder.com/250x200"></span>
          <div class="product-details">
            <div class="name-header">NAME</div>
            <div class="product-quantity-details">
              <span class="quantity">QTY</span>
              <span class="color-circle"></span>
              <span class="color">COLOR</span>
            </div>
            <div class="price-open">
              <span class="product-price">XX.XX</span>
              <span class="product-link"><a href="#">öffnen</a></span>
            </div>
          </div>

      </div>
      <div class="checkout">
        <div class="taxes">
          <span class="label">Taxes</span>
          <span class="value">0</span>
          <hr>
        </div>
        <div class="cart-total">
          <span class="label">Total</span>
          <span class="value">0</span>
        </div>
        <button><a href="#">Checkout</a></button>
      </div>
    </div>
  </div>
</div>

CSS:

.cart-preview {
    float: right;
    position: relative;
}

.cart-preview a,
.cart-preview a:hover,
.cart-preview a:visited {
    text-decoration: none;
    color: inherit;
}

.cart-preview .header {
    display: block;
    font-weight: bold;
    border: 1px solid #808080;
    padding: 5px;
    cursor: pointer;
    background-color: #fff;
}

... (CSS code continues) ...

JS:

function toggleClass() {
  document.getElementById('shopping-cart-body').classList.toggle('open');
} 

I am facing an issue aligning the checkout div relative to its parent and positioning its child elements absolutely. I want the checkout div to always have 20% of the parent's height with the child elements aligned accordingly. I need the button to have a margin from the bottom for example 30px. However, I couldn't get it to work using position: relative for the .checkout div.
I have created a codepen demonstrating this issue: https://codepen.io/anon/pen/BOLpdG

If anyone can help me with aligning the div properly, I would greatly appreciate it.

https://i.sstatic.net/CJctH.png

Answer №1

Hey there, I've created a functional version of what you're looking for (hopefully it matches the desired outcome).

I made some adjustments to the CSS in

.cart-preview .body .checkout.car
,
.  .cart-preview .body .checkout>button
, .checkout .taxes, and .checkout .cart-total. Additionally, I moved the checkout <div> outside the products container and added another <div> with the .meta-data-wrap class.

Here are some tips for future reference. Explore different responsive length units like vh, vw, rem, etc. Also, familiarize yourself with the calc function, which can be quite helpful (these were the aspects I utilized to adjust your CSS). When structuring an HTML component (such as the sidebar-cart), aim to separate each logical section into its own container to make it easier to manipulate and manage (as demonstrated in the example with separating the 'checkout' block from the products container).

PS. Be sure to view the demo in full screen for the best experience! :D

function toggleClass() {
  document.getElementById('shopping-cart-body').classList.toggle('open');
} 
// Updated CSS Styles
  <div class="blockcart cart-preview">
    // HTML Structure
  </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

Personalized AngularJS required text

After utilizing the built-in AngularJS directive required and encountering a validation error, I receive a small popup near the field displaying "Please fill out this field". My issue lies in needing the text to be displayed in another language. How should ...

Responsive design issues with Bootstrap 3 box layout

I am currently working on creating a bootstrap4 layout that includes three boxes arranged side by side on a wide screen. However, my goal is to ensure that if the screen size decreases, the red and green boxes always stay adjacent to each other while the b ...

Is there a way to automatically transfer the ID from the first table to the id_position field in the second table upon clicking submit?

I'm dealing with two tables and I need to figure out how to add the id of the first table to the position_id field of the second table upon submitting. My request is done through ajax. The fields for the second table are dynamic, meaning you can keep ...

Determine the amount of unused vertical space within a block of text

Having applied CSS to a span element: font-height = 120px; height = 120px; line-height = 120px; The text inside the span does not completely fill the height of 120px. Is there a method to determine the offset of the text from the top and bottom boundar ...

"Strategically placing elements on an HTML grid

My current project involves creating a grid layout in HTML using CSS. The goal is to use this layout for various elements such as images, text, and links. What I envision is a visually appealing grid where each object fits together seamlessly with no gaps ...

Submitting a form using JQuery triggers an error message related to character encoding

While attempting to submit a form using jQuery, I encountered an issue. Firebug displayed a strange error message in German, leading me to believe that there may be a peculiar character in the HTML causing the problem. The character encoding of the plai ...

The issue of ngModel not binding to the value of ion-select in Angular Ionic

Having an ion select outside of a form with an ngModel attribute bound to "selectedValue", I encounter an issue where my selections are not being properly populated in the selectedValue variable even though they appear in the ionChange method. The main pur ...

Unable to display PHP variable in HTML

I am currently facing an issue while attempting to display user input that includes double quotes in an HTML form. When I echo the input, everything before the double quotes is being shown. For example, if the input is "6" Tropical Premium $8.99", only "6" ...

Various image resolutions designed for extra small, small, medium, and large screens

My image has a relative width and currently the src points to a single, large file (approximately 1000px). The issue arises on small devices with Opera and IE where the browser scaling results in pixelated images. Conversely, using a smaller image leads to ...

Declaring a Javascript variable within an if statement does not alter the value of the global variable

Currently, I am working on enhancing my HTML projects by using JavaScript to modify the video source. Below is the video element in question. <div> <video id="songVid" onmouseover="controlsVid()"> <source src=&qu ...

The combination of Javascript and CSS allows for interactive and visually

I'm currently working on a project where I had to create a simulated weather page using only Javascript. However, I am struggling with the overall layout and have a few questions that I hope someone can help me with: Despite trying various methods ...

I want to update the toggle switches within the GridToolbarColumnsButton component from MUI's DataGrid to checkboxes

Currently, I am developing a website where one of the pages has tabular data fetched from the backend. To filter this table, I utilized Mui filters from datagrid({...data}components={{ toolbar: CustomToolbar }}). Among these filters is a GridToolbarColumns ...

The z-index property in CSS is ineffective on Firefox browsers

My current project involves implementing a dual slider input range. The HTML code I have written looks like this: .acCalcInputRange input[type="range"] { pointer-events: none; position: absolute; bottom: 0.6rem; border: none; outline: none; ...

I crafted this dropdown menu, but for some reason, the selections aren't registering when clicked. Below is the code I used. Any assistance would be greatly appreciated!

Hi there, I need some help with getting my code to run properly. I've created a dropdown box using HTML and CSS, but it seems like there's an issue with the JavaScript portion as the options are not being selected. I've included a code snipp ...

Using SVG and CSS to color multiple paths with individual colors

I am working with an svg that contains various paths, ellipses, and shapes each with different fill colors such as red and blue. When I combine them into a sprite, I want to be able to change the fill color on hover using CSS. Typically, I would remove the ...

My JavaScript/jQuery code isn't functioning properly - is there a restriction on the number of api calls that can be made on

Below is the code I have implemented from jquery ui tabs: <script> $(function(){ // Tabs $('#tabs1').tabs(); $('#tabs2').tabs(); $('#tabs3').tabs(); //hover states on the sta ...

Create an HTML table with a static header, adjustable columns, and the ability to resize to a width smaller than the content

Looking to create a unique web application with a table that has a fixed, styled header, resizable columns, and the ability to resize the columns without truncating content. Additionally, I want the body of the table to scroll if it cannot all fit on the p ...

Is there a way to align one <article> next to another <article> to create a horizontal layout?

Can anyone help me figure out how to display two <article>s on the same line or side-by-side, with the FirstArticle positioned to the left and the SecondArticle to the right? .MainContent { border-radius: 5px; -moz-border-radius: 5px; ...

The full screen menu is exclusively displayed in the navigation bar

My HTML and CSS code is causing an issue where the full-screen menu only appears in the nav bar. This problem seems to be specific to Safari. To solve the issue, I need to remove the following code: .nav { position: fixed; } However, I still ...

The JS Fiddle code fails to function properly once it has been downloaded to the local computer

I came across a helpful example fiddle webpage: jsfiddle.net/yijiang/6FLsM/2 After following examples from others, I attempted to download (right click and Save As) using the latest Chrome browser with the following links: jsfiddle.net/yijiang/6FLsM/2/s ...