Tips for maintaining the size of a div container

Take a look at the following code snippet:

<head>
  <title></title>
  <style>
    .section_divs {
      background-color: lightblue;
      float: left;
    }
    
    #section_empty {
      background-color: tomato;
      width: 10%;
    }
    
    #section_words {
      width: 80%;
    }
  </style>
</head>

<body>
  <section>
    <div class="section_divs" id="section_empty">a</div>
    <div class="section_divs" id="section_words">a</div>
    <div class="section_divs" id="section_empty">a</div>
  </section>
</body>

The issue arises when I remove the letter 'a' inside the div tags from:

<div class="section_divs" id="section_empty">a</div>

to

<div class="section_divs" id="section_empty"></div>

In this scenario, the div disappears. I am puzzled as to why this happens and whether there is a way to maintain the dimensions of the div identical to the other divs?

Answer №1

Utilize flexbox instead of using floats. To achieve this, apply display: flex to the parent element and assign a default width of 10% to each child element. Additionally, create a special class for the center div with flex-grow: 1 to make it expand and fill up any remaining space.

.container {
  display: flex;
}

.item {
  height: 50px;
  width: 10%; /* default size */
  background: tomato;
}

.item-center {
  flex-grow: 1; /* middle div will grow and stretch */
  background: lightblue;
}
<div class="container">
  <div class="item">a</div>
  <div class="item item-center">a</div>
  <div class="item">a</div>
</div>

<br />

<div class="container">
  <div class="item">a</div>
  <div class="item item-center"></div>
  <div class="item">a</div>
</div>

Answer №2

When devoid of content, the default height is set to 0.

If there are elements present within, the height will automatically adapt to fit the content.

To maintain a specific height even when empty, utilize the min-height property.

 <head>
    <title></title>
    <style>
      .section_divs {
        background-color: lightblue;
        float: left;
        min-height: 18px;
      }
      
      #section_empty {
        background-color: tomato;
        width: 10%;
      }
      
      #section_words {
        width: 80%;
      }
    </style>
  </head>
  
  <body>
    <section>
      <div class="section_divs" id="section_empty"></div>
      <div class="section_divs" id="section_words">b</div>
      <div class="section_divs" id="section_empty">c</div>
    </section>
  </body>

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

The functionality of my website is not optimized for viewing on iPhones

While designing a responsive website that looks great on most devices, I noticed some issues with iPhones. The layout breaks in two specific ways on these devices. View Galaxy Screenshot View Sony Screenshot The menu doesn't scale properly on iPho ...

Send form data using ajax technology

When testing my code on localhost, everything runs smoothly. However, when I tried running it on the server, it doesn't seem to be functioning properly. $("#MyUploadForm").ajaxSubmit(options); I would greatly appreciate any assistance with t ...

Experiencing difficulty transmitting information from the view to the controller within CodeIgniter

I am currently creating a website using the CodeIgniter framework. One of the features on my site is a view that displays unverified members in a table. Each member has a button next to their name that can be clicked to verify their account. Below is the ...

How to interact with a C# List using JavaScript

Our internship project entails creating a business application using Unity WebGL. However, we're facing a challenge when it comes to retrieving a list from C# to populate a Select element on our webpage. We've successfully communicated and retrie ...

How come my footer is appearing at the top of my webpage?

I have encountered a problem where the footer is displaying on top of my page, despite not using any floats, wrappers, or grids in my code. The code snippet can be found below this text. Could someone assist me in identifying why this issue is occurring ...

Mastering jQuery UI: A beginner's guide to utilizing the date picker widget

This is my first venture into the world of HTML and JavaScript, so I am asking for your patience. I am trying to incorporate the Datepicker widget from jQuery UI, following the instructions on the Getting Started page. There seems to be an issue with lin ...

Selectize-dropdown menu shines brightly as it opens upwards

In my sleek dashboard design, I have implemented a few dropdown menus using selectizeInput. These menus are currently positioned at the bottom of the page, but I want them to open in an upward direction instead of downward. While I found a workaround for ...

Is it possible to modify the appearance of the element that was just clicked on?

Hello everyone, I'm currently working on a form with different inputs, all with the same class: <input type="text" name="username" class="field" /> <input type="text" name="email" class="field" /> I'm trying to figure out how to ch ...

In-depth preg_match_all analysis

I'm encountering an issue with my detailed preg_match_all not working as expected. Instead of retrieving the desired data, I am getting a blank Array. Below is the code snippet I'm struggling with: <?php $remote_search = file_get_content ...

Perfectly aligning the Update Progress Animation in real-time

I am currently utilizing the Ajax UpdateProgress with a loading gif attached. The issue I am facing is how to ensure that the loading.gif always appears in the center of the screen, even if the user is at the very top or bottom of the page. Is there a meth ...

Expanding the axes using Google Charts

Seeking to Expand Y-Axis I am attempting to stretch the Y-axis so that it counts up to 300 instead of just 100. The issue arises when I adjust the Y-axis to reach 300, as the values remain in the same position as they did when counting to 100. Fo ...

Choosing JavaScript

<select> <script type="text/javascript"> $.get( 'http://www.ufilme.ro/api/load/maron_online/470', function(data){ var mydata = new Array(); var i = 0; // индекс масси ...

CSS navigation bar (background gradient problem)

I have encountered an issue with my multi-tier navigation menu where tiers below tier 1 are displaying an unexpected block to the left of the options. I suspect this problem is related to the background gradient applied, but I haven't been able to fin ...

Minimizing Unused Space After Media Query Adjustment

Using the bootstrap grid system, I am facing some uncertainties. I would like to create a header where there is a logo on the left and links on the right. Below this header, I want a menu that spans the same width as the content above. In my current setu ...

working with html data in a bootstrap modal using javascript

Utilizing an id, I pass data to a bootstrap modal and link that id with stored data. $("#fruit").html($(this).data("fruit")); In addition, I am appending data to the bootstrap modal $('#mymodal').find('.modal-body').append('< ...

Is there a solution to move my navbar that is frozen halfway on the screen?

I'm in the process of developing a website and have implemented a sticky navbar using CSS. However, I've noticed that when I scroll down, the navbar appears to be "stuck" slightly further down the page (as shown in the second image). Can anyone p ...

.toggleClass malfunctioning inexplicably

I've been struggling to make a div box expand when clicked and revert to its original size when clicked again. It seems like a simple task, but no matter what I try, I can't seem to get it right. I'm not sure where I'm going wrong, as t ...

ensured maximum height of a div attached to the bottom

<body style="width: 100%; text-align: center;"> <div style="margin: 0 auto;">container <div style="width: 200px; margin-top: 100px; float: left;"> left </div> <div style="width: 728px; floa ...

Incorporating an image as a clickable element instead of a traditional button using HTML and

Here's the issue at hand: I currently have "+" and "-" icons at the end of each row to add and delete rows as needed. However, I would like to replace the "-" icon with a trashcan symbol instead. I attempted to do this by replacing it with an image s ...

Necessary Selection from Dropdown

I've created a contact form with both optional and required fields. One of the mandatory fields is a drop-down menu, which looks like this: <div> <select name="favFruit[]" size="1" required> <option value="apple">Apple&l ...