CSS inline-block problem - Either everything stays or everything collapses

I am facing a specific scenario where I have three inline-elements. Two of them are essentially the same, but the one in the middle has an unknown width.

I want to achieve one of two things:

  • Have all three elements display inline on the same row
  • If the width is too small, stack all three elements on top of each other

Using the display: inline-element property doesn't quite meet my requirements, as it will collapse when needed and might not align properly according to the desired conditions.

    .div-a {
      background-color: red;
    }
    
    .div-b {
      background-color: blue;
    }
    
    div {
      display: inline-block;
      font-size: 4em;
    }
    <div class="div-a">
     L
    </div>
    <div class="div-b">
     Mid
    </div>
    <div class="div-a">
     R
    </div>

Is there a way to ensure that either all blocks are stacked or displayed inline? Keep in mind that the width of the middle element will always be unknown.

EDIT: I'm not sure why this was flagged as a duplicate, as my question is not solely about using media query resizing as a solution.

Answer №1

The positioning of your elements will vary based on the size of your viewport and the width of the elements themselves. When you include an inline element in a div, all divs will be treated as inline elements unless you specify otherwise with a new display property for a specific div. To ensure they are stacked properly, you can use the display flex property.

For more guidance on this topic, please check out the following article: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Answer №2

utilize the power of display flex to keep elements in a single row and dynamically manage the width of the middle element

<div class="a">
<div class="div-a">
L
</div>
<div class="div-b">
Midghghghghchcghgdc
</div>
<div class="div-a">
 R
</div>
</div>

styling with CSS

    .a {
  display:flex;
}
.div-a {
  background-color: red;
}

.div-b {
  background-color: blue;
}

div {
  display: inline-block;
  font-size: 2em;
}

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

Parent div not properly adjusting its height

I am currently working on developing a fluid layout, however I have encountered a minor issue with the height of the container. The outer <div> (highlighted in yellow, ip_A-1) is not adjusting its height according to its child elements. You can view ...

Tips for connecting a DOM element when a link is clicked

I am currently browsing http://localhost:8000/index.html My goal is to navigate to localhost:8006/someAddress and interact with an element with the id '157579' as well as click on a button with the id '1787' that will trigger a change ...

Using Javascript to dynamically enable or disable an input field depending on the choice made in another field

I attempted to implement the solution provided in this answer for my code: How do I disable input field when certain select list value is picked but unfortunately, it is not working as expected. The scenario involves an HTML select field with the ID &apos ...

Include a clickable jQuery icon in an HTML document for testing purposes

Is there a way to include an icon [+] at the end of this statement, so that when clicked it triggers an alert? <div id="catalog" style="width: 518px;"> <ul id="catalog111" style="left: -37px;top: -140px;width=500%;"> < ...

What is the best method for utilizing an autocomplete plugin to update a div element?

I am seeking assistance regarding implementing an autocomplete feature in a DIV that displays the names, images and ages of other users as they are typed into a field positioned above the DIV. I want this autocomplete functionality to mimic Facebook' ...

Arrange the bootstrap columns in a grid layout both vertically and horizontally adjacent to one another

I need help with displaying my columns in this specific layout https://i.stack.imgur.com/2hzju.jpg Code <div class="row"> <div id="id-orders-stats" class="col-md-6 col-sm-6"> </div> <div class="col-md-6 col-sm-6 stats-box"&g ...

Is it possible to conceal the :value within a datalist while still being able to send the value using the POST method?

I have developed an Editable combobox where users can "Search", "Select", or "Edit/Insert" new values. To achieve this functionality, I am utilizing Input and Datalist elements. If you want to see the implementation, you can check it out on jsfiddle: htt ...

What is the process of closing a dropdown in Vue 3 JS when clicking on any part of the page?

I am currently working with Vue 3 JS and Tailwind CSS. My dropdown functionality is working correctly, but I want the dropdown to close when clicking anywhere on the page. Initially, Tailwind guided me towards using Popovers and PopoverButtons, which cau ...

Creating HTML or PHP pages using jQuery

Is it possible to generate a file using jQuery? I am creating a website maker. On the left side, I have a list of elements such as: ---------------------------------------------------------------- Elements Attri ...

What is the best way to show <input type='text'> without generating a textbox using jQuery?

When a user enters a comment in the text box and hits enter, it is automatically displayed in the 'comment section'. Upon hitting submit, the following code is executed: var comment = $("#commentBox").val(); var commentSection = $("#commentSecti ...

What is the best way to align the Burger menu with the logo on the same row and have the menu dropdown open below the header?

Is there a way to align the Burger menu with the logo and have the menu dropdown open below the header? I attempted to position the burger menu next to the logo, but it didn't turn out the way I thought it would. Coding in html and css is still new t ...

One particular page is having trouble loading CSS, whereas it loads perfectly fine on a different page

I have two pages, 403.html and 404.html, both of which need to load the same style.css file. Strangely, when I visit the 403.html page, the CSS loads perfectly fine. However, when I navigate to the 404.html page, even though it has identical code with only ...

No matter how many times I click the next button, I am unable to proceed to the next page

Trying to create a form for an insurance company using basic HTML, CSS & JQuery. However, encountering an issue where I cannot proceed past the first page after clicking the Next button, and therefore unable to view the remaining 2 fieldsets. Can someone h ...

How to Align <ul> to the Right in Bootstrap 4

Looking to align a list to the right in the header section, here is the code snippet: <div class="collapse navbar-collapse" id="app-header-nav"> <ul class="navbar-nav mr-auto mt-2 mt-lg-0"> <li class="nav-item"> <a class=" ...

Restrict the input to only allow for parentheses, disallowing any letters or numerical characters

Only parentheses are allowed in the input field; letters and numbers will not be accepted. function checkBrackets() { var inputVal = document.getElementById("input").value; var result = document.getElementById("strong").value; console.log(inputVal, ...

Is it possible to issue commands within JavaFX-hosted Javascript?

I'm currently working on developing a JavaFX Application that assists me in creating JavaScript scripts. The application consists of a raw text Pane on the left, and an HTML Pane on the right which displays the rendered HTML content from the left Pane ...

Design of website built on Bootstrap framework seems distorted on Firefox browser

I built this website with the help of Bootstrap and you can view it at js-projects. Everything looks great when I open it in Chrome or Safari, but there are weird white strips that resemble scroll bars when I view it in Firefox. Can someone assist me in ...

Preventing Text Truncation in THREE.js Texture Created from 2D Canvas

Many people tend to use the 2D canvas texture method for creating text billboards, sprites, and overlays in THREE.js scenes instead of relying on imported image files. You can check out an example by Lee Stemkoski here. However, I have noticed that when a ...

Android not showing scroll bar in ion-scroll feature

I am experiencing an issue where a div with a fixed height displays a scroll bar on browsers but not on an Android phone. <ion-scroll style="height:300px;" scrollbar-y='true'> //content </ion-scroll> ...

Attempting to preserve the CSS transform effect as I switch between menu options

Whenever I stop hovering over my element, it reverts back to its original position. However, I want it to remain in place when I am navigating through the "sousmenu" menu and only return to its original position when I am not hovering over the "sousmenu". ...