Align multiple elements across bootstrap columns in the same row - html

I would like to horizontally align multiple items across two columns within the same row in bootstrap. An example HTML Code can be found below:
<div class="container">
<div class="row">
<div class="col-sm-6">
<h1>Short Heading</h1>
<p>Some super long content that will push the button far downward so that it will certainly not align with the next column anymore</p>
<button class="btn btn-primary">Button</button>
</div>
<div class="col-sm-6">
<h1>Super long Heading that requires 2 rows</h1>
<p>Some short content</p>
<button class="btn btn-primary">Button</button>
</div>
</div>
</div>
or in this fiddle http://jsfiddle.net/6pYhx/689/
Within the example, I would like to have 2 conditions
The header, p and button elements should be on the same horizontal height. In my example the right header is two rows, therefore the right p element is one row lower than the left p
If the screen size shrinks, all elements of column 1 should be right below each other, followed by all elements of column 2 -> I tried to solve it by having multiple rows, but then you'd have header 1, header 2, p 1, p 2, ... Instead I would like to have header 1, p 1, button 1, followed by header 2, p 2, button 2
Any ideas how to solve it with CSS?

I have three solutions.
Use flexbox at media >= 768px.
#media (min-width: 768px) {
.make-it-flex {
display: flex;
flex-wrap: wrap;
}
.flex-item-1 { order: 1; }
.flex-item-2 { order: 2; }
.flex-item-3 { order: 3; }
.flex-item-4 { order: 4; }
.flex-item-5 { order: 5; }
.flex-item-6 { order: 6; }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div class="container">
<h2>Flexbox</h2>
<div class="row make-it-flex">
<div class="col-xs-12 col-sm-6 flex-item-1"><div class="well">Header 1</div></div>
<div class="col-xs-12 col-sm-6 flex-item-3"><div class="well">Paragraph 1<br>has three lines<br>it's true</div></div>
<div class="col-xs-12 col-sm-6 flex-item-5"><div class="well">Button 1</div></div>
<div class="col-xs-12 col-sm-6 flex-item-2"><div class="well">Header 2<br>has two lines</div></div>
<div class="col-xs-12 col-sm-6 flex-item-4"><div class="well">Paragraph 2</div></div>
<div class="col-xs-12 col-sm-6 flex-item-6"><div class="well">Button 2<br>has two lines</div></div>
</div>
</div>
http://jsfiddle.net/glebkema/uuLqokhm/
Duplicate data and make two layouts for different screen sizes.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div class="container visible-xs">
<h2>Columns</h2>
<div class="row">
<div class="col-xs-12 col-sm-6">
<div class="row">
<div class="col-xs-12"><div class="well">Header 1</div></div>
<div class="col-xs-12"><div class="well">Paragraph 1<br>has three lines<br>it's true</div></div>
<div class="col-xs-12"><div class="well">Button 1</div></div>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="row">
<div class="col-xs-12"><div class="well">Header 2<br>has two lines</div></div>
<div class="col-xs-12"><div class="well">Paragraph 2</div></div>
<div class="col-xs-12"><div class="well">Button 2<br>has two lines</div></div>
</div>
</div>
</div>
</div>
<div class="container hidden-xs">
<h2>Lines</h2>
<div class="row">
<div class="col-xs-12 col-sm-6"><div class="well">Header 1</div></div>
<div class="col-xs-12 col-sm-6"><div class="well">Header 2<br>has two lines</div></div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6"><div class="well">Paragraph 1<br>has three lines<br>it's true</div></div>
<div class="col-xs-12 col-sm-6"><div class="well">Paragraph 2</div></div>
</div>
<div id="line-3" class="row">
<div class="col-xs-12 col-sm-6"><div class="well">Button 1</div></div>
<div class="col-xs-12 col-sm-6"><div class="well">Button 2<br>has two lines</div></div>
</div>
</div>
http://jsfiddle.net/glebkema/cjs1q42m/
Add the script and transmit data from one layout to another.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<div class="container">
<h2>Columns</h2>
<div class="row">
<div class="col-xs-12 col-sm-6">
<div id="column-1" class="row">
<div class="col-xs-12 column-1 line-1"><div class="well">Header 1</div></div>
<div class="col-xs-12 column-1 line-2"><div class="well">Paragraph 1<br>has three lines<br>it's true</div></div>
<div class="col-xs-12 column-1 line-3"><div class="well">Button 1</div></div>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div id="column-2" class="row">
<div class="col-xs-12 column-2 line-1"><div class="well">Header 2<br>has two lines</div></div>
<div class="col-xs-12 column-2 line-2"><div class="well">Paragraph 2</div></div>
<div class="col-xs-12 column-2 line-3"><div class="well">Button 2<br>has two lines</div></div>
</div>
</div>
</div>
</div>
<div class="container">
<h2>Lines</h2>
<div id="line-1" class="row">
</div>
<div id="line-2" class="row">
</div>
<div id="line-3" class="row">
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$( document ).ready(function() {
var idColumn1 = $('#column-1');
var idColumn2 = $('#column-2');
var idLine1 = $('#line-1');
var idLine2 = $('#line-2');
var idLine3 = $('#line-3');
var classColumn1 = $('.column-1');
var classColumn2 = $('.column-2');
var classLine1 = $('.line-1');
var classLine2 = $('.line-2');
var classLine3 = $('.line-3');
checkLayout();
$( window ).resize(function() {
checkLayout();
});
function checkLayout() {
var classSm6 = 'col-sm-6';
if (( window.innerWidth <= 768 ) && classColumn1.hasClass(classSm6)) {
// console.info( "resize to xs" );
classColumn1.appendTo(idColumn1).removeClass(classSm6);
classColumn2.appendTo(idColumn2).removeClass(classSm6);
} else if (( window.innerWidth > 768 ) && !classLine1.hasClass(classSm6)) {
// console.info( "resize to sm" );
classLine1.appendTo(idLine1).addClass(classSm6);
classLine2.appendTo(idLine2).addClass(classSm6);
classLine3.appendTo(idLine3).addClass(classSm6);
}
}
});
</script>
http://jsfiddle.net/glebkema/cjwc6uev/

Do you want them to be centered in the columns? You could use text-center class on each column.
<div class="col-sm-6 text-center">
If you want them vertically centered, there are a few ways to do it. I recently had this issue and we changed display to table-cell and used the vertical-align: middle; in CSS (example here). Or you can use flexbox, but it doesn't work in all browsers.

Is this what you are trying? Make the preview screen bigger to see the result if you are testing it in jsfiddle:
<div class="container">
<div class="row">
<div class="col-sm-6">
<div class="row">
<h1 class="col-md-3">Short Heading</h1>
<p class="col-md-7">Some super long content that will push the button far downward so that it will certainly not align with the next column anymore</p>
<button class="btn btn-primary class="col-md-2"">Button</button>
</div>
</div>
<div class="col-sm-6">
<div class="row">
<h1 class="col-md-3">Super long Heading that requires 2 rows</h1>
<p class="col-md-7">Some short content</p>
<button class="btn btn-primary col-md-2">Button</button>
</div>
</div>
</div>
</div>
also, you can play with the inner columns to make them the right size for your needs, but this is, as you needed, on the same row. I made this for medium or wider screens and on small screens it will be defaulted to the normal behaviour of having them one under another, but again, you can play with that by changing the md part or adding more classes like col-xs-... col-sm-... col-lg-...
here's how that looks:
you can also try this if you want your content to be horizontally aligned as well, it is a bit tricky but it works:
https://plnkr.co/edit/s01bZi2OJc8xm2QlKuII?p=preview

Related

How to fix problem with bootstrap 4 grid system?

I am facing a problem with the Bootstrap-4 grid system. There are 12 columns, and whenever I use less than 12 columns, the rows are automatically centered. I want left alignment, how can this be fixed?
A snippet of my code:
<div class="row justify-content-start con-flex">
<?php foreach($books as $book):?>
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-6">
<div id="single-book">
<div id="book-image">
<?php print '<img src = "'.strip_tags($book->book_image).'" alt = "">';?>
<div id="addto-cart"><i class="fas fa-shopping-cart"></i> Add to cart</div>
</div>
<div class="book-text">
<div id="book-name"><?= substr(htmlentities($book->book_name),0,21) ?></div>
<div id="author">By <i><?= htmlentities($book->author) ?></i></div>
<div id="price"><?= htmlentities($book->price) ?>.TK</div>
<div id="book-details">
<?php print 'View details'; ?>
</div>
</div>
</div>
</div>
<?php endforeach;?>
</div>
image of code:
enter image description here
According to Bootstrap doc that should solve your problem
<div class="container">
<div class="row justify-content-start">
<div class="col-4">
content here...
</div>
<div class="col-4">
content here...
</div>
</div>
</div>
The code you have should be working fine, columns will be aligned left by default; maybe what you are seeing is the result of using .container instead of .container-fluid which uses the full width of the viewport. It's either this or you might have some custom CSS that's affecting the layout of the columns
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row ">
<div class="col-4 col-md-4 border">content here...</div>
<div class="col-4 col-md-4 border">content here...</div>
</div>
</div>
<div class="container-fluid">
<div class="row ">
<div class="col-4 col-md-4 border">content here...</div>
<div class="col-4 col-md-4 border">content here...</div>
</div>
</div>
This problem was for my other base CSS.
Instead of:
*{
margin: 0 auto;
}
For this margin: 0 auto; in my main style.css file My bootstrap's grids were not working properly. Now I remove this margin: 0 auto from *.
Now
I write
* {
margin: 0;
padding: 0;
}
Now my bootstrap grids are working properly

Bootsrap 3: Responsive breakpoint from rows of 3 columns

I have two rows which contain 3 column each , I would like at certain breakpoint to switch from 3 columns to 2 column and from 2 columns to 1 column (mobile breakpoint). so far this is what I have.
#media (min-width:768px) {
.col-md-4{
min-width: 50%;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<div class="row">
<div class="col-md-4">1</div>
<div class="col-md-4">2</div>
<div class="col-md-4">3</div>
</div>
<div class="row" >
<div class="col-md-4">4</div>
<div class="col-md-4">5</div>
<div class="col-md-4">6</div>
</div>
Question
This does not work , what am I missing in my code? help am newbie though
I would place them all in the same .row and use the appropriate grid classes. This should get you what you want:
<div class="row">
<div class="col-sm-6 col-md-4">1</div>
<div class="col-sm-6 col-md-4">2</div>
<div class="col-sm-6 col-md-4">3</div>
<div class="col-sm-6 col-md-4">4</div>
<div class="col-sm-6 col-md-4">5</div>
<div class="col-sm-6 col-md-4">6</div>
</div>
Note: I would not redefine .col-md-4 or any of the Bootstrap built-in grid glasses like that.
For more details, read the Bootstrap documentation on using grid glasses: https://getbootstrap.com/docs/4.0/layout/grid/#mix-and-match
Just use the different breakpoint column classes of bootstrap.
If you want to change order in bootstrap 3 (as requested in comments) you can easily use push and pull.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-md-4 col-md-push-4 col-sm-6 col-sm-push-6">1</div>
<div class="col-md-4 col-md-pull-4 col-sm-6 col-sm-pull-6">2</div>
<div class="col-md-4 col-sm-6">3</div>
</div>
https://getbootstrap.com/docs/4.0/layout/grid/#mix-and-match
Use col-sm-6 to set 2 divs in 1 row on sm size...
(the calc is: 12/6=2 => 100% /2=50% means each div gets 50% to his width)
By default in xs size's screen will set 1 div in 1 row because you did not specify otherwise...
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4">1</div>
<div class="col-sm-6 col-md-4">2</div>
<div class="col-sm-6 col-md-4">3</div>
</div>
<div class="row" >
<div class="col-sm-6 col-md-4">4</div>
<div class="col-sm-6 col-md-4">5</div>
<div class="col-sm-6 col-md-4">6</div>
</div>
</div>

Why isn't BootStrap column offset working?

I am trying to build a website with flash-cards to help learn the Hebrew alphabet. My Card partial view looks like this:
#model FlashCards.MultipleChoice.ViewModels.CardViewModel
<div class="index-card">
<div class="row">
<div class="col-md-offset-5"></div>
<div class="col-md-2 text-center">
#Model.Numeric
</div>
</div>
<div class="row">
<div class="col-md-offset-2"></div>
<div class="col-md-8 text-center card-symbol">
#Html.Raw(Model.UnicodeEscape)
</div>
</div>
<div class="row">
<div class="col-md-offset-2"></div>
<div class="col-md-8 text-center">
#Model.Name
</div>
</div>
</div>
Now the col-md-offset works on all but the first row, causing the card to render as in the below image:
Now why isn't the 1 in the image offset like the glyph and the name for the letter Aleph?
My CSS file for these cards only contains the following so far:
.index-card {
height: 150px;
}
.index-card .card-symbol {
font-size: large
}
I believe you're using the offset classes wrong; do not apply them to empty divs.
Your text is centered. Second and third rows have widths of 8 columns. It looks like the offset is working but it really isn't. The first row is only 2 columns wide. None of your offset divs are having any effect on the layout.
You need to do something like this, at the very least:
<div class="index-card">
<div class="row">
<div class="col-md-offset-5 col-md-2 text-center">
#Model.Numeric
</div>
</div>
<div class="row">
<div class="col-md-offset-2 col-md-8 text-center card-symbol">
#Html.Raw(Model.UnicodeEscape)
</div>
</div>
<div class="row">
<div class="col-md-offset-2 col-md-8 text-center">
#Model.Name
</div>
</div>
</div>

Getting rid of padding or marging from bootstrap columns

I'm using bootstrap (version 3.3.6) for the first time to make my site. Readed the docs and start to code. Excluding the <link>'s the code below is my site's structure:
<body>
<div class="container">
<div class="row">
<div class="col-sm-2 col-md-2 col-lg-2 col-lg-pull-1">
<div class="loader">
<div class="loader-bg">
...
</div>
</div>
</div>
<div class="col-sm-10 col-md-10 col-lg-10 col-lg-push-1">
<div class="black-box">
<p class="info-message">Coming <span>VERY</span> soon!</p>
<p class="text-message">Until then, my contacts:</p>
...
</div>
</div>
</div>
</div>
Using push and pull classes I thought I was removed padding for the columns. Setting the main div with 'class="container"' I have the result:
Changing to 'class="container-fluid"' the behavior remains strange. Notice shapes overlapping page:
What I want in my results is: the circle and rectangle (both are divs) remains aligned to the edges of the page (left and right) with no padding or marging. Following docs until now doesn't work. What this behavior occurs?
by default col-* have padding so you just need to reset them
[class^="col"] {
padding: 0
}
[class$="-2"] {
background: orange
}
[class$="-10"] {
background: grey
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<div class="row">
<div class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
<div class="loader">
<div class="loader-bg">
...
</div>
</div>
</div>
<div class="col-xs-10 col-sm-10 col-md-10 col-lg-10">
<div class="black-box">
<p class="info-message">Coming <span>VERY</span> soon!</p>
<p class="text-message">Until then, my contacts:</p>
...
</div>
</div>
</div>

How to stretch image based on right side?

I have this demo : https://jsfiddle.net/DTcHh/18752/ I want that image height is based on right side so that image always goes to the end of div. Any suggestion?
<div class="blog_list">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="blog_image">
<img src="img/property_img.png" />
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="blog_details">
<div class="blog_title">This comfortable apartment is located in the heart of Zurich</div>
<div class="blog_posted">POSTED ON: <span>JANUARY 24 2016 14:35h </span>IN <span>KREIS 1</span></div>
<p class="blog_more-details">
Wheather a single room studio or multi-room apartment, each has at least one own bathroom and a fully quipped kitches.
</p>
<div class="sold_by">
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="row">
<div class="col-md-2 col-sm-2 col-xs-12 ">
<div class="profile-image"><img src="img/profile.png" /></div>
</div>
<div class="col-md-10 col-sm-10 col-xs-12 no-padding-left">
<span class="property">ARTICLE WRITEN BY</span>
<span class="property_name">Janine Lindenmuller</span>
<div class="blog_read_more">
READ MORE
</div>
</div>
</div>
</div>
</div></div></div></div></div></div>
i think u need to remove bootstrap grid class and have to write some custom css with display:table-cell
for example
.img,.text {
display: table-cell;
vertical-align: top;
}
.img {
width:60%;
}
.text {
width:40%;
}
Also use low dimension image. Fiddle here
Set your image row like this:
JSFiddle
<div class="col-md-12 col-sm-12 col-xs-12">