How Do Browsers Calculate Width When Child Depends on Parent, and Parent's Depends on Child'S

How do browsers calculate width when child depends on parent, and parent's depends on child's

To explain this, I will start with a more simplified example that will produce the same output:

<div style="display:inline-block">
<img src="https://via.placeholder.com/1000x100">
</div>

<div style="display:inline-block">
<img src="https://via.placeholder.com/1000x100" style="max-width:100%">
</div>

<div style="display:inline-block">
<!-- even a big explicit width specified won't change the behavior -->
<img src="https://via.placeholder.com/1000x100" style="wdith:2000px;max-width:100%">
</div>

<div style="display:inline-block">
<img src="https://via.placeholder.com/1000x100" style="width:100%">
</div>

Set Parent Width equal to Children Total Width using only CSS?

I know this is a bit late, but Hope this will help somebody who is looking for similar solution:

<div class="parent" style="display: inline-flex">
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
</div>

the trick is to use inline-flex for the parent and inline-table for the child. Everything is dynamic. I make the table scrollable horizontally by adding another grandparent with overflow-x:scroll;:

<div class="grandparent" style="width: 300px; overflow-x: scroll; background: gray">
<div class="parent" style="display: inline-flex">
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
<div class="child" style="display: inline-table">some button</div>
</div>
</div>

Make children CSS attributes depend on parent element size (like media queries depend on browser width)

Thanks to the second link in @torazaburo's comment under question post, I found the perfect solution:

https://github.com/marcj/css-element-queries

Yet it's very young project it still has some vulnerabilities (29 july 2013) but also has a potential. It isn't based on @element queries but on attributes. Example:

.fooDiv[min-width="1000px"] .form { /* code for 1. */ }
.fooDiv[min-width="900px"] .form { /* code for 2. */ }
.fooDiv[min-width="800px"] .form { /* code for 3. */ }

Those attributes are set by listeners, so they work on each change of size of .fooDiv.


Another project (incompatible with SASS/SCSS),

https://github.com/Mr0grog/element-query

will work as follow:

.test-element:media(max-available-width: 400px) {
background: purple;
}

In the above CSS, .test-element will have a purple background if it is inside an element that is 400px wide or smaller.


Yet another,

https://github.com/tysonmatanich/elementQuery
I use it in everyday projects.


Even more about this:

http://coding.smashingmagazine.com/2013/06/25/media-queries-are-not-the-answer-element-query-polyfill/

CSS: how to make children fit parent's width

Added css:

#component>div{height:100%;}
#component>div:first-of-type{border-radius:30px 0 0 30px;}
#component>div:last-of-type{border-radius: 30px;}

Edit in js:

$('#component').children().not(':last').each(function () {

What happens:

The last div will not float left, and will just fill the space that is left.
I added rounded corners to the first and last div to fix the corner issue. the last div has a 30px radius in every corner, because the div is actualy behind the other divs (you can see this with inspect element)

Demo:

http://jsfiddle.net/tqVUy/48/

Cross browser method to fit a child div to its parent's width

The solution is to simply not declare width: 100%.

The default is width: auto, which for block-level elements (such as div), will take the "full space" available anyway (different to how width: 100% does it).

See: http://jsfiddle.net/U7PhY/2/

Just in case it's not already clear from my answer: just don't set a width on the child div.

You might instead be interested in box-sizing: border-box.

Why the width of the floated parent doesn't depends from negative margin of its child?

Since .main-container is div (with property display: block) it's width is based on parent container. Children's of .main-container widths are also based on parent container (.main-container).

So in this case, children's widths are based on parents' widths and not the other way around.

Also, consider this: if child is 35% wide as it's parent, but parent's width is decreased because of children's margins and positioning, then children's width should also decrease leading to parent's width decrease, leading to..... and so on and so forth until their all widths become 0.

Set child width relative to its parents height in pure css

Just assign your parent's height property value to a css variable and then use calc() to assign your child element's width to a value that is 10% of your parent's height.

Check and run the Code Snippet below for a practical example of what I have described above:

.parent {
position: relative;
--parentHeight: 300px;
height: var(--parentHeight);
width: 600px;
background-color: red;
}
.parent .resized {
height: 100px;
}

.child {
position: absolute;
height: 20%;
width: calc(var(--parentHeight) / 10);
background-color: green;
}
<div class="parent">
<div class="child"></div>
</div>

Is there a way to make a child DIV's width wider than the parent DIV using CSS?

Use absolute positioning

.child-div {
position:absolute;
left:0;
right:0;
}

How flex affects the width of an image?

First, you should note that flex-grow is by default equal to 1 so you don't need to set it. Then, you need to understand "cyclic dependency" and "percentage sizing".

In your case, you have a image that should be 100% of its parent width but its parent is a flex item and its width depend on its content (the image width as well).

I have explained a similar behavior here: How do browsers calculate width when child depends on parent, and parent's depends on child's. The same apply in your case and the width:100% will disable the minimum contribution of the image size. It's like the image doesn't exist initially.

Here is the steps the browser will perform.

First, each column is sized with its content. No shrink is occurring (It's like setting flex-shrink:0)

#about {
border: 2px solid blue;
}

.row {
display: flex;
border: 2px solid black;
}

img {
display: block;
width: auto;
}

.column {
flex-shrink: 0;
border: 2px dashed red;
}
<section id="about">
<div class="row">
<div class="column">
<img src="https://i.stack.imgur.com/yat2N.jpg" alt="Sample Image">
</div>
<div class="column"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Consequatur quibusdam dicta dolore suscipit quidem, hic nihil aliquid officia porro illum! Necessitatibus cupiditate, sapiente cum recusandae tenetur dolore veritatis in temporibus perferendis.
Ex corrupti voluptatibus eaque aliquam quis soluta veniam non dicta repellendus ea iure temporibus assumenda placeat accusantium quae iste, corporis maxime dolorum quisquam neque est sint asperiores doloribus. Quibusdam ducimus saepe distinctio
illum veniam voluptates amet quod perferendis dolorem, deleniti mollitia. Ab aperiam, ea itaque tempore molestias ullam sint accusamus totam reiciendis laborum. At natus consequatur ex officia. Porro dolor accusamus blanditiis nam commodi provident
assumenda facere adipisci perferendis. </div>
</div>
</section>


Related Topics



Leave a reply



Submit