Style sheets for two dynamically-sized boxes side by side

There are two boxes or columns positioned on the same line.

Both have varying widths that need to remain on the same row.

To clarify:

.----------container 570px-----------.
|[box1]                        [box2]|  Ideal scenario
|                                    |
|[box1box1box1box1box1box1box1][box2]|  Desirable setup
|                                    |
|[box1box1box1box1box1box1box1]      |  However, if [box1] and [box2] are too wide,
|              [box2box2box2box2box2]|  [box2] will move to a new line
|                                    |
|[box1box1b...][box2box2box2box2box2]|  I want [box1] to be truncated with ellipsis
|____________________________________|

Is it possible to achieve this using only CSS? Alternatively, I could calculate the width of box2 in JavaScript and then adjust the width of box1.
If there is a CSS solution compatible with IE9, that would be fantastic.

Answer №1

While options such as SASS enable the use of variables within stylesheets, performing calculations in CSS is not feasible. One workaround could be to define a max-width for each div to guarantee proper spacing allocation.

Answer №2

To update elements in a single row, designate them as table-cell:

Sample Code:

<div class=horizontal>
  <div>First</div>
  <div>Second</div>
</div>

CSS Style:

.horizontal > * {
  display:table-cell;
}

Check out this demonstration:

http://jsfiddle.net/QYq2R/

Answer №3

Unfortunately, CSS does not support dynamic styling based on the size of content, so you would need to use some clever jQuery to achieve this. Here's how I would approach it:

function AdjustWidths() {
    var TotalWidth = $('#Box1').outerWidth() + $('#Box2').outerWidth();
    var ContainerWidth = $('#Container').innerWidth();

    if (TotalWidth > ContainerWidth) {
        // Reduce the width of Box1 to fit within Container
        $('#Box1').outerWidth(ContainerWidth - $('#Box2').outerWidth());
    }
}

To add the ellipsis, you can further reduce the width of Box1 by a few pixels and then programmatically insert the ellipsis (...).

Edit: Upon re-reading your question, I see that you have already implemented this solution. I'll keep the code here for reference.

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

Issue with Flask: Data sent via render_template not being rendered in HTML

I have a query set up to access my database, create an object, and then pass it to my HTML using render_template. The problem I'm facing is that the information is not being displayed on the HTML page. Relevant code: @app.route('/profile', ...

Tips on configuring a hidden input field to validate a form

I am currently attempting to create a blind input field using PHP that will validate if the input field is empty. If it's not empty, the message set up to be sent will not send. However, I have encountered several issues with the placement and wording ...

Steps for creating a resizable and automatically hiding side menu

I am working on a side menu that can be resized, and I want it to collapse (set to zero width) when it is empty. I have considered implementing one of these features individually, but I'm not sure how to make both work together. Is there a way to ad ...

Adjust the date input alignment in mobile browsers

I am trying to align the entered date to the left in input[type='date']. However, when testing on mobile browsers like safari and chrome, the date remains centered within the input. mobile example This is the code I have been working on: .co ...

Height of the div dynamically increases upwards

Just a quick question - is there a way to make position: fixed divs at the bottom of an element (such as the body) grow upwards as content is dynamically added? Maybe something like anchor: bottom or increase: up? I'm thinking that using JavaScript m ...

Ways to adjust the dimensions of a division element using Bootstrap

I have a CSS class called "brands" and I am looking to change the size of the thumbnail div within this brand class. How can I go about doing this? <div class="brands"> <div class="row"> <?php foreach ($item['brands'] ...

Is there a way to retrieve the modal's viewport height in Angular?

Is it possible to determine the viewport height of my ng bootstrap modal within my Angular application? Here is what I currently have: I have a modal with CSS styling as shown below: .modal-xxl { width: 95% !important; max-height: 90% !important; ...

Ways to eliminate the Vuetify append-icon from the sequential keyboard navigation

In my Vue.js application using Vuetify, I have implemented a series of password fields using v-text-field with an append-icon to toggle text visibility. Here is the code snippet: <v-text-field v-model="password" :append-icon="show1 ? 'mdi-eye& ...

Having trouble with Python Selenium CSS Selector? If your element is not visible, it

My goal is to search for all hyperlinks on a webpage that contain an XML download link and download them in a continuous loop. When I click on these hyperlinks, a form pops up that needs to be completed before I can proceed with the download. However, I ...

using javascript to dynamically color rows in a table based on the value in the status field

Hi, I'm new to javascript and seeking help for my table coloring issue. I have a dynamic table where data is inserted through an Ajax call. The goal is to color specific records based on their status. I attempted a solution but only one record gets c ...

"Obtaining Google locations within a modal box - a step-by-step

Having trouble with my Google Places search integration in Angular 2. <input type="text" type="text" class="form-control" placeholder="Address" [ngClass]="{active:signupSubmitted && !signUpForm.controls['formatted_address'].valid}" [ ...

JavaScript Birthday Verification: Regular Expression Pattern

I am currently testing out JavaScript form validation. The information provided is not being sent to any database, it's just for format testing purposes. All the regex checks are functioning correctly apart from birth. It seems like there may be an i ...

Rapache and R team up for dynamic leaflet design

I'm currently working on creating a webpage using Rapache and "Leaflet for R". My main goal is to integrate R into an html page using brew. However, I am facing difficulties in displaying my map within the html page without having to save it as an ext ...

List organized in two columns utilizing the data-* attribute

Is it possible to display a list in a two-column format based on a data attribute of each item? <ul> <li data-list="general_results">general text1</li> <li data-list="general_results">general text2</li> <li dat ...

Is there a way to use JQuery to make one button trigger distinct actions in two different divs?

For instance: Press a button - one div flies off the screen while another div flies in. Press the button again - Same div flies out, other div returns. I'm completely new to Javascript/JQuery so any assistance would be highly appreciated! Thank you ...

Python web scraping is unable to access span elements

Attempting to extract data from the provided link: The goal is to determine the industry group using a checkbox, but encountering difficulty accessing the span element in the code snippet below: <td><table border="0" summary="Table with single C ...

Having trouble with the rendering of the Stripe Element Quickstart example

Currently, I am diving into the world of Stripe's Element Quickstart. Take a look at this fiddle that I have been working on. It seems to be quite different from the example provided. Although I have included the file, I can't seem to figure out ...

With *ngFor in Angular, radio buttons are designed so that only one can be selected

My goal is to create a questionnaire form with various questions and multiple choice options using radio buttons. All the questions and options are stored in a json file. To display these questions and options, I am utilizing nested ngFor loops to differ ...

Leveraging PHP functions for form validation

Previously, the code worked flawlessly until I integrated it into functions. Now, I am struggling to get this form to function correctly with the functions I created. It seems that passing variables and implementing the main logic are key steps, but I&apos ...

"Encountering difficulties in dynamically populating a dropdown menu with ng-options in Angular

In my web application, I have a table displaying user information. Below the table, there is an input box where users can enter the row index. Based on this row index, I am dynamically populating an object using ng-options. Here is a snapshot of the HTML ...