Horizontally scrollable div inside full width div [duplicate] - html

This question already has answers here:
Make content horizontally scroll inside a div
(5 answers)
CSS Flex - I have a list of items can need to make them horizontally scroll with overflow hidden
(1 answer)
How can i make this flexbox container scroll instead of adapting the width?
(1 answer)
Closed 5 years ago.
I have a division which horizontally fits the screen inside which I have 5 divisons, I want 4 divisions to appear on screen and 1 division to appear when I scroll the division horizontally. And I want the scrollbar to appear inside the div only and not on the browser window.
Below is my non working code which puts the h1 tag in the left, I want it on the top-left then under it all 5 divs
.outer {
overflow-x: scroll;
width: 100%;
}
.inner {
width: 25%;
float: left;
}
<div class="outer">
<h1>Header Title</h1>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
<div class="inner">
</div>
</div>

You can do it with Flexbox:
.outer {
display: flex; /* displays flex-items (children) inline */
overflow-x: auto;
}
.inner {
flex: 0 0 25%; /* doesn't grow nor shrink, initial width set to 25% of the parent's */
height: 1em; /* just for demo */
}
<div class="outer">
<div class="inner" style="background: red"></div>
<div class="inner" style="background: green"></div>
<div class="inner" style="background: blue"></div>
<div class="inner" style="background: yellow"></div>
<div class="inner" style="background: orange"></div>
</div>
Solution with the h1 element:
.outer {
display: flex;
flex-direction: column;
overflow-x: auto;
}
.middle {
display: flex;
flex: 1;
}
.inner {
flex: 0 0 25%;
height: 1em;
}
<div class="outer">
<h1>Header Title</h1>
<div class="middle">
<div class="inner" style="background:red"></div>
<div class="inner" style="background:green"></div>
<div class="inner" style="background:blue"></div>
<div class="inner" style="background:yellow"></div>
<div class="inner" style="background:orange"></div>
</div>
</div>

I'm not sure if I've misunderstood you, but I think that what you want to do is have the H1 over the 5 div, like this:
https://jsfiddle.net/p78L2bka/
.outer {
display: flex;
overflow-x: scroll;
}
.middle {
display: flex;
width: 100%;
}
.inner {
flex: 0 0 25%;
height: 100px;
}
<h1>Header Title</h1>
<div class="outer">
<div class="middle">
<div class="inner" style="background: red">
1
</div>
<div class="inner" style="background: green">
2
</div>
<div class="inner" style="background: blue">
3
</div>
<div class="inner" style="background: yellow">
4
</div>
<div class="inner" style="background: orange">
5
</div>
</div>
</div>

Related

Align matching content in separate containers depending on width of largest content

I'm aware of CSS Subgrid being able to solve a layout like this, but what I'm looking to achieve is a list of containers with content inside. The content inside the containers is aligned right in the containers, but all the content is aligned (left) to the longest content.
Is this possible with flex? Are there any strategies to achieve this?
I suppose the HTML structure would be something like:
<div class="container">
<div class="content" style="100px"></div>
</div>
<div class="container">
<div class="content" style="300px"></div>
</div>
<div class="container">
<div class="content" style="400px">All other content aligned to this longest content</div>
</div>
<div class="container">
<div class="content" style="200px"></div>
</div>
It is most definitely doable with flex.
What I've done is create 2 columns inside the .container element. Column 2 will be right aligned inside the container, and your .content will be left aligned inside .column2.
All you need to do to adjust the alignment of the content inside the containers, is to play around with the widths of .column1 and .column2 in the snippet below:
* {
font-family: sans-serif;
}
.container {
display: flex;
background: lightgray;
align-items: center;
border-radius: 5px;
margin-bottom: 10px;
padding: 7px;
}
.content {
display: flex;
align-items: center;
padding: 0 10px;
background: #666;
border-radius: 5px;
color: white;
height: 50px;
}
.column1 {
width: 30%;
}
.column2 {
width: 70%;
}
<div class="container">
<div class="column1">Container</div>
<div class="column2">
<div class="content" style="width: 100px"></div>
</div>
</div>
<div class="container">
<div class="column1">Container</div>
<div class="content" style="width: 250px"></div>
</div>
</div>
<div class="container">
<div class="column1">Container</div>
<div class="column2">
<div class="content">All other content aligned to this longest content</div>
</div>
</div>
<div class="container">
<div class="column1">Container</div>
<div class="column2">
<div class="content" style="width: 200px"></div>
</div>
</div>

How to wrap overflowing content in a single row using flexbox? [duplicate]

This question already has answers here:
Can't scroll to top of flex item that is overflowing container
(12 answers)
Closed 2 years ago.
I have a container div with many child div having fixed width. I am using flexbox to align the child divs in a single row. Below html and css works except that the first and last child are still overflowing the container div. I want the child divs to be wrapped in single row using a horizontal scroll -
.container {
display: flex;
justify-content: center;
width: 500px;
height: 200px;
background-color: gray;
overflow-x: auto;
}
.child {
margin: 5px;
min-width: 100px;
height: 150px;
background-color: darkgray;
}
<div class="container">
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
</div>
Please see this codepen I've created.
You should wrap all children into other tag to display full content without overflow
.container {
display: flex;
width: 500px;
height: 200px;
overflow: auto;
}
.flex-box {
display: flex;
justify-content: center;
background-color: gray;
flex: 1;
}
.child {
margin: 5px;
min-width: 100px;
height: 150px;
background-color: darkgray;
}
<div class="container">
<div class="flex-box">
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
<div class="child"> </div>
</div>
</div>

Problem when using with overflow-x: scroll and justify-content: center [duplicate]

This question already has answers here:
Can't scroll to top of flex item that is overflowing container
(12 answers)
Closed 2 years ago.
I am having issue while using overflow-x: scroll and justify-content: center on flex parent container.
Please see my code below.
issue: first flex child item is not showing it is crop in left or other all child item. please see my screenshot and code below.
I need your help. thank you in advance.
.container {
width: 500px;
margin: 0 auto;
display: flex;
justify-content: center;
flex-direction: row;
overflow-x: scroll;
}
.box {
height: 100px;
border: 1px solid red;
min-width: 100px;
margin-right: 10px;
flex-grow: 1;
}
<div class="container">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
The justify-content:center is making the content to align to center and some of the left is cut off. You could remove it and try.
.container {
width: 500px;
margin: 0 auto;
display: flex;
flex-direction: row;
overflow-x:scroll
}
.box {
height: 100px;
border: 1px solid red;
min-width: 100px;
margin-right: 10px;
flex-grow: 1;
}
<div class="container">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
remove "justify-content:center". And you said that you need center aligned elements when there are only 1 or 2 elements...so the answer is they will by aligned automatically...if there will be only two elements each of them will have 250px width and if there will be only one then width of this element will be 500px.

How can I wrap columns vertically when their width doesn't fit their containers? [duplicate]

This question already has answers here:
When flexbox items wrap in column mode, container does not grow its width
(9 answers)
Closed 4 years ago.
I have problem with flexbox wrapping into column. The container doesn't fit the content width as seen in the snippet below.
This works if you replace both flex-flow of .wrapper and .container with flex-flow: row wrap, the height fit the content height its children, but the problem then is that the columns then flow horizontally and appear under each other, rather than flowing vertically and beside each other.
I expect the following result:
.wrapper {
display: flex;
flex-flow: column wrap;
max-height: 500px;
max-width: 500px;
overflow: scroll;
}
.container {
display: flex;
flex-flow: column wrap;
align-content: flex-start;
background-color: red;
margin: 5px;
}
.product {
margin: 3px;
min-width: 100px;
min-height: 100px;
background-color: #ccc;
text-align: center;
font-weight: bold;
line-height: 100px;
}
<div class='wrapper'>
<div class='container'>
<div class="product">0.1</div>
<div class="product">0.2</div>
<div class="product">0.3</div>
<div class="product">0.4</div>
<div class="product">0.5</div>
<div class="product">0.6</div>
<div class="product">0.7</div>
<div class="product">0.8</div>
</div>
<div class='container'>
<div class="product">1.1</div>
<div class="product">1.2</div>
<div class="product">1.3</div>
<div class="product">1.4</div>
<div class="product">1.5</div>
<div class="product">1.6</div>
<div class="product">1.7</div>
<div class="product">1.8</div>
</div>
<div class='container'>
<div class="product">2.1</div>
<div class="product">2.2</div>
<div class="product">2.3</div>
<div class="product">2.4</div>
<div class="product">2.5</div>
<div class="product">2.6</div>
<div class="product">2.7</div>
<div class="product">2.8</div>
</div>
<div class='container'>
<div class="product">3.1</div>
<div class="product">3.2</div>
<div class="product">3.3</div>
<div class="product">3.4</div>
<div class="product">3.5</div>
<div class="product">3.6</div>
<div class="product">3.7</div>
<div class="product">3.8</div>
</div>
</div>
The problem is that the .container doesn't have a width defined, so how .wrapper does have a maximum of with and it's a Flexbox, all the children (.container) will fit automatically to their parent, that's the problem.
You can solve it by setting a with to the container class.
Something like this: width: 212px;

Flexbox reserves space for svg images that have been scaled

I have a site header and I would like to use flexbox for this.
I use justify-content: space-between; to evenly divide the free space between the div's but when I add my svg and scale it down to the size of the header bar flexbox reserves the same amount of space as if the svg was displayed at 100%.
I made a example to show what I mean:
#wrapper {
width: 700px;
height: 100px;
display: flex;
justify-content: space-between;
background: blue;
}
.child {
flex: content;
background: red;
}
<div id="wrapper">
<div class="child">
<img src="https://s.cdpn.io/3/kiwi.svg" height="100%" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/200/300/?random" height="100%" alt="">
</div>
2
3
</div>
And a JSiddle:
https://jsfiddle.net/03r8vzkn/6/
Is there a way I can avoid this or should I make the svg smaller? This feels a bit hacky because I don't want to make every svg the right size; the scalability is one of its biggest advantages.
You can do it with the display: inline-flex for the .child divs, then they will only take the content's width, of course you also need to make your imgs responsive:
#wrapper {
width: 700px;
height: 100px;
display: flex;
justify-content: space-between;
background: blue;
}
.child {
display: inline-flex; /* only takes the content's width */
background: red;
}
img {
display: block; /* removes bottom margin/whitespace */
max-width: 100%; /* horizontally responsive */
max-height: 100vh; /* vertically responsive */
}
<div id="wrapper">
<div class="child">
<img src="https://s.cdpn.io/3/kiwi.svg" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/300/400/?random" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/200/300/?random" alt="">
</div>
2
3
</div>
<br>
<div id="wrapper">
<div class="child">
<img src="https://picsum.photos/100/200/?random" alt="">
</div>
2
3
</div>