bootstrap CSS: center any col- that overflows a line - css

As can be seen in this fiddle, I have series of columns that are nested in a single row and designed to overflow onto as many new lines as necessary.
Since each col-* has the property align: left; from Bootstrap, cols that overflow onto a new line stay left aligned.
Visually, I'd like to have "incomplete" lines have divs that are centered, like this fiddle. But, these col- divs are created dynamically, so I can't just use offsets or additional rows to fix.
Any ideas on how to accomplish this? Thanks in advance.

Very easy. First, you change your HTML and add some special class just to make sure you don't affect the columns in other places of your layout. Since you have app-style, let's use that for the row, and let's add the myCentre class name to the xs-4 columns. Like this:
<div class="row app-style">
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>1</p>
</div>
</div>
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>2</p>
</div>
</div>
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>3</p>
</div>
</div>
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>4</p>
</div>
</div>
</div>
<div class="row app-style">
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>1</p>
</div>
</div>
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>2</p>
</div>
</div>
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>3</p>
</div>
</div>
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>4</p>
</div>
</div>
<div class="col-xs-4 myCentre">
<div class="box-btn-reports">
<p>Another col just because I can</p>
</div>
</div>
</div>
Now, we can use some extremely simple CSS. Basically, we're removing Bootstrap styles to use old school responsive approach: text-align:center for container, text-align:left for item elements, nothing new:
.app-style{text-align:center;}
.myCentre{display:inline-block;
float:none;
text-align:left;
margin-right:-4px;
vertical-align:top;}
.box-btn-reports {
text-align: center;
width: 100%;
margin-top: .5em;
margin-bottom: .5em;
border: 1px solid #888888;
border-radius: 8px;}
p {
padding: .1em;
font-size: 1.25em;
line-height: 150%;
}
and now you can [see the fiddle here] and how those columns are happily centered1

Related

How to get div's centered next to each other in this Bootstrap 3 grid?

I have 6 divs, each of which has a specific width in number of pixels. Using Bootstrap 3 I would like to have a maximum of 3 boxes on one row/line. As the screen gets smaller it should move to 2 divs on one line, and when it gets smaller even more just 1 div on a row. How can I do this?
I expected the code below to achieve this.
However, the divs displayed on one row unfortunately are not centered. How to center these divs?
.mainDiv {
margin: 0 auto;
}
.specificWidthInNumberOfPixels {
border: 14px solid #dd5;
width: 160px;
height: 160px;
padding: 15px;
text-align: center;
margin: 5px;
}
.float {
float: left;
display: table;
margin: auto;
}
<div class="mainDiv container-fluid">
<div class="col-xs-12 col-sm-12" style="">
<p style="text-align: center;">Header</p>
</div>
<div class="row" style="max-width: 700px; float: none; display: table; margin: auto; background-color: #000;">
<div class="col-xs-6 col-sm-4 float">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col-xs-6 col-sm-4 float">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col-xs-6 col-sm-4 float">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col-xs-6 col-sm-4 float">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col-xs-6 col-sm-4 float">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col-xs-6 col-sm-4 float">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
</div>
</div>
Update: In response to this answer: so the answer now produces the visual at the top, but instead what I'm looking for is the visual at the bottom:
If you can use CSS Grid, you're in good shape. Look on codepen to preview it, the code snippet seems like it's acting up:
https://codepen.io/tobyinternet/pen/bGdoBEr
.mainDiv {
margin: 0 auto;
width: 100%;
text-align: center;
}
.row-grid:before, .row-grid:after {
display: none;
}
.row-grid {
margin: 0 auto;
width:100%;
max-width: 500px;
display: grid !important;
grid-template-columns: repeat(auto-fit,160px);
column-gap: 10px;
row-gap: 10px;
justify-content: center;
}
.specificWidthInNumberOfPixels {
border: 14px solid #dd5;
width: 160px;
height: 160px;
padding: 15px;
text-align: center;
}
p {
color: white;
}
<div class="mainDiv container-fluid">
<div class="col-xs-12 col-sm-12" style="">
<p style="text-align: center;">Header</p>
</div>
<div class="row row-grid" style="background-color: #000;">
<div class="col">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
</div>
</div>
You're supposed to wrap them in a container-fluid or container class with a row child element, like so:
<div class="container-fluid">
<div class="row">
<div class="col-xs-6 col-sm-4">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col-xs-6 col-sm-4">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
<div class="col-xs-6 col-sm-4">
<div class="specificWidthInNumberOfPixels">
<p>Text</p>
</div>
</div>
</div>
</div>
This will display 1 row of 3 columns. Add a second row within the container div with the same 3 columns to display a second row of 3 columns

Elements inside the row to be center aligned

I want is that these circles to be center aligned , I tried using some display inline block but no affect on it and some other properties that make center align but i failed picture of those circles
I am searching for this already the whole day :s
How can help me on this one?
Your help would be much appreciated.
Thank you
.menu{
width:70px;
height:70px;
border-radius:50px;
font-size:20px;
color:green;
line-height:100px;
background:#32C947;
overflow: hidden;
list-style-type: none;
margin-left: auto;
margin-right: auto;
display: table-cell;
vertical-align: middle;
}
.menu:hover{
color:#ccc;
text-decoration:none;
background:#333
}
<div class = "container">
<div class="row ">
<div class="col-md-12 ">
<h1 class = "_font ">All Plans Include</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Hello</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 ">
<h1 class="_circle">Google Analytic</h1>
</div>
</div>
</div>
To the parent div of a circle you have to give some width property and use
this and margin: 0 auto;. I hope this will works.
In your code parent div .col-md-1 .so you have to create one more div and
apply above property.
<div class="col-md-1 ">
<div class="circle_menu">
</div>
<h1 class="_circle">Google Analytic</h1>
</div>
.menu{
width:70px;
height:70px;
border-radius:50px;
font-size:20px;
color:green;
line-height:100px;
background:#32C947;
display: block;
overflow: hidden;
list-style-type: none;
margin-left: auto;
margin-right: auto;
vertical-align: middle;
}
.center{
text-align: center;
}
.menu:hover{
color:#ccc;
text-decoration:none;
background:#333
}
<div class = "container">
<div class="row justify-content-center">
<div class="col-md-12 center">
<h1 class = "_font ">All Plans Include</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Hello</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Google Analytic</h1>
</div>
<div class="col-md-1 center">
<h1 class="_circle">Google Analytic</h1>
</div>
</div>
</div>
The col-md-1 class is probably from bootstrap and therefor floating to the left. You could add the col-md-offset class to push the menu items to the center.
There are several ways to center align that block. But since you use the Bootstrap it will be best to use the in build display:block property of a center-block class.
Set an element to display: block and center via margin. Available as a
mixin and class.
Read More from Here Bootstrap Documentation About centering a Class
You can do either any one of this
<div class = "container center-block">
<div class="row ">
CODE SNIPPPET
</div>
</div>
OR
<div class = "container">
<div class="row center-block">
CODE SNIPPPET
</div>
</div>
The center-block class which we add overrides the display:block put in by using the regular container or row class
Hope This helped. Happy Coding. :)
Do you have Bootstrap actually linked to your code? I just put it into a CodePen
and linked Bootstrap3 and it formatted correctly as you want (except the text is to big). I took out these lines from your .menu class as well:
vertical-align: middle;
list-style-type: none;
margin-left: auto;
margin-right: auto;
Edit:
So the reason why your circles are not centered is because you are using Bootstrap's grid system. The grid is sliced into 12 different sections and you have 8 of the sections filled with elements (and 12 isn't divided by 8 evenly). That means that there are 4 spaces not being used to the right of your circles.
To fix this, I added two empty div tags before all of the elements you have implemented like so:
<div class="col-md-1"></div>
This makes it so that there are two empty grid slots before your circles (to the left) and two after (to the right). Here is the Updated CodePen for you to look at.
Note: I added this style for you to center your text through CSS:
div.row {
text-align: center;
}

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>

Why do browser prefixes for text-align have different behaviour and which is correct?

I want to vertically centre <div> tags that have a horizontal margin between each other.
The problem is that this behavior appears to be inconsistent between text-align: center and text-align: -webkit-center or text-align: -moz-center:
.parent {
display: inline-block;
border: 1px dotted #fd0;
position: relative;
}
.parent.ta {
text-align: center;
}
.parent.browser-ta {
text-align: -webkit-center;
text-align: -moz-center;
}
.child {
display: inline-block;
vertical-align: top;
}
.child > .content {
display: block;
margin: 0 10px;
border: 1px solid #888;
width: 200px;
text-align: left;
}
.wrong {
background-color: #e00;
color: #fff;
}
.right {
background-color: #0a3;
color: #fff;
}
<div>
Using <tt>text-align: center</tt>;
<div class="parent ta">
<div class="child">
<div class="content wrong">child 1 LEFT</div>
<div class="parent ta">
<div class="child">
<div class="content">child a</div>
</div>
<div class="child">
<div class="content">child b</div>
</div>
<div class="child">
<div class="content">child c</div>
</div>
</div>
</div>
<div class="child">
<div class="content wrong">child 2 LEFT</div>
<div class="parent ta">
<div class="child">
<div class="content">child d</div>
</div>
<div class="child">
<div class="content">child e</div>
</div>
<div class="child">
<div class="content">child f</div>
</div>
</div>
</div>
<div class="child ">
<div class="content right">child 3 CENTRE</div>
</div>
</div>
</div>
<br>
<br>
<div>
Using <tt>text-align: -vendor-center</tt>
<div class="parent browser-ta">
<div class="child">
<div class="content right">child 1 CENTRE</div>
<div class="parent browser-ta">
<div class="child">
<div class="content">child a</div>
</div>
<div class="child">
<div class="content">child b</div>
</div>
<div class="child">
<div class="content">child c</div>
</div>
</div>
</div>
<div class="child">
<div class="content right">child 2 CENTRE</div>
<div class="parent browser-ta">
<div class="child">
<div class="content">child d</div>
</div>
<div class="child">
<div class="content">child e</div>
</div>
<div class="child">
<div class="content">child f</div>
</div>
</div>
</div>
<div class="child">
<div class="content right">child 3 CENTRE</div>
</div>
</div>
Run that snippet and the two similar HTML and CSS produce different layouts in Chrome (Webkit/Blink) and FireFox. The red panels are in the wrong location, the green ones are correct.
So text-align: -webkit-center and text-align: -moz-center appear to be correct (to me) but text-align: center appears to be bugged in both browsers.
Digging out the venerable old <centre> tag (that we're not supposed to use) and that works right too (though examining it reveals it uses the browser prefix too).
Is this correct? Is this a bug? Is there a reason for the difference? Which one should I use?
The prefixed values are described by MDN to be "block alignment values", which means block boxes themselves are aligned in addition to the inline content within them. This is the exact behavior of the <center> element, and the prefixed values are in fact intended for that element — if you look in the UA stylesheets for each engine you'll find a ruleset that says exactly center { display: block; text-align: -vendor-center; }.
The reason text-align: center is not implemented this way is because text-align is designed to affect inline-level boxes (as evidenced by the "text-" in its name), not block-level boxes. But that, I suspect, is not the answer you're really looking for.
What's happening is that the boxes that are actually being aligned in your snippet are the .content elements, which are block boxes, not inline-blocks. The reason that last element is being centred is because its parent, an inline-block, is being shrink-wrapped, and itself then centred by the text-align: center declaration in its ancestor.

How do I align images to the bottom of a div (in bootstrap)?

I would like to be able to align different sized images to the bottom of a div.
I have the following markup:
<div class="container">
<div class="container">
<div class="footer-images">
<img src="img1">
<img src="img2">
<img src="img3">
</div>
</div>
<div class="container">
<div class="copyright">
<p>© Some Company YYYY</p>
</div>
</div>
</div>
I can't figure out how to have all the images aligned to the bottom of the footer-images div. Any help would be greatly appreciated.
try this
.footer-images img{
vertical-align:bottom;
border:0;
}
In Bootstrap v4 you can use the class align-items-end to achieve bottom alignment in a div. You can use this class on the row or on the column.
Example with all column content aligned to the bottom.
<div class="container">
<div class="row align-items-end">
<div class="col">
One of three columns
</div>
<div class="col">
One of three columns
</div>
<div class="col">
One of three columns
</div>
</div>
</div>
Example with first column top, second colum middle and third colunm bottom alignment.
<div class="container">
<div class="row">
<div class="col align-self-start">
One of three columns
</div>
<div class="col align-self-center">
One of three columns
</div>
<div class="col align-self-end">
One of three columns
</div>
</div>
</div>
Source: Bootstrap documentation.
Does this help?
<style type="text/css">
.footer-images {
margin: auto;
display: block;
position: absolute;
bottom: 0;
}
.footer-images .copyright {
text-align: center;
}
</style>
Using this HTML
<div class="container">
<div class="container">
<div class="footer-images">
<img src="http://placehold.it/350x150">
<img src="http://placehold.it/640x480">
<img src="http://placehold.it/120x120">
<div class="container">
<div class="copyright">
<p>© Some Company YYYY</p>
</div>
</div>
</div>
</div>
</div>
used to this css and apply
.footer-images img{
width:xxpx;
height:xxpx; // add here your style as like with height border etc.
vertical-align:top;
border:0;
}
More about bootstrap
It would be enough:
margin-top: auto;
See https://jsfiddle.net/d3jau1gx/

Resources