Troubleshooting problems with CSS inline-block widths

In my website, I have a container div with two columns inside, both set as inline-block elements. The left column is where users make selections from a shopping catalogue, while the right column displays the output of those selections.

The problem arises when setting widths for each column based on percentages relative to the parent width. The left column has a fixed minimum width of 155px and takes up 20% of the parent width, while the right column occupies 79% of the width (with some room for variability).

However, when resizing the browser window, issues occur if the left column's width doesn't meet the required 79% of the screen size. As a result, the right column drops below the content of the left column.

For example:

HTML:

<div id="container">
    <div class="leftsideMenu">Menu column.</div>
    <div class="rightsideShop">Shop Contents</div>
</div> 

CSS:

#container{
    width:80%;
    min-width:555px; /* ensure 400px for shop contents */
}

.leftsideMenu {
    display:inline-block;
    width:20%;
    min-width:155px;
    vertical-align:top;
}

.rightsideShop{
    display:inline-block;
    width:79%;
    max-width:calc(100% - 156px) !important; /* attempt at fixing issue */
    vertical-align:top;
}

I've tried several solutions like using 'calc' function or floats and margins, but they haven't worked as expected. Flexbox might be the best solution, but it would require significant CSS changes that I was hoping to avoid.

After struggling with this issue and writing this out, I finally found a solution so sharing it here!

Answer №1

Oh my goodness, after numerous attempts and trying out different ideas, I finally figured it out!

It's interesting that my calculation works with:

 max-width:calc(100% - 160px); Giving a spare space of 5px . 

I'm not sure why this is the case, especially since the container div has standard width percentages and some padding in the product container inside the RHS. However, adding more "padding" space in the calc method ended up doing the trick.

But hey, it works now and I couldn't be happier. Maybe this solution will benefit someone else as well?

Answer №2

When using inline-block, you may notice some whitespace affecting the layout.

If you have two inline-block divs with a width of 400px within a container div of 800px, they might not always display next to each other as expected.

To avoid this issue, ensure that the closing tag of an element directly follows the opening tag of the next element in the HTML without any newlines or spaces (e.g. </div><div>).

Alternatively, you can set font-size: 0 on the containing element and then reset the font-size to something like 1rem for the inline-block elements for better control over spacing.

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

What steps can I take to update the image displayed in the Google search results for my website?

Check out the image I found on Google search Is it possible to replace the toothbrush image with a different one? Or even remove it completely? In addition, my favicon has been a different image for about a week. How can I update that in the search resul ...

Choose an element on a webpage by leveraging the power of Selenium

When working with Selenium in FireFox, I encountered an issue while trying to select specific fields on a webpage. The HTML pages I am referring to are available at the following links: Sample HTML Page, another sample page. The code snippet I used is sh ...

Smooth-scrolling feature with touch interaction available in the scrollbar extension

Anyone here aware of a high-quality jQuery or CSS plugin for scrollbars that supports touch functionality and smooth easing? I've been on the lookout for one, but haven't had any luck so far. Even if it comes with a price tag, I'm interested ...

The media query does not apply to other pages because they are not responsive

Currently experiencing an issue with fullpage.js on my single page website. The home page fits perfectly within the viewport, but for subsequent pages, the bottom portion is not visible on the same media query. ...

Is it possible to call a service within an NgFor loop in Ionic/Angular 2?

I am facing an issue with my ngFor directive. Here is the code snippet: <ion-card *ngFor="let review of reviews"> <ion-card-content> {{getUserName(review.user_ID)}} </ion-card-content> </ion-card> The challenge I a ...

Having trouble getting my soundboard to work properly on mobile devices

I recently created a soundboard using HTML5, CSS3, and JavaScript. While the audio plays back perfectly on my computer when I click the buttons, I encounter an issue when trying to use it on a smartphone. <!DOCTYPE html> <html lang="en"> <h ...

Issue with Mat-AutoComplete arising while utilizing a formArray

After adding a form array to account for multiple rows, I had to modify my definition from: shoppingCartList Observable<any[]>; to shoppingCartList: Observable<any[]>[] = [];. However, this change resulted in an error in my filter function whic ...

What causes an image's size to change when it floats within the parent container?

There seems to be a height mismatch issue between the container and the image inside it when the parent is floated, but not the child. However, when both are given the float property, the height matches perfectly. Why is this? <div class="parent">&l ...

Receive a notification when the div element stops scrolling

I am attempting to replicate Android's expandable toolbar within an Angular component. My HTML code appears as follows: <div (scroll)="someScroll($event)"> <div class="toolbar"></div> <div class="body"></div> </d ...

Receiving Null Value Upon Asynchronous API Call Before Data Retrieval

Struggling with Fetching and Displaying API Data in a Table I am facing a challenge where I need to fetch an API multiple times and populate the data into a table. The issue arises when the data for a specific year is not available, causing the table to b ...

Setting a defined width and using block display will not solve the issue of the margin:auto not working

rough sketch of my desired design I am trying to center a label on the page. The parent container is set to a width of 100% and the label itself is displayed as a block element with margin set to auto. When I set the width of the label to a smaller value, ...

Issues arose regarding the display of arrows in the Magnific Popup gallery

Everything with my Magnific Popup is functioning as desired, except for the fact that the arrows are not appearing in the gallery mode. What I see instead is two blank boxes with a thin border on top. After examining the CSS code, I am unable to locate a ...

Tips on implementing polymer.js to toggle the visibility of an element

On my HTML page, I have two different paper-card elements: <paper-card done> <paper-card> In addition, there is a button: <paper-icon-button icon="menu" on-tap="_show"></paper-icon-button> I am trying to utilize the _show fun ...

Steps for configuring UI-Router to open a new tab

I have a link on the homepage that directs users to a second page with two tabs. I want the second tab to be active by default so that users can see its content immediately. I tried using ui-sref, but it only takes me to the second page without activating ...

Tips for aligning the title to the left of text within a Bootstrap 4 card component

I have a situation where I need to adjust the layout of a card item in Bootstrap 4. Specifically, I want to move the title to the left of the text for screens that are not mobile. Is there a way to achieve this? Thank you! <div class="card flex-row fle ...

How to reduce the default width of Bootstrap carousel components

Click here for the 1st image. To view the 2nd image, click here. This represents the carousel code used on this particular website: <div class="row testing"> <div id="carouselExampleMen" class="carousel carousel-dark s ...

JavaScript - The left dropdown menu stubbornly remains visible when clicked in white space, unlike the right dropdown which disappears as expected. Puzzled by this inconsistency

Whenever I click on the selection criteria box, a dropdown menu appears. Clicking on the white space or the data table button makes the drop-down menu disappear, which is good. However, when I perform the same action for 'choose data table,' the ...

The click event in AngularJS is functioning properly, while the load event is not functioning correctly

As a newcomer to angularjs, I am attempting to run a function once a div has finished loading. app.js var app = angular.module('myApp',[]); app.directive('orientable', function () { return { link: function(scope, e ...

Integrating a fictitious style using jQuery

Having some trouble with this code snippet. The issue arises when trying to apply the following style using jQuery. CSS .arrow_box { position: absolute; width: 24px; border-radius: 30px 30px 3px 3px; height: 17px; float:left; } .arrow_box:after { bord ...

Leveraging the Power of Bootstrap 4 to Customize Color and Alignment within a Single Class

I am currently working with Bootstrap 4 and I have a specific requirement to align text in the middle and change its color to white using one class. Here is an example of what I tried: <p class="text-center white">Center aligned text.</p> How ...