Employing flexbox to allow columns to shrink only when their width exceeds half of the container's size

Can you take a look at my drawing?

Here is the scenario I'm trying to achieve:
(1) I have a flexbox with two columns. If one box is bigger than half the width and there is enough space for both boxes, they should remain unchanged (or stretch proportionally).

(3) If one box is bigger than half the width but both boxes don't fit, then only shrink the larger box.

(4) If both boxes are bigger than half the width, both should shrink proportionally.

For a visual example, please visit: http://jsbin.com/upArEVI/3/edit

Here is the graphical representation based on the code example above:

And here is the code snippet:

body{
  width: 100%;
  padding:0;
  margin:0;
  background: white;
}
.line{
  display: flex;
  height: 50px;
  border-top:1px solid lightgray;
  border-bottom:1px solid lightgray;
}
.left, .right{
  flex: 0 1 auto;
  overflow:hidden;
  text-overflow: ellipsis;
  background: rgba(0,200,0,0.1);
  white-space: nowrap;
  border-right:2px solid black;
}
.right{
  text-align: right;
  flex: 1 1 auto;
  background: rgba(0,0,200,0.1);
}
.middle{
  position: absolute;
  top:0;
  width:50%;
  height: 100%;
  border-right:2px dotted red;
}

Displayed with HTML:

<div class="line">
  <div class="left">
    something 2 something 3
  </div>
  <div class="right">
    something 5
  </div>
</div>

Answer №1

Take a look at this potential solution:

* {
  padding:0;
  margin:0;
}
.line{
  display: flex;
  height: 50px;
  border-top:1px solid lightgray;
  border-bottom:1px solid lightgray;
}
.left, .right{
  flex: 1 auto;
  text-overflow: ellipsis;
  background: rgba(0,200,0,0.1);
  border-left:1px solid lightgray;
  border-right:1px solid lightgray;
  overflow: hidden;
  white-space: nowrap;
}
.right{
  text-align: right;
  background: rgba(0,0,200,0.1);
}
<div class="line">
  <div class="left">
    something 2 something 3 something 4
  </div>
  <div class="right">
    something 5 something 5 something 6
  </div>
</div>
<div class="line">
  <div class="left">
    something 2 something 3 something 4 something 4 
  </div>
  <div class="right">
    something 5 something 5 something 6
  </div>
</div>
<div class="line">
  <div class="left">
    something 2 something 3 something 4 something 4 something 4  
  </div>
  <div class="right">
    something 5 something 5 something 6
  </div>
</div>

Check out this link to see a live demonstration.

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

I am encountering some problems with the Drop Down navigation feature on my Bootstrap website. Does anyone know how to properly center the drop down menu?

Hey there! So, I've been trying to fix the wonky drop-down menu on my website, but everything I've attempted so far hasn't worked. I've tinkered with float, margin, and a bunch of other CSS techniques, but no luck. Someone suggested usi ...

How can you avoid an abrupt transition when using `ng-hide` for animations?

I've been working on an accordion animation using ng-hide based on the panels height. However, I've noticed a slight jump when the ng-hide is applied. (click on the first title to show and hide) Can anyone offer assistance in resolving this issu ...

The height of adjacent divs should be uniform, resize seamlessly, and contain an absolutely positioned element within

Is there a way to make these neighboring divs the same height, responsive to browser window resizing, and with icons positioned on the corners? This method using display:table-cell works in most browsers but fails in Firefox due to a bug that causes the ic ...

Is there a way to eliminate the blue formatting on my phone?

I've encountered a styling issue on my website when viewing it on mobile, specifically with links on iPhone. I've already attempted the solutions mentioned in this discussion: How do I remove the blue styling of telephone numbers on iPhone/iOS? ...

What is the best way to include a padding at the top of the second row?

I have two rows with three columns each. I am trying to create some spacing between the two rows. I attempted to achieve this by wrapping the rows and adding padding to the last child (which should be the second row). However, this approach did not work a ...

Continuously tapping on overlapping slides

I encountered an issue where the slide that I set the opacity to 0 on is still clickable, despite setting pointer events to none. In my slideshow, consisting of 2 slides, clicking on the first slide redirects me to the hyperlink of the second slide. Chec ...

Choose every fourth row in the table

Is there a way to alternate the background colors of selected groups of 4 rows in a table? I want to make one group red and the next group blue. Any suggestions or ideas on how to achieve this? <table> <tr style="background-color: red;"> ...

What could be the possible reason for the controls in my element getting disabled due to adding a JavaScript background

First and foremost, I want to share a link with you to a page that I am currently developing so you can better understand the situation: Additionally, here is a link to the background effect: https://github.com/jnicol/particleground If you visit the page ...

CSS/jQuery animation alignment problem

Exploring an animation using CSS transitions and jQuery has been my recent project. The concept involves presenting the user with clickable divs to load a new page. When a div is clicked, it expands to cover the entire screen and transition to the next pag ...

Is there still excess top padding or margin in Firefox after clearing the default reset for the <ul> element?

My floated horizontal list is looking great on IE and Opera, but for some reason, Firefox is adding extra padding or margin at the top. I'm not sure how to fix it. Everything was fine until I had to add a display:inline to an image link next to it to ...

Tips for positioning a 404 message at the center of a webpage

Struggling with centering a 404 error message on your Bootstrap 5 page without messing up the footer layout? When the viewport is small, the 404 message may end up getting covered by the footer. Feel free to check out the code snippet in full view to see ...

Tips for selecting a secondary or even tertiary ancestor in a Sass project

Check out this piece of HTML code: <body> <div> <li class="more">hello <ul class="hello"> <li>hello</li> <li>hello</li> ...

Struggling with finding the appropriate CSS selector?

I'm struggling to find the correct selector. Let me do my best to explain the situation: I am currently working on a project where I am unable to make changes to the HTML and JavaScript due to dynamic content and other constraints. Within this proj ...

The navigation bar and text subtly shift when displayed on various devices or when the window size is adjusted

Hello, I am brand new to web development and have encountered an issue on my website. Whenever the window is resized or viewed on a different screen, the navigation bar (which consists of rectangles and triangles) and text elements start moving around and ...

What is the best way to ensure that the search box automatically adjusts its position and size regardless of the screen resolution?

I'm currently working on a responsive website project and facing an issue with the absolute positioning of the search bar on the main page, which is crucial for me. Below is the code snippet: <div class="span4"> <form class="well form ...

Using CSS to align the position of a div with the last word position of an h4 element

Trying to figure out how to position a div at the end of an h4 text element has been challenging. When the h4 text is on a single line, it's easy to place the div correctly. However, things get complicated when the text spans multiple lines. In that c ...

Implementing a Popover Notification When Clicked

I'm a beginner at this. I came across an example of a popover message box in the link provided below. I attempted to implement it, but for some reason, it's not working. Could I be overlooking something? Alternatively, is there a simpler way to ...

What is the best way to align content in the middle of a containing div?

I am struggling with centering text and images inside a div that is 190px x 190px. Despite searching on various platforms, including SO and Google, I have not found a simple solution. Can someone please provide guidance on the easiest way to achieve verti ...

Verify if there are DOM elements located within a DIV container, execute the functions associated with those elements sequentially

I am in the process of creating a game using HTML, CSS, and JavaScript. My focus right now is on manipulating DOM elements without relying on the canvas tag. The goal is to develop a pseudo graphical programming language, similar to the environment provide ...

Content overflowing beyond the boundaries of the parent DIV in Internet Explorer 6

Take a look at the code snippet below: <div style="width: 50px; border: 1px solid green;"> <div style="position: absolute;"> add whatever text you'd like </div> </div> The display in modern browsers (such as I ...