Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I have 5 child divs in a parent div. The parent div does not fill the whole "window"-width", but all 5 children share the same space and fill their parent properly.
<div class="parent">
<div class="child" style="background-color:red" >child 01</div>
<div class="child" style="background-color:blue">child 02</div>
<div class="child" style="background-color:yellow">child
03</div>
<div class="child" style="background-color:grey">child 04</div>
<div class="child" style="background-color:green">child05</div>
</div>
When I use width: 90vw; for the container of the parent div, the parent div scales, yet the child divs remain the same size. Shouldn't the child divs scale automatically with their parent? How do I get it to do that?
My CSS code:
#import url('https://fonts.googleapis.com/css?
family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.parent {
display: flex;
width: 90vw;
}
According to your explanation, I think you need to give a value for a parent div width in Viewport Width(vw) and, you need to give the same width for their children div tags. For the demonstration purpose, I included five children div tags inside the parent tag and gave different colors to see their widths.
edit: I have done some changes to your CSS styles and I removed the import statement. I think it overrides our CSS styles.
* {
box-sizing: border-box;
}
body {
font-family: 'Muli', sans-serif;
display: flex;
height: 100vh;
overflow: hidden;
margin: 0;
}
.parent {
display:"flex";
width: 90vw;
}
.child{
width:"100%";
}
<div class="parent">
<div class="child" style="background-color:red" >child 01</div>
<div class="child" style="background-color:blue">child 02</div>
<div class="child" style="background-color:yellow">child
03</div>
<div class="child" style="background-color:grey">child 04</div>
<div class="child" style="background-color:green">child05</div>
</div>
Related
Here is a codepen.
Here is code:
HTML
<div class="body">
<div class="page">
<div class="regular-flow">
</div>
<div class="regular-flow">
</div>
<div class="regular-flow">
</div>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
<div class="item">11</div>
<div class="item">12</div>
<div class="item">13</div>
<div class="item">14</div>
<div class="item">15</div>
<div class="item">16</div>
</div>
<div class="regular-flow">
</div>
<div class="regular-flow">
</div>
</div>
</div>
CSS
.body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: grey;
}
.page {
height: 100vh;
width: 100%;
background-color: lightgrey;
overflow: hidden;
}
.regular-flow {
height: 20px;
background-color: limegreen;
margin: 10px 24px;
}
.container {
width: 100%;
height: 25%;
padding-left: 24px;
padding-right: 24px;
background-color: orangered;
display: flex;
gap: 16px;
overflow-x: scroll;
-ms-overflow-style: none;
scrollbar-width: none;
}
.container::-webkit-scrollbar {
display: none;
}
.item {
height: 100%;
width: 121px;
color: yellow;
background-color: blue;
flex-shrink: 0;
}
We have several "regular flow" items (green) that keep their own margin consistently down the page (grey). However, I'd like to have a horizontal scrolling div (blue and orangered) that disrespects these boundaries, allowing items to seemingly pass from "edge" to "edge" of the screen. My thought was to overflowX: 'scroll' a 100% width div and put padding on the left and right equal to the margin being set by the other items. This way, on the "edges" of the scrollable content, it appears to still respect the margin set by the other regularly flowing items. However, no matter what I try or how many items you put into this container, it will always scroll right only to the right edge of the last item. It will not show the last bit of right padding.
Things to keep in mind about the codepen
You have to have enough items for it to need to scroll horizontally, obviously.
I am hiding the scrollbar, so hover over the blue/orange container and shift+scrollwheel to move the items. I know this isn't great UX.
Most Basic Question
Why can you not scroll right far enough such that the padding-right (orangered) is visible?
Update
I was able to come up with a workaround for this issue, and that is to go into the parent component (in my codepen, this would be .container and remove the padding-right and padding-left at that level, but then add:
.container & .div:first-child {
padding-left: 24,
}
.container & .div:last-child {
padding-right: 24,
}
This seems to accomplish the same goals, but only if you do NOT have any background color to the .item. For my purposes, this works. I only added a background color here for visualizing the issue. I would still be curious if anyone can tell me what the problem above was, so I will keep this question up and not edit the codepen. Thanks!
This question already has answers here:
Why is a flex item limited to parent size?
(1 answer)
Why don't flex items shrink past content size?
(5 answers)
Closed 3 years ago.
See problem in codepen: https://codepen.io/pencillrpal/pen/QWLOLGv
I have a flex container in which I'm going to have a horizontal slider with elements as long as the full page width.
But I realized I can't control the width of the child elements, because they will always have a width that makes them fit the container, and they don't extend beyond it.
I've found a trick though. If I wrap my child divs to a div one by one it will work as I want it to.
Why does this work this way?
.container {
display: flex;
flex-direction: row;
justify-content: center;
height: 40vh;
max-width: 100%;
overflow-x: hidden;
border: 5px solid black;
margin: 0 auto;
}
.box {
position: relative;
width: 90vw;
margin: 10px;
line-height: 10vh;
text-align: center;
background: black;
color: white;
border: 5px solid red;
}
<h1>Box should have 100vw width</h1>
<div class="container">
<div class="box">
<h3>Box</h3>
</div>
<div class="box">
<h3>Box</h3>
</div>
<div class="box">
<h3>Box</h3>
</div>
</div>
<h1>This one has though</h1>
<div class="container">
<div>
<div class="box">
<h3>Box</h3>
</div>
</div>
<div>
<div class="box">
<h3>Box</h3>
</div>
</div>
<div>
<div class="box">
<h3>Box</h3>
</div>
</div>
</div>
You can control the width of the flex children. All flex children have default flex values to control the flex-grow and flex-shrink properties. Disable these properties and you'll have control over the width.
Add these lines to your CSS.
/**
* Disable flex grow and shrink so that it no longer tries to divide the
* space between the .box elements.
*
* Use flex-basis or width to control the width of the element.
*/
.box {
flex-grow: 0;
flex-shrink: 0;
flex-basis: 90vw;
width: 90vw;
}
This question already has answers here:
CSS when inline-block elements line-break, parent wrapper does not fit new width
(2 answers)
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 4 years ago.
I have a container div that has child divs with fixed widths and wraps. What I realised is that the container's width doesn't fit tightly to the content after it wraps, usually leaving a 'ghost' space on the right. Is there a way to force it to readjust the width according to its content?
.container {
max-width: 12em;
background-color: black;
}
.child {
display: inline-block;
width: 5em;
background-color: red;
}
<div class="container">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
</div>
So in this case when the child wraps after 2 of them add up to 10em, the container instead of being 10em, it is still 12em. And if the window size forces it down to a single div wrapping, the container rather than being 5em, could be 6em, 7em, 8em, etc depending on window width.
Is there a way to get rid of the 'ghost' space and make the container fit exactly to how the child is wrapping and it's total width?
Note: I am not talking about the extra space in between each child element. I'm referring to the giant gap left in the container, which causes the container to not accurately reflect the size of its child content. I understand that I can simply count how many child can fit in 12em and change the container width to be 10em to fit 2 childs perfectly. But I want that to be flexible. Is that possible?
The extra space after each child element is a result of the display: inline-block property and is due to the literal whitespace between each div in your HTML. You may verify this by removing the linebreaks between child divs so that their open and close tags are back-to-back:
<div class="container">
<div class="child">1</div><div class="child">2</div><div class="child">3</div>/*...*/
</div>
Although this will eliminate the pesky whitespace, it comes at the expense of code clarity/readability and is surely an irritating way to write HTML.
In my experience, often the best solution to this issue is to set the parent container to display: flex:
.container {
display: flex;
flex-wrap: wrap;
max-width: 10em;
background-color: black;
}
.child {
display: inline-block;
width: 5em;
background-color: red;
}
<div class="container">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div><div class="child">5</div>
<div class="child">6</div>
</div>
In this case you will also need to provide the flex-wrap: wrap property to inform the flex container to wrap its contents. Presumably you can now update the container's max-width property to 10em to fit exactly the width of two child elements so I've taken the liberty of this change in the code snippet.
Looks like you want to render a table. So you may want to use:
<table>
<tr>
<td>Col1</td>
<td>Col2</td>
</tr>
...
</table>
In case im wrong:
You can do this with flex or grid
Helpful link Flexbox, Grid
.container {
max-width: 12em;
background-color: black;
display: flex;
}
.child {
display: block;
min-width: 5em;
background-color: red;
border: 1px dashed blue;
}
/* FLEX */
.container-flex {
/* new row if next element doesnt fit */
flex-wrap: wrap;
}
.container-flex .child {
/* makes children grow evenly after wrapping */
flex-grow: 1;
}
/* GRID */
.container-grid {
display: grid;
/* 2 auto-horizontally sized colums */
grid-template-columns: auto auto;
}
.container-grid .child {
/* noting to do here */
}
<div style='float: left; margin-right: 10px;'>
Flex<br>
<small>extend elements to 6em</small><br>
<hr>
<div class="container container-flex">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
</div>
</div>
<div style='float: left; margin-right: 10px;'>
Grid<br/>
<small>collapse container to 10em</small><br>
<hr>
<div class="container container-grid">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
</div>
</div>
You can do it like this
.container { max-width: 4.5em;background-color: black; }
.child { display: inline-block; width:cover; background-color: red; }
<!DOCTYPE html>
<html>
<head>
<title>Answer</title>
</head>
<body>
<div class="container">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
<div class="child">6</div>
</div>
With the width set to 'cover' it covers the complete area leaving no space.In order to fix the black background (of container which is more or less acting like border), you can manually adjust it's size.
I have a problem with a translated flex item still occupying its original space so that the following flex item does not immediately follow. Please see JSFiddle or snippet for more details. I want the green rectangle to follow immediately after the red so that is will be visible within the border. I can unfortunately not translate the parent, it will have overflow: hidden so that only the child within the border is visible.
Here is a JSFiddle https://jsfiddle.net/3wv9d9dm/
Or a snippet:
.parent {
display: flex;
width: 100px;
margin-left: auto;
margin-right: auto;
border: 1px solid black;
}
.child {
flex: 0 0 100%;
}
<div class="parent">
<div class="child" style="background-color:red;transform:translateX(-100%);"><h1>1</h1></div>
<div class="child" style="background-color:green;"><h1>2</h1></div>
<div class="child" style="background-color:blue;"><h1>3</h1></div>
</div>
From MDN, emphasis mine:
The <transform-function> CSS data type denotes a function used to modify an element's appearance.
Transforming an element only modifies appearance, not position in the document flow. This means that even though the element appears to have moved its position in the DOM it continues to affect sibling/other elements because its physical dimensions remain in place prior to the transformation.
A way to get around this is to animate or modify properties that affect document flow such as margin.
.parent {
display: flex;
width: 100px;
margin-left: auto;
margin-right: auto;
border: 1px solid black;
}
.child {
flex: 0 0 100%;
}
<div class="parent">
<div class="child" style="background-color:red;margin-left:-100%;">
<h1>1</h1>
</div>
<div class="child" style="background-color:green;">
<h1>2</h1>
</div>
<div class="child" style="background-color:blue;">
<h1>3</h1>
</div>
</div>
An alternate way is to transform all elements together. This method is more performant (as it skips the layout and paint steps of the browser rendering pipeline). Visit this article on Rendering Performance for a detailed explanation.
One possible way of doing this:
let children = document.querySelectorAll('.child');
[].forEach.call(children, (child) => {
child.style.transform = 'translate(-100%)';
});
.parent {
display: flex;
width: 100px;
margin-left: auto;
margin-right: auto;
border: 1px solid black;
}
.child {
flex: 0 0 100%;
}
<div class="parent">
<div class="child" style="background-color:red;">
<h1>1</h1>
</div>
<div class="child" style="background-color:green;">
<h1>2</h1>
</div>
<div class="child" style="background-color:blue;">
<h1>3</h1>
</div>
</div>
This question already has answers here:
Make div (height) occupy parent remaining height
(9 answers)
Closed 6 years ago.
I have one div-container and children inside it.
<div id="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
<div class="child" id="last-child"></div>
</div>
I don't know height of children (every time different content), but i have
#parent {
min-height: 500px;
}
I want to stretch last-child to the bottom of parent (it has to fill free space from his subling to parent bottom).
You can use flexbox for this (background colors added for visibility):
#parent {
min-height: 500px;
display: flex;
flex-direction: column;
background: #999;
}
.child {
background: #eee;
}
#last-child {
flex-grow: 1;
background: #faa;
}
<div id="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child" id="last-child">4</div>
</div>
Make the parent a flex box with display:flex; and the flex--direction being column. Next give the last child element the flex:1; property to make it expand to the remainder of the space in the parent.
#parent {
min-height: 500px;
display:flex;
flex-direction: column;
}
#last-child{
flex:1;
background:blue;
}
.child{
background:red;
}
<div id="parent">
<div class="child">jljlkj</div>
<div class="child">kjljlkjl</div>
<div class="child">kljlkjlhi</div>
<div class="child" id="last-child"></div>
</div>