When the right div is empty, the left div's width increases to 100%

Is there a way to adjust the width of two div elements when floated left so that if the right div is empty, the left div automatically expands to 100% width?

if ($('.wrap .box2').is(':empty')) {
  $('.box2').hide();
  $('.wrap .box1').css({
    "width": "100%"
  });
}
.wrap {
  width: 100%;
}

.wrap .box1 {
  width: 50%;
  float: left;
  background: red;
}

.wrap .box2 {
  width: 50%;
  float: left;
  background: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
  <div class="box1">
    <h1>Hello 1</h1>
  </div>
  <div class="box2">
    <h1>Hello 2</h1>
  </div>
</div>

In summary, I am looking for a way to make the width of the left div 100% when the right div does not have text inside its h1 element.

Answer №1

if ($('.wrap .box2 h1').is(':empty')) {
  $('.box2').hide();
  $('.wrap .box1').css({
    "width": "100%"
  });
}
.wrap {
  width: 100%;
}

.wrap .box1 {
  width: 50%;
  float: left;
  background: red;
}

.wrap .box2 {
  width: 50%;
  float: left;
  background: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
  <div class="box1">
    <h1>Hello 1</h1>
  </div>
  <div class="box2">
    <h1></h1>
  </div>
</div>

I executed the above code and observed that the left div occupied the entire space.

Answer №2

Give this a try:

.container {
  width: 100%;
  display: flex;
}

.container .item1 {
  width: 50%;
  background: blue;
  flex-grow: 1;
}

.container .item2 {
  width: 50%;
  background: orange;
}
.container .item2:empty {
  width: 0;
}

This solution utilizes flex-boxes and the :empty selector, eliminating the need for javascript/jquery.

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

Displaying the getJSON output only once, then having it automatically update at regular intervals without any repetitive results

I previously had an issue resolved on stackoverflow, but the requirements of my project have changed. Therefore, I am in need of a new solution. In summary, I have a getJSON function that runs every 5 seconds to check for changes in a JSON file. The proble ...

Should I consolidate my ajax requests into groups, or should I send each request individually?

Currently working on an ajax project and feeling a bit confused. I have 3 functions that send data to the server, each with its own specific job. Wondering if it's more efficient to group all the ajax requests from these functions into one big reques ...

Is it possible to apply several 'where' condition in pg-promise?

I am currently using pg-promise in combination with express 4 on node 8.2.0. I am trying to concatenate the where condition. However, I have been unable to find a way to achieve this. Can you please assist me? This is what I am aiming for. let ids = [10 ...

Having trouble with Lerna bootstrap? You might be running into the dreaded npm error code E401

Every time I run Lerna bootstrap on Jenkins, it fails with an error, but it works fine on my local machine. npm ERR! code E401 npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager" Package.json in the main folder ...

Angular and Node.js Integration: Compiling Files into a Single File

I have a question, is it possible to include multiple require js files in one file? If so, how can I create objects from them? Calling 'new AllPages.OnePage()' doesn't seem to work. To provide some context, I'm looking for something sim ...

Utilize AJAX, jQuery, and Symfony2 to showcase array information in a visually appealing table format

I have a requirement to showcase data utilizing ajax and jQuery in a table on my twig file. The ajax call is made with a post request to the controller, where the controller attempts to input several rows from a csv file into the database. However, there ...

Include an HTML header and footer on every page when printing an HTML document

I designed an HTML page with a header, content, and footer. When I attempted to print the page, it spanned across 2 pages with the header on the first page and the footer on the last page. My goal is to have the header and footer display on every page, si ...

Processing the results from a recursive function call in Node.js

I have an objects array that needs to be processed with delayed calls to an external server, then collect all results into an array for postprocessing. Here is an example: (async () => { const serverOperation = () => new Promise(resolve => setT ...

Designing a dynamic and interactive floating navigation bar

I've been experimenting with creating a floating menu using SMINT. The idea is to have the menu stay visible as you scroll down the page. The issue I'm facing is that the current menu structure uses divs. I would like to switch to using li and ...

block the UI when a certain amount of time has passed

When I click on a button, I implement the following code: $(document).ready(function () { $('.find').click(function () { $.blockUI({ message: '<table><tr><td><img src="images/pleas ...

When the jQuery handler is triggered, Ajax fails to invoke the server-side function

I am in the process of creating a basic forum where I have a user details page containing two text fields - one for the user's biography and another for their interests. Upon clicking the save icon, a jQuery handler is supposed to initiate an ajax cal ...

Issue with border styling in Material-UI vertical ToggleButtonGroup

In my current front-end project, I have implemented the React Material UI ToggleButtonGroup. For a Multiple Selection example with vertical styling, you can refer to this code snippet: https://codesandbox.io/s/eyk66?file=/demo.js However, I encountered a ...

The function is not being executed when using $scope.$apply()

I am in need of a customized click directive that can execute the passed code using scope.$apply(). $(elem).on('click', function(){ scope.$apply(attrs.wdClick); }); Everything works smoothly when I pass something like wd-click="something = ...

The absence of the import no longer causes the build to fail

Recently, after an update to the yup dependency in my create react-app project, I noticed that it stopped launching errors for invalid imports. Previously, I would receive the error "module filename has no exported member X" when running react-scripts buil ...

Tips for performing a redirect in JavaScript/ReactJS without storing the redirection in the cache

In my TypeScript ReactJS SSR App, I have the following logic: public login() { let error: boolean = false const isLoggedIn: boolean = doSomeLogicHereLikeBackendCheck() if(isLoggedIn) { window.location.href = "/home" // this is getting c ...

Guide to building a basic Table View for iOS with the help of HTML, Twitter Bootstrap, jQuery Mobile and more

I have experience with Objective-C, but I am still learning HTML/jQuery/JS. My goal is to create a Table view using these languages. Can anyone provide assistance by guiding me on how to achieve this? I was able to create a static Table view using the co ...

`Troubleshooting Issue: Autocomplete feature in Jquery Codeigniter not functioning

Having an issue with autocomplete in my codeigniter project. When I input text into the field, a dropdown appears but no values are shown. It looks like this: Screenshot The error displayed in the console is: Screenshoot Below is the relevant code: Mode ...

Converting HTML and CSS into a PDF using JavaScript

I am on the lookout for libraries that can cater to my specific needs, as I haven't found one that fits perfectly yet. My setup involves Node.js on Express.js for the backend, and html/css/js for the front-end. Browser support includes IE8 and up, chr ...

Is it better to have Angular and Laravel as separate applications or should Laravel serve the Angular app?

I have a new project in the works, aiming to create a large SAAS application. Although I come from a CakePHP background, I've decided to use Laravel and Angular for this venture. As I navigate through unfamiliar territory with these technologies, I a ...

Difficulty arises with z-index when hover effects are applied to clip-path shapes

I'm encountering an issue where I am attempting to make two clip path polygons overlap each other when hovered over by the mouse. I have been using z-index values and trying to adjust them based on which overlay is being hovered over, but unfortunatel ...