I am trying to achieve a centered layout for a div and its content on a webpage. The div includes a background image.
Here is my approach using Flexbox
:
<head>
<style>
.flex_container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.inner {
height: 788px;
width: 1400px;
background-image: url('matrix_bg.png');
background-size: contain;
background-repeat: no-repeat;
color: white;
display: flex;
justify-content: center;
}
</style>
</head>
<div class="flex_container">
<div class="inner">
<div style="margin-top: 100px;"> Some text in the center </div>
</div>
</div>
The output appears as shown here:
https://i.sstatic.net/DHXam.png
Although the inner text is centered on the page, I also intend for the background image to be centered. Any ideas on achieving this?
Update: Despite experimentation with different approaches, none have yielded the desired effect:
Utilizing background-position: center;
: This method seems suitable, but only a portion of the image displays. My goal is to have the image scale within the parent div
.
https://i.sstatic.net/kNQr7.png
Applying background-size: cover
: While this solution would be ideal, the challenge remains that the image is not perfectly centered.
https://i.sstatic.net/pJ6PS.png