Place two divs within a parent div that can adjust its width dynamically

Within this lengthy div, there are 4 elements arranged as follows:

icon       (fixed width)
item_name  [needs to be accommodated]
item_type  [needs to be accommodated]
item_date  (fixed width)

I am currently attempting to find a way to ensure that the item_name and item_type always fit inside the div regardless of resizing.

For reference, here is a jsfiddle.

HTML

<div class="item">
    <div class="icon"><i class="fa fa-file-text-o"></i></div> 
    <div class="item_date">7:25 AM</div>
    <div style="border-top:1px solid green;width:500px;">
        <div class="item_name">Item name</div>
        <div class="item_type">Item type</div>
    </div>
</div>

CSS

.item {
    margin:0 auto;
    height:50px;
    background-color:#fff;
    width:80%;
    box-shadow: 0 -1px 0 #e5e5e5, 0 0 2px rgba(0, 0, 0, .12), 0 2px 4px rgba(0, 0, 0, .24);
}
.item .icon {
    float:left;
    height:50px;
    width:75px;
    text-align:center;
    border-top:1px solid blue;
    font-size:23px;
    color:#252525;
    line-height:50px;
}
.item .item_date {
    float:right;
    text-align:right;
    width:100px;
    border-top:1px solid blue;
    height:50px;
    padding:0 25px;
    font-size: 13px;
    color:#999999;
    line-height:50px;
}
.item .item_name {
    float:left;
    display: inline-block;
    width:49%;
    height:50px;
    line-height:50px;
    border-top:1px solid purple;
}
.item .item_type {
    float:right;
    display: inline-block;
    width:49%;
    height:50px;
    line-height:50px;
    text-align:center;
    font-weight:bold;
    border-top:1px solid yellow;
}

Answer №1

To ensure that the two floated blocks are contained within the designated area, I suggest adding the CSS property overflow: auto to the div.item-panel element. By setting overflow: auto, you create a block-formatting context that confines the floats within the boundaries of the block, which is essential in this scenario.

.item {
    margin:0 auto;
    height:50px;
    background-color:#fff;
    width:80%;
    box-shadow: 0 -1px 0 #e5e5e5, 0 0 2px rgba(0, 0, 0, .12), 0 2px 4px rgba(0, 0, 0, .24);
}
.item .icon {
    float:left;
    height:50px;
    width:75px;
    text-align:center;
    border-top:1px solid blue;
    font-size:23px;
    color:#252525;
    line-height:50px;
}
.item .item_date {
    float:right;
    text-align:right;
    width:100px;
    border-top:1px solid blue;
    height:50px;
    padding:0 25px;
    font-size: 13px;
    color:#999999;
    line-height:50px;
}
.item-panel {
    border-top: 5px solid green;
    overflow: auto; 
}
.item .item_name {
    float:left;
    width:49%;
    height:50px;
    line-height:50px;
    border-top:1px solid purple;
}
.item .item_type {
    float:right;
    width:49%;
    height:50px;
    line-height:50px;
    text-align:center;
    font-weight:bold;
    border-top:1px solid yellow;
}
<div class="item">
    <div class="icon">X</div>
    <div class="item_date">7:25 AM</div>
    <div class="item-panel">
        <div class="item_name">First</div>
        <div class="item_type">Second</div>
    </div>
</div>

Answer №2

It appears that the width of item is set to 80%, and it includes icon (measuring 75px wide) and item_date (100px wide). Due to this setup, when the window size is reduced, there won't be enough space for item_name and item_type .

To prevent complete collapse and ensure adequate space for inner divs, consider adding a min-width to item:

.item {
    width: 80%;
    min-width: 400px;
 }

You may also need to define a min-width and/or margins for your inner divs in order to maintain their desired sizes.

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

Directing BeautifulSoup to a specific <tr> class

I'm currently working on a project where I need BeautifulSoup to extract the numbers located in the "Units Sold" column: from bs4 import BeautifulSoup from urllib import urlopen html = urlopen('http://www.the-numbers.com/home-market/dvd-sales/2 ...

Display a preview image at the conclusion of a YouTube video

I am currently working on an iOS device and have a Youtube video thumbnail that, when clicked, disappears and the video automatically plays in fullscreen mode using an iframe. It's working perfectly. Now, I would like to know how I can make the thumb ...

Create a unique design using two vertical sections with Bootstrap 5

I have been grappling with this issue for the past day, and I would greatly appreciate any assistance I can get! I am attempting to achieve a design similar to this using Bootstrap 5. Here is the goal I am aiming for While I have a good handle on creating ...

I am running into issues getting Tailwind CSS to work in my project. Despite following the installation instructions in the documentation and trying to learn this new CSS framework, it doesn't seem to

//I followed the instructions in the tailwind documentation to install and set up everything. However, when I try to use tailwind utility classes in my HTML file, they don't seem to work. Can someone please assist me with this issue? // Here is my sr ...

Using the onclick attribute as a unique identifier for a button

I am currently facing a challenge with a form that does not have an ID Here is the code snippet in question: <button class="btn btn-primary" onclick="showModal()" type="button">Browse Data</button> Unfortunately, I don't have any contro ...

Copying text from an iframe using HTML and JavaScript

As someone who is relatively new to web development, I am currently attempting to transfer text from an iframe to a textarea located on a Bootstrap-html webpage. You can view an example of the code I am working with here: https://jsfiddle.net/fe5ahoyw/ J ...

Dynamic Loading of CSS Styles

My style-sheet is saved in this unique location: /opt/lampp/htdocs/project/core/styles/Style.css To load the CSS file using the full directory, considering that the files utilizing it are situated here: /opt/lampp/htdocs/project/client/ The ultimate ob ...

Creating a platform where your choices determine the results is my goal for a new website

My goal is to create a unique website that generates text based on responses to multiple choice questions. For instance, users can select symptoms from a list of options and receive recommended medicine at the end of the process. I'm wondering where ...

The output when evaluating the xpath query "html/body/div/text()[1]" shows as [object Text], indicating there may be an issue with accessing the element's text using Selenium

My goal is to retrieve the value "479" from the given HTML snippet: <div data-testid="testid"> "479" " Miles Away" </div> In my Python Selenium script, the code I am using looks like this: xpath = 'html/b ...

Refreshing all parts of a webpage except for a single div

Currently, I am in the process of creating a simple web page that includes a small music player (niftyPlayer). The individuals I am developing this for request that the player be located in the footer and continue playing when users navigate to different s ...

The CSS stylesheet appears to be properly linked, however, it is not displaying correctly on the preview page

Here are the locations of my folders: index.ejs can be found inside Markdown Blog/views/articles This is where my css stylesheet is located: ../../css/styles.css Despite ctrl+clicking the path and successfully navigating to the css file, the styles do no ...

Tips for enclosing the "body" element using a background image

Hey there! I'm a newbie web developer with a casual approach to coding. I'm trying to jazz up my menu by adding a background image, but no matter what I do, it keeps showing the default white background. Can anyone lend me a hand? If you're ...

Initially, the 'display none' CSS command may not take effect the first time it is used

I am currently working on a slideshow application using jquery.transit. My goal is to hide a photo after its display animation by setting the properties of display, transform, and translate to 'none' value. Although the slideshow application work ...

Make a flexbox item scroll in the cross-axis once it aligns with the size of another element

In my search, I've come across several questions that are somewhat similar but none quite address the exact issue at hand. While this question on Stack Overflow bears some resemblance, I am specifically looking to utilize a flex element instead of a s ...

Establishing a connection between HTML and MySQL database

I've been researching how to create a login system on my HTML page. From what I've learned, it seems I need to use an intermediate script to allow my HTML access to the database. So, I've created a PHP page that verifies the username and pas ...

An unusual icon with three varying sizes in a single file

I have come across an interesting favicon file. Click here to download the favicon file Unfortunately, I am unable to upload this image file as it does not meet the required format standards. Upon viewing the file in Windows 7, a fascinating discovery w ...

How can I display a pre-existing div with a dropdown button?

I have individual div elements for each type of clothing item (shirt, pant, suit) that I want to display when the corresponding service is selected. This means that when I click on one of them, only that specific div will be shown. I am looking for a solut ...

Using CSS3, you can apply multiple backgrounds to an unordered list with one set

Currently, I am in the process of working on a unique Wordpress template. One of the challenges I am facing is figuring out how to incorporate three background images for each <li> element in the dynamically generated navigation menu. Here are two ex ...

Failure to send chat form

I have been developing a PHP chat application and everything is working well. However, I am looking to implement AJAX in order to prevent the page from refreshing. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></s ...

Learn how to display a "not found" message in a React.js application

I have a piece of code where I am sending a post request to an API and retrieving all the data from the API in a table. I am trying to find currency data based on the currency name, if found I display the data in a div, if not found I want to print "not ...