To begin, here is a Fiddle example.
What I am attempting to do is center the content (i.e. the h1
and h2
) with a flex box that scales to the current size of the background image. Based on my own attempts, I'm assuming this will have to be a JavaScript solution (e.g., I'll have to calculate the size of the background image post-scaling), but I'd certainly welcome any type of solution at all.
I suppose the most succinct way to ask this is: How does one center content on an element using background-size: contain
?
Other general questions I have:
- What's the best method for calculating the dimensions of a background-image, regardless of what
background-size
property it is using? - Would something other than a flex box be better at centering content for this specific scenario? It's easy enough to center things when using
cover
but this seems like a whole new ball game. - Is it possible to make the flex-container (denoted by the red border) match the size and constraints of the background-image with pure CSS? Note that I ultimately want this to be responsive and am not necessarily looking to define static heights.