The flexbox container experiences overflow when accommodating flexbox items that contain block content

Issue

The presence of block content such as buttons is causing my flex items to expand and overflow the container.

Desired Outcome

In this particular case, I want the buttons to remain adjacent to each other with the button text truncated using an ellipsis. The widths of the flex items should be determined by their siblings rather than the content itself, ensuring that they stay within the container and adjust responsively as the container's size changes or flex items are added or removed. It's important to note that in my specific situation, using overflow:hidden on the flex items or their parent div is not a viable solution.

You can view the sample code on this fiddle link.

https://i.sstatic.net/kYm78.png

Sample HTML

<div class="container" style="width: 400px;">

  <div class="child one">
    Child One
    <br>Lorem ipsum
    <br>dolor sit amet
  </div>

  <div class="child two"><div><button class="text">Child Two with a loooooooooooooooooong naaaaaaaaaaaaaaaaaaaaaaaaaaaaaame</button><button>button two</button></div></div>

<div class="child three">Some Text</div></div>

Sample CSS

div {
  border: 3px solid;
}

.container {
  padding: 10px;
  background-color: yellow;
  display: -webkit-flex;
  display: flex;
}

.child {
  flex: 1 1 auto;
  padding: 10px;
  margin: 10px;
  background-color: #eee;
}

.child.one {
  color: green;
}

.child.two {
  flex: 6 1 auto;
  color: purple;
}

.child.two button {
  display: inline-block;
}

.child.three {
  color: blue;
}

.text {
  text-overflow: ellipsis;
  overflow: hidden;
}

UPDATE: Instead of displaying multiple buttons, I now only need to show one button. However, I would still appreciate it if someone could solve it with multiple buttons.

Answer №1

I have found the solution for those who are curious. I had to make some changes to my original requirements. Instead of attempting to display two buttons side-by-side, I simplified it to just one button. Here is the updated answer that seems to work well on different browsers.

View fiddle example

https://i.sstatic.net/l8d8E.png

Apply the following styles:

.child {
    width: 1%;
}

.child.button {
    width: 100%;
}

I am curious if someone can explain the behavior of the width percentage. It appears to behave more like how min-width functions.

EDIT: A big thank you to Michael_B for sharing a helpful explanation on the width relationship in flex items.

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

When I switch to a different navigation system, the css class gets removed

Let me clarify what I am looking for: Upon entering the index.php page, LINK_1 button is active. When I switch to LINK_2, it becomes active. I have only one index.php page where I include parts of external pages using PHP. Page_1 https://i.sstatic.net/Yv ...

When trying to click the button in Navbar.jsx, I encounter an error when attempting to invoke the setShowCart(true) function

I've encountered an issue while trying to call the setShowCart(true) function in Navbar.jsx. I'm unsure of how to fix this. import React from 'react' import Link from 'next/link'; import {AiOutlineShopping} from 'react-ic ...

Which is better: CSS gradients or background images?

Years ago, I came across many CSS codes that used background-image: url(xxx.png) to mimic a gradient background filter, as only (please correct me if I'm mistaken) IE's proprietary filter could achieve a CSS gradient. In today's world - giv ...

Get rid of the <h1> tag surrounding the Wordpress logo

Currently, I am utilizing an Ultimatum child theme that was not created or selected by me. Upon inspection, I discovered that the logo is enclosed in an <h1> tag. For the sake of SEO, I require a different, more optimized logo as it seems the curre ...

PHP form - The issue with validation not functioning properly

I'm having trouble validating a simple PHP form with HTML code and it's not working as expected. Here is the HTML code: <form action="expresscontactform.php" method="post" name="form1" id="form1"> <label>Name </label>< ...

Is there a way to switch the main image by clicking on different thumbnails in a sidebar using javascript/jQuery

Currently on my page, I have a large plot created with jqplot along with a sidebar containing several smaller plots. I am trying to find a way to dynamically change the large plot based on which of the smaller plots the user clicks on, without needing to ...

Fundamental CSS - the technique of layering a DIV with a semi-transparent DIV on top

Having trouble getting this to display correctly in my Chrome browser. I have a wrapper containing all the HTML elements, and I want to create a DIV (let's call it div-1) that includes an image with an overlay div positioned to the left, as shown in t ...

My HTML components are not showing alert messages at the top as expected

My application's alert message is supposed to stay on top of all other HTML elements as the page scrolls. However, it seems to be going behind certain components instead of staying on top like it should. This issue is puzzling because all components u ...

Newcomers: Introduction to PHP password security

As someone who is new to PHP password hashing and coding, I recently created a simple login form that requires a username and password. After successfully submitting the form, the password is stored in a database by using $_POST['password']. Howe ...

Creating a WordPress widget that links to a local file

Hey there, I've been attempting to utilize the text widget in order to link to a local file, but unfortunately, it's not working as expected, and I'm unsure of the reason why: Here is what I am using: <a href="file:///D:/" target="_blan ...

What are the steps to implementing a grid image system in Bootstrap4?

How can I use Bootstrap4 and CSS3 to position picture 4 and 5 under picture 2 and 3 within a grid layout? <main> <div class="container-fluid"> <div class="row"> <h1 class="col-12">Anonymous& ...

Tips for changing the font size of labels in a tree view using Material UI

While working with material ui treeview, I have customized the fontSize for each treeItem as shown below: <TreeItem key={mainCategory.name} nodeId={mainCategory.name} label={mainCategory.name} disabled={ ...

What's the best way to fix a div to the bottom left corner of the screen?

I have explored various solutions regarding this issue, but none of them seem to meet my specific requirements. What I am trying to achieve is the correct positioning of a div as depicted in the green area in the image. https://i.sstatic.net/O9OMi.png Th ...

Adjust the focus on the Angular JS input element

I have a student database with an input text element. When the page loads, I want a student to be highlighted with the input text focused. I created a dynamic class that will set the focus if it is true. If I apply it to a static class, it works fine. How ...

manipulating dropdown visibility with javascript

I'm feeling a bit lost on how to structure this code. Here's what I need: I have 5 dropdown boxes, with the first one being constant and the rest hidden initially. Depending on the option chosen in the first dropdown, I want to display the corres ...

Positioning two images at the bottom left and bottom right corners of a container

I have successfully implemented a justified block of text within the footer of my Bootstrap HTML page. Now, I want to enhance it by adding two images underneath the text. These images should have the same height but different widths and be aligned to eithe ...

Hover effect for Bootstrap cards

I am working on a view where I display dynamically expanding cards upon hover. Below is the CSS code snippet: .card-deck .card { height:200px; width: 200px; transition: transform .1s; } .card-deck .card:hover { -ms-transform: scale(1. ...

Graphic background integrated into form input

Is it advisable to create colored shapes in Illustrator, export them as SVG files, and then embed input tags or textareas within these shapes to allow users to enter text? Are there alternative methods we could use for this functionality? https://i.sstat ...

Issues creating bar graphs using HTML

I am currently working on creating a script that can display statistics from a database in the form of a bar chart. My idea is to have two colored bars stacked on top of each other, representing different values simultaneously - for example, the number of ...

Adding a custom stylesheet to a particular table within an HTML document

Is it possible to apply an external CSS stylesheet to only one table out of several on the same page? ...