Make "overflow: scroll" work with centering flex-box layout [duplicate] - css

This question already has answers here:
Can't scroll to top of flex item that is overflowing container
(12 answers)
Generate scroll bar for overflow to the left
(5 answers)
Closed 5 years ago.
Consider the following example:
HTML
<div id="body">
<div id="container">
<div id="item-1"></div>
<div id="item-2"></div>
</div>
</div>
CSS
#body {
height: 500px;
width: 500px;
background-color: yellow;
overflow: scroll;
}
#container {
display: flex;
flex-direction: column;
align-items: center;
}
#item-1 {
background-color: red;
width: 200px;
height: 100px;
}
#item-2 {
background: linear-gradient(to right, black, white);
width: 1000px;
height: 100px;
}
see also https://jsfiddle.net/nfzo3xfj/5/
I have a flex container with two items as a centered column. These items can grow larger than the surrounding <div> element, so I want to use overflow: scroll to enable scrolling in that case. However, scrolling only works in one direction (to the right side). How can I enable scrolling towards the left (black) side of the second item as well?

Related

horizontal center align an div with flexible with [duplicate]

This question already has answers here:
How can I center an absolutely positioned element in a div?
(37 answers)
Closed 2 years ago.
I need horizontal center align an flexible width div element.
<div class="outer">
<div class="inner"> ...this content will grow or shrink while on items changed... </div>
</div>
.outer {
height: 500px;
position: relative;
background-color: gray;
}
.inner {
position: absolute;
left: 50%;
bottom: 10px;
transform: translateX(-50%);
border: 1px solid blue;
}
As you can see the div.inner is perfectly horizontal center aligned, but its max width will always be 50% of the div.outer while I hope the max width could be more when the content is grown.
And:
I don't want to give div.inner a fixed or min with, I hope it's flexible;
I know "max-width" does not work
Any suggestion will be appreciated!
Use a flexbox
.outer {
height: 500px;
background-color: gray;
display: flex;
justify-content: center;
}
.inner {
align-self: flex-end;
}
<div class="outer">
<div class="inner"> ...this content will grow or shrink while on items changed... </div>
</div>

Flex layout takes up full width when a cell's contents break to a second line [duplicate]

This question already has answers here:
Make container shrink-to-fit child elements as they wrap
(4 answers)
CSS when inline-block elements line-break, parent wrapper does not fit new width
(2 answers)
Closed 2 years ago.
I have a layout where I have a number next to a name and I want that layout to be centered in its container. It works fine if the name stays on one line, but if the name requires two lines then the entire flex element takes up the full width even though the broken lines visually don't take up the full space.
Notice how "Aaron Gray" stays on one line and the flexbox (yellow BG) remains centered in it's container (which is visually centered relative to the blue box on top of it, which is what I want)?
But notice how the "Peja Stojakowi" layout takes up the full width even though visually there's a lot of empty space on the right and consequently the num/name combo does not look centered relative to the blue box above it?
Here's the markup and css I'm using:
.LinkMap-playerWrap {
position: relative;
background-color: blue;
.LinkMap-playerWrapNameArea {
position: absolute;
bottom: -10px;
left: -20px;
right: -20px;
transform: translateY(100%);
display: flex;
justify-content: center;
background-color: pink;
.LinkMap-playerWrapNameAreaInner {
display: flex;
align-items: center;
justify-content: center;
background-color: yellow;
.LinkMap-playerWrapNum {
margin-right: 5px;
padding-right: 5px;
border-right: 1px solid #000;
align-self: start;
height: 100%;
}
}
}
}
<div class="LinkMap-playerWrap">
<div class="LinkMap-playerWrapNameArea">
<div class="LinkMap-playerWrapNameAreaInner">
<div class="LinkMap-playerWrapNum">16</div>
<div class="LinkMap-playerWrapName">Peja Stojakowi</div>
</div>
</div>
<div class="Player" style="width: 90px; height: 90px;">
<div class="Player-innerWrap">
</div>
</div>

CSS responsiveness, inline-divs with unequal heights [duplicate]

This question already has answers here:
Why is this inline-block element pushed downward?
(8 answers)
Closed 2 years ago.
If we style multiple divs with property display set to "inline-block" and one of them has a height longer than the others, all the smaller divs align at the bottom creating extra space at the top. But all I wanted is that the smaller divs align at the top and leave any extra space at the bottom. Is there any way I can achieve the required effect?
vertical-align: top for inline-block divs does that. vertical-align
.b{
display: block;
background-color: red;
}
.ib {
display: inline-block;
background-color: blue;
width: 50px;
vertical-align: top; /*this line*/
}
.d1 {
height: 100px;
}
.d2, .d3 {
height: 50px;
}
<div class="b">
<div class="ib d1"></div>
<div class="ib d2"></div>
<div class="ib d3"></div>
</div>

Vertically align a child div element with parent div having dynamic height [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 6 years ago.
How do I vertically align a child element with the parent having a dynamic height.
CSS code:
parent{
height: 400px;
}
.child{
position:relative;
top:50%;
transform: translateY(-50%;);
}
This sets the child once, then doesn't change. How do I change it so that it changes with the dynamic height of the parent?
You can use display: flex.
.parent {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
width: 100px;
margin: 1em;
background-color: tomato;
}
.child {
width: 10px;
height: 10px;
background-color: yellow;
}
You can use `displa: flex`.
The following doesn't rely on parent's height.
<div class="parent">
<div class="child">
</div>
</div>

Overflow not hidden inside nested flexbox divs [duplicate]

This question already has answers here:
text-overflow: ellipsis not working in a nested flex container
(2 answers)
Closed 6 years ago.
Why isn't text overflow hidden inside two nested flexbox divs?
It works when it is inside one div.
In particular: why is the inner div larger than the outer div?
Browser: Chrome 52.0.2743.60
Here is a minimal code example:
<style>
.outer {
display: flex;
width: 500px;
border: 2px solid red;
}
.inner {
display: flex;
border: 2px solid blue;
}
.text {
overflow: hidden;
white-space: nowrap;
}
</style>
<div class="outer">
<div class="inner">
<div class="text">
My overflow should be hidden but it's not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>
</div>
</div>
I believe it is something to do with your outer flex having a width, but the inner one does not, so this confuses the browser as to where the overflow is located. You can fix this by applying a width to your inner or text div like so:
.inner {
display: flex;
width: 100%; /* or 500px */
}
Another alternative is to move the overflow: hidden to your outer flex.

Resources