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

Strategies for avoiding a hover component from causing distortion to its parent component in React

I am encountering an issue with a hover component that is causing distortion in its parent component when displayed. Essentially, I need to prevent the hover component from affecting the layout of its container. Below is the code snippet: Styling for Lang ...

What is the best way to align 3 divs next to each other with spacing and borders?

I'm having trouble aligning three divs side by side. When I try to add borders and a small gap between each div, the third div ends up on a new line. Is there a way to automatically resize the divs so they fit together? HTML: <div class="trendi ...

I'm baffled by this unsemantic grid - why is the final div getting pushed below the rest?

I am working with the unsemantic fluid grid system and aiming to create a row of divs that are all in line and together cover 100% of the page. <div class="grid-container"> <div class="grid-parent yy name-strip zz"> <div class="yello ...

Stop HTML <dialog> from automatically closing using Vue

I'm working on a project where I need to use Vue to programmatically prevent an HTML dialog element from closing when the close event is triggered. Here's the code snippet I am currently using: import {ref} from 'vue'; const dialogTe ...

Utilizing JSON Data with JQuery: A Beginner's Guide

I am using a setTimeout function to reload another function every 5 seconds. The update_list function is responsible for rendering entrances in a view. However, when there are many entrances and you have scrolled down, the list empties and reloads every e ...

What is the method for embedding the creation date of a page within a paragraph on Wagtail?

Whenever a page is created in the admin panel of Wagtail, it automatically displays how much time has elapsed since its creation. https://i.stack.imgur.com/6InSV.png I am looking to include the timestamp of the page's creation within a paragraph in ...

Unable to change Bootstrap variables within a Next.js project

I'm having trouble changing the primary color in Bootstrap within my NextJS project. I've tried different methods but nothing seems to work as expected. Here is the code snippet from my "globals.scss" file that I imported in "_app.js": $primary: ...

Removing a background by linking a CSS file

Issue Resolved: I have found the solution. The problem was that the css document still contained html tags. Thank you to everyone who provided assistance. While designing a website using css, I encountered a situation where the site worked perfectly when ...

Obtaining solely the words found within HTML documents

In my Python 2.7 project, I have a folder containing multiple HTML pages that I need to extract only words from. My current process involves opening the HTML file, using the Beautiful Soup library to extract text, and then writing it to a new file. However ...

What is the best way to structure Vue.js components for optimal organization?

Imagine having an index page called index.vue consisting of various components like this: index.vue <template> <div> <component-1 /> <section class="section-1"> <div class="container section-container"> <com ...

AngularJS allows you to toggle the visibility of a div at set intervals, creating

I am struggling with the task of showing and hiding a div that serves as an alert for my application. Currently, I am using $interval to create a continuous show and hide action on the div. However, what I aim for is to have the DIV visible for X amount o ...

JavaScript must be able to detect when the checkbox value is reversed, which is dependent on the user-entered data

Hey there, I come across a situation where users are selecting a checkbox to insert or update a row of data in a MySQL database through SparkJava/ Java. The functionality is working fine except for a minor glitch. The issue arises when the checkbox behav ...

tag directive not functioning properly

I have encountered an issue while trying to create a simple custom directive. When I specify the directive as <div my-info></div> in my HTML file, it works fine. However, when I specify it as <my-info></my-info>, it does not work. ...

Retrieve the contents of the browser's DOM from within an Android operating system runtime

Imagine a typical login page similar to the one depicted below: https://i.stack.imgur.com/SF1Bd.jpg To interact with the HTML elements within the DOM, we can utilize either jQuery or conventional JavaScript like so: https://i.stack.imgur.com/e5aQZ.png ...

A guide on activating Effect in Div using just css

I have tried all the questions and answers related to this topic. Additionally, I attempted to solve related questions but was not successful. Please read my question thoroughly. What I Want: When I click on the click me Div, the second hiddendiv Div i ...

The IIS URL rewrite is causing issues with the rewriting of CSS and JS files

Struggling with my URL rewrites - every time I set up a rewrite for a page, it ends up affecting the CSS and JS files linked within the webpage, resulting in them not displaying properly. In an attempt to fix this issue, I tried using fully qualified path ...

Tips for toggling the visibility of a revolution slider based on current time using JavaScript

Currently, I am integrating the revolution slider into my WordPress website. My goal is to have the slider change according to the standard time of day. For instance, I want slider1 to display in the morning, slider2 at noon, slider3 in the evening, and sl ...

The left border is failing to appear on the Td element

When attempting to add border styling to td elements, the left border is not displaying correctly. This issue seems to vary across different browsers. Here is the HTML code: <table class="table-bordered"> <thead> <tr> ...

Issue with custom Javascript not executing in Internet Explorer versions 9 and 10

Initially, this script is found in the document's head section. <script> document.addEventListener("DOMContentLoaded", function () { var e, t = document.querySelectorAll("div.bounceInDown"); for (var n = 0, r = t.length; n & ...

Achieve a Smooth Transition: Utilizing Bootstrap 3 to Create an Alert Box that Fades In upon Click and Fades Out

Incorporating AngularJS and Bootstrap 3 into my web app, there is an "Update" button that saves user changes. Upon clicking the "Update" button, I aim to trigger and display bootstrap's alert box with the message "Information has been saved," followed ...