What are some methods to boost height in the opposite direction?

I'm looking for a way to make this div fold from top to bottom. How can I achieve this effect without the height increasing abruptly?

var box = document.getElementById("box");

box.onmouseover = function(){
      box.className = "expand";
  }
box.onmouseout = function(){
    box.className = "";
  }
div{
  position:absolute;
  top:50%;
  left:50%;
  height:50px;
  width:100px;
  background-color:#000000;
  transition:height 0.3s;
  }
div.expand{
  position:absolute;
  top:50%;
  left:50%;
  height:100px;
  width:100px;
  background-color:#000000;
  transition:height 0.3s;
  }
<div id="box"></div>

Answer №1

If you want to achieve this effect, make sure to specify the bottom position rather than the top position.

let container = document.querySelector("#container");

container.onmouseover = function() {
  container.classList.add("expand");
}
container.onmouseout = function() {
  container.classList.remove("expand");
}
#container {
  position: absolute;
  bottom: 50%;
  left: 50%;
  height: 50px;
  width: 100px;
  background-color: #000000;
  transition: height 0.3s;
}

#container.expand {
  position: absolute;
  bottom: 50%;
  left: 50%;
  height: 100px;
  width: 100px;
  background-color: #000000;
  transition: height 0.3s;
}
<div id="container"></div>

Answer №2

To simplify this task, utilize the jQuery function. It enhances code readability and facilitates debugging.

For expanding elements

var increaseSize = 50;
$("#element").css("width", "+=" + increaseSize);

For reducing size

$("#element").css("width", "-=" + increaseSize);

This operation increments the current value by 50.

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

Error: Cannot access 'addEventListener' property of null in Chrome Extension due to TypeError

I've been working on developing a chrome extension that autofills input forms in web pages. However, I encountered an error that says "cannot read properties of null." This issue arises when I try to add an event listener to a button in my code. The f ...

Struggling to figure out how to properly shuffle my deck of cards array in JavaScript. Can't seem to grasp how to pass the array correctly

Currently, I am engrossed in my personal project and watching tutorials just for the fun of it. My goal is to create a game for my school project which is inspired by a war card game that I used to play as a child - where the highest number wins. I have ...

A method for adding a strikethrough to text within a list item upon clicking a button

Hi there! I'm just starting out with JS, HTML, and CSS and I could really use some help. I've been researching solutions but I'm struggling to apply them to my specific situation. I'm trying to add an onClick function to Button 2 and im ...

Using jQuery validation to verify that a minimum of one radio button holds a true value

I have a form with two questions. The first question asks if the product value exceeds a certain fixed amount, and the second question asks if the product value is below that fixed amount. Upon submitting the form, validation should ensure that at least on ...

Working with string interpolation in SQLite3 and Nodejs

Just starting out with this and I'm running into an issue with trying to insert a variable into my sqlite3 query. I keep getting the error { [Error: SQLITE_ERROR: no such column: shmee] errno: 1, code: 'SQLITE_ERROR' } where "shmee" is actua ...

Bus Boy's employment record shows no termination

I am working on a Next Js project that includes an upload image endpoint. const busboy = require('busboy') export default function handle(req, res) { const bb = busboy({headers: req.headers}); bb.on('file', (fieldname, file, fi ...

Which file is the optimal placement for the Bootstrap directory: 'header.php' or 'function.php'?

Having dabbled in WordPress Theme templates for a while, I've decided to try my hand at creating a theme from scratch. After uploading the Bootstrap features onto my server, I'm pondering whether it's better to call the Bootstrap.css files ...

choose a row from the list

Looking for assistance from seasoned professionals as I have been grappling with this issue for a while now. Here's the breakdown of the problem: The list of products that have already been viewed is provided below serial | name | description ...

Avoid updating numerous divs when hovering over them

Creating a brief survey utilizing HTML, CSS, and JavaScript. The goal is to present a question and allow the user to rate it on a scale of 1-5. It would be ideal to have a responsive scale where hovering over 1 turns it yellow, hovering over 2 highlights ...

Establish a trigger in mongoDB that monitors changes in document values and triggers an email notification

Hello there! As a newcomer to MongoDB and the world of web development, I have a question that I hope you can help me with. I am looking to send out emails based on the time difference between the current time and the event time stored in the event model a ...

Prevent the footer from overlapping with the text when printing several pages

Is there a way to prevent text from overlapping the footer when printing a page containing both? I have tried using CSS and HTML to adjust the positioning of the footer, but it still overlaps with long blocks of text. Any suggestions on how to fix this i ...

What is the best way to share specific links on LinkedIn articles?

When the LinkedIn button is clicked, the entire link does not get passed when the image is clicked. However, the Facebook link works perfectly fine. The LinkedIn button used to work before, has anything changed since then? <div align="center"> < ...

Issue with data type not refreshing in mySQL Workbench

I'm facing an issue with changing the datatype of my "first_name" column. I want it to be VARCHAR(45), but it's stuck at INT(11) as shown in the table diagram and connection provided below. https://i.sstatic.net/0CbkD.png https://i.sstatic.net/8 ...

What is the best way to modify Mega Menus using JavaScript?

I am currently managing a website that features "mega menu" style menus. These menus consist of nested <UL> elements and contain approximately 150 to 200 entries, resulting in a heavy load on the page and posing challenges for screen readers. To add ...

Is there a method for manually determining a map's LatLngBound based on the center point and zoom level

Currently, I am attempting to display a Google Map using the react-google-maps library. Before the map is completely loaded, I am retrieving the user's current location using the geolocation.navigator function. However, I am facing a challenge in det ...

Streamlined method for extracting information from div elements sourced from a webpage

Hey there! I have a total of 100 elements across 40 files, which equals to 4000 elements in total. My goal is to extract the src and href from each element and compile them into an array for easy database submission. <a class="market_listing_row_link ...

Binding text inputs in Laravel Vue.js does not work

I am working on building a basic edit application for a post using Laravel. I am utilizing Vue.js 2 to bind the data, but I am encountering an issue where the data is not displaying as expected. When I use Vue's syntax, such as @{{ postData.title }}, ...

Passport.js is throwing an error due to an unrecognized authentication

I need to implement two separate instances of Passport.js in my application - one for users and one for admins, both using JWT authentication. According to the official documentation, the way to differentiate between them is by giving them unique names. W ...

Determine the height of the left div, which is set to auto, and apply the same height to the right div, which has overflow set

I am facing an issue that needs a solution. I need to ensure that two div elements have the same height. The left div should have 'auto' height while the right one must match it. Moreover, the right div contains 14 nested divs that need to be scr ...

Utilizing arrays in ejs with HTML elements

I have an array that looks like this: ["Item1", "Item2", "Item3"] I want to utilize this array to iterate through the items and populate the following HTML format: <option data-img-src=ITEM 1 value="1"></option> <option data-img-src=ITEM ...