What is the method for calculating the 100% width of a flex child?

Adjusting the width of the .sidebar to 100% seems to make it smaller in size.

Interestingly, removing the body's font-size: 1.3rem and toggling the .sidebar's width: 100% results in a slightly larger appearance.

It is expected that setting the font-size to 1.3rem should maintain the same ratio for the horizontal width of .primary-content as the .sidebar, assuming no wrapping occurs.

I am unsure how flexbox interprets the width: 100% property.

body {
  font-size: 1.3rem;
}

h1 {
  margin-top: 0;
}

.container {
  width: 80%;
  max-width: 1100px;
  margin: 0 auto;
}

.row {
  display: flex;
}

.primary-content {
  background-color: moccasin;
}

.sidebar {
  /* width: 100%; */
  padding: 1em;
  text-align: center;
  color: #fff;
  background-color: #136c72;
}
<main class="main container row">
  <section class="primary-content">
    <h2>Quality designs made custom, on demand, just for you</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore
      et dolore magna aliqua. Ut enim ad minim veniam.</p>
  </section>
  <aside class="sidebar">
    <h2>Cheap</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
  </aside>
</main>

For further exploration, please refer to this CodePen link.

https://codepen.io/Fullchee/pen/OJMBovq

Answer №1

In this scenario, the crucial factor to consider is the initial width of the elements. To better grasp this concept, let's explore a simplified example:

.box {
  display: flex;
  width: 50%;
  border: 2px solid red;
  margin: auto;
}

.box>div {
  border: 1px solid green;
}
<div class="box">
  <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nulla nisi, accumsan vel purus nec, pretium dictum ex. Suspendisse pellentesque velit eget turpis porttitor efficitur
  </div>
  <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nulla nisi, accumsan vel purus nec, pretium dictum ex. Suspendisse pellentesque velit eget turpis porttitor efficitur
  </div>
</div>


<div class="box">
  <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nulla nisi, accumsan vel purus nec, pretium dictum ex. Suspendisse pellentesque velit eget turpis porttitor efficitur
  </div>
  <div style="width:100%;">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris nulla nisi, accumsan vel purus nec, pretium dictum ex. Suspendisse pellentesque velit eget turpis porttitor efficitur
  </div>
</div>

It becomes evident that in the second case, one element appears smaller due to reduced width, which makes sense.

Note that both elements contain the same content and necessitate wrapping within each due to limited space available.

If we decrease the content length, the outcome changes:

.box {
  display: flex;
  width: 50%;
  border: 2px solid red;
  margin: auto;
}

.box>div {
  border: 1px solid green;
}
<div class="box">
  <div>
    Lorem ipsum dolor sit amet, 
  </div>
  <div>
    Lorem ipsum dolor sit amet, 
  </div>
</div>


<div class="box">
  <div>
    Lorem ipsum dolor sit amet,
  </div>
  <div style="width:100%;">
    Lorem ipsum dolor sit amet,
  </div>
</div>

To comprehend both scenarios, understanding the flexbox algorithm summarized in three points is essential:

  1. The initial width setting for each element
  2. If total width exceeds container width, both elements shrink
  3. Shrink factor calculation involves negative free space (total width - container width) and individual element widths

The key lies in point (1).

Omitting width:100% yields these results for (1)

$('.box div').each(function() {
  console.log($(this).width());
})
.box {
  display: flex;
  width: 50%;
  border: 2px solid red;
  margin: auto;
}

.box>div {
  border: 1px solid green;
  flex-shrink:0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="box">
  <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
  </div>
  <div>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...
  </div>
</div>

...

For expanding an element's width, adjusting the first element to shrink more can be a viable solution:

body {
  font-size: 1.3rem;
}

h1 {
  margin-top: 0;
}

.container {
  width: 80%;
  max-width: 1100px;
  margin: 0 auto;
}

.row {
  display: flex;
}

.primary-content {
  background-color: moccasin;
}

.sidebar {
  padding: 1em;
  text-align: center;
  color: #fff;
  background-color: #136c72;
}
<main class="main container row">
  <section class="primary-content">
    <h2>Quality designs made custom, on demand, just for you</h2>
    <p>Lorem ipsum dolor sit amet, ...</p>
  </section>
  <aside class="sidebar">
    <h2>Cheap</h2>
    <p>Lorem ipsum dolor sit amet, ...</p>
  </aside>
</main>

<main class="main container row">
  <section class="primary-content" style="flex-shrink:1.2;">
    <h2>Quality designs made custom, on demand, just for you</h2>
    <p>Lorem ipsum dolor sit amet, ...</p>
  </section>
  <aside class="sidebar">
    <h2>Cheap</h2>
    <p>Lorem ipsum dolor sit amet, ...</p>
  </aside>
</main>

Alternatively, setting flex-shrink values for both elements, with a higher value for the first one, can achieve desired width adjustment:

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

Implementing event handling with .On() in Jquery following .Off()

I need assistance with my responsive navigation bar. I am having trouble with the jQuery code to disable hover events if the width is less than or equal to 768px and enable it for desktop screens. $(window).on('load resize', function (e) { v ...

Increasing the width of a line to fill the entire space using CSS properties

Seeking alternative ideas to replace my current solution using only CSS for one of the problems. I already have a working JS solution, but I'm interested in exploring a CSS-only approach. https://i.sstatic.net/PQ7er.png A) All elements here have the ...

Retrieving text from a draggable div using jQuery

I have a draggable div that I can move over another element with the class .outerDiv which contains text content. Is there a way for me to retrieve the text from .outerDiv that overlaps with the draggable div? $(".outerDiv .isStore").draggable({ contain ...

Controlling multiple bx-sliders with a single pager using only JavaScript

Is there a way to control 3 sliders using the same pager? I attempted to use the following code but it did not work: <script language="javascript"> $('.mobile_left_mble,.mobile_right_mble,.text_btn').bxSlider({ //pagerCustom: '#bx- ...

Include a new row in the form that contains textareas using PHP

I'm trying to add a new row to my form, but I'm facing challenges. When I click the add button, nothing happens. If I change the tag to , then I am able to add a row, but it looks messy and doesn't seem correct to me. Here is my JavaScript ...

What is the maximum file size that the data link is able to store?

For instance, a file like an image, video, or sound can be saved in the data link Take an image for example, it may be stored with the initial link: data:image/jpeg;base64,/..... followed by various characters. But, is there a specified size limit at whic ...

Tips on adding space between a material icon and its corresponding description

I have a design that needs some adjustments. The material icon is too close to the description, and I attempted to create padding by adding the following CSS: .location-secondary-info span:first-child { padding-right: 5px; } However, this change did no ...

Uploading large files on Vuejs causes the browser to crash

Whenever I try to upload a file using my browser (Chrome), everything goes smoothly for files with a size of 30mb. However, if I attempt to upload a file that is 150mb in size, the browser crashes. The server's maximum upload size has been configured ...

I'd love some clarity on the concept of stacking contexts

After encountering a bug with a non-clickable video player inside a jquery ui dialog, I resolved the issue by changing position:relative; to position:inherit; Other solutions included removing position:relative; altogether or adjusting the z-index of the ...

What impact does setting a variable equal to itself within a Dom Object have?

Within my code example, I encountered an issue with image sources and hrefs in a HTML String named tinymceToHTML. When downloading this html String, the paths were set incorrectly. The original image sources appeared as "/file/:id" in the String. However, ...

Using Reactjs Material UI Table component results in grid expansion

I'm facing an issue with my Table component nested inside a Grid component. Whenever the Table component is rendered, it causes the Grid component to expand in width. I want the Grid component to maintain its original width when the Table component is ...

Aligning the stars with CSS

One of the components I have deals with a star rating system, and I thought it would be cool to use Font Awesome icons for half stars. Everything is working well except for the CSS styling aspect. While I managed to position some of the stars correctly by ...

PHP generated timetable displaying improperly formatted HTML cells

Having some trouble with my HTML table for a school timetable. This is what I want: Required Timetable. But instead, I'm getting something like this: Incorrect Timetable Here's the code snippet: // days of week array $days = array( 1 => &ap ...

Inconsistencies with Colorbox behavior in an external HTML document

Encountering a strange issue with colorbox while attempting to display a "div" element from another HTML file. The code functions perfectly, except for the final "div" in the external file. The structure of my external file is as follows; div1 img par ...

What could be the reason for the hover class not functioning properly in Tailwind?

Looking for some help with styling a button in Next.js using Tailwind. Specifically, I want the background color of the button to change when hovered over. I've been experimenting with code like this: <button className="rounded-full bg-gradie ...

Dynamic sizing for sublists

I have come across a nested list structure that looks like this: <ul id="nav"> <li id="" class=""> <a href="http://site.com/wedding.html" class="">Wedding</a> <div class="flyoutWrapper" style="display:block;"> ...

Sending HTML output to a PHP script

After setting up my HTML form with an action attribute pointing to "filename.php" and a method of post, I managed to generate JSON output by clicking the Submit button using the following code: $('#result').text(JSON.stringify($('form' ...

When you hover over the vertical sidebar menu in Bootstrap material design, a submenu will appear

Looking to create a three-column display that functions as one submenu when hovering over each menu item? <div class="container"> <div class="row"> <div class="col-sm-4"> <ul class="vertical-nav"> <li>&l ...

Turn off the scrollbar without losing the ability to scroll

Struggling with disabling the HTML scrollbar while keeping the scrolling ability and preserving the scrollbar of a text area. Check out my code here I attempted to use this CSS: html {overflow:hidden;} Although it partially worked, I'm not complete ...

Utilizing CSS in JS for nested hover styles with Material UI: a step-by-step guide

I am currently utilizing Material UI and in the process of converting regular CSS classes into a JavaScript file. .nav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; } .navItem { float: left; flex: 1; } .navLin ...