What is the best way to control the distance of a div from the top, rather than the

I'm currently trying to increase the height of a div on button click. However, I am encountering an issue where the div is expanding from the bottom (default behavior). What I actually want is for the div to expand from the top while keeping its content intact.

In my scenario, I have two colored divs - yellow and white. When the button is clicked, I want the height of the white box to increase from the top rather than the bottom.

Check out the code here:

https://codesandbox.io/s/still-lake-lw07u?file=/index.html

<body>
    <div class="parent">
        <div class="abc">
            <div class="img"></div>
            <div class="content"></div>
        </div>
    </div>

    <button onclick="abc()">click</button>
    <script>
        function abc() {
            document.querySelector(".content").style.height = "300px";
        }
    </script>
</body>

Answer №1

Not completely certain about the ultimate design, but here's a suggested approach:

      function xyz() {
        const ele = document.querySelector(".content");
        ele.style.height = "300px";
        ele.style.position = 'absolute';
        ele.style.bottom = '0px';
      }

Answer №2

You have the option to utilize transform: scaleY(val) along with transform-origin: bottom to adjust the size and positioning of your element.

 .abc {
        width: 80%;
        margin: auto;
        display: grid;
        grid-template-columns: repeat(10, [col-start] 1fr);
        grid-template-rows: repeat(8, [row-start] 1fr);
        padding-top: 3.4em;
        padding-bottom: 3.4em;
      }
      .img {
        grid-column: col-start / span 8;
        grid-row: row-start / span 6;
        background-size: cover;
        max-width: 1074px;
        max-height: 200px;
        background-color: #ee0;
      }
      .content {
        grid-column: col-start 6 / span 6;
        grid-row: row-start 2 / span 6;
        background: #fff;
        min-height: 100px;
        border: 1px solid blue;
        padding: 3em;
      }
      
      .bigger {
      transform: scaleY(2);
      transform-origin:bottom;
      }
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <style>
     
    </style>
  </head>
  <body>
    <div class="parent">
      <div class="abc">
        <div class="img"></div>
        <div class="content"></div>
      </div>
    </div>

    <button onclick="abc()">click</button>
    <script>
      function abc() {
        document.querySelector(".content").classList.add('bigger');
      }
    </script>
  </body>
</html>

Answer №3

A different perspective:

<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">

<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=yes"/>

<title> Sample Webpage </title>
<!-- link rel="stylesheet" href="styles.css" media="screen" -->
<!-- Source: https://example.com/sample-question/12345/how-to-create-a-div-element/67890#67890 -->
<style>
 .container { background-color: purple; width: 15em; }
 .star { height: 200px; }
</style>

</head><body>
   <div class="wrapper">
      <div class="xyz">
        <div class="pic">Picture</div>
        <div class="container">Sample</div>
      </div>
    </div>
    <button onclick="xyz()">click me</button>

<script>
  function xyz() {
    const element = document.querySelector(".container");

    var newDiv = document.createElement('div');
//    div.innerHTML = '*';
    newDiv.className = 'star';

    element.parentNode.insertBefore(newDiv, element);
  }
</script>
</body></html>

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

Ensuring Safe Data Storage for an Angular 2 Project with Local Storage

One thing that worries me is the possibility of a hacker accessing the developer's console or local storage to alter user data, like the company name, and then gaining unauthorized access to my database. Here's a closer look at the issue: Curren ...

Ways to incorporate debounce time into an input search field in typescript

Is there a way to incorporate debounce time into a dynamic search box that filters data in a table? I have explored some solutions online, but my code varies slightly as I do not use throttle or similar functions. This is my template code: <input matI ...

Sliding to alter the vertex coordinates

Seeking help to dynamically change the x and y position of the first vertex in a triangle using sliders on datgui. Below is my code, can someone help me figure out what I'm doing wrong by assigning variables inside animate()? I've been struggling ...

"Troubleshooting: Why isn't the AJAX Live Search displaying any

When I try using the live search page in livesearch.php by typing entries into the input box, I am not seeing any results displayed below it. Below is the HTML code and script in search.php. search.php <!DOCTYPE HTML> <html> <he ...

Converting a time string into a date object using JavaScript and AngularJS

After receiving the string from the REST API as "20160220", I am aiming to format it as "20/02/2016". Utilizing AngularJS for this task, I understand that I will need to implement a filter. I have already attempted the code snippet below: app.filter(&apo ...

Achieving nested routes without the need for nested templates

My Ember routes are structured like this: App.Router.map(function() { this.resource("subreddit", { path: "/r/:subreddit_id" }, function() { this.resource('link', { path: '/:link_id'} ); }); }); However, ...

Verify that the JSON data contains the desired data type before adding it to jQuery

I'm working with JSON data extracted from an Excel file. I need to match the First Name in the JSON data with the 'First Name' field and append those elements to a specific div. Additionally, if the JSON data includes a 'status', I ...

The div elements are not nested correctly as they should be

Essentially, I have a div element that contains a wrapper along with two other nested divs. One of these divs floats to the left while the other floats to the right. You can see how it appears here: Upon examining the code provided below, you'll not ...

Creating a clickable map for a PNG image: Step-by-step tutorial

My goal is to create a interactive map similar to this one. Click here for the image ...

What is the best way to ensure an observable has been updated before proceeding with additional code execution?

Is an observable the best choice for providing live updates of a variable's value to another class? I have a loop in my service class that looks like this: elements.forEach(element => { doStuff(); this.numberSubject.next(valueFromDoStuff); }) ...

Invoking a SOAP service method defined by a message contract in soap.js

I am currently working with a soap service that utilizes message contracts. According to the rules of message contracts, both the request and response messages must be objects. Message contracts are essential for me because they provide complete control ov ...

Increasing the size of elements with jQuery animate method

I've been utilizing the animate function in jQuery to dynamically resize a content area upon hovering over the element. Although the script is functioning correctly, I'm facing a challenge in preventing the script from resizing the content multi ...

Missing data: Node JS fails to recognize req.body

I've looked through various posts and I'm feeling quite lost with this issue. When I run console.log(req), the output is as follows: ServerResponse { ... req: IncomingMessage { ... url: '/my-endpoint', method: &a ...

Using JavaScript, aim for a specific element by its anchor headline

I'm looking to make some changes to my navigation menu, specifically hiding the home menu item unless the mobile navigation menu is toggled. Is there a way for me to target the "home" anchor title and apply the active class to it when toggled, similar ...

What is the best way to design a Bootstrap grid layout that includes a responsive left column with a minimum width and a scrollable right column?

I'm so close to getting the desired look and behavior, but I'm facing a problem with the scrolling in the right column. The scrollbar is visible but it's not scrolling the content on the right side. It should look like this: . Currently, i ...

Automatic File refresh in Javascript without page reload

My webpage includes a javascript file that is called in the head tags of the document. I need this javascript file to reload every 30 seconds. In my research, I found issues with pulling a locally stored copy of the file and potential cross-browser compat ...

Error: The value of data.isbn is not defined

After encountering the error message below: TypeError: data.isbn is undefined I modified the code to include the data.isbn == null condition as shown below: $.ajax({ type: "POST", url: "getInventory.php", datatype: "json", data: ({skuSta ...

How to align several DIVs within a container with an unspecified size

I have several blue rectangles with known dimensions (180 x 180 px) within a container of unknown size. <html> <head> <style> @import url('http://getbootstrap.com/dist/css/bootstrap.css&a ...

localStorage access denied in IE11 on desktop mode, but not in metro mode

I am encountering an issue with a website that is built on Adobe AEM 5.6.1 and checks for localStorage accessibility. The problem arises when I try to access the site from the Desktop version of IE11 on an HP Elite Pad 900 running Windows 8.1 Pro, as it re ...

Why is Socket.io functioning on localhost but fails to work once deployed to Heroku?

Sharing my socket server code: const io = require("socket.io")(8080, { cors: { // origin: "http://localhost:3000", origin: "https://mern-bubble.herokuapp.com", }, }); This is the client-side implementation: useEf ...