Bootstrap two equal height div inside a div - html

I am new to bootstrap and trying.
I need a pattern as follows
How ca we split the div's vertically with two equal divs ?
In horizondal we can do that by col-md-6.
Thanks in advance

Bootstrap mainly focuses on WIDTH, thus to my knowledge there are
no special classes to make two div's of equal height.
You can do it by specifying height:/* value in px */; in the <div>'s styling !
<div class="container"> /*Grid Layout*/
<div class="row testdiv">/*row cuts of 15px margin of left&right*/
</div>
<div class="row testdiv">
</div>
</div>
Now the CSS :
.testdiv{
height:400px;/*or some other value*/
}
A class selector is used to affect the styling of both div's at the same time !

add two div as follows
<div class="container-fluid mainbg">
<div class="row">
<div class="col-md-12 div1">.col-md-12</div>
</div>
<div class="row">
<div class="col-md-12 div2">.col-md-12</div>
</div>
</div>
Some css code to include
.mainbg{
background:grey;
padding:10px; }
.div1{
background:red; }
.div1, .div2{
height:100px;
line-height:100px;
font-size:50px; }
.div2{
text-align:center;
}

You could use css tables to achieve equal-height rows
Bootply
Markup
<div class="container-fluid">
<div class="row">
<div class="one">DIV1</div>
</div>
<div class="row">
<div class="two">DIV2</div>
</div>
</div>
(Relevant) CSS
.container-fluid
{
height: 200px; /* whatever you need */
display:table;
width:100%;
}
.one,.two
{
display:table-cell;
width:100%;
vertical-align: middle;
}
.row {
display:table-row;
}

Depending on your browser support you could always use CSS3 Flexbox
I did a quick demo here of what you're after.
http://codepen.io/tom-maton/pen/LCbIx
.container {
align-content: stretch;
background-color: gray;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-flow: row wrap;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
height: 350px;
margin: 20px auto;
padding: 15px;
width: 750px;
}
All the flexbox settings are set on the containing element and it does the rest from there on in.
A good article about FlexBox can be found here http://css-tricks.com/snippets/css/a-guide-to-flexbox/

Related

How to stack 2 divs left and align last div right using Flex

I want to stack 2 divs on top of each other aligned left and make the last div align right.
They should all 3 be vertically centered.
This is the markup I have. This can't be changed.
<div class="wrapper">
<div class="first">First</div>
<div class="second">Second</div>
<div class="third">Third</div>
</div>
This is how i want it to be.
Is this possible using Flex and not changing the markup?
Here you go:
.box{
width:100px;
height:100px;
background-color:red;
margin:5px;
}
.box:nth-of-type(3){
align-self:end;
}
.con{
display:flex;
width:350px;
height:250px;
flex-wrap:wrap;
flex-direction:column;
align-items:center;
justify-content:center;
background-color:yellow;
}
<div class="con">
<div class="box">001</div>
<div class="box">002</div>
<div class="box">003</div>
</div>
If you can add wrapper divs to those three divs you could do it as following:
You can wrap your first two divs in another div and apply justify-content: space-between to the container.
To center them vertically, add display: flex; and flex-direction: column to the wrapper class and add justify-content: center
.container {
display: flex;
justify-content: space-between;
}
.box {
background-color: green;
height: 100px;
width: 100px;
margin: 10px;
}
.col {
display: flex;
flex-direction: column;
justify-content: center;
}
<div class="container">
<div class="col">
<div class="box">First</div>
<div class="box">Second</div>
</div>
<div class="col">
<div class="box">Third</div>
</div>
</div>

How to make 2 blocks "div" go in a row

I need my two blocks to go in a row one after another, but when the screen resolution decreases, they are placed under each other, that is, in the column
<div>
<div>
<h1>Block1</h1>
</div>
<div>
<h1>Block2</h1>
</div>
</div>
We can use flex (by default flex-direction is row so we don't need any other styling in css) -:
<div class="container">
<div>
<h1>Block1</h1>
</div>
<div>
<h1>Block2</h1>
</div>
</div>
.container{
display: flex;
}
Also this is one way of doing things, flex is not supported everywhere so you can go for inline-block also -:
<div>
<div class="inline">
<h1>Block1</h1>
</div>
<div class="inline">
<h1>Block2</h1>
</div>
</div>
.inline{
display: inline-block;
}
As the div element is known as a block element, you need to use display:inline-block. This means 'if there is space next to the element, place the next inline block element next to it' (in essence).
div {
display: inline-block;
background:tomato;
}
#media only screen and (max-width: 600px) {
div{
display:block;
background:green;
}
<div>
<div>1
</div>
<div>2
</div>
</div>
For your width to then turn back into a block element, you will need to use the media query - something like above.
You should use CSS grid:
<div class="wrapper">
<div>
<h1>Block1</h1>
</div>
<div>
<h1>Block2</h1>
</div>
</div>
Css:
.wrapper{
diplay: grid;
grid-template-columns: 1fr;
}
Try using display:flex and use flexbox to place next to each other when the width is high. When the width reduces the div cols will go down.
.row {
width: 100vw;
display: flex;
flex-wrap: wrap;
}
.cols {
height: 400px;
width: 400px;
margin: 5px 5px 5px 5px;
background-color: blue;
}
<div class="row">
<div class="cols">
</div>
<div class="cols">
</div>
</div>
Like this:
#media all and (max-width: 480px) {
div{
float: left;
width: 98%;
margin-left: 1%;
overflow: hidden;
border: 1px solid #000;
box-sizing: border-box;
padding: 5px;
}
}
Or with class
.wrapper div{
...
}
More about #media
https://developer.mozilla.org/pl/docs/Web/CSS/Media_Queries/Using_media_queries

CSS - How to achieve a mixed effect of "float left" and "text-align: center"?

This is fairly a simple question but I cant wrap my head around a simple solution. I need to center 3 squares in a row, but I dont know the total amount of squares (while the simple solution to this would be to use text-align: center), BUT I dont want to center the last elements. Long story short, how to create float: left effect + centering all elements inside the main container?
JSfiddle here.
HTML:
<div class="row b">
<div class="col-lg-6 col-md-6 col-sm-12 col-lg-offset-3 col-md-offset-3">
<div class="row maxW b">
<div class="postContainer"></div>
<div class="postContainer"></div>
<div class="postContainer"></div>
<div class="postContainer"></div>
</div>
</div>
</div>
CSS:
.postContainer {
width: 230px;
height: 230px;
border: 1px solid lightblue;
display: inline-block;
}
.maxW {
max-width: 900px;
}
.ce {
text-align: center;
}
.b{
border:1px solid black;
}
Expected result:
These squares should be responsive, max squares per row is 3. If I use float: left, I get almost what I need, but the squares are pulled to the left and not centered inside the main container. If I use text-align: center, the squares are centered in the main container, but I dont want the last squares to be centered, they must remain floated to the left.
I would recommend using flexbox. It's a pretty new concept in css, but it has good support in all modern browsers. I personally use it in production.
The markup would be simplified as:
<div class="posts">
<div class="postContainer"></div>
<div class="postContainer"></div>
<div class="postContainer"></div>
<div class="postContainer"></div>
</div>
And the container would have the CSS:
.posts {
display: flex;
flex-flow: row wrap;
justify-content: flex-start;
margin: 0 auto;
}
Display: flex tells the browser it's a flexbox, flex-flow says that it should be rows that wraps when full and that the content should be left-aligned by flex-start.
Fiddle
Flexbox basics
You can set the container width to 33% and then set the post border and width on a contained div.
.postContainer {
width: 33%;
display: inline-block;
text-align: center;
}
.post {
width: 230px;
height: 230px;
border: 1px solid lightblue;
display: inline-block;
}
https://jsfiddle.net/k9597cLq/
You can just use display:inline-block; instead of float.
You CSS would look something like this:
.maxW{
border:1px solid black;
padding:5px;
}
.postContainer{
width:150px;
height:150px;
border:1px solid black;
display:inline-block; /* <-- the magic part*/
}
DEMO

How do I automatically add spacing between divs without using percentage?

I have a few divs aligned horizontally.
How do I make the spacing between them automatic so that if I resize the screen or add another div, there will be equal spacing between divs.
Example when screen width is 600px:
Example when screen width is 330px:
Hopefully my explanation is good enough.
Thanks for any help!
Flexbox can do that https://jsfiddle.net/2Lzo9vfc/210/
HTML
<div class="content">
<div class="box">Box</div>
<div class="box">Box</div>
<div class="box">Box</div>
</div>
CSS
.content {
display: -webkit-flex;
display: flex;
-webkit-justify-content: space-around;
justify-content: space-around;
width: 100%;
}
.box {
background: black;
padding: 25px;
color: white;
}
Here you can find a solution with flexbox:
.container {
display:flex;
justify-content:space-between;
}
.item {
background:#000;
height:50px;
width:120px;
}
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
More information about using flexbox you can find here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
You can use flexboxes, just appy these proprieties to the container of divs:
.container {
display: flex;
justify-content: space-between;
}
You may use inline-block + text-align:justify; for older browser generating an extra last invisible line with :after, or flex + justify-content:space-betwween;
.ib {
text-align:justify;
}
.ib:after {
content:'';
display:inline-block;
width:99%;
}
.flex {
display:flex;
justify-content:space-between;
}
.d100 {
width:100px;
height:2em;
background:blue;
display:inline-block;
}
<div class="ib">
<div class="d100"></div>
<div class="d100"></div>
<div class="d100"></div>
</div>
<div class="flex">
<div class="d100"></div>
<div class="d100"></div>
<div class="d100"></div>
</div>
Use the flex declaration - see here: http://www.w3schools.com/cssref/css3_pr_flex.asp
You can use flexboxes, this solustion is for IE 10+ and latest other browsers.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Option 1: Add a border thats the same color as background
Option 2: add a background colored div and set display to inline

Vertically & horinzontally align multiple div children in container

I currently have the following code:
<div id="container">
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
</div>
<style>
#container{
width:100%;
height:100%;
overflow-y:auto;
position:absolute;
left:0; top:10px;
padding:10px;
}
.card{
width:100px; height:100px; margin:10px;
float:left;
}
</style>
I am trying to vertically and horizontally align the div boxes so that the more boxes that appear, it still stays both vertically and horizontally centred in the container. For example:
Example of what it would look like with 4 cards which fit in the container..
Example of what it would look like with 12 cards which overflow in the container..
Example of what it would look like with cards that dont fit in the container..
DEMO
HTML
<div class="container">
<div class="container-wrapper">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
</div>
CSS
.container {
position: relative;
display:table;
background: red;
width:100%;
height: 100%; /* auto is default, you can have ur height here */
}
.container-wrapper {
display: table-cell;
margin:auto;
text-align:center;
font-size:0;
width:90%;
height:90%;
vertical-align:middle;
}
.card {
display: inline-block;
height:100px;
width:100px;
border: 1px solid #ddd;
background: #eee;
margin:10px;
}
DEMO 2 with some height of the container
Try Flexbox DEMO
#container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0 auto;
flex-wrap: wrap;
box-sizing: border-box;
}
.inner {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
max-width: 750px;
margin: 0 auto;
flex-wrap: wrap;
}
.card{
width:100px;
height:100px;
margin:10px;
border: 1px solid black;
}
<div id="container">
<div class="inner">
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
<div class="card">1</div>
<div class="card">2</div>
<div class="card">3</div>
<div class="card">4</div>
<div class="card">3</div>
<div class="card">4</div>
</div>
</div>
You may not be able to use external libraries for your project, but there is still much to learn from them in my opinion.
Your situation is a basic example of how a grid system can be useful. In deed, this problem has been solved and equated many times before by such systems.
Normally I would suggest Twitter Bootstrap 3, but since this framework is somewhat complex, I think it would be easier to read something more lightweight, like 960 grid system. Here are two links that can give you a brief introduction into the library:
http://sixrevisions.com/web_design/the-960-grid-system-made-easy/
http://webdesign.tutsplus.com/articles/using-the-960-grid-system-as-a-design-framework--webdesign-2036
Once you understand this, IMO, you have no other choice than to dive into the framework and see how it is done. It will be messy.
I also believe you will have to use JavaScript. Can you use jQuery?
Either way, when adding a new card, detect it using JavaSctipt, and then change the DOM based on that.
Hope I helped.