Responsive text area with Django and Bootstrap

I created a small JavaScript script to ensure that a text area adjusts responsively based on the size of the accompanying image. It generally works well, but there is a strange random bug that I can't quite figure out. This is how the page should be displayed:

https://i.sstatic.net/k2RrP.jpg

About 70% of the time, the text area loads with the correct height. However, occasionally, it displays like this:

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

If I refresh the page, it immediately adjusts to the correct height. Here is the code I am using:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d2dfdfc4c3c4c2d1c0f0859e839e80">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
        <h1 class="display-3 text-center">Welcome to</h1>
        <h1 class="display-6 text-center">This page is currently under construction</h1>
        <div class="row">
            <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3">
                <div id="targetcard" class="card">
                    <img id="pic_uc" class="nav-item rounded" src="https://placehold.co/600x400"
                        alt=""></img>
                    <div class="card-img-overlay">
                        <a target="_blank" href="http://www.freepik.com"></a>
                    </div>
                    <div class="card-body">
                        <p class="card-text fs-6 fw-light lh-1">Designed by BiZkettE1 / Freepik</p>
                    </div>
                </div>
            </div>
            <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3">
            <div id="textContentAre" class="card border-0 overflow-y-scroll">
                    Lorem ipsum dolor sit amet consectetur adipisicing elit. Consectetur officiis iusto dolorum aspernatur
                    itaque sunt placeat veniam velit eius! Itaque molestias laborum repellat dicta doloribus? Unde atque
                    perferendis eaque architecto. Tempora sapiente accusantium iste libero qui fugiat consectetur laudantium
                    placeat ab explicabo inventore sunt modi facere a dolor, vitae at ratione! Explicabo nulla quasi
                    provident facilis repellendus esse deleniti tenetur eaque ratione perferendis sunt rerum amet
                    accusantium hic
                    temporibus quidem, laborum nemo blanditiis doloremque! Fugit eligendi atque illo. Molestiae modi ex
                    magnam id quaerat perferendis laboriosam illo repellat, in reiciendis! Rem voluptate quae fugit
                    doloremque
                    molestias unde ab necessitatibus perspiciatis, ipsa autem, voluptas commodi esse ex. Incidunt libero
                    dolores consequuntur distinctio sequi impedit voluptatibus, eum inventore, deserunt facere quidem error,
                    minima
                    sunt labore enim? Recusandae labore dignissimos libero ab asperiores maiores tenetur magnam, rerum
                    itaque
                    voluptate ipsa dolorum voluptatem officia in alias omnis! Animi incidunt, ipsa molestias a ut ducimus
                    consequuntur libero dicta expedita voluptatem fuga nam veniam facere ad pariatur voluptate autem sunt
                    reiciendis reprehenderit maiores. Nostrum, architecto. Veniam id facere suscipit aliquid vitae
                    consequuntur eum aspernatur quam magnam libero illo voluptate corporis iste pariatur consequatur, totam
                    eligendi
                    eveniet adipisci obcaecati qui architecto incidunt molestias mollitia fugiat. Laborum quod, sunt
                    perspiciatis
                    minima totam architecto quibusdam! Eligendi dignissimos eos ut? Maxime distinctio repellat non magnam
                    cupiditate beatae corrupti laboriosam deserunt quisquam ullam quam assumenda itaque dolores debitis
                    facere
                    exercitationem, officiis explicabo doloribus nobis. Error quibusdam ut suscipit in a. Nisi ad doloremque
                    ipsum sit tempore perferendis reiciendis accusamus tenetur facilis cupiditate molestias est autem ea
                    vero nostrum ipsa magnam, iste ipsam deserunt rem fugit. Quidem magnam recusandae doloribus dignissimos
                    ad
                    modi inventore sint labore saepe! Tenetur, neque hic ducimus quaerat odit maiores officiis nostrum
                    aliquid,
                    architecto ut perspiciatis qui adipisci quas. Ab, laudantium ratione. Cupiditate consequatur iure.
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col my-3 text-center">
                <p class="text-muted">
                    Contact <a href="#" class="text-reset">Contact</a>.
                </p>
            </div>
        </div>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            let height = document.querySelector("#targetcard").clientHeight;
            document.querySelector('#textContentAre').style.height = height + "px";
            window.addEventListener("resize", function () {
                //console.log('View Port: ' + window.innerWidth)
                if (height != document.querySelector("#targetcard").clientHeight) {
                    height = document.querySelector("#targetcard").clientHeight;
                    document.querySelector('#textContentAre').style.height = height + "px";
                }
            });
            return false;
        })
    </script>
</div>

If anyone could help me understand why the text area occasionally loads with the wrong height, I would greatly appreciate it.

Answer №1

Without the need for JavaScript, achieving this task is entirely possible.

When utilizing two col elements that are positioned next to one another, their automatic equal height is maintained due to Bootstrap 5's application of flexbox in the grid system.

The primary resolution involves addressing the conflict where both columns are essentially vying to be the taller one, consequently determining the overall height of the row.

By employing absolute positioning for the text element within its column, a desired outcome can be achieved - the text's removal from the layout flow results in its inability to influence the row's height, with the image alone dictating it.

Positioning the text element at all four "corners" of its containing column automatically sets its width and height. (inset simultaneously defines the values for top, bottom, left, and right.)

The column itself garners a position: relative attribute to act as the anchor point, while the text element is assigned inset: 0 12px to mirror the 12px padding on both left and right sides that the column typically applies to its content.

To observe the intended two-column layout, it is recommended to view the rendered snippet in full-screen mode. Additionally, appropriate media queries are essential to prevent my modifications from affecting layouts below the designated two-column breakpoint. Absolute positioning of the text should be avoided in such scenarios to prevent disruption of the overall design.

.scrollTextColumn {
  position: relative;
}

#textContentAre {
  position: absolute;
  inset: 0 12px;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ae8e5e5fef9fef8ebfacabfa4b9a4ba">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <h1 class="display-3 text-center">Welcome to</h1>
  <h1 class="display-6 text-center">This site is under construction</h1>
  <div class="row">
    <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3">
      <div id="targetcard" class="card">
        <img id="pic_uc" class="nav-item rounded" src="https://placehold.co/600x400" alt="">
        <div class="card-img-overlay">
          <a target="_blank" href="http://www.freepik.com"></a>
        </div>
        <div class="card-body">
          <p class="card-text fs-6 fw-light lh-1">Designed by BiZkettE1 / Freepik</p>
        </div>
      </div>
    </div>
    <div class="col-xxl-6 col-xl-6 col-lg-6 col-md-12 col-sm-12 col-xs-12 my-3 scrollTextColumn">
      <div id="textContentAre" class="card border-0 overflow-y-scroll">
        Lorem ipsum dolor sit amet consectetur adipisicing elit. ...
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col my-3 text-center">
      <p class="text-muted">
        Contact <a href="#" class="text-reset">Contact</a>.
      </p>
    </div>
  </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

Looking for a way to store data in a sub-document in MongoDB? I am having an issue where my farm sub-documents

After many attempts, I am still facing issues while saving farm data for a User. I created an API to sign up a user and save their data, including the farm object. However, every time I try to update the code, the farm object turns into null. The goal is t ...

Enhancing Data Tables with Jquery: Implementing Column Filtering for Multiple Elements within Table Cells

I am looking to utilize jQuery datatables for filtering a column that may contain multiple values within a single td. To achieve this, I referenced the example provided in the datatables documentation. In the image below, my goal is to filter the office ...

From Google Assistant to Python Results

Is there a way to control a Bitcraze Crazyflie drone using Google Home? I'm looking to give the command "Drone fly to x3 y4" which will be processed through Firebase and result in the Google Assistant Output: "Flying to x3 y4". Additionally, I need an ...

Issue with joining tables in query on Cordova Mobile app

I have 2 queries that will return results which I plan to use in JSON format. The first query is $query = "SELECT * FROM info_location WHERE location_id=".$id.""; and the second query $query = "SELECT t1.location_id,t1.street,t1 ...

Creating sitemaps for multi domain websites using NextJS

We are implementing a next-sitemap package to generate sitemaps for our Next.js pages located in the /pages directory. For pages that come from the CMS, we use server-sitemap.xml with SSR. Despite having 6 different domains, we manage them within a single ...

How to display specific JSON objects that meet particular criteria in HTML cards using Ionic and Angular?

As a beginner in Ionic/Angular, I am attempting to fetch data from a JSON file and display it using cards in the HTML. The JSON contains numerous objects that are either marked as "deTurno == true" or "deTurno == false". Here is what I have so far: publi ...

The jCapSLide Jquery Plugin is experiencing compatibility issues in Chrome browser

While this JQuery plugin works perfectly in Internet Explorer and Firefox, it seems to be malfunctioning in Chrome. The plugin is not being recognized at all by Chrome, and the captions are appearing below the image instead of on top with a sliding effect. ...

Close the popover when clicked elsewhere

My code for creating a popover in HTML is shown below: <a data-placement="bottom" style="float:right;margin-right:20px;" id="new-quote-popover" popover><img src="img/user.png"/>&nbsp;<b>{{UserName}}</b> <div id="popover- ...

Adjust the scroll bar to move in the reverse direction

I'm trying to modify the behavior of an overlay div that moves when scrolling. Currently, it works as expected, but I want the scroll bar to move in the opposite direction. For example, when I scroll the content to the right, I want the scroll bar to ...

Exploring the mern.io scaffolder tool - Unraveling the essence of the .need method

While exploring the code of the scaffolder mern.io, I came across an intriguing method called ".need". It appeared to be related to es6 classes, but unfortunately, I couldn't find any valuable information about it. Thus, I'm curious to know: what ...

Utilizing HTML's multiple input type color feature to save selected colors directly to the database

I am currently using the input type color to select colors for customizing my site. I save the selected color to a database and then retrieve it to apply it to different areas of the site. This works well for a single input type color, but now I have mul ...

Display notification following successful data saving in CodeIgniter

I attempted to set up a notification to appear when saving data and displaying it in the view, but it didn't work as expected using notify.js. Can someone offer some assistance? This is my save function: function save() { $data = array( ...

Make sure to keep Vue-Cookies intact even when closing the browser or tab

I have integrated vue-cookies into my Vue application. The code I'm using to store a cookie is as follows: $cookies.set('authUser', authUserObj); The object authUserObj contains the access_token. However, when I close and reopen the ta ...

The fullcalendar is experiencing difficulties in showing the event

I am facing an issue with Fullcalendar where the events fetched by ajax are not showing up on the calendar. Even though the events are successfully passed and can be displayed in a pop-up, they do not render on the calendar. When manually setting the event ...

How can I use JavaScript to find a keyword on a webpage that is not located within an <a> tag or its href attribute?

I'm on a mission to locate a specific keyword within a webpage. Sounds simple, right? Well, here's the tricky part - I need to disregard any instances of this keyword that are nested within an <a> tag. For example: <p>Here is some s ...

Leveraging the power of the .includes() method

Here is a question regarding the issue at hand. To tackle this problem, create a function called checkForPlagiarism with two parameters: an array containing responses from a specific individual and a string representing an external source. The function sh ...

What is the process for sending JavaScript data to a Rails controller action?

Utilizing jQuery Shapeshift for drag and drop reordering of lists on my web application. I am looking to send the data below to my Rails controller action in order to update the list's order. Every time I drag a list, this is the output that appears ...

Having issues with the basic KnockoutJS binding not functioning as expected

There appears to be an issue with my KnockoutJS script that I'm struggling to pinpoint. Below is the snippet of HTML code: <h2>SendMessage</h2> <div class="form-group" id="messageSender"> <label>Select User Type</l ...

Verification of unique custom string

How can I ensure that a string follows the specific format of x.xx.xxxxx? The first character is mandatory, followed by a period, then two characters, another period, and finally any number of characters of varying lengths. ...

Unable to modify the value of an object variable generated from a query in JavaScript, ExpressJS, and MongoDB

Here is the code snippet I've been working on: let addSubmissions = await Submission.find({"type": "add-information"}, (err) => { if(err) { console.log(err) req.flash('error', 'No "add submissions" were found&apo ...