I’m writing a React web application and I also use Chakra-UI. In general, I have a header, main section and a footer. Like this
https://jsfiddle.net/7cmasxLq/
<div id="header">
header
</div>
<div id="main">
Main
</div>
<div id="footer">
Footer
</div>
and
#header {
background-color: red;
}
#main {
background-image: url('https://www.epfl.ch/schools/ic/wp-content/uploads/2019/02/EPFL-aerial-Rolex-towards-East-1920x1080.jpg');
}
#footer {
background-color: yellow;
padding: 10px 0;
position: fixed;
left: 0px;
bottom: 0px;
width: 100%;
text-align: center;
}
So the header is always at the top and the footer is always at the bottom.
Now for the landing page, I’d like to add a background image that spans the space between the header and the footer but I’m unsure what the best approach is, especially when using Chakra-UI.
In my example, I use background-image but I’ve read that one should rather use an image element but in the light of me using Chakra-UI, it’s even more confusing.
What is a good solution?
2
Answers
I created a simple example in the chakra-ui playground: play.chakra-ui.com
So, you can use the Box component as main building block for header, main and footer. Also, you can add a background image to the Box.
This isn’t Chakra specific, but the problem is that your main content isn’t filling the space – it’s only as tall as the content within it.
To get it to stretch and fill the space you could use flexbox:
as for your background-or-image question, i prefer to use backgrounds in this case – that’s what this is at the end of the day! plus, if the background doesn’t load in you don’t end up with an ugly broken image icon appearing