center a row using Bootstrap 3 - css

How to center a row (12 column) in Bootstrap 3 ?
I do not want to use the offset
I am using this way but not worked.
.col-centered{
float: none;
margin: 0 auto;
}
<div class="row" style="max-width: 300px;">
<div class="col-md-12 col-xs-12 col-centered">
<div class="input-group">
<div class="input-group-btn">
<button id="ItemForSearch" type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
All Items
<span class="caret"></span>
</button>
<ul id="NormalSearch" class="dropdown-menu customize-dropdown-menu">
<li> Test 1 </li>
<li> Test 2 </li>
<li> Test 3 </li>
<li> Test 4 </li>
</ul>
</div>
<!-- /btn-group -->
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<i class="fa fa-search"></i>
</button>
</span>
</div>
</div>
</div>
Is there any solution to this? I have not an idea for this work.

Why not using the grid system?
The bootstrap grid system consist of 12 columns, so if you use the "Medium" columns it will have a 970px width size.
Then you can divide it to 3 columns (12/3=4) so use 3 divs with "col-md-4" class:
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
Each one will have 323px max width size.
Keep the first and the last empty and use the middle one to get your content centerd:
<div class="col-md-4"></div>
<div class="col-md-4">
<p>Centered content.</p>
</div>
<div class="col-md-4"></div>

What you are doing is not working, because you apply the margin: auto to the full-width column.
Wrap it in a div and center that one. E.g:
<div class="i-am-centered">
<div class="row">...</div>
</div>
.
.i-am-centered { margin: auto; max-width: 300px;}
http://www.bootply.com/93751
Its a cleaner solution anyway, as it is more expressive and as you usually don't want to mess with the grid.

I know this question was specifically targeted at Bootstrap 3, but in case Bootstrap 4 users stumble upon this question, here is how i centered rows in v4:
<div class="container">
<div class="row justify-content-center">
...
More related to this topic can be found on bootstrap site.

Instead of
<div class="col-md-4"></div>
<div class="col-md-4"></div>
<div class="col-md-4"></div>
You could just use
<div class="col-md-4 col-md-offset-4"></div>
As long as you don't want anything in columns 1 & 3 this is a more elegant solution. The offset "adds" 4 columns in front, leaving you with 4 "spare" after.
PS I realise that the initial question specifies no offsets but at least one previous answer uses a CSS hack that is unnecessary if you use offsets. So for completeness' sake I think this is valid.

you can use grid system without adding empty columns
<div class="col-xs-2 center-block" style="float:none"> ... </div>
change col-xs-2 to suit your layout.
check preview: http://jsfiddle.net/rashivkp/h4869dja/

We can also use col-md-offset like this, it would save us from an extra divs code. So instead of three divs we can do by using only one div:
<div class="col-md-4 col-md-offset-4">Centered content</div>

Simply use text-center class
<div class="row">
<div class="col-md-12">
<h3 class="text-center">Here Comes your Text</h3>
</div>
</div>

Add this to your css:
.row-centered {
text-align:center;
}
.col-centered {
display:inline-block;
float:none;
/* reset the text-align */
text-align:left;
/* inline-block space fix */
margin-right:-4px;
}
Then, in your HTML code:
<div class=" row row-centered">
<div class="col-*-* col-centered>
Your content
</div>
</div>

this peace of code can help you
<div class="row" style="display: flex; justify-content: center;"></div>

Try this, it works!
<div class="row">
<div class="center">
<div class="col-xs-12 col-sm-4">
<p>hi 1!</div>
</div>
<div class="col-xs-12 col-sm-4">
<p>hi 2!</div>
</div>
<div class="col-xs-12 col-sm-4">
<p>hi 3!</div>
</div>
</div>
</div>
Then, in css define the width of center div and center in a document:
.center {
margin: 0 auto;
width: 80%;
}

I use text-align-center in a row like this
<div class="row tac">
<h1>Centered content</h1>
</div>
<style>
.tac { text-align: center}
</style>

I use this peace of code and I have successeful
<div class="row center-block">
<div style="margin: 0 auto;width: 90%;">
<div class="col-md-12" style="top:10px;">
</div>
<div class="col-md-12" style="top:10px;">
</div>
</div>

Instead of trying to center div's, just add this to your local css.
.col-md-offset-15 {
margin-left: 12.4999999%;
}
which is roughly offset-1 and half of offset-1. (8.333% + 4.166%) = 12.4999%
This worked for me.

Related

How to verticvally align center icons in bootstrap col?

Using bootstrap 4 inside Vue.js, I'd like to make left and right arrows to appear at the middle of the page around the image.
Here is the code:
<div class="row">
<div class="col-1">
<span class="align-self-center"><h1>❮</h1></span>
</div>
<div class="col-9">
<h1>Modal comes here</h1>
<img class="img-fluid modal-img" :src=" getImgUrl(currentMediaUrl)">
</div>
<div class="col-1">
<span class="align-self-center"><h1>❯</h1></span>
</div>
</div>
But whatever I tamper with CSS, I can not acheive this. How can I fix this?
Well, thanks to this answer, I realized it can be done with minimal fuss, by adding a simple flex css:
.img-box {
display: flex;
align-items:center;
}
And don't forget to add img-box class:
<div class="row img-box">
<div class="col-1">
<span class="nav-arrow">❮</span>
</div>
<div class="col-9">
<h1>Modal comes here</h1>
<img class="img-fluid modal-img" :src=" getImgUrl(currentMediaUrl)">
</div>
<div class="col-1">
<span class="nav-arrow">❯</span>
</div>
</div>

Bootstrap buttons and columns expanding the entire width of a div tag

I am trying to build a bootstrap calculator using the grind system provided by Bootstrap. I have two rows using the class col-md-4 with each row comprising of 4 buttons. I would expect to see the buttons expand the entire width of the div tag. When I put a background color to the div tag the color fills the entire div tag, as I would except. I created a codepen to illustrate my problem.
<div class="container">
<div class="cal-body">
<div class="header">
</div>
<div class="lcd">
</div>
<div class="row">
<div class="col-md-4">
<button>7</button>
<button>8</button>
<button>9</button>
<button>%</button>
</div>
</div>
<div class="row">
<div class="col-md-4" style="margin-bottom: 20px;">
<button>4</button>
<button>5</button>
<button>6</button>
<button>X</button>
</div>
</div>
</div>
</div>
</div>
In other words, I would like my buttons to take of the full length of the column row. As of now, it only takes 1/4 of the column space. I am trying to replicate the look of a calculator.
My code pen:http://codepen.io/louis345/pen/KNoypX
Any help will be greatly appreciated.
Just change the col-md-4 to col-md-12 if you want to span the whole row.
You can also add class text-center into your columns to make buttons centered position.
Sample:
<div class="col-md-12 text-center">
</div
Check this to see if that's what you are looking for
The idea is to wrap each button on a different column
<div class="col col-sm-3">
<button>7</button>
</div>
#claudios your answer is right [ use class (text-center) ] and I am just implement in this snippet.
.line{
box-shadow:inset 0px 0px 2px #ccc;
padding-top: 10px;
padding-bottom: 10px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<br>
<div class="container">
<div class="row">
<div class="col-sm-12 text-center line">
<button class="btn btn-success">Submit</button>
<button class="btn btn-default">Cancel</button>
</div>
</div>
</div>
Each row has 12 columns, you're setting a div to use 4 columns and it will make the div to use 1/3 of the size.
Also, col-md must be used to sizes >= 768px. You should use col-xs because you're limiting the size of cal-body.
I made it, based on your codepen: http://codepen.io/anon/pen/BQMeXQ
Take a look here, 2 rows, each one has 4 divs, each using 3 columns.
<style>
.button-group {
width: 100%;
margin-left: 15px;
}
.button-line {
width: 100%;
}
.button-col {
padding-left: 0px;
padding-right: 0px;
}
.button-col button{
width: 100%;
}
</style>
<div class="button-group">
<div class="row button-line">
<div class="col-xs-3 button-col">
<button>7</button>
</div>
<div class="col-xs-3 button-col">
<button>8</button>
</div>
<div class="col-xs-3 button-col">
<button>9</button>
</div>
<div class="col-xs-3 button-col">
<button>%</button>
</div>
</div>
<div class="row button-line">
<div class="col-xs-3 button-col">
<button>4</button>
</div>
<div class="col-xs-3 button-col">
<button>5</button>
</div>
<div class="col-xs-3 button-col">
<button>6</button>
</div>
<div class="col-xs-3 button-col">
<button>x</button>
</div>
</div>
</div>

Vertical middle content in div?

I using bootstrap. Does bootstrap have selector put text in the middle in the div rather than at the top?
See example: https://jsfiddle.net/DTcHh/23497/
Can you can see Create user accounts for your staff is at the top which should be in the middle lined up with a button.
How to do this in bootstrap?
<div class="row">
<div class="col-md-8">
Create user accounts for your staff.
</div>
<div class="col-md-4">
<div class="clearfix">
<div class="pull-right">
<a class="btn btn-lg btn-primary " href="#">Add Staff</a>
</div>
</div>
</div>
</div>
.row .col-md-4,
.row .col-md-8{
display:inline-block;
vertical-align:middle;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row row-vertical">
<div class="col-md-8">
Create user accounts for your staff.
</div>
<div class="col-md-4">
<div class="clearfix">
<div class="pull-right">
<a class="btn btn-lg btn-primary " href="#">Add Staff</a>
</div>
</div>
</div>
</div>
If you have the capability of using flex you can extend Bootstrap to support a vertical flexbox solution using just a bit of simple CSS:
.row.row-vertical {
display: flex;
align-items: center;
justify-content: center;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row row-vertical">
<div class="col-md-8">
Create user accounts for your staff.
</div>
<div class="col-md-4">
<div class="clearfix">
<div class="pull-right">
<a class="btn btn-lg btn-primary " href="#">Add Staff</a>
</div>
</div>
</div>
</div>
Note: You can read more about FlexBox and its support here: http://caniuse.com/#feat=flexbox
add these rules to your css
.col-md-8,.col-md-4{
display:inline-block !important;
vertical-align:middle !important;
}
.col-md-4{
float:right !important
}
It is wise to add an Id to your specific html elements to prevent breaking bootstrap and add these css rules to your ids.good luck...

Centered text and left aligned button in panel heading using Twitter Bootstrap

I am trying to center some text in the heading of a panel, along with a left-aligned button on the same row. The approach I'm currently taking works sort of OK for small widths, but as soon as the window gets to .col-sm territory (>750px) the text is no longer centered and instead seems to align right. Plus I'm not actually sure if this approach of trying to overlap a col-xs-1 and col-md-12 is really centering the text even for small window widths.
<div class="container">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-8">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="container" style="padding:0;">
<div class="row">
<div class="col-xs-1">
<button type="button" class="btn btn-default btn-sm">Button</button>
</div>
<div class="col-md-12" style="text-align:center">Heading</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Here is a JSFiddle.
stdout, Hi there. You can actually have the Heading in the center if you like.
I mean actually centered in the full width. Your example is offset by 1 col.
I do it without that extra col offset and the button still to the left.
Have a look at this example in my Fiddle to see how the Header text is actually centered in the whole div.
It has a lot let cols/row etc.
I left one of the other examples above so you can see the difference.
Here is the CSS used for this example.
<style>
.align-left{
float:left !important;
}
.center-text-vert{
line-height:30px; /*height of the button*/
}
.center {
text-align: center;
margin-right: 55px; /*width from the left to the far right of the button */
}
</style>
<div class="container">
<div class="row ">
<div class="col-sm-8 col-sm-offset-2 ">
<div class="panel panel-primary ">
<div class="panel-heading">
<div class="center-text-vert">
<button type="button" class="btn btn-default btn-sm align-left">Button</button>
<div class="center">
Heading
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Try removing the container inside you panel and use pull-left class for your button and text-center class for your heading and see if that works for you.
<div class="container">
<div class="row">
<div class="col-sm-3"></div>
<div class="col-sm-8">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="row">
<div class="col-xs-1">
<button type="button" class="btn btn-default btn-sm pull-left">Button</button>
</div>
<div class="col-xs-11 text-center">Heading</div>
</div>
</div>
</div>
What I guess you want is for the title to be full width at xs size and only take up a portion of the screen at sm and higher. To do that, remove the empty column and instead apply an offset. Also, note there are 12 columns so offsetting by 3 (i.e. 3 columns on the left and 3 on the right) means the middle column can only be 6 wide. I've set this one to be offset by 2 as an example. I've also fixed the inner columns to add up to 12 (although you should not nest container classes:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-sm-offset-2 col-sm-8">
<div class="panel panel-primary">
<div class="panel-heading">
<div class="row">
<div class="col-xs-1">
<button type="button" class="btn btn-default btn-sm">Button</button>
</div>
<div class="col-md-11" style="text-align:center">Heading</div>
</div>
</div>
</div>
</div>
</div>
</div>

Bootstrap: Button too high

I have a legend containing a button that I pull-right. Unfortunately, the button is too high. Please see a fiddle here.
<div class="container">
<div class="row pane" id="statistics-pane" style="">
<legend>Network<span class="btn btn-mini pull-right">Test</span>
</legend>
</div>
</div>
I would separate the pull-right class from your button, and wrap in a div, like
<div class="container">
<div class="row pane" id="statistics-pane" style="">
<legend>Network
<div class="pull-right">
<span class="btn btn-mini">Test</span>
</div>
</legend>
</div>
</div>
Maybe give it a top-margin to push it down a little?
legend .btn {
margin-top: 10px;
}

Resources