How to make a span with sticky element float through 2 bootstrap's container divs?
Right now I couldn't make the span element stick on top of the container div, the container will always stay below of the span element. Also, When shrink into mobile view, I would like to have content 1 stay above then follow by span element with image then lastly is content 2. Each of the container has a background-image with it, so I want the span element can move up and down through 2 containers. Thank you so much in advance.
This is what I expected result:
Here is the jsfiddle link: https://jsfiddle.net/oafwmghd/2/
#sticky {
position: -webkit-sticky;
position: sticky;
display: flex;
display: -ms-flex;
display: -webkit-flex;
justify-content: center;
margin-top: 30px;
padding-bottom: 80px;
top: 0;
z-index: 1;
}
<div class="container">
<span><img
src="https://previews.123rf.com/images/sooolnce/sooolnce1611/sooolnce161100038/66323541-realistic-plastic-bottle-for-water-on-a-transparent-background-vector-illustration.jpg"
width=20% height=50% id="sticky"></span>
<section>
<div class="container" style="height:500px; background-color: green;">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-0">
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
Content 1
</div>
</div>
</div>
</section>
<section>
<div class="container" style="height:500px; background-color: red;">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-0">
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
Content 2
</div>
</div>
</div>
</section>
</div>
You might not need these empty elements if you use margin-left:auto (class : ml-auto).
To reorder elements, you need a flex or a grid container , you may reset container to a column flex box (class : d-flex flex-column)
There is nor ordering responsive class (as much as i know), you may create the classes and the breaking point you need and remove t the same time the sticky behavior.
A negative margin or absolute position could be used to avoid image pushing the content down . let's try with position or margin.
An approach could be :
#sticky {
position: -webkit-sticky;
position: sticky;
display: flex;
display: -ms-flex;
display: -webkit-flex;
justify-content: center;
margin-top: 30px;
width: 50%;
top: 1em;
z-index: 1;
margin-left: 0;
border: 0;
order: -1
}
#sticky img {
position: absolute;
}
#media (max-width: 768px) {
.order-sm-1 {
order: 1;
}
#sticky.order-sm-2 {
order: 2;
}
#sticky img {
position: static;
}
.order-sm-3 {
order: 3;
}
#sticky {
position: static;
margin: auto;
width: auto;
}
}
/* demo purpose*/
section {
border: solid;
margin-bottom: 1em;
}
.row .ml-auto {
min-height: 300px;
background: green;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<!--
Bootstrap docs: https://getbootstrap.com/docs
-->
<div class="container d-flex flex-column">
<section class="row col-lg-6 col-md-6 col-sm-12 order-sm-2" id="sticky"><img src="https://images.containerstore.com/catalogimages/318610/ColoredStopperBottlesClear_x.jpg?width=1200&height=1200&align=center" width=80%></section>
<section class="order-sm-1">
<div class="container">
<div class="row">
<!--<div class="col-lg-6 col-md-6 col-sm-0"></div>-->
<div class="col-lg-6 col-md-6 col-sm-12 ml-auto">
<p><b>Play the snippet in full page mode and resize the window to checkout the behavior</b></p>
</div>
</div>
</div>
</section>
<section class="order-sm-3">
<div class="container">
<div class="row">
<!--<div class="col-lg-6 col-md-6 col-sm-0"></div>-->
<div class="col-lg-6 col-md-6 col-sm-12 ml-auto">
Content 2
</div>
</div>
</div>
</section>
</div>
follow up from comment :
#sticky {
position: -webkit-sticky;
position: sticky;
display: flex;
display: -ms-flex;
display: -webkit-flex;
justify-content: center;
margin-top: 30px;
width: 30%;
top: 4em;
z-index: 1;
margin-left: 0;
border: 0;
order: -1;
flex: 0 0 auto;
}
#sticky img {
position: absolute;
}
#media (max-width: 768px) {
.order-sm-1 {
order: 1;
}
#sticky.order-sm-2 {
order: 2;
}
#sticky img {
position: static;
}
.order-sm-3 {
order: 3;
}
#sticky {
position: static;
margin: auto;
width: auto;
}
}
/* demo purpose*/
section {
border: solid;
margin-bottom: 1em;
}
.row .ml-auto {
min-height: 200px;
background: green;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="container d-flex flex-column p-0">
<section class="row col-lg-6 col-md-6 col-sm-12 order-sm-2" id="sticky"><img src="https://images.containerstore.com/catalogimages/318610/ColoredStopperBottlesClear_x.jpg?width=1200&height=1200&align=center" width=80%></section>
<section class="order-sm-1">
<div class="container">
<div class="row">
<!--<div class="col-lg-6 col-md-6 col-sm-0"></div>-->
<div class="col-lg-6 col-md-6 col-sm-12 ml-auto">
<p><b>Play the snippet in full page mode and resize the window to checkout the behavior</b></p>
</div>
</div>
</div>
</section>
</div>
<section class="order-sm-3">
<div class="container">
<div class="row">
<!--<div class="col-lg-6 col-md-6 col-sm-0"></div>-->
<div class="col-lg-6 col-md-6 col-sm-12 ml-auto">
Content 2
</div>
</div>
</div>
</section>
</div>
<div class="container">
<section>
<div class="container">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 ml-auto">
Content 3
</div>
</div>
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 ml-auto">
Content 4
</div>
</div>
</div>
</section>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<style>
.row.one{
align-items: stretch;
justify-content: center;
display: flex;
}
.col-lg-6 { width:50%;}
.col-lg-12 { width:100%;}
</style>
<section>
<div class="container">
<div class="row one">
<div class="col-lg-6 col-md-6 col-sm-12" style="background:#006;padding:50px 0;">
Content 1
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12" style="background:#993;padding:50px 0;">
Content 2.1
</div>
<div class="col-lg-12 col-md-12 col-sm-12" style="background:#3FC;padding:50px 0;">
Content 2.2
</div>
</div>
</div>
</div>
</div>
</section>
Hope so this will work for you
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<style>
.row.one{
align-items: stretch;
justify-content: center;
display: flex;
}
.col-lg-6 { width:50%;}
.col-lg-12 { width:100%;}
</style>
<section>
<div class="container">
<div class="row one">
<div class="col-lg-6 col-md-6 col-sm-12" style="background:#006;padding:50px 0;">
Content 1
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12" style="background:#993;padding:50px 0;">
Content 2.1
</div>
<div class="col-lg-12 col-md-12 col-sm-12" style="background:#3FC;padding:50px 0;">
Content 2.2
</div>
</div>
</div>
</div>
</div>
</section>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<style>
.row.one{
align-items: stretch;
justify-content: center;
display: flex;
}
.col-lg-6 { width:50%;}
.col-lg-12 { width:100%;}
</style>
<section>
<div class="container">
<div class="row one">
<div class="col-lg-6 col-md-6 col-sm-12" style="background:#006;padding:50px 0; background:url(https://www.nationalgeographic.com/content/dam/travel/rights-exempt/2018-travel-photographer-of-the-year/magical-mountains/dawn-vestrahorn-iceland-mountains.jpg); background-size:cover;">
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12" style="background:#006;padding:50px 0; background:url(https://www.dw.com/image/48396304_303.jpg); background-size:cover;">
</div>
<div class="col-lg-12 col-md-12 col-sm-12" style="background:#006;padding:50px 0; background:url(https://amp.businessinsider.com/images/5ac3bdf57a74af1b008b4612-960-480.jpg); background-size:cover;">
</div>
</div>
</div>
</div>
</div>
</section>
Hop so this will help you
Related
I am trying to vertically center h5 in a div but I want to do it responsively, so those solutions where you set the parent's height to a certain amount of pixels aren't a solution for me.
The h5 with id "channelName" is what I am trying to vertically center in the parent div.
<div class="col-10 text-left channelTab"><h5 id="channelName">Name</h5></div>
#greyBox {
background-color: grey;
}
#title {
color: white;
}
.logo {
max-height: 50px;
max-width: 50px;
border-radius: 50%;
border: 3px solid white;
}
#channelName {
vertical-align: middle;
display: inline-block;
}
#channelRow {
background-color: red;
}
<div class="container-fluid">
<div class="row">
<div class="col-4"></div>
<div id="greyBox" class="col-4 text-center">
<div class="row">
<div class="col-12 text-center">
<h1 id="title">TITLE</h1>
<div class="row" id="channelRow">
<div class="col-2">
<img src="image.png" class="logo"></div>
<div class="col-10 text-left channelTab">
<h5 id="channelName">Name</h5>
</div>
</div>
<div class="col-6">
</div>
</div>
</div>
</div>
</div>
</div>
If this could be done more easily with some more changes to my code, any such tips would be welcome as I am really new to this and still learning.
Add this class to parent of h5 d-flex align-items-center and remove bottom margin of h5
This class (d-flex) will make the display of parent of h5 flex and the align-items-center will vertically center it.
#greyBox {
background-color: grey;
}
#title {
color: white;
}
.logo {
max-height: 50px;
max-width: 50px;
border-radius: 50%;
border: 3px solid white;
}
#channelName {
margin:0;
}
#channelRow {
background-color: red;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="col-4"></div>
<div id="greyBox" class="col-4 text-center">
<div class="row">
<div class="col-12 text-center">
<h1 id="title">TITLE</h1>
<div class="row" id="channelRow">
<div class="col-2">
<img src="image.png" class="logo"></div>
<div class="col-10 text-left d-flex align-items-center channelTab">
<h5 id="channelName">Name</h5>
</div>
</div>
<div class="col-6">
</div>
</div>
</div>
</div>
</div>
</div>
HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="content">1x1</div>
</div>
<div class="col-md-4">
<div class="content">1x2</div>
</div>
<div class="col-md-4">
<div class="content">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">2x1</div>
</div>
<div class="col-md-4">
<div class="content">2x2</div>
</div>
<div class="col-md-4">
<div class="content">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">3x1</div>
</div>
<div class="col-md-4">
<div class="content">3x2</div>
</div>
<div class="col-md-4">
<div class="content">3x3</div>
</div>
</div>
</div>
CSS
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.row{
padding: 5px;
}
I have managed to make vertical spaces between the columns by adding padding to the rows. But now the horizontal spaces between the contents are ways too much. How can I configure the spacing between them?
That large horizontal space is because of the fixed width of the content class if you remove that, you'll see it grow.
You can set the width of the content in % or add a margin to the content class.
.container {
text-align: center;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
}
.row{
padding:15px;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-4 col4">
<div class="content">1x1</div>
</div>
<div class="col-md-4 col4">
<div class="content">1x2</div>
</div>
<div class="col-md-4 col4">
<div class="content">1x3</div>
</div>
</div>
<div class="row ">
<div class="col-md-4 col4">
<div class="content">2x1</div>
</div>
<div class="col-md-4 col4">
<div class="content">2x2</div>
</div>
<div class="col-md-4 col4">
<div class="content">2x3</div>
</div>
</div>
<div class="row col4 col4">
<div class="col-md-4 col4">
<div class="content">3x1</div>
</div>
<div class="col-md-4 col4">
<div class="content">3x2</div>
</div>
<div class="col-md-4">
<div class="content">3x3</div>
</div>
</div>
</div>
CSS Grid to achieve that layout you desire:
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 5px;
position:absolute;
left:50%;
transform:translateX(-50%);
width:80%;
}
.item {
background-color: gray;
text-align: center;
font-size: 30px;
min-height:100px;
max-width: 350px;
}
<div class="grid-container">
<div class="item">1x1</div>
<div class="item">1x2</div>
<div class="item">1x3</div>
<div class="item">2x1</div>
<div class="item">2x2</div>
<div class="item">2x3</div>
<div class="item">3x1</div>
<div class="item">3x2</div>
<div class="item">3x3</div>
</div>
Using flexbox:
.flex-container {
display: flex;
/*Generates a flexbox layout with default flex direction as row */
width: 100%;
/* Not really required */
align-items: center;
/*Aligns contents vertically */
justify-content: center;
/*Aligns contents horizontally */
text-align: center;
/*Aligns further text in the center */
}
.item {
background-color: gray;
text-align: center;
font-size: 30px;
min-height: 400px;
width: 300px;
margin: 5px;
}
<div class="flex-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
<div class="flex-container">
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<div class="flex-container">
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>
Use float:left; with class content as below:
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
float: left;
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.row{
padding: 5px;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="content">1x1</div>
</div>
<div class="col-md-4">
<div class="content">1x2</div>
</div>
<div class="col-md-4">
<div class="content">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">2x1</div>
</div>
<div class="col-md-4">
<div class="content">2x2</div>
</div>
<div class="col-md-4">
<div class="content">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">3x1</div>
</div>
<div class="col-md-4">
<div class="content">3x2</div>
</div>
<div class="col-md-4">
<div class="content">3x3</div>
</div>
</div>
</div>
If I'm reading your request correctly, you are just asking how to make it look like the boxes are in the center of the page, and they are evenly spaced. see if this is what you're looking for:
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.col-md-4 {
margin: 5px -10px 0px -4px;;
}
play with the numbers until you get the desired location.
Though I would strongly suggest that you add your own class in addition to col-md-4 to the boxes, which will prevent this new setting to col-md-4 from affecting any future use of this bootstrap class.
in other words . . .
CSS:
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.box-move {
margin: 5px -10px 0px -4px;;
}
and HTML:
<div class="container">
<div class="row">
<div class="col-md-4 box-move">
<div class="content">1x1</div>
</div>
<div class="col-md-4 box-move">
<div class="content">1x2</div>
</div>
<div class="col-md-4 box-move">
<div class="content">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4 box-move">
<div class="content">2x1</div>
</div>
<div class="col-md-4 box-move">
<div class="content">2x2</div>
</div>
<div class="col-md-4 box-move">
<div class="content">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4 box-move">
<div class="content">3x1</div>
</div>
<div class="col-md-4 box-move">
<div class="content">3x2</div>
</div>
<div class="col-md-4 box-move">
<div class="content">3x3</div>
</div>
</div>
</div>
You can remove a lot of the stuff there and simplify it to make it responsive width a set margin:
.contents {
background-color: black;
color: white;
height: 500px;
margin: 20px 0px;
}
<div class="container-fluid text-center">
<div class="row">
<div class="col-md-4">
<div class="contents">1x1</div>
</div>
<div class="col-md-4">
<div class="contents">1x2</div>
</div>
<div class="col-md-4">
<div class="contents">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="contents">2x1</div>
</div>
<div class="col-md-4">
<div class="contents">2x2</div>
</div>
<div class="col-md-4">
<div class="contents">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="contents">3x1</div>
</div>
<div class="col-md-4">
<div class="contents">3x2</div>
</div>
<div class="col-md-4">
<div class="contents">3x3</div>
</div>
</div>
</div>
</div>
I have an image and a description of a panel panel-default. The image is pulled to the left and the description is on the right side.
The image has the img-responsive class so when I resize the window it fits the panel body.
The question is how can I achieve the same effect with the description. I would like to align it vertically to the panel and when I resize the window it should also fit the panel body.
What I have tried so far:
.container {
padding-top: 30px;
}
p {
font-size: 2vw;
}
.panel {
border-radius: 0;
}
.panel-body {
padding: 0;
}
.panel-description {
margin-top 12px;
padding-left: 3px;
}
.vertical-center {
display: flex;
align-items: center;
}
.no-padding {
padding: 0;
}
.no-padding-right {
padding-right: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="col-sm-4 col-lg-4">
<div class="panel panel-default">
<div class="panel-body">
Basic panel example
</div>
</div>
</div>
<div class="col-sm-4 col-lg-4">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="col-sm-4 col-md-4 no-padding-right">
<div class="panel-image">
<img src="http://via.placeholder.com/150x150" class="img-responsive" alt="img">
</div>
</div>
<div class="col-sm-8 col-md-8 no-padding">
<div class="panel-description vertical-center">
<p>Name<br>Surname</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-4 col-lg-4">
<div class="panel panel-default">
<div class="panel-body">
Basic panel example
</div>
</div>
</div>
</div>
</div>
I added
.vertical-center {
display: flex;
align-items: center;
}
to the .panel-description but it's not working.
What is the best solution for this problem?
https://jsfiddle.net/DTcHh/39282/
I made little change in your code and its working well now. Here is the updated code for your reference
.container {
padding-top: 30px;
}
p {
font-size: 2vw;
}
.panel {
border-radius: 0;
}
.panel-body {
padding: 0;
}
.panel-description {
margin-top 12px;
padding-left: 3px;
}
.no-padding-right {
padding-right: 0;
}
.main-class {
display: flex;
width: 100%;
flex-wrap: wrap;
align-items: center;
}
<div class="container">
<div class="row">
<div class="col-sm-4 col-lg-4">
<div class="panel panel-default">
<div class="panel-body">
Basic panel example
</div>
</div>
</div>
<div class="col-sm-4 col-lg-4">
<div class="panel panel-default">
<div class="panel-body">
<div class="row">
<div class="main-class">
<div class="col-sm-4 col-md-4 no-padding-right">
<div class="panel-image">
<img src="http://via.placeholder.com/150x150" class="img-responsive" alt="img">
</div>
</div>
<div class="col-sm-8 col-md-8 no-padding">
<div class="panel-description vertical-center">
<p>Name<br>Surname</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-4 col-lg-4">
<div class="panel panel-default">
<div class="panel-body">
Basic panel example
</div>
</div>
</div>
</div>
</div>
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.
I have the following 4 squares (two rows and two columns):
<section id="gallery">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
</div>
</div>
</section>
I would like them to be displayed in a 4 square fashion and that the grid takes the whole width and height of the page. I also want it to be responsive and I don't want any spaces between each square.
I tried setting the css on my section tag like this:
section
{
height: 100vh;
}
But that had no effect. Does anybody know how to achieve this please?
Thank you
Try this as the CSS
section#gallery
{
height:100%;
width:100%;
}
div.row
{
height:50%;
}
div.col-lg-6.col-md-6.col-sm-6.col-xs-12
{
width:50%;
}
I think you are looking for something like this? https://jsfiddle.net/01djtnyd/
note: I changed all classes to "-6" so you can see it work easily in the fiddle
add class no-padding to the divs that are wrapping the img div
in the css add this:
.no-padding {
padding: 0 !important;
margin: 0;
}
.no-padding > div {
background-repeat: no-repeat;
background-size: cover;
height: 50vh;
}
You can do this with css flexbox and jQuery. Here are two options to maintain the square shape of each div.
Option 1:
HTML:
<section id="gallery">
<div class="container-fluid">
<div class="row grid-wrapper">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 grid-block">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 grid-block">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
</div>
<div class="row grid-wrapper">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 grid-block">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 grid-block">
<div style="background-image: url(http://placehold.it/675x400)">
</div>
</div>
</div>
</div>
</section>
CSS:
section
{
height: 100vh;
}
.grid-wrapper{
display: flex;
flex-direction: row;
}
.grid-block{
margin: 0;
padding: 0;
background-color: #e2e2e2;
border: 1px solid #000;
}
Jquery:
$(document).ready(function(){
var grid_w = $(".grid-block").width();
$(".grid-block").height(grid_w);
});
Fiddle: https://jsfiddle.net/puxLmy1y/2/
Option 2 (without jQuery):
HTML: Same as above
CSS:
section
{
height: 100vh;
}
.grid-wrapper{
display: flex;
flex-direction: row;
}
.grid-block{
width: 50vh;
height: 50vh;
margin: 0;
padding: 0;
background-color: #e2e2e2;
border: 1px solid #000;
}
Fiddle: https://jsfiddle.net/puxLmy1y/3/