Expanding a div's size with CSS to fill the remaining space

Here's the setup I'm working with:

<div class="parent">
   <div class="moverBoy"></div>
   <div class="smartBoy"></div>
</div>

The parent element is fixed at 100x20 indefinitely.

moverBoy and smartBoy are always 20 units in height, displayed as inline-block elements with vertical-align: top to ensure they line up next to each other without any white space or text interference.

The width of moverBoy can change dynamically through JS or CSS animations. My goal is to have smartBoy adjust its width accordingly to always fill up the remaining space within the parent element.

Answer №1

To achieve your desired outcome, you can make use of the float property on .moverBoy and adjust the overflow property of .smartBoy (This method is compatible even with older browsers like IE6):

window.onload = function(){
  document.getElementsByClassName('moverBoy')[0].style.width = '100px';
}
body{ margin:0 }
.moverBoy{
    float: left;
    background:#F00;
    width:300px;
    transition: width 2s;
}
.smartBoy{
    overflow hidden;
    background:#0F0;
}
<div class="parent">
   <div class="moverBoy">&nbsp;</div>
   <div class="smartBoy">&nbsp;</div>
</div>

Alternatively, another approach is to style your elements like a table:

window.onload = function(){
  document.getElementsByClassName('moverBoy')[0].style.width = '100px';
}
body{ margin: 0; }
.parent{
    display:table;
    width: 100%;
}
.moverBoy{
    display: table-cell;
    background:#F00;
    width:300px;
    transition: width 2s;
}
.smartBoy{
    display: table-cell;
    background:#0F0;
}
<div class="parent">
   <div class="moverBoy">&nbsp;</div>
   <div class="smartBoy">&nbsp;</div>
</div>

In either case, adjusting the width of the parent element or .moverBoy and .smartBoy will cause them to adapt accordingly.

Answer №2

To create a responsive layout with changing width on hover, you can utilize CSS3 flex. By hovering over the xBoys in the code snippet below, their width will adjust to 75%, with the other filling the remaining space. Please note that this effect is supported only in modern browsers.

You simply need to set the width for .moverBoy using the flex-basis property. For example, assigning flex: 1 1 75% will change its width to 75% and automatically adjust the size of .smartBoy. The properties include automatic grow and shrink, where 1 indicates true.

Snippet:

.parent {
    width: 100px; height: 20px;
    border: 1px solid gray;
    display: flex;
    flex-direction: columns;
}
.parent > div {
    flex: 1 1 0%;
    transition: all 500ms;
}
.parent > div:hover {
    flex: 1 1 75%;
}
.moverBoy { background-color: #f00; }
.smartBoy { background-color: #00f; }
<div class="parent">
   <div class="moverBoy"></div>
   <div class="smartBoy"></div>
 </div>

Answer №3

Initially, there is a natural whitespace between "mover" and "smart" since they are on separate lines. To avoid this, you can place them on the same line or include comments like this:

<div class="parent">
   <div class="moverBoy"></div><!--
   --><div class="smartBoy"></div>
</div>

As for the css aspect, you have the option to use Calc function:

.moverBoy {
    width:20px;
}
.smartBoy {
    width: calc(100% - 20px);
}

Here is a demonstration of it in action: http://jsfiddle.net/083pnubk/

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

I am interested in discovering the optimal method for loading a vehicle to its maximum capacity by exploring various combinations

My current task involves developing an algorithm to optimize the filling of a vehicle to its capacity based on various input combinations. Although the core problem has been resolved, the algorithm's efficiency is inadequate when dealing with a higher ...

Having trouble running Ajax with JavaScript on a Wamp server

Exploring the realm of ajax, I embarked on a journey guided by a YouTube tutorial to construct a basic food search application. The concept was simple - users would input the name of a food item, and if available, its name would be displayed below. On the ...

CSS:Tips for removing borders with border-radius in CSS

I have a div that has been styled with a left-hand border, and it's causing me some trouble. You can view the styling here on Jsfiddle. Is there a way to remove just the left hand side border without affecting the rest of the design or creating an un ...

Using various designs on elements generated from ng-repeat

Is it possible to apply different styles to buttons created using ng-repeat by having b.color return a value from the btnValues object? Currently, it is not returning the value. If this is not possible, are there alternative methods to achieve this? < ...

Displaying the second div once the first div has loaded, then concealing the first div

Current Approach: There are two divs occupying the same space, with div1 set to display:block and div2 set to display:none When a tab is clicked, jQuery hides one div over a period of 2000ms and reveals the other div. Challenge: The goal is for the ...

Displaying images in Ionic from a JSON URL source

I am having trouble getting an image from a JSON to display on an Ionic card. Although I can see the JSON response in the console log, the image is not showing up on the card, leaving it blank. It seems like I'm making a mistake in the HTML code. Any ...

Angular 5 is throwing an error that says: "There is a TypeError and it cannot read the property 'nativeElement' because it

Being aware that I may not be the first to inquire about this issue, I find myself working on an Angular 5 application where I need to programmatically open an accordion. Everything seems to function as expected in stackblitz, but unfortunately, I am enco ...

Utilizing AJAX to load a WAV file

One of the tasks I'm working on involves a collection of audio files. When a file from this list is clicked, I want JavaScript to load and display the waveform of that specific audio file. The following function is responsible for drawing the wavefor ...

SailsJS - handling blueprint routes prior to configuration of routes

I am trying to configure a route in my config/routes.js file as shown below '*' : { controller: 'CustomRoutes', action: 'any', skipAssets:true } The CustomRoutes controller is responsible for handling custom routes. Th ...

Tips for retrieving return values from AJAX URL content

As I am writing some code to send data from one page to another through the ajax URL, I encountered an issue where the retrieved values on the previous page are showing as null. The first page code looks like this: <script> function myFunction() { ...

The fancybox's excess content is concealed

I've recently integrated fancybox 2 into my project and have encountered an issue when appending HTML content to the modal. I disabled scrolling, but now any overflowing content is being hidden. Could this be related to the max-height of the modal? Ho ...

There is a vast expanse separating the header of the page and the navigation bar

Hello! I'm trying to figure out why there is a huge space between the top of the page and the navbar on this site: . I've included the CSS and HTML below. Any help would be greatly appreciated, thank you! HTML: <!DOCTYPE html> <html> ...

Disabling a button following a POST request

Is there a way to prevent multiple clicks on a button after a post request is made? I want the button to be disabled as soon as it is clicked, before the post request is executed. Below is an example of my code where the button is supposed to be disabled w ...

Styling with CSS: Utilizing the Float Left Property to Anchor Elements

Currently, I have implemented an anchor element that utilizes a CSS class to replace the text with an image. Despite its unusual nature, the end result is mostly satisfactory. .toolLink a { overflow:hidden; } .toolEdit { float:left; ba ...

What is the reason for not allowing return statements in the ternary operator?

Imagine you have a basic form and you want to determine if the form has been modified. If it has changed, you want to submit it; otherwise, you want to prevent form submission. To tackle this, instead of using an if-else statement, I decided to go for a te ...

Refresh Twitter Bootstrap Tooltip after deactivating/removing

I have a quick question. I am currently dealing with data that is constantly changing and displayed from a selected item in a table. To monitor for overflow, I have implemented the following code: if (event.target.offsetWidth < event.target.scrollW ...

The type '{}' cannot be assigned to type 'IntrinsicAttributes & FieldsProp'. This error message is unclear and difficult to understand

"The error message "Type '{}' is not assignable to type 'IntrinsicAttributes & FieldsProp'.ts(2322)" is difficult to understand. When I encountered this typeerror" import { useState } from "react"; import { Card } fr ...

Tips for transferring express route handling to the subsequent middleware and bypassing the current block of code

Here's the current setup of my route handler in my express app: app.use('/', function(res, req, next) => { if (!authorised) next(); f1(); f2(); }); Is there a way to prevent f1() and f2() from running without adding conditional st ...

Struggling to make PayPal Cordova Plugin function properly in both production and sandbox modes

While using the cordova paypal plugin and switching to sandbox mode, I encountered an error. The plugin can be found at https://github.com/paypal/PayPal-Cordova-Plugin https://i.stack.imgur.com/lD2EH.png Running the plugin in PayPalEnvironmentNoNetwork m ...

Display a webpage once its design is completely loaded in Nuxt

I have implemented a layout for my admin pages that displays user information in a consistent format, eliminating the need to fetch this data every time the page reloads. However, I am facing an issue where the page does not wait for the layout to fully l ...