Flexbox height and background image height - html

I am a beginner in html and CSS, and I will try to make a map with clickable points, but I do not get it right.
I have received a tip of using flexboxes and creating a container with the map as a background image, and creating separate containers that can hold imagebuttons for the clickable points. The solution must be responsive and work for both desktop and mobile.
I have started by trying to get the background image and points/(boxes) to resize equally and then try to set the position on the points/(boxes), but I have encountered a problem already with the background image and the parent container.
Because I can not set the height of the parent container to the same height as the background image, so now only half the image is displayed.
If I insert the height of the parent container equal to the background image in px, the background image will not resize responsive.
Does anyone know how to set the height of the background image and container to the same, or maybe know a better solution to this?
In that case, I'll be happy to hear from you :-)
My coding so far:
.map-container {
display: flex;
justify-content: center;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
background-image: url("Images/home/map_front.jpg");
background-size: cover;
background-repeat: no-repeat;
}
.flexbox-point {
width: 20%;
margin: 20px;
border: 3px solid #B90F12; /* Color to see the points/boxes */
}
.flexbox-point1 {
min-height: 100px;
}
.flexbox-point2 {
min-height: 100px;
}
.flexbox-point3 {
min-height: 100px;
}
<!-- Test for flexbox with map as BG-picture*---->
<div class="map-container">
<div class="flexbox-point flexbox-point1"></div>
<div class="flexbox-point flexbox-point2"></div>
<div class="flexbox-point flexbox-point3"></div>
</div>

I would recommend using a fixed width instead of % on your child because the % means it's going to take up 20% of screen width which when resizing the browser could get really small. To center your background image you can use background-position: center; See below.
.map-container{
display: flex;
justify-content: center;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
background-image: url("https://dummyimage.com/400x400/000/fff");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.flexbox-point{
width: 400px;
margin: 20px;
border: 3px solid #B90F12; /* Color to see the points/boxes */
}
.flexbox-point1 {
min-height: 100px;
}
.flexbox-point2 {
min-height: 100px;
}
.flexbox-point3 {
min-height: 100px;
}
<div class="map-container">
<div class="flexbox-point flexbox-point1"></div>
<div class="flexbox-point flexbox-point2"></div>
<div class="flexbox-point flexbox-point3"></div>
</div>
EX with a different height background image.
.map-container{
display: flex;
justify-content: center;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
background-image: url("https://upload.wikimedia.org/wikipedia/commons/c/cc/ESC_large_ISS022_ISS022-E-11387-edit_01.JPG");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.flexbox-point{
width: 400px;
margin: 20px;
border: 3px solid #B90F12; /* Color to see the points/boxes */
}
.flexbox-point1 {
min-height: 100px;
}
.flexbox-point2 {
min-height: 100px;
}
.flexbox-point3 {
min-height: 100px;
}
.container {
height: 100vh;
}
<div class="container">
<div class="map-container">
<div class="flexbox-point flexbox-point1"></div>
<div class="flexbox-point flexbox-point2"></div>
<div class="flexbox-point flexbox-point3"></div>
</div>
</div>

Related

How do i set a background image for a division with an image already in it?

I've ran out of hope for this for the past few days,what I'm basically trying to do is to do this:
CSS:
.div1{
/* background-image code */
}
HTML:
<div class="div1">
<!--Image here-->
</div>
Is it even possible to have a background image larger than the image in the div itself?
See the following example to achieve what you are looking for. Basically you can combine a color and an image by using both the background-color and background-image props at the same time. Position and scale the image with background-size and background-position. background-repeat: no-repeat; is important to be able to see the area that is the simple color background.
.div1 {
background-color: blue;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: 50%;
background-position: center;
background-repeat: no-repeat;
width: 400px;
height: 300px;
}
<div class="div1">
</div>
For two images layered in this way:
.div1 {
background-image: url(https://www.realtree.com/sites/default/files/styles/site_xl/public/content/inserts/2022/imagebybarriebird-ducklings.jpg);
width: 400px;
height: 300px;
position: relative;
color: white;
background-size: 50%;
background-repeat: no-repeat;
background-position: center;
/*to center the text */
display: flex;
align-items: center;
justify-content: center;
}
.div1::before {
content: "";
position: absolute;
width: 100%;
height: 100%;
background-image: url(https://thumbs.dreamstime.com/b/forrest-27720334.jpg);
background-size: cover;
/*to set this image layer behind the duck one */
z-index: -1;
}
<div class="div1">
Example content text
</div>
you can add width and height in each img and background-image
.div1{
width: 100vw;
height: 500px;
/* background-image code */
}
img {
width : 200px;
height : 200px;
}
<div class="div1">
<img src="code.png" alt="">
<!--Image here-->
</div>
Give the img some padding and put the background image on it.
div {
width: fit-content;
}
img {
background-image: url(https://picsum.photos/id/1015/300/300);
background-size: cover;
padding: 30px;
}
<div>
<img src="https://picsum.photos/id/1016/200/300">
</div>

The box is not centering and is also moving the background image along with it

I want to centre the black box on the webpage, but whenever I try to adjust the padding of #banner_image the black box starts moving up and down and whenever I try to adjust margins of #banner_content which contains the black box to center it the bg image starts scrolling.
I'm not being able to understand what is happening around here, can someone please explain?
#banner_image{
background: url(../Images/intro-bg_1.jpg) no-repeat center center;
padding-top: 10%;
padding-bottom: 25%;
text-align: center;
color: #f8f8f8;
background-size: cover;
}
#banner_content{
background-color: rgba(0, 0, 0, .7);
max-width: 660px;
position: relative;
padding-top: 6%;
padding-bottom: 6%;
margin-top: 6%;
margin-bottom: 6%;
margin-left: auto;
margin-right: auto;
}
<div id="banner_image">
<div class="container">
<div id="banner_content">
</div>
</div>
</div>
<style>
#banner_image{
background: url(./bg.jpg) no-repeat center center;
/* Adjust the values as you need them */
width: 100vw;
height: 100vh;
text-align: center;
color: #f8f8f8;
background-size: cover;
}
/* Use the container to center the black box */
.container {
position: relative;
/* Adjust values to what you need. Remember to always have a set width and height if you want your elements to not move around the page and cause cumulative layout shift problems */
width: 100vw;
height: 100vh;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
#banner_content{
background-color: rgba(0, 0, 0, .7);
min-width: 400px;
min-height: 400px;
}
</style>
<div id="banner_image">
<div class="container">
<div id="banner_content">
</div>
</div>
</div>

justify content space evenly not showing the sub div to the center

i have a parent container inside two sub containers are there each container have a fixed height and width in each sub containers i have a loader loader gif.
.parent-container{
display: flex;
justify-content: space-evenly;
border: 7px solid black
}
.sub-container-one{
width:400px;
height: 300px;
border: 3px solid blue;
}
.sub-container-two{
width: 300px;
height: 300px;
border: 3px solid red;
}
.loader{
display: block;
top: 0;
left: 0;
width: 50%;
height: 100%;
position: absolute;
background-image: url('http://loadinggif.com/images/image-selection/3.gif');
background-repeat: no-repeat;
background-position: center;
}
.second-loader{
margin-left:50%;
}
<div class="parent-container">
<div class="sub-container-one">
<div class="loader"></div>
</div>
<div class="sub-container-two">
<div class="loader second-loader"></div>
</div>
</div>
how can is show the same loader in both sub div at the center , i have added one more class, right now loader is showing not to the center.
when i have given
justify-content: center
it works , but how can i align the loader to the center of the both div making
justify-content: space-evenly;
Also for IE will this work ?
It Worked. Made the css for the loader as below, removed the position and made width to 100%
.loader{
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('http://loadinggif.com/images/image-selection/3.gif');
background-repeat: no-repeat;
background-position: center;
}

unable to display the background image to full screen of the browser and make it responsive

The background image doesn't fits to the browser window's screen. The background image covers only a portion of the screen maintaining its aspect ratio.I want the background image to cover the entire screen on which it runs.Please help!
html
<div class="container">
<h1> GAME</h1>
<div class="color-overlay"></div>
</div>
css
.container {
background-size: cover;
background: #000 url(images/group11.jpg);
color: #fff;
background-repeat:no-repeat;
background-position:center;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.color-overlay {
width: 100%;
height: 100%;
background: blue;
opacity: .6;
position: absolute;
}
The problem is that you first set background-size:cover then you set background .
Setting background ( which includes repeat,size,position,image etc. ) after the background-size will overwrite cover with the default setting.
You either set background-size:cover after the background either you set all background declarations that you need ( color,repeat,image etc ) separately, either you declare all in one line using just background
See below
.container {
background: #000 url(http://via.placeholder.com/350x150);
/* bg size after */
background-size: cover;
color: #fff;
background-repeat: no-repeat;
background-position: center;
display: flex;
justify-content: center;
align-items: center;
position: relative;
height:100vh;
/* OR set all separately
background-image: url(http://via.placeholder.com/350x150);
background-color: #000;
background-size:cover;
background-repeat:no-repeat;
background-position:center;
*/
/* OR set all in one line
background: url(http://via.placeholder.com/350x150) no-repeat scroll center center / cover #000;
*/
}
.color-overlay {
width: 100%;
height: 100%;
background: blue;
opacity: .6;
position: absolute;
}
<div class="container">
<h1> GAME</h1>
<div class="color-overlay"></div>
</div>

Why doesn't object-fit affect width or height?

I have a 240*240px image inside a 100*300px div (demo values, actual values vary and are unknown). I use object-fit: contain to make the image completely visible inside the div and also keep it's aspect ratio. The problem is that object-fit isn't modifying the width of the image, resulting in a weird "padding" (so to say).
How can I make the image take only as much width as required, instead of taking the original width?
Demo: http://codepen.io/alexandernst/pen/ONvqzN
.wrapper {
height: 100px;
width: 300px;
border: 1px solid black;
}
.flex {
display: flex;
}
img {
object-fit: contain;
}
<div class="flex wrapper">
<img src="https://unsplash.it/240/240" />
</div>
The object-fit property normally works together with width, height, max-width and max-height. Example:
.wrapper {
height: 100px;
width: 300px;
border: 1px solid black;
}
.flex {
display: flex;
}
img {
object-fit: contain;
height: 100%;
width: auto;
}
<div class="flex wrapper">
<img src="https://unsplash.it/240/240" />
</div>
In fact, it works fine too even without object-fit, see this jsFiddle.
.wrapper {
height: auto;
width: 100%;
border: 1px solid black;
background-image: url(https://unsplash.it/240/240);
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
.flex {
display: flex;
}
img {
object-fit: contain;
}
<div class="flex wrapper">
<img src="https://unsplash.it/240/240" />
</div>