Set DIV to size of background-image - html

So I am trying to figure out, how I can set the size of the second Box (see screenshot) to the size of the background image. I am using bootstrap and it's build flex classes. Maybe you guys can help me out here.
Essentially what I would like to have is the purple box to be the same size of the background image and keep being the same size on resizing the screen.
To test things out, I provided a prepared Stackblitz environment: https://stackblitz.com/edit/javascript-rs3ykl?file=index.html
.page-container {
width: 100vw;
height: 50vh;
}
.background-image {
background: url('https://dummyimage.com/258x362/007aa5?text=Banner-Desktop-Large') center center / contain no-repeat;
height: 100%;
width: auto;
background-origin: content-box;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="page-container">
<div class="row w-100 h-100">
<div class="col-4 bg-primary">
<p>Box 1</p>
</div>
<div class="col-4">
<div class="background-image">
<div style="background: purple">
<div class="d-flex justify-content-around" style="color: rgb(255, 255, 255)">
<div class="d-flex flex-column">
<div>Tage</div>
<div>16086</div>
</div>
<div class="d-flex flex-column">
<div>Stunden</div>
<div class="">6</div>
</div>
<div class="d-flex flex-column">
<div>Minuten</div>
<div>13</div>
</div>
<div class="d-flex flex-column">
<div>Sekunden</div>
<div>34</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-4 bg-secondary">
<p>Box 3</p>
</div>
</div>
</div>

I think it's better to use image tag instead of url with div. so I changed it like this
<link
href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<style>
.page-container {
width: 100vw;
height: 50vh;
}
.background-image {
height: 100%;
width: auto;
background-origin: content-box;
}
.bk-image {
object-fit: cover;
width: 100%;
height: 100%;
}
</style>
<div class="page-container">
<div class="row w-100 h-100">
<div class="col-4 bg-primary"><p>Box 1</p></div>
<div class="col-4">
<div class="background-image">
<div style="background: purple">
<div class="d-flex justify-content-around" style="color: rgb(255, 255, 255)">
<div class="d-flex flex-column">
<div>Tage</div>
<div>16086</div>
</div>
<div class="d-flex flex-column">
<div>Stunden</div>
<div class="">6</div>
</div>
<div class="d-flex flex-column">
<div>Minuten</div>
<div>13</div>
</div>
<div class="d-flex flex-column">
<div>Sekunden</div>
<div>34</div>
</div>
</div>
</div>
<img class="bk-image" src = "https://dummyimage.com/258x362/007aa5?text=Banner-Desktop-Large">
</div>
</div>
<div class="col-4 bg-secondary"><p>Box 3</p></div>
</div>
</div>
and now the purple bar stays with the background picture, also since you used height 100% I did it too but if you want it to stay inline with the row you can set the height as the same as row height.

Related

Centering a bootstrap grid on page

I have been trying to get a Bootstrap grid to center vertically on my page, but can't seem to get it to center. Horizontal center works fine.
HTML:
<div class="container-fluid">
<div class="board vh-100">
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">A</div>
<div class="col-4 tic-box">B</div>
<div class="col-4 tic-box">C</div>
</div>
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">D</div>
<div class="col-4 tic-box">E</div>
<div class="col-4 tic-box">F</div>
</div>
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">G</div>
<div class="col-4 tic-box">H</div>
<div class="col-4 tic-box">I</div>
</div>
</div>
</div>
CSS:
.tic-box {
border: solid black 1px;
font-size: 3vw;
text-align: center;
width: 90px;
height: 90px;
}
Thanks for any help!
Since your board element has a specific height v-100 the you can add the following css attributes to your board element to center its contents.
.board{
display:flex;
flex-direction: column;
justify-content:center;}
Hi I think this is the cleanest approach to do what you require!
.tic-box {
border: solid black 1px;
font-size: 3vw;
text-align: center;
width: 90px;
height: 90px;
}
<!doctype html>
<html lang="en">
<head>
<title>Center</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS v5.0.2 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid d-flex align-items-center min-vh-100">
<div class="board mx-auto">
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">A</div>
<div class="col-4 tic-box">B</div>
<div class="col-4 tic-box">C</div>
</div>
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">D</div>
<div class="col-4 tic-box">E</div>
<div class="col-4 tic-box">F</div>
</div>
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">G</div>
<div class="col-4 tic-box">H</div>
<div class="col-4 tic-box">I</div>
</div>
</div>
</div>
<!-- Bootstrap JavaScript Libraries -->
<script src="https://cdn.jsdelivr.net/npm/#popperjs/core#2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
</body>
</html>
I Got it -
body{
justify-content: center;
align-items: center;
display: flex;
}
.tic-box {
border: solid black 1px;
font-size: 3vw;
text-align: center;
width: 90px;
height: 90px;
}
<div class="container-fluid">
<div class="board vh-100">
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">A</div>
<div class="col-4 tic-box">B</div>
<div class="col-4 tic-box">C</div>
</div>
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">D</div>
<div class="col-4 tic-box">E</div>
<div class="col-4 tic-box">F</div>
</div>
<div class="row t-row justify-content-center">
<div class="col-4 tic-box">G</div>
<div class="col-4 tic-box">H</div>
<div class="col-4 tic-box">I</div>
</div>
</div>
</div>

SVG Gets Cut Off

The svg i have position is inside a container that occupies the bottom 40% of the page. This is because the lower half of the image is undesirable. However, as a side effect, the svg gets cut off near the text. I have tried overflow: visible; but hasn't worked. What else can I try? This is the code:
HTML (Bootstrap Used):
<header class="h-60 w-100 text-white hidden">
<div class="container-fluid h-100 w-100">
<div class="row h-100">
<div class="container d-flex align-items-center justify-content-center w-100 h-100">
<div class="col text-center w-100 heading">
<h1 class="reveal0">Ambrosia<h1>
<h3 class="reveal5">An Open-Source Website Theme</h3>
<br>
See My Other Work!
</div>
</div>
</div>
</div>
</header>
<div class="container-fluid h-40">
<div class="row h-100">
<div class="container d-flex align-items-center justify-content-center h-100">
<div class="col h-100 w-100 pic1">
</div>
<div class="col">
</div>
</div>
</div>
</div>
CSS:
.hidden {
overflow: hidden;
}
.pic1 {
position: fixed;
background: url("../img/pic1.svg");
background-position: center;
background-repeat: no-repeat;
background-attachment: scroll;
background-size: cover;
display: block;
margin: auto;
overflow: visible!important;
}
you can either use:
background-size: contain
which will make your image smaller, but without cutting it.
Or use:
background-position: top
to let it cut the bottom instead of the top

Bootstrap images in columns overlapping

I have been trying to get 3 images to be responsive using bootstrap columns. They seem to be responsive, but the problem is that the images overlap except for the last column which remains being normal.
<div class="row">
<div class="col-sm-2 offset-sm-3">
<img class="img-responsive" src="../assets/images/roy-creates-categories-02.png">
</div>
<div class="col-sm-2">
<img class="img-responsive" src="../assets/images/roy-creates-categories-02.png">
</div>
<div class="col-sm-2">
<img class="img-responsive" src="../assets/images/roy-creates-categories-02.png">
</div>
</div>
I would like all 3 columns to like the one all the way on the right.
I've tried adding class "img-responsive" & "img-fluid" and neither have worked.
I'm using bootstrap 4.0.
The images are overflowing their container. The image on the right is full height but it is overflowing its container. Using the bootstrap '.col-2' means that you are limiting the column widths based on the window width. You could use background css rules instead of img elements to ensure containment or coverage of images or you could just use .col-auto instead of .col-2 with .justify-content-center provided you have appropriately sized images.
All three examples are in the snippet.
.row {
min-height: 150px;
}
.col-auto {
background-color: green;
}
.col-sm-2 {
background-color: red;
}
.bg-image {
width: 100%;
height: 100%;
background-image: url("https://picsum.photos/100");
background-position: center;
background-size: contain;
background-repeat: no-repeat;
}
.bg-image.bg-image--cover {
background-size: cover;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="row justify-content-center">
<div class="col-auto">
<img class="img-fluid" src="https://picsum.photos/100">
</div>
<div class="col-auto">
<img class="img-fluid" src="https://picsum.photos/100">
</div>
<div class="col-auto">
<img class="img-fluid" src="https://picsum.photos/100">
</div>
</div>
<div class="row">
<div class="col-sm-2 offset-sm-3">
<div class="bg-image"></div>
</div>
<div class="col-sm-2">
<div class="bg-image"></div>
</div>
<div class="col-sm-2">
<div class="bg-image"></div>
</div>
</div>
<div class="row">
<div class="col-sm-2 offset-sm-3">
<div class="bg-image bg-image--cover"></div>
</div>
<div class="col-sm-2">
<div class="bg-image bg-image--cover"></div>
</div>
<div class="col-sm-2">
<div class="bg-image bg-image--cover"></div>
</div>
</div>

Bootstrap 4 row fill remaining height

I'm struggling to make the a row stretch to fill the rest of the available height. I tried adding h-100 to the row class but that causes a white space at the bottom of the screen. There must be a way to do it but I'm totally stumped.. Here is my code:
<div class="container-fluid h-100">
<div class="row justify-content-center h-100">
<div class="col-4 bg-red">
<div class="h-100">
<div class="row justify-content-center bg-purple">
<div class="text-white">
<div style="height:200px">ROW 1</div>
</div>
</div>
<div class="row justify-content-center bg-blue">
<div class="text-white">ROW 2</div>
</div>
</div>
</div>
<div class="col-8 bg-gray"></div>
</div>
</div>
codepen: https://codepen.io/ee92/pen/zjpjXW/?editors=1100
I'd like to make the the blue row (ROW 2) fill all the red space. Any suggestions?
Thanks
Use the Bootstrap 4.1 flex-grow-1 class...
https://codeply.com/go/Iyjsd8djnz
html,body{height:100%;}
.bg-purple {
background: rgb(48,0,50);
}
.bg-gray {
background: rgb(74,74,74);
}
.bg-blue {
background: rgb(50,101,196);
}
.bg-red {
background: rgb(196,50,53);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container-fluid h-100">
<div class="row justify-content-center h-100">
<div class="col-4 bg-red">
<div class="h-100 d-flex flex-column">
<div class="row justify-content-center bg-purple">
<div class="text-white">
<div style="height:150px">ROW 1 - fixed height</div>
</div>
</div>
<div class="row justify-content-center bg-blue flex-grow-1">
<div class="text-white">ROW 2 - grow remaining height</div>
</div>
</div>
</div>
<div class="col-8 bg-gray"></div>
</div>
</div>
Update 4.3.1: Another example using the vh-100 utility class
https://codeply.com/go/h3bZbM6eSS
.bg-purple {
background: rgb(48,0,50);
}
.bg-gray {
background: rgb(74,74,74);
}
.bg-blue {
background: rgb(50,101,196);
}
.bg-red {
background: rgb(196,50,53);
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="container-fluid">
<div class="row justify-content-center min-vh-100">
<div class="col-4 bg-red">
<div class="d-flex flex-column h-100">
<div class="row justify-content-center bg-purple">
<div class="text-white">
<div style="height:150px">ROW 1 - fixed height</div>
</div>
</div>
<div class="row justify-content-center bg-blue flex-grow-1">
<div class="text-white">ROW 2 - grow remaining height</div>
</div>
</div>
</div>
<div class="col-8 bg-gray"></div>
</div>
</div>
Related: How to make the row stretch remaining height
This is a solution. The wrapper div has to have h-100, the div that adapts to height has to have flex-grow-1 and overflow-auto. This way, the div will grow to fill the space when its content is minor than the available space and will show the scrollbar when its content is higher than the available space.
Demo jsfiddle
<div class="h-100 d-flex flex-column bg-yellow px-2">
<div class="flex-column justify-content-center bg-purple px-2">
<div class="text-white p-0" style="height:50px">HEADER</div>
</div>
<div class="flex-column justify-content-center bg-red text-white px-2 flex-grow-1 overflow-auto">
<div>Item1</div>
<div>Item2</div>
INNER text 1<br>
INNER text 2<br>
</div>
<div class="flex-column justify-content-center bg-darkblue px-2">
<div class="text-white p-0" style="height:50px">FOOTER</div>
</div>
If anyone has tired of getting this to work, here is a super easy solution which will work in most scenarios. Just add the below class to the div for which you are trying to fill the remaining height:
Please note that 100vh is 100% of visible height and 200px is the total fixed height of the remainging divs above.
.fillRemaining {
height: calc(100vh - 200px);
}

Vertical and horizontal centering of the block

I am trying to center red block Horizontally and Vertically as in the picture below, but unfortunately red block is placed in the wrong place. I use bootstrap 4. Сan anyone say what's wrong??
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<div class="container-fluid">
<div class="row header-layout">
*Some content*
</div>
<div class="row content-layout">
<div class="col-6 left-side">
*Some content*
</div>
<div class="col-6 right-side">
<div class="row right-title">
*Some Text*
</div>
<div class="row parent">
<div class="child">
*Some content*
</div>
</div>
</div>
</div>
</div>
You can use flexbox for effortless centering of element.
Here's a full guide for Flexbox
.parent {
height: 100px;
width: 100px;
background: black;
display: flex;
align-items: center;
justify-content: center; }
.child {
height: 50px;
width: 50px;
background: red; }
<div class="parent">
<div class="child"></div>
</div>
i just change some bootstrap class and css. hope it help :)
div {
border: thin solid black;
}
.child {
position: relative;
margin: auto;
width: 60px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<div class="container-fluid">
<div class="row header-layout">
*Container content*
</div>
<div class="row content-layout">
<div class="col-6 left-side">
*left content*
</div>
<div class="col-6 right-side">
<div class="row right-title">
*Right Text*
</div>
<div class="row">
<div class="child">
*Child content*
</div>
</div>
</div>
</div>
</div>
You can do this using the new Bootstrap 4 flexbox utilities..
<div class="container-fluid h-100 d-flex flex-column">
<div class="row header-layout">
*Some content*
</div>
<div class="row content-layout flex-grow">
<div class="col-6 left-side d-flex flex-column">
<div class="row">
<div class="col-12 text-center">content</div>
</div>
<div class="row flex-grow">
<div class="col-12 text-center my-auto">content</div>
</div>
</div>
<div class="col-6 right-side d-flex flex-column">
<div class="row right-title">
<div class="col-12 text-center">title</div>
</div>
<div class="row parent flex-grow">
<div class="child col-6 mx-auto my-auto">
<div class="card card-block">child</div>
</div>
</div>
</div>
</div>
</div>
http://www.codeply.com/go/6nALguHQyw
Just make sure the body,html are 100% height. Also, I added a flex-grow class to make content fill the remaining height. In the future, this may be added as a util class so that you won't need this extra CSS.