I have 2 blocks using bootstrap grid system.
On mobile view, I want the right block above the left one. The right block should also occupy 100% width of the screen.
My code so far:
<style>
.block {
border-radius: 8px;
box-shadow: 0 0 8px 4px rgba(0,0,0,0.1);
padding: 25px;
}
.block2 {
border-radius: 8px;
box-shadow: 0 0 8px 4px rgba(0,0,0,0.1);
}
</style>
<div class="container checkoutform">
<form class="row mx-n2">
<div class="col-sm-8 px-2">
<div class="block">
<!-- code -->
</div>
</div>
<div class="col-sm-4 px-2">
<div class="block2">
<!-- code -->
</div>
</div>
</form>
</div>
I have no idea how to proceed. Any help would be greatly appreciated
Try adding width: 100%; to the .block2 and .block style rules.
As well, try to switch the places of the block and block2 div elements.
It should be looking like that:
The CSS:
.block {
border-radius: 8px;
box-shadow: 0 0 8px 4px rgba(0,0,0,0.1);
padding: 25px;
width: 100%;
}
.block2 {
border-radius: 8px;
box-shadow: 0 0 8px 4px rgba(0,0,0,0.1);
width: 100%;
}
And the HTML:
<div class="container checkoutform">
<form class="row mx-n2">
<div class="col-sm-8 px-2">
<div class="col-sm-4 px-2">
<div class="block2">
<!-- code -->
</div>
</div>
<div class="col-sm-8 px-2">
<div class="block">
<!-- code -->
</div>
</div>
</form>
</div>
Please let me know if it worked!
Related
I have html code like this
<div class="container-fluid" style="height:calc(100vh - 120px)">
<div class="row h-100" style="margin-top:20px;">
<div class="col-lg-10 col-md-12 col-xs-12 h-100 div-border-shadow mx-auto " >
<div class="row mt-3">
<div class="col-lg-10 col-md-12 col-sm-12 text-center mx-auto">
<div id="gallery" class="clearfix">
#for (int i =0; i<8 ; i++)
{
<div class="gallery-div-format d-inline-block" style="width:200px; height:200px; background-color:red">
</div>
}
</div>
</div>
</div>
</div>
</div>
</div>
it makes a page like this
this is the css
.div-border-shadow {
width: 100%;
border: 1px solid #e2efef;
box-shadow: 0 0 7px 0 rgba(0, 0, 0, 0.29);
position: relative;
background: #fff;
border-radius: 10px;
margin-bottom: 15px;
}
.gallery-div-format {
margin-left: 10px;
margin-right: 10px;
border-radius: 10px;
box-shadow: 0 5px 5px 3px rgba(0,0,0,0.4);
}
but for checking the page being responsive(when I make the browser smaller) it becomes like this
as you can see the white background would not extend, I want the white background extends as long as the red box is repeating. which part of my code is wrong?
if you remove h-100 class from below tag it display correctly:
...
<div class="col-lg-10 col-md-12 col-xs-12 h-100 div-border-shadow mx-auto ">
...
</div>
....
My page is very simple : it is made of a navbar, and two panels.
I am trying to get my navbar fixed on top, and the two panels vertically centered (for any screen size or browsers).
These two panels are placed in a container.
I have been unsuccessful in vertically centering the container so far.
Here is what I have now :
Here is what I want to achieve :
Here is my code :
html {
overflow: hidden;
height: 100%;
}
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
.panel-default1 {
padding-top: 8px;
border-radius: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.10);
height: 400px;
width: 100%;
overflow: hidden;
margin: 0 auto;
position: relative;
}
.panel-default2 {
padding-top: 10px;
padding-right: -15px;
border-radius: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.10);
height: 400px;
width: 100%;
margin: 0 auto;
position: relative;
}
<body>
<nav class="navbar transparent navbar-static-top">
<div class="navbar-header">
<p>Welcome</p>
</div>
</nav>
<div class="container d-flex h-100">
<div class="row justify-content-center align-self-center">
<!-- 1st panel -->
<div class="col-md-4 mb-4" id="panel">
<div class="panel panel-default1">
<div class="panel-body">
<p>Blablabla</p>
</div>
<!-- panel body -->
</div>
<!-- panel-default1-->
</div>
<!-- col md 4 -->
<!-- 2nd panel -->
<div class="col-md-8 col-md-8" id="panel2">
<div class="panel panel-default2">
<div class="panel-body">
<p>Blablabla</p>
</div>
<!-- panel-body -->
</div>
<!-- panel-default2-->
</div>
<!-- col md 8 -->
</div>
<!-- row -->
</div>
<!-- container-->
</body>
As you can see from the HTML, I have tried to use Bootstrap's flexbox on container and row, without success.
Please advise a working solution to vertically center my container, except the navbar.
From what I notice, you want an element to be centered, but not using all 12 columns, so you can use mx-auto to center an element that has less than 12 columns. You also said
.noMX {
background-color: red;
}
.withMX {
background-color: green;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="noMX col-8">awts</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="withMX col-8 mx-auto">awts</div>
</div>
</div>
To center it vertically you can try this. First add height: 100% to the body and html, since I noticed you used h-100 meaning you want 100% height on the container. You also need to wrap the panels into another div so that the element that will be centered is the div wrapper rather than the panels. I also removed md in the columns so that you can the result in smaller viewport
html,
body {
height: 100%;
}
.wrapper {
background-color: Red;
}
#panel {
background-color: blue;
}
#panel2 {
background-color: green;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar transparent navbar-static-top">
<div class="navbar-header">
<p>Welcome</p>
</div>
</nav>
<div class="wrapper container h-100">
<div class="row d-flex align-items-center h-100">
<div class="col-12">
<div class="row">
<div class="col-4 mb-4" id="panel">
<div class="panel panel-default1">
<div class="panel-body">
<p>Blablabla</p>
</div>
</div>
</div>
<div class="col-8" id="panel2">
<div class="panel panel-default2">
<div class="panel-body">
<p>Blablabla</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
You should be able to just use col-4 and col-8 for columns inside row inside container
see here
see example
Edit:
to align them vertically you should be able to just add align-items-center class on a row, without additional css.
Updated example
try this
html {
overflow: hidden;
height: 100%;
}
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
.panel-default1 {
padding-top: 8px;
border-radius: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.10);
height: 400px;
width: 100%;
overflow: hidden;
margin: 0 auto;
position: relative;
background:red;
}
.panel-default2 {
padding-top: 10px;
padding-right: -15px;
border-radius: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.10);
height: 400px;
width: 100%;
margin: 0 auto;
position: relative;
background:green;
}
.navbar-header{
float: none;
padding: 20px;
background:black;
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<body>
<nav class="navbar transparent navbar-static-top text-center">
<div class="navbar-header">
<p>Welcome</p>
</div>
</nav>
<div class="container">
<div class="row">
<!-- 1st panel -->
<div class="col-md-4 mb-4" id="panel">
<div class="panel panel-default1">
<div class="panel-body">
<p>Blablabla</p>
</div>
<!-- panel body -->
</div>
<!-- panel-default1-->
</div>
<!-- col md 4 -->
<!-- 2nd panel -->
<div class="col-md-8 col-md-8" id="panel2">
<div class="panel panel-default2">
<div class="panel-body">
<p>Blablabla</p>
</div>
<!-- panel-body -->
</div>
<!-- panel-default2-->
</div>
<!-- col md 8 -->
</div>
<!-- row -->
</div>
<!-- container-->
</body>
I'm trying to put three divs side by side which works fine but when i add a border and a small gap between each div the 3rd div goes onto a new line. Is there a way to auto resize the divs so they fit?
HTML:
<div class="trendingContainer">
<div class="col-lg-4 trendingBox">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
</div>
CSS:
.trendingContainer {
margin: 0 auto;
}
.trendingBox {
border: 1px solid #e5e5e5;
background: #fff;
margin: 0 2px 0 0;
}
You can always use calc and percentage based widths:
.trendingBox {
border: 1px solid #e5e5e5;
background: #fff;
margin: 0 2px 0 0;
width: calc(100% / 3 - 4px);
}
If you want to support multiple browser widths separately you can use media queries:
#media(max-width: 600px) {
.trendingBox {
border: 1px solid #e5e5e5;
background: #fff;
margin: 0 2px 0 0;
width: calc(100% / 2 - 4px);
}
}
#media(min-width: 601px) {
.trendingBox {
border: 1px solid #e5e5e5;
background: #fff;
margin: 0 2px 0 0;
width: calc(100% / 3 - 4px);
}
}
You can use either float or flex model, but make sure you have border-box set. And when you are using margin along with a fixed layout like this, ensure the widths correctly:
* {
box-sizing: border-box;
}
.trendingContainer {
margin: 0 auto;
overflow: hidden;
}
.trendingBox {
border: 1px solid #e5e5e5;
background: #fff;
float: left;
width: 30%;
margin: 0 0.5%
}
<div class="trendingContainer">
<div class="col-lg-4 trendingBox">
<div class="block-title">
<h3>Trending</h3>
</div>
123
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="block-title">
<h3>Trending</h3>
</div>
123
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="block-title">
<h3>Trending</h3>
</div>
123
</div>
</div>
Preview
One more try, this time totally different, added a child DIV to specify the content with margin and border:
(This maintains the original sizing and other bootstrap stuff, only defines the additional styling on a separate div, which is more clean to use)
HTML:
<div class="trendingContainer">
<div class="col-lg-4 trendingBox">
<div class="trendingBox-content">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="trendingBox-content">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="trendingBox-content">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
</div>
</div>
CSS:
.trendingContainer {
margin: 0 auto;
}
.trendingBox {
/* Add other style here */
}
.trendingBox-content{
border: 1px solid #e5e5e5;
margin: 0 2px 0 0;
background: #fff;
}
You can try this.
.trendingContainer {
margin: 0 auto;
}
.trendingBox {
border: 1px solid #e5e5e5;
display: inline-block;
position: relative;
float: left;
background: #fff;
margin: 0 2px 0 0;
}
<div class="trendingContainer">
<div class="col-lg-4 trendingBox">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="block-title"><h3>Trending Big</h3> </div>
123
</div>
<div class="col-lg-4 trendingBox hidden-sm hidden-xs">
<div class="block-title"><h3>Trending</h3> </div>
123
</div>
</div>
.trendingBox {
border: 1px solid #e5e5e5;
background: #fff;
margin: 0 2px 0 0;
display: inline-block;
float: left;
width: calc(33.3% - 4px);
}
calc is new in css3 I believe.
4px is 2px(margin) + 2*1px(border)
Another thing you can try is using outline. I believe its width would is considered as a port of the box element, haven't tried though.
I believe rest all is clear
.trendingBox {
border: 1px solid #e5e5e5;
background: #fff;
margin: 0 2px 0 0;
box-sizing: border-box;
}
The box-sizing: border-box; does the trick. This makes sure the div doesn't resize when adding padding to it.
EDIT: See my other answer, which is the right way to do it in my opinion.
I'm creating a small website as a home project to show off my artworks. At the main page I want to show recently updated collections with the latest works stacked like a stack of cards with a small offset. Each stack gets its own grid column.
This is what I have so far: http://www.bootply.com/Dus21AApn5
<div class="container-flow">
<h2> Recent Collection Updates</h2>
<div class="row" style="margin: 0.05%;">
<div class="col-sm-4 col-xs-6">
<div style="position: absolute;
max-height: 200px;
margin: auto;
overflow: hidden;
top: 0px;
left: 0px;
z-index: -0;
-webkit-box-shadow: 2px 2px 1px 3px rgba(0,0,0,0.2);
-moz-box-shadow: 2px 2px 1px 3px rgba(0,0,0,0.2);
box-shadow: 2px 2px 1px 3px rgba(0,0,0,0.2);">
<img style="max-width: 200px;" class="img-responsive" src="http://placehold.it/250x350">
</div>
...
</div>
<div class="col-sm-4 col-xs-6"> ... </div>
<div class="col-sm-4 col-xs-6"> ... </div>
</div>
</div>
The code is a bit messy because it's generated by a template engine (I'll move all the styles to CSS-files later).
I got it to work so it looks like the images are stacked on each other but unfortunately at the same time it broke the responsiveness of the bootstrap grid. The grid columns don't stack anymore and just merge into each other. I figured out it's due to the position of the div, which contains the images, being set to absolute. I can't find a way to keep my image stack and at the same time the responsiveness.
I searched the internet for a while but didn't find a solution. Since I'm not a Web-(Front-End)-Developer I have very little experience with HTML/CSS and the like.
If someone could give me a solution or point me in the right direction so I can fix it myself, that would be nice.
I'd like to avoid additional JavaScript wherever possible.
Here is a solution, but due to all divs inside bootstrap grid cell (col-*...) are absolutely positioned, they forced to get their height by jquery script to avoid overlapping.
.imageBanner {
display: block;
position: relative;
margin: 0 auto;
padding: 0;
width: 100%;
height: 40vw;
}
#media screen and (max-width: 767px) {
.imgBanner {
height: 30vw;
}
}
.image1 {
left: 0;
top: 0;
z-index: -1;
}
.image2 {
top: 0;
left: 10%;
z-index: -2;
}
.image3 {
top: 0;
left: 20%;
z-index: -3;
}
.imageBanner div {
width: 75%;
max-height: 85%;
display: block;
position: absolute;
overflow: hidden;
-webkit-box-shadow: 2px 2px 1px 3px rgba(0,0,0,0.2);
-moz-box-shadow: 2px 2px 1px 3px rgba(0,0,0,0.2);
box-shadow: 2px 2px 1px 3px rgba(0,0,0,0.2);
}
.imageBanner div img {
width: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-flow">
<h2 class=""> Recent Collection Updates</h2>
<div class="row" style="margin: 0.05%;">
<div class="col-sm-4 col-xs-6">
<div class="imageBanner">
<div class="image1">
<img src="https://unsplash.it/600/400/" class="img-responsive">
</div>
<div class="image2">
<img src="https://unsplash.it/400/600/" class="img-responsive">
</div>
<div class="image3">
<img src="https://unsplash.it/500/500/" class="img-responsive">
</div>
</div>
</div>
<div class="col-sm-4 col-xs-6">
<div class="imageBanner">
<div class="image1">
<img src="https://unsplash.it/800/600/" class="img-responsive">
</div>
<div class="image2">
<img src="https://unsplash.it/200/200/" class="img-responsive">
</div>
<div class="image3">
<img src="https://unsplash.it/600/800/" class="img-responsive">
</div>
</div>
</div>
<div class="clearfix visible-xs-block"></div>
<div class="col-sm-4 col-xs-6">
<div class="imageBanner">
<div class="image1">
<img src="https://unsplash.it/300/400/" class="">
</div>
<div class="image2">
<img src="https://unsplash.it/600/600/" class="">
</div>
<div class="image3">
<img src="https://unsplash.it/400/200/" class="">
</div>
</div>
</div>
</div>
</div>
Also you can view another variant of images interposition here: https://stackoverflow.com/a/41048391/4206079
I am using Bootstrap and trying to center 3 div blocks using the bootstrap 12 columns style. So each div takes up 4 columns. I want some spacing between these columns thus I added 10px margin to the right. This pushes down the 3rd div to the next row. How do I get my spacing and still keep all 3 columns on the same row?
HTML
<div class="container">
<div class="form-group col-sm-4 col-lg-4 cust_box">
<h3>Something</h3>
</div>
<div class="form-group col-sm-4 col-lg-4 cust_box">
<h3>Something</h3>
</div>
<div class="form-group col-sm-4 col-lg-4 cust_box">
<h3>Something</h3>
</div>
</div>
CSS
.cust_box{
margin-top: 15px;
margin-bottom:15px;
padding-left: 10px;
padding-right:10px;
background-color: #1A284B;
color: #fff;
height: 290px;
max-width:100%;
border: 1px solid #162444;
margin-right: 10px; /*Line that causes issue*/
}
Problem recreated in CodePen
Place your class cust_box inside the the column. The h3 has margin so that may also be interfering with your layout. You may also want to just use the row class instead of form-group.
See working Example Snippet. (colors added so you can see what's actually happening)
.cust_box {
margin: 15px 2.5px;
padding: 25px;
height: 290px;
max-width: 100%;
background-color: #1A284B;
color: #fff;
border: 1px solid #162444;
}
.cust_box h3 {
margin: 0;
}
.red {
border: 2px solid red;
}
.yellow {
background: yellow;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row yellow">
<div class="col-sm-4 red">
<div class="cust_box">
<h3>Something</h3>
</div>
</div>
<div class="col-sm-4 red">
<div class="cust_box">
<h3>Something</h3>
</div>
</div>
<div class="col-sm-4 red">
<div class="cust_box">
<h3>Something</h3>
</div>
</div>
</div>
</div>
Example without Color
.cust_box {
margin: 15px 2.5px;
padding: 25px;
height: 290px;
max-width: 100%;
background-color: #1A284B;
color: #fff;
border: 1px solid #162444;
}
.cust_box h3 {
margin: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-sm-4">
<div class="cust_box">
<h3>Something</h3>
</div>
</div>
<div class="col-sm-4">
<div class="cust_box">
<h3>Something</h3>
</div>
</div>
<div class="col-sm-4">
<div class="cust_box">
<h3>Something</h3>
</div>
</div>
</div>
</div>
with the additional margin you are destroying the bootstrap grid system.
have you tried using padding instead of margin?
apart from this you can customize bootrap a lot!
have a look on this:
http://getbootstrap.com/customize/#grid-system
EDIT:
i looked at your code and saw your struggle:
use a inner div like this:
<div class="form-group col-sm-4 col-lg-4 cust_box">
<div class="inner">
<h3>Something</h3>
</div>
</div>
and use your style on the inner div:
.cust_box .inner{
margin-top: 15px;
margin-bottom:15px;
padding-left: 10px;
padding-right:10px;
background-color: #1A284B;
color: #fff;
height: 290px;
max-width:100%;
border: 1px solid #162444;
margin-right: 10px; /*should not be an issue anymore*/
}