Inline-block elements within other inline-block elements may not wrap correctly

Perfect Solution from Oriol: Oriol provided a great solution to my issue. Instead of using inline-block on both containers, he suggested floating the first container and leaving the second container with no styling. Additionally, he recommended using overflow:hidden on the main container and the second container to prevent any awkward wrapping outside the box.


The Initial Question

I'm currently designing a profile page with a responsive layout. The profile is enclosed in a fixed DIV at the bottom of the screen.

Check out the demo here: https://jsfiddle.net/ner8j6vz/

The profile comprises two container DIVs nested inside a main container:

  • Profile picture and UL with information (fixed width)
  • Menu with DIVs (should wrap as the container shrinks or expands)

The containers are displayed as inline-block with white-space:nowrap to prevent wrapping, but this has led to a new problem - the width of the second container doesn't match the main container, causing the menu DIVs not to wrap as desired.

What seemed like a simple task has proven challenging. I've experimented with floating and inline-block variations, and read numerous articles on managing float and inline-block behaviors. I suspect the issue lies in the static width of the first container versus the fluid width of the second container, but I'm unsure.

Here's the code snippet:

<div id="maincontainer">
    <div id="subcontainer1">
    </div>
    <div id="subcontainer2">
        <div class="menuelement"></div>
        <div class="menuelement"></div>
        <div class="menuelement"></div>
        <div class="menuelement"></div>
        <div class="menuelement"></div>
        <div class="menuelement"></div>
    </div>
</div>

#maincontainer
{
    height:150px;
    display:block;
    vertical-align:top;
    white-space:nowrap;
}
#subcontainer1
{
    width:100px;
    height:100%;
    display:inline-block;
    border:2px solid rgb(240,90,40);
}
#subcontainer2
{
    height:100%;
    display:inline-block;
    vertical-align:top;
}
.menuelement
{
    display:inline-block;
    width:50px;
    height:50px;
    background-color:rgb(240,90,40);
}

Answer №1

If you want to prevent #subcontainer2 from overflowing #maincontainer, using floats is a good option. This will keep #subcontainer2 as a block element and adjust its width accordingly.

#maincontainer {
  height: 150px;
  display: block;
  vertical-align: top;
}
#subcontainer1 {
  width: 100px;
  height: 100%; /* consider `calc(100% - 3px)`,
                   or `box-sizing: border-box`  */
  float: left;
  border: 2px solid rgb(240, 90, 40);
}
#maincontainer, #subcontainer2 {
  overflow: hidden; /* clear float */
}
.menuelement {
  display: inline-block;
  width: 50px;
  height: 50px;
  background-color: rgb(240, 90, 40);
}
<div id="maincontainer">
  <div id="subcontainer1"></div>
  <div id="subcontainer2">
    <div class="menuelement"></div>
    <div class="menuelement"></div>
    <div class="menuelement"></div>
    <div class="menuelement"></div>
    <div class="menuelement"></div>
    <div class="menuelement"></div>
  </div>
</div>

Answer №2

Perhaps this is the solution you're seeking? Utilizing float properties may be the best approach. Visit this JSFiddle link for a demonstration.

#maincontainer {
overflow:hidden;
width:80%;
height:158px;
}
#subcontainer1 {
float:left;
margin-right:5px;
width:100px;
height:154px;
border:2px solid rgb(240, 90, 40);
}
#subcontainer2 {
height:100%;
vertical-align:top;
}
.menuelement {
display:inline-block;
width:50px;
height:50px;
background-color:rgb(240, 90, 40);
}

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

Develop a custom Dockerfile for hosting a Python HTTP server in order to showcase an HTML file

I have a folder containing a single HTML file (named index.html) with basic text. When I execute the python command: python -m SimpleHTTPServer 7000 a server is initiated at port 7000 in the same directory to display the page in a web browser. Now, I am ...

Having trouble with your jQuery slideDown menu?

I am facing the exact same issue I had last week. Here is an example of my HTML code: <article class="tickets"> <div class="grid_12 left"> <div class="header"> <i class="ico ...

The HTML Bootstrap collapse feature is not functioning correctly upon the initial button press

Could you assist me with implementing a bootstrap and javascript collapse feature? I have two collapsible cards. The first card should be visible initially, but then switch to the second card when clicked on a link. However, upon the first click, both card ...

The script fails to function on elements contained within an infinite scrolling feature

I am facing an issue with my code after implementing infinite scroll. The like script seems to be broken as the page gets refreshed when I try to like a post. Here is the Like-btn script: <script> $(document).ready(function(){ ...

What causes certain properties of html and body elements to behave in this way?

I'm on a quest for understanding some mysteries. First riddle: Why does the body have a mysterious margin-top? html { height: 100%; background-color: red; } body { height: 100%; margin: 0; background-color: yellow; } <h1>Hello P ...

How can we apply position: fixed in print media queries using CSS?

In order to display a footer on every printed page, we have implemented the use of position: fixed. However, one issue that has arisen is that content ends up flowing underneath the footer. Is there a solution to prevent this from happening while still k ...

Retrieve information from the input field and then, upon clicking the submit button, display the data in the

When a user inputs their name, email, subject, text, and telephone number, I want to send an email with that data. The email will be sent to a specific email address, such as [email protected]. This process is crucial for a hotel website, where the em ...

Inheriting the viewBox attribute with SvgIcon

I have a collection of unique custom SVGs, each with its own distinct viewBox. First Custom SVG <svg viewBox="-4 -4 24 24"><path something/></svg> Second Custom SVG <svg viewBox="-5 -7 24 24"><path something ...

Displaying local time alongside global time using PHP

I have a challenge - I am storing time in DATETIME format and now I need to display it in the local timezone. Does anyone know how to achieve this using PHP? ...

Tips for eliminating extra blank space under the footer on an HTML website

Just beginning my journey with bootstrap and I am encountering an issue on my website where there is space after the footer when resizing to a mobile size. I've tried setting margin: 0; padding: 0; but it hasn't resolved the problem. Here's ...

Transfer multiple files by dragging and dropping them into a single file upload option

I was experimenting with adding a drag and drop feature to my website. After trying several scripts, I came across one on CodePen. However, the script allows for the upload of multiple files, while I only need to upload a single PDF or .doc file. Can someo ...

When the JS function 'postMessage()' is invoked on an HTML object tag, what specific action does it perform?

Not too long ago, I found myself on a quest to figure out how to trigger the print function on a PDF that I was displaying in Adobe AIR. With a bit of guidance from a helpful individual and by utilizing postMessage, I successfully tackled this issue: // H ...

Filtering input that is compatible with Firefox

Attempting to restrict certain special characters (excluding "_" and "-") from being used in a text input field has been a bit of a challenge. I initially tried using regex filtering by pattern, but it didn't work as expected. Then I turned to dynami ...

Sending you to a new page and popping up an alert

I have set up an HTML form for user registration and after a successful registration, I want users to be redirected back to my index.html page with an alert confirming their successful registration. Currently, the alert is working but it opens in a blank ...

What is the best way to use Javascript in order to automatically open a designated tab within SharePoint 2013?

Currently, I am in the process of developing a website project which incorporates Bootstrap tabs utilizing jQuery. While these tabs are functioning excellently on various pages, I am faced with the challenge of linking specific icons to corresponding tabs ...

Experience a glint in the React Suspense with React Router - Struggling with CSS properties post utilizing Suspense and Lazy

I'm experiencing an issue with my code where the CSS properties are not working properly when I wrap the code in suspense. The page loads and the suspense functions as expected, but the styling is not being applied. However, if I remove the suspense, ...

Creating Event Handlers for corresponding elements in HTML with the help of JQuery and JavaScript

Struggling with HTML and debugging an issue in my ASP.NET Core App. The problem lies in a CSHTML view that functions as a timeclock system for tracking user input against job numbers. The current Index.cshtml is operational, verifying JobNumbers against t ...

In AngularJs, I am unable to prevent my ui-sref anchor tag from being triggered using event.preventDefault()

Here is the code snippet I am working with: <a ui-sref="MyModule">My Module</a> When the link is clicked, the following function will be triggered: $scope.$on("$locationChangeStart", function (event) { if (!confirm('You have un ...

jQuery UI dynamically adjusting content layout based on browser size changes

Having an issue with my custom accordion script where resizing the browser causes formatting problems in one of the sections. I want the content to remain intact and to utilize a scrollbar if necessary to view the content. The problem is visible in section ...

Struggling to get collapsible feature functioning on website

I'm trying to create a collapsible DIV, and I found a solution on Stack Overflow. However, I'm having trouble getting it to work on my website. I created a fiddle with the full code, and it works there. But when I implement it on my site, the con ...