Bootstrap Grid - Square Tiles with same horizontal/vertical gap - html

I'm trying to create a Grid of Tiles with Bootstrap with the following properties:
all tiles should be squares
horizontal and vertical gaps should be the same (even the same compared on different screen-widths)
the tiles should always be centered in the middle of the screen(distance from the left edge of website to the left border of the leftmost tile = distance from the right edge of the website to the right border of the righmost tile)
It should look like this:
body{
background: #a5b5c5;
background:lightblue !important;
}
.box{
height: 180px;
width: 180px;
background: #fff;
border-radius: 4px;
}
.col-lg-2, .col-md-3, .col-xs-6{
margin-top: 12px !important;
}
<div class="container">
<div class="row">
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
<div class="col-lg-2 col-md-3 col-xs-6 ">
<div class="box"></div>
</div>
</div>
</div>
What should I do to accomplish a responsiveness which sustains my properties?
Thanks!!

You didn't mention specific box dimensions, only that they should remain square. In that case, use margin-top:30px to corresponding with the Bootstrap gutter width, and padding-bottom: 100%; on the box.
.box {
background: #fff;
border-radius: 4px;
padding-bottom: 100%;
}
.col-lg-2, .col-md-3, .col-xs-6{
margin-top: 30px !important;
}
Bootstrap 3 demo
Bootstrap 4 demo
Note: Setting a px size on the .box (ie:width:180px;height:180px) will prevent the boxes from resizing responsively.
You can increase the gutter by changing margins and padding. Margin is double the padding for example...
.col-lg-2, .col-md-3, .col-xs-6{
padding-left: 30px;
padding-right: 30px;
margin-top: 60px;
}

try below code
<div class="container">
<div class="row">
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
</div>
<div class="row">
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
<div class="col-xs-3 col-sm-4 col-md-2">
<div class="box"></div>
</div>
</div>
</div>
body{
background: lightblue;
}
.container{
margin-top: 20px;
}
.box{
height: 100px;
background: #fff;
border-radius: 5px;
margin-bottom: 10px;
max-width: 100px;
}

Related

Why is my container-fluid div not including the box divs as well?

I am trying to make a dice with the help of bootstrap grid. Although I am not done with it yet, here is small snippet of code containing what I have written. When I am inspecting my webpage using dev tools (picture attached), my container-fluid div which basically includes all my code is not displaying to contain the box divs?
Can you explain me why so?
I have attached my HTML and CSS code as well.
body {
background-color: rgb(41, 36, 36);
text-align: center;
font-family: 'Bungee Spice', cursive;
margin: auto;
}
h2 {
margin-bottom: 10%;
margin-top: 10%;
}
.result {
font-size: 500%;
margin: 5% auto;
}
.box {
width: 60%;
height: 180%;
/* padding-bottom: 60%; */
background-color: yellow;
margin: auto;
border-radius: 10%;
}
.container-fluid {
margin-bottom: 5%;
}
/* .circle1 {
background-color: black;
width: 100%;
height: 100%;
border-radius: 50%;
} */
/* .box-row {
width: 100%;
padding-bottom: 10%;
margin: auto;
background-color: red;
} */
#media (max-width: 1300px) {
.result {
font-size: 400%;
}
}
#media (max-width: 1100px) {
.result {
font-size: 300%;
}
}
#media (max-width: 991px) {
button {
display: block;
}
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bungee+Spice&display=swap" rel="stylesheet">
<h1 class="result">Welcome, <span class="playerName"></span></h1>
<div class="container-fluid">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3"></div>
<div class="col-lg-3 col-md-3 col-sm-3">
<h2>You</h2>
<div class="box">
<div class="row box-row">
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle1"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle2"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle3"></div>
</div>
</div>
<div class="row box-row">
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle4"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle5"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle6"></div>
</div>
</div>
<div class="row box-row">
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle7"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle8"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle9"></div>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-3">
<h2>Computer</h2>
<div class="box">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle1"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle2"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle3"></div>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle4"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle5"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle6"></div>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle7"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle8"></div>
</div>
<div class="col-lg-4 col-md-4 col-sm-4 col-4">
<div class="circle9"></div>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-3"></div>
</div>
</div>
<button type="button" class="btn btn-outline-warning">Roll</button>
I see you have commented out the only style that would apply to the dots inside of the yellow boxes, but in general, the issue is that the bootstrap grid doesn't inherit any height, and the height: 100% that you have defined comes out as 0. If you would change the .circle1 height to a fixed size or apply a height to the dots row, then it would show up.

CSS - completely remove spacing between columns?

I have this HTML:
<div class="container">
<div class="box center-block">
<div class="row">
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4">
<div class="thumbnail">A</div>
</div>
</div>
</div>
</div>
And CSS:
.box {
width: 300px;
padding-top: 100px;
}
.row > div > div {
width: 100px;
height: 100px;
margin: 0px;
}
I'm also using bootsrap (version 3).
I was able to remove margin between rows, but can't remove margin between columns. Is there a way to force it somehow?
I need all "squares" next to each other without any margins (both between rows and columns).
You can see how it looks here: https://jsfiddle.net/a91zujkj/
If you really want to use col- class, make sure to set the .thumbnail elements width to 100% and use a no-padding class on the col- elements.
Here is an example:
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
.box {
width: 300px;
padding-top: 100px;
}
.no-padding {
padding-left: 0;
padding-right: 0;
}
.thumbnail {
width: 100%;
height: 100px;
margin: 0;
}
<div class="container">
<div class="box center-block">
<div class="row">
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 no-padding">
<div class="thumbnail">A</div>
</div>
</div>
</div>
</div>
You actually do not need to use col- classes at all and floats are not needed either. Another way to do this is using display: inline-block.
Here is how that looks:
#import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css');
.wrapper {
width: 300px;
padding-top: 100px;
margin: 0 auto;
font-size: 0;
}
.item {
border: 1px solid #eee;
width: 100px;
height: 100px;
display: inline-block;
font-size: initial;
}
<div class="wrapper">
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
</div>
And if you're really brave you could also use CSS Grid layout. ;)
CSS Grid Layout
CSS Grid layout excels at dividing a page into major regions, or defining the relationship in terms of size, position, and layer, between parts of a control built from HTML primitives.
Here is how that goes:
.wrapper {
padding-top: 100px;
width: 300px;
margin: 0 auto;
display: grid;
grid-template-columns: repeat(3,100px);
grid-template-rows: repeat(3,100px);
}
.item {
border: 1px solid #ddd;
height: 100px;
width: 100px;
}
<div class="wrapper">
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
<div class="item">A</div>
</div>
It's because .col-*-4 is wider than 100px, which is the width you've defined for the .thumbnail elements.
You can just remove the .col-* classes and use .pull-left instead.
.box {
width: 300px;
padding-top: 100px;
}
.row > div > div {
width: 100px;
height: 100px;
margin: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="box center-block">
<div class="row">
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
<div class="pull-left">
<div class="thumbnail">A</div>
</div>
</div>
</div>
</div>
Remove padding-left and padding-right for .col classes and then change width: 100px; to width: 100%; for .row > div > div
.box {
width: 300px;
padding-top: 100px;
}
.padding-0{
padding-right:0 !important;
padding-left:0 !important;
}
.row > div > div {
width: 100%;
height: 100px;
margin: 0px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="box center-block">
<div class="row">
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
<div class="col-md-4 col-xs-4 padding-0">
<div class="thumbnail">A</div>
</div>
</div>
</div>
</div>
Row has it's own style and add margin to element, you can remove it by adding :
.box >.row{
margin:0;
}

Creating a black and white grid HTML/CSS

I am trying to create a 3 by 3 black and white grid. But instead only black square is showing. I have tried using Bootstrap.
My one square of the grid should cover 3 grids space as per bootstrap grid system and I am using padding-top:100% to maintain the aspect ratio for the squares in the 3 by 3 grid.
body {
margin: 0;
color: pink;
}
.square {
width: 100%;
padding-top: 100%;
background-color: black;
}
.square-w {
width: 100%;
padding-top: 100%;
background-color: white;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<div class="square"></div>
</div>
<div class=" col-md-3 ">
<div class="square-w"></div>
</div>
<div class=" col-md-3 ">
<div class="square"></div>
</div>
</div>
<div class="row">
<div class=" col-md-3">
<div class="square-w"></div>
</div>
<div class="col-md-3 ">
<div class="square"></div>
</div>
<div class="col-md-3">
<div class="square-w"></div>
</div>
</div>
<div class="row">
<div class="col-md-3 ">
<div class="square"></div>
</div>
<div class="col-md-3 ">
<div class="square-w"></div>
</div>
<div class="col-md-3 ">
<div class="square"></div>
</div>
</div>
</div>
Your screen view might be too small.
Changing all the col-md-3 classes to col-sm-3 allows it to be shown on my view
JSFiddle: https://jsfiddle.net/2k5523ym/1/
You just need to put below code in css
.nopadding {
padding: 0 !important;
margin: 0 !important;
}
and then assign nopadding class to every col-md-3 div
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-md-3 nopadding">
<div class="square"></div>
</div>
<div class=" col-md-3 nopadding">
<div class="square-w"></div>
</div>
<div class=" col-md-3 nopadding">
<div class="square"></div>
</div>
</div>
<div class="row">
<div class=" col-md-3 nopadding">
<div class="square-w"></div>
</div>
<div class="col-md-3 nopadding">
<div class="square"></div>
</div>
<div class="col-md-3 nopadding">
<div class="square-w"></div>
</div>
</div>
<div class="row">
<div class="col-md-3 nopadding">
<div class="square"></div>
</div>
<div class="col-md-3 nopadding">
<div class="square-w"></div>
</div>
<div class="col-md-3 nopadding">
<div class="square"></div>
</div>
</div>
</div>
Hope this help you

first element of div inside div

Code
<div class="row mgu-w-tdw-brow">
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-widget-teaser-1x1">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-teaser-list-1x1-large">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-teaser-list-1x1-large">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-widget-teaser-1x1">...</div>
</div>
CSS
.mgu-w-tdw-brow div.mgu-w-tdw-box:first-child { border: 2px solid green; }
I'd like to approach the first element of "mgu-w-tdw-box" inside the row "mgu-w-tdw-brow". Unfortunately this doesn't work.
thanks for help.
this is what you need, first select div:first-child and style inside div
.mgu-w-tdw-brow div:first-child .mgu-w-tdw-box {
border:2px solid green;
}
<div class="row mgu-w-tdw-brow">
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-widget-teaser-1x1">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-teaser-list-1x1-large">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-teaser-list-1x1-large">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-widget-teaser-1x1">...</div>
</div>
</div>
I would prefer a better readable version which does the same for this example.
.mgu-w-tdw-brow >:first-child {
border: 2px solid green;
}
Here is a jsfiddle for it.
.mgu-w-tdw-brow [class*="col"]:nth-child(1) .mgu-w-tdw-box{
border:solid 2px #000;
}
<div class="row mgu-w-tdw-brow">
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-widget-teaser-1x1">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-teaser-list-1x1-large">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-teaser-list-1x1-large">...</div>
</div>
<div class="col-xs-6 col-sm-6 col-md-3">
<div class="mgu-w-tdw-box mgu-widget-teaser-1x1">...</div>
</div>
</div>

Keep Divs at same height

I'm trying to figure out how to keep all of the rows the same height. The problem I run into is that when they have no content they shrink down to just the padding height.
The full example can be found here: https://jsfiddle.net/c85ajj9a/
Code snippet:
<div class="col-md-12">
<div class="col-md-1 detail-box leftcolumn-bold">Co-Bor 1:</div>
<div class="col-md-1 detail-box">Maria</div>
<div class="col-md-1 detail-box">Smith</div>
<div class="col-md-2 detail-box">699, 685, 675</div>
<div class="col-md-1 detail-box">$6,500.00</div>
<div class="col-md-2 detail-box email-text">maria.smith#gmail.com</div>
<div class="col-md-2 detail-box">714-555-5555</div>
<div class="col-md-2 detail-box">626-555-5555</div>
</div>
<div class="col-md-12">
<div class="col-md-1 detail-box leftcolumn-bold">Co-Bor 2:</div>
<div class="col-md-1 detail-box"></div>
<div class="col-md-1 detail-box"></div>
<div class="col-md-2 detail-box"></div>
<div class="col-md-1 detail-box"></div>
<div class="col-md-2 detail-box email-text"></div>
<div class="col-md-2 detail-box"></div>
<div class="col-md-2 detail-box"></div>
</div>
a min-height will add the height to the rows even if there is no content.
.detail-box {
padding: 5px;
overflow: auto;
border: 1px solid #000000;
text-align: center;
font-size:18px;
color:black;
min-height:18px;
}