Padding has an impact on the widths of flexbox items

Take a look at this code snippet: http://jsfiddle.net/56qwuz6o/3/

<div style="display:flex">
        <div id="a">a</div>
        <div id="b">b</div>
        <div id="c">c</div>
    </div>

    div div {
        flex: 1 1 0;
        border:1px solid black;
        box-sizing: border-box;
    }

    #a {
        padding-right: 50px;
    }
    

In this scenario, setting padding on a flex item (#a) alters its width in the 'border-box' model. How can I make its computed width not consider the padding? Essentially, I want each box to occupy 33% of the document width.

Update: Thank you for the responses so far. In actuality, there are additional boxes in the row with fixed widths. For example, check out this updated snippet: http://jsfiddle.net/56qwuz6o/7/. Here, I aim for #a, #b, and #c to all have identical widths.

<div style="display:flex; width: 400px">
        <div id="a">a</div>
        <div id="b">b</div>
        <div id="c">c</div>
        <div id="d">d</div>
    </div>

    div div {
        flex: 1 1 33%;
        border:1px solid black;
        box-sizing: border-box;
    }

    #a {
        padding-right: 100px;
    }

    #d {
        flex-basis: 200px;
        width: 200px;
    }
    

Answer №1

Using the correct `flex: declaration appears to be successful.

div div {

  flex: 0 0 33%; 
  /* don't grow, don't shrink, be 33% wide */
  /* flex:1 0 33% works too */
  
  border: 1px solid black;
  box-sizing: border-box;
  }

#a {

  padding-right: 100px;

}
<div style="display:flex">
  <div id="a">a</div>
  <div id="b">b</div>
  <div id="c">c</div>
</div>

Answer №2

One possibility is to define flex-basis: 33.33% or a value proportional to the number of children you are working with. I am uncertain if there is a method to achieve this dynamically.

Answer №3

When addressing the issue more broadly, it seems that padding, border, and margin are all fixed deductions from the total available width before the remaining space is divided within flex-boxes. This means that boxes with more padding will effectively gain more space. This can be best illustrated by having two flex boxes stacked vertically, one with a double column:

(the table as it should be. Bad things applio below ↓)

https://i.sstatic.net/Vfo2N.png

→ Visit Codepen for demonstration.

section
  display: flex
  width: 90%

  div   
    flex: 1 1 100%
    background: #aca
    // good to illustrate boundaries, since it has no width effect…
    box-shadow: inset 0 2px 6px rgba(black, .4)
    
  .double
    flex: 2 1 200%

Additional Notes:

auto set as flex-basis (the third parameter in flex:) is not recommended as it adjusts based on actual content. The amount of content should not shift your layout. Use 100% instead, even if it results in a cumulative percentage exceeding 100.

Any of the following actions will disrupt the alignment of the grid mentioned above:

border: 8px solid orange
padding-left: 8px
margin-left: 8px
  

Neither value for box-sizing will resolve this issue.

→For optimal padding of flex boxes within a grid, use an inner tag with full width and padding.

Answer №4

Speed up your development process with Flexbox Styles

<style>
  .flex-row {
    display: flex;
    display: -webkit-flex;
    flex-direction: row;
    -webkit-flex-direction: row;
  }

  .flexSize-1 {
    flex: 1;
    -webkit-flex: 1;
  }
  .flexSize-2 {
    flex: 2;
    -webkit-flex: 2;
  }
</style>

By keeping the flex property consistent for each column, you ensure they are all equal size across the window.

<div class="flex-row">
    <div class="flexSize-1">
      <h1 class="page-header">Test</h1>
    </div>
    <div class="flexSize-1">
      <h1 class="page-header">Test</h1>
    </div>
    <div class="flexSize-1">
      <h1 class="page-header">Test</h1>
    </div>
  </div>

If you need one column to be larger than another, simply assign the class .flexSize-2 for a double width compared to .flexSize-1.

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

Aligning elements next to each other in html / css without using any top margins

How can I arrange four blocks of text side-by-side, center them over a background image, and ensure they respond well on different screen sizes without using tables? I typically use tables for this layout, but I've heard that CSS/HTML5 can achieve th ...

Ways to differentiate between buttons in a button cluster?

I have created a set of 3 buttons using the code from this resource. However, the buttons are currently positioned vertically close to each other as shown in the source code. I would like to adjust the spacing between them. Is there a way to achieve this ...

When scrolling the page, the Circle Mouse Follow feature will dynamically move with your cursor

Hey everyone! I'm currently working on implementing a mouse follow effect, but I'm facing an issue where the circle I've created moves along with the page when scrolling, instead of staying fixed to the mouse. Any suggestions or tips would b ...

Difficulty altering link hover background color

I'm having trouble changing the hover effect background-color on the tweets of this page: Despite my efforts, all the links except for the latest tweets are working perfectly. What am I missing? Here's what I've been trying... <script& ...

Interactive jQuery Dropdown Menu with Multiple Divs

I have been working on this and I'm almost there, but the jQuery part is driving me crazy (I'm not very proficient in it yet, but I am learning). Here's the link to the fiddle: http://jsfiddle.net/5CRA5/4/ Since I can't demonstrate th ...

Node.js has no trouble loading HTML files, however, it seems to encounter issues when trying to

Having a bit of trouble with my JavaScript skills as I try to load my index.html file (seems like it should be the 5th easiest thing to do in JavaScript). Let me get straight to the point; when I manually open my index.html, it loads perfectly WITH the CS ...

Ways to combine multiple CSS styles for an element using .css()

I'm struggling to apply styles to a deeply nested element using jQuery and JavaScript. I understand how to style nested elements, but I'm having trouble targeting a specific deeply nested element. My website is built on WordPress, and it has ins ...

Unusual CSS rendering hiccup

Using jQuery, I am manipulating the display of an <a> element. Depending on certain keypress events, it adds or removes a class from an <input> element (which controls the display) that is related as a sibling to the mentioned <a>. The i ...

Arranging two classes in close proximity

I need help aligning two classes that are stacked on top of each other to be displayed side by side. Below is a screenshot of the two classes: https://i.sstatic.net/V0tLL.png The right class is .testm_boxes1, and the left class is .testm_boxes2. Here is ...

Javascript dynamically generates CSS animations

I need assistance creating a div with a CSS3 animated timer inside it when a button is clicked. Here is the code I have been working on: http://jsfiddle.net/arcco96/y5nov6rz/1/. Unfortunately, the div is not being created as expected. I believe the code sh ...

Begin expanding new circles in jQuery from the exact location where the previous ones ended

A captivating circle animation unfolds before your eyes, featuring dark blue circles that materialize at random points and gradually expand. As these expanding circles cover more than half of the screen, their color shifts to a lighter shade of blue. Exper ...

Jssor Slider: Captions briefly overlap just as the slideshow begins to play

I am currently working on a slideshow with 3 caption-only slides. Here is the code snippet: <div id="sliderHeader_container" style="position: relative; width: 965px; height: 85px;"> <!-- Slides Container --> <div u="slides" style=" ...

Leveraging SignalR in conjunction with jQuery to dynamically alter the CSS style of a span

Utilizing SignalR to dynamically update a span's color based on real-time data source. The connection is established successfully, but encountering difficulties with updating the css classes on the span. The issue arises when attempting to update mul ...

Is there a way to remove the initial number entered on a calculator's display in order to prevent the second number from being added onto the first one?

I am currently in the process of developing a calculator using HTML, CSS, and JavaScript. However, I have encountered an issue with my code. After a user inputs a number and then clicks on an operator, the operator remains highlighted until the user inputs ...

Encountering a 404 error while loading CSS in a Django application

I'm currently in the process of developing a chatbot application. I'm encountering issues with loading the CSS for the frontend using django's static data load feature. Here are the file paths: CSS path: C:\Documents\Django_Works ...

The Angular Material autocomplete panel does not disappear when autocomplete is hidden within a scrollable region

https://i.sstatic.net/0eS4M.gif Having encountered a bug, I have raised an issue for it (https://github.com/angular/components/issues/20209). I am reaching out to inquire if there exists any workaround or solution known to anyone. You can observe the pro ...

How to prevent the animation from triggering when changing the theme

Currently, I am developing a game that is similar to the concept of . In my game, I have implemented both dark and light themes along with animations that are triggered when the API sends a response (such as flipping a div and changing the background col ...

Struggling to make changes to a Styled Component in a React application

In a certain file, I have a BaseComponent composed of various other components and being exported. The top level of the BaseComponent contains a wrapper responsible for managing margins. When I export it and attempt to override some styles with: //other fi ...

What is the best way to achieve a stylish Bootstrap modal design with a blurred and transparent header, as well as a left sidebar that seamlessly blends into

Is it feasible to create a modal with a blurred (transparent) background for the header section, allowing the site to show through? Additionally, can a sidebar on the left side of the modal also be transparent and blurred, revealing the site underneath? C ...

When incorporating props into styled components, the outcome often turns out to be unexpectedly

I have implemented styled components to change the text color by passing props. <Button variant = 'colour' type="submit" form="myForm" className="submit-btn"> Submit </Button& ...