Calculate the width of the outer division based on the chosen inner division

Looking for a solution related to an HTML segment?

<div class="outer" style="min-width: 150px;">
   <div class="inner1">
      <span>Lorem ipsum dolor sit amet</span> // Possibly long, truncate if needed
   <div class="inner2"/>
</div>

The outer div has a minimal width that can expand. The objective is for the outer div's width to grow with inner2, but not with inner1. In essence, the outer div's width should be dictated by inner2, regardless of inner1. For instance:

If inner2's width is less than 150px, inner1 will have a width of 150px.

If inner2's width exceeds 150px, inner1's width will match inner2's div.

Answer №1

To achieve your objective, JavaScript is required.

$(document).ready(function () {
    var $outer = $('.outer');
    var $inner1 = $('.inner1');
    var $inner2 = $('.inner2');
    
       
    if ($inner2.width() > 150) {
       $inner1.css('width', $inner2.width()+'px');
       $outer.css('width', $inner2.width()+'px');
    }
  else
    {
      $outer.css('width', '150px');
    }
});
.outer  {
  background-color: yellow;
}
.inner1 {
  display: inline-block;
  background-color: red;
}
.inner2 {
  display: inline-block;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer" style="min-width: 150px;">
   <div class="inner1">
      <span>Lorem ipsum dolor sit amet</span> // Can be very long, truncate as needed
  </div>
   <br />
  <div class="inner2">
  sadasda
</div>
</div>

I trust this guidance will assist you in reaching your goal :)

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

Experiencing a problem with adjusting the layout on a backdrop picture

https://i.sstatic.net/s6eD5.png Encountering issues depicted in the image while attempting to shift the form on the background image. Problems include the inability to change the width of textboxes, dropdown lists, and buttons. Below are the CSS and regis ...

Adjust the white background to match the gray background seamlessly

I'm in the process of developing a forum and encountering some challenges with CSS. My goal is to extend the white area all the way to the right edge of the gray background, leaving a gap of around 5px/10px. I've experimented with different CSS ...

Conceal the message using star symbols

I need help figuring out how to hide a certain type of string input from the user, and then use Angular data binding to display it in another component with part of the data masked with asterisks. I'm not very skilled in JavaScript, so I'm wonder ...

Intersecting table columns with a different data table

My task is to create a table with a column that contains another table, where I want the colors of each alternating row to be different. Please refer to the image below (ignore the "CharacterAgain" column). Below is an example of AngularJS code for the ta ...

Pandas now supports exporting data frames to HTML with the added feature of conditional

Is there a way to format a table in pandas where data in each column are styled based on their values, similar to conditional formatting in spreadsheet programs? An example scenario is highlighting significant values in a table: correlation p-value ...

Content towering over the footer

Can anyone help me figure out how to create a footer that appears behind the content when you scroll towards the end of a website? I've tried looking for tutorials online, but haven't found exactly what I'm looking for. Most of what I'v ...

Using jQuery, learn how to successfully call a selector from dynamic content

I am currently facing a challenge with a table that is generated server-side and then appended to the view page (client-side). Since the table is not directly included in the DOM, I am using the StickyTableHeaders jQuery plugin to create a sticky header fo ...

Steps to conceal a <select> element and reveal it by clicking a button

I'm currently working with Bootstrap 3 and I am attempting to replicate jQueryMobile's select option feature. This would involve replacing the standard select textbar and arrows with a single button that can toggle the opening of the select. Ins ...

Editable content <div>: Cursor position begins prior to the placeholder

Having an issue with a contenteditable div where the placeholder text starts behind the cursor instead of at the cursor position. Any suggestions on how to correct this? <div id="input_box" contenteditable="true" autofocus="autofocus" autocomplete="o ...

What is the process for setting dynamic variables as CSS content within Vue.js?

Forgive my lack of expertise, but I am interested in learning more about utilizing dynamic variables and CSS within Vue. I have a concept in mind where pressing a button would result in the letters of the button label spacing out further. Is it feasible t ...

New to CSS Media Queries? Discover the Correct Way to Define Element Height!

Sorry for the oversized images! I'm diving into media queries for the first time. When I adjust my browser window to 320px, here's how my site displays: This is what I want it to look like on mobile phones. However, when I view it on my iPhone, ...

The arrangement of my inline-block divs is oddly meandering in a zig-zag pattern

I am attempting to visualize basic commands and their arguments in HTML. Here is the expected output: https://i.stack.imgur.com/MTxbV.png However, the actual output looks like this: https://i.stack.imgur.com/biPFw.png This is the code I utilized: ht ...

Forcing the display of invalid-feedback in Bootstrap 4 is crucial

Suppose I am working with HTML code that looks like this: ... <div class="form-group"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="invalidCheck"> <label class="form-check-label" for ...

"Choosing the text within an <a> tag: A step-by

I am dealing with the a tag. <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4c7dbd9d1f4d9d5ddd89ad7dbd9">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Achieving vertical alignment in Bootstrap for card header

I'm troubleshooting a card layout on my Bootstrap page <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" cro ...

Using Javascript to access the index of an element that has been checked and

I am working on a dynamic feature that involves a JavaScript variable called 'options'. This variable is designed to store and update a string with the .innerHTML content of every checkbox checked by the user. For instance, if the user checks Ins ...

Alignment and stretching characteristics of three divs (one with a set height and two with uncertain heights)

Trying to find the right wording for this question was quite challenging due to the specific nature of the issue, so my apologies if it doesn't fully capture what I'm seeking. Just to clarify, I am specifically looking for a solution that is com ...

Limiting style changes to specific labels in Highcharts

Can someone assist me in bolding only the middle elements of a highcharts x axis? I have tried to apply a solution in the provided fiddle but it doesn't work as expected. Specifically, I am looking to bold the x-axis labels for June or July. http://j ...

Enable Component to Exceed to the Following Line

I'm having trouble getting a map of a component to move to the next line (as indicated by the yellow arrow). Currently, instead of moving below, it is squeezing the Component (the Cards). I've added background colors for clarity. Any advice would ...

Challenges related to height

Having an issue with height in my code. My footer is set up like this: <html> <body> <div class='wrap'> <div class=principal></div> <div class='clear'></div> <foo ...