Help with CSS, centering a container.
Unanswered
Barbary Lion posted this in #help-forum
Barbary LionOP
I have a container that's 1500pxs wide. Inside it I have another container that holds 40 elements that add up to 4000px with overflow hidden so it only shows the items that I am on at the moment. The problem is that if I resize the browser it sizes in from the right, I want the view to always be centered, sizing in from the right and left so the center item is always in the center.
69 Replies
Shy Albatross
flex is the answer
Barbary LionOP
I’ve tried flex, grid and relative/absolute positioning
@Shy Albatross flex is the answer
Barbary LionOP
Does’t work eventho it’s being applied
Shy Albatross
I mean it's not enough to say
display: flex and hope it sorts itself out, but you should be able to achieve this behavior with flex rather easilyBarbary LionOP
I know
Justify center gets applied but it doesn’t do anything
I’ll try to recreate in codepen
Barbary LionOP
Something like this
What can I do to keep it centered inside main?
Shy Albatross
huh, there's 2 circles
Barbary LionOP
Bruh
Shy Albatross
Barbary LionOP
<main>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</main>
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</main>
Shy Albatross
maybe you didn't save
Barbary LionOP
.container{
display: flex;
gap: 8px;
overflow: hidden;
width: 90%;
}
.item{
width: 188px;
height: 50px;
background-color: red;
flex-shrink:0;
}
main{
}
display: flex;
gap: 8px;
overflow: hidden;
width: 90%;
}
.item{
width: 188px;
height: 50px;
background-color: red;
flex-shrink:0;
}
main{
}
Shy Albatross
open that link you posted in incognito
@Shy Albatross maybe you didn't save
Barbary LionOP
Yeah I’m on my phone in bed rn
Never used codepen before
Should be easy to see what I’m trying to do though
Shy Albatross
idk man, literally
justify-content: center; added to .container does what you wantBarbary LionOP
Ahhhhh
Why doesn't it work on my site..
I also tried absolute relative positioning
Giant Chinchilla
maybe like this?
.outer-container {
width: 1500px;
overflow: hidden;
position: relative;
}
.inner-container {
width: 4000px;
display: flex;
justify-content: center; horizontally
0.3s ease;
}Blue horntail woodwasp
you can try make your
and, make the
main as flex and justify-content:center.and, make the
container as absolute.@Giant Chinchilla maybe like this?
.outer-container {
width: 1500px;
overflow: hidden;
position: relative;
}
.inner-container {
width: 4000px;
display: flex;
justify-content: center; horizontally
0.3s ease;
}
Barbary LionOP
Huh, this would flex center the items inside the container though?
I've done it similarly a few days ago but that's not what I'm looking for
@Blue horntail woodwasp you can try make your `main` as `flex` and `justify-content:center`.
and, make the `container` as absolute.
Barbary LionOP
This seems to get it somewhat closer?
It sizes down from both sides but it does it in a weird manner
Blue horntail woodwasp
wait a second
@Barbary Lion This seems to get it somewhat closer?
Giant Chinchilla
well we kinda got it working at least 🙂
@Barbary Lion This seems to get it somewhat closer?
Barbary LionOP
it drags out from the left and in on the right
I want it in on the left and right
@Shy Albatross Click to see attachment
Barbary LionOP
Any idea as to why it isn't working as normal like this?
@Barbary Lion It sizes down from both sides but it does it in a weird manner
Barbary LionOP
I think that's a result of the width being 100%, if I set a solid px width it has the same behaviour as before
@Giant Chinchilla well we kinda got it working at least 🙂
Barbary LionOP
4000px gives the same behaviour as before
Blue horntail woodwasp
https://codepen.io/arsadii/pen/ZEwpOyV
here's the final result
here's the final result
Barbary LionOP
Yeah exaclty like that
I tried absolute relative positioning
Blue horntail woodwasp
please check the latest, cause I fix some bugs on it
Barbary LionOP
Ok, I'll try
I haven't seen some of these properties before
Blue horntail woodwasp
what properties?
maybe it don't need to absolute. I comment this code it also works
also don't need to
transform-originBarbary LionOP
Yeah
Oh actually just saw it now
@Barbary Lion I've done it similarly a few days ago but that's not what I'm looking for
Barbary LionOP
This is this^^
just justify-centering the items container, I know it works but it also displaces the items, I want it to start at item 1
Blue horntail woodwasp
oh okay then
Barbary LionOP
Is there another way to do ti?
Blue horntail woodwasp
wait a sec
@Barbary Lion just justify-centering the items container, I know it works but it also displaces the items, I want it to start at item 1
Barbary LionOP
I thought since it works with justify-center on the container that flex-start would work, but that doesn't do the same..
Blue horntail woodwasp
like this?
Blue horntail woodwasp
I have some updates on codepen, maybe you want to check it
@Blue horntail woodwasp like this?
Barbary LionOP
This is how it is right now
if I shrink the browser it only moves in from the right
I want it to move in the same on both sides so number stays in the middle
Blue horntail woodwasp
okay, that's interesting.
Barbary LionOP
ChatGPT's solution is adding a left 50% and transform -50% to the container
The transform -50% is important but I can't do that because I'm using the transform property for something else
Here's an example of what I'm trying to recreate if that helps
@Barbary Lion https://jsfiddle.net/ywm3zbc4/10/
Barbary LionOP
This is before flexbox though
@Barbary Lion This is before flexbox though
Barbary LionOP
Nevermind this also has that bug