I’m making a small Etch-a-Sketch-esque webpage.
For now, I’m just trying to get the CSS of the page right before moving onto the Javascript that will generate a canvas of a designated size, so I’m working with a 16×16 grid of divs statically on the webpage.
See this codepen for what I have so far. Sorry about the ridiculously long HTML, as I said for now I’m just working on the formatting and put in a 16×16 grid (800px by 800px
) of divs with class .tabletCanvas
. Basically, just 256 divs that have that class.
As you can see, the “canvas” is sticking to the left-hand side, and I want it to look like it does in this picture (very rough outline done in 5 minutes using photoshop).
What I’ve tried so far:
- Many different values for
flex:
. What happens is that, without setting amax-width
ormax-height
(from my understanding of flexbox, specifying specific heights and widths defeats the purpose of using it and that everything should be handled through theflex:
property), the grid will simply overflow to the right and fill up the rest of the screen. Obviously not ideal, as I want it contained within the 800×800 pixels, and even after specifying themax-width
andheight
, it will still hang to the left. - Making the
tabletContainer
a flex parent (container) itself, and trying all possible variations ofalign-content
,align-items
andjustify-content
within it. None of them had a visible effect - Many different values of
align-self
,justify-self
on the.tabletCanvas
class. - One thing that sprung to mind would be absolutely positioning the canvas div, but this defeats the purpose of using a flex container in the first place, I imagine
Something that I think might work would be to use the align-content
or align-items
properties on the #bodyWrapper
to get all of its children to be centered, however if I do this then I risk ruining the flow of every other property and putting myself more or less back to square one where I started.
Another thing that sprung to mind was just to add a lot of padding-left
to the #sketchButtons
, however this also seems like a hilariously wrong and hacky approach to take.
Any and all help will be greatly appreciated.
3
Answers
I made a jsfiddle draft of your case with a simplified styling. I hope you can analyse the code and spot the approach.
In a nutshell it’s just a centered flex column and absolutely positioned menu.
One way to do it would be to:
set .tabletContainers position to absolute,
set margin to auto,
set left and right to 0,
and then set your top property as desired.
If you are just looking to center your grid horizontaly, here is a simple solution :