How to center a col-xs-12 in bootstrap? - css

I am having trouble centering the columns on bootstrap when the screen is sm or xs.
The fiddle is here https://jsfiddle.net/iggyfiddle/DTcHh/34028/. Here is what I am talking about:
When the screen is large (or medium), I have no problem. It appears centered.
When the screen starts to get smaller, it starts to become noticeable. If you notice, the whole column stack leans to the left, leaving some space on the right (I added red bg for emphasis. Hard to tell when bg is white).
When screen is xs, it is even more noticeable. Note that everything is leaning to the left, leaving a lot of space on the right.
I saw a popular SO post that addressed similar issue here. The solution suggested float: none; margin: 0 auto;, which I did - but it didn't do anything. I also tried adding center-block class, but the result is still nothing.
What do I have to do to center my columns? My goal is on xs screen, I want to have one column stacked up. When it is on sm screen, I want to have two columns, side-by-side, stacked. Anything larger should have 4 adjacent columns.

It's because you're using a width on the .card. Adding margin: 0 auto to the .card class will solve your issue.

I also tried adding center-block class, but the result is still nothing.
That's because you're using bootstrap 3.0. .center-block was added in bootstrap 3.1. If you can upgrade to 3.1 (or even better, the current release 3.3.7), .center-block fixes it.
If not, you can use #media (max-width: 992px) { .card { margin: auto; } }
I used both in my solution below.
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.col-centered {
margin: 0 auto;
}
#media (max-width: 992px) {
.col-centered .card {
margin: auto;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-md-3 col-sm-6 col-xs-12 col-centered center-block">
<div class="card center-block" style="width: 200px;">
<img class="card-img-top img-circle img-responsive" style="margin:0 auto;" src="http://placekitten.com/g/150/150" alt="Card image cap">
<div class="card-block">
<h4 class="card-title text-center">Card title 1</h4>
<p class="card-text text-justify">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 col-centered center-block">
<div class="card center-block" style="width: 200px;">
<div class="card-block">
<h4 class="card-title text-center">Card title 2</h4>
<p class="card-text text-justify">Omnichannel CPM call-to-action low hanging fruit branding. Inbound holistic tweens dynamic content branding granular. Mission critical lean content snackable content drone inbound. Leading the pack buzzword flat design click bait B2C conversions millennials. Alignment pass the clap content marketing multiple points of entry context.</p>
</div>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 col-centered center-block">
<div class="card" style="width: 200px;">
<img class="card-img-top img-circle img-responsive" style="margin:0 auto;" src="http://placekitten.com/g/150/150" alt="Card image cap">
<div class="card-block">
<h4 class="card-title text-center">Card title 3</h4>
<p class="card-text text-justify">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
</div>
</div>
<div class="col-md-3 col-sm-6 col-xs-12 col-centered center-block">
<div class="card" style="width: 200px;">
<div class="card-block">
<h4 class="card-title text-center">Card title 4</h4>
<p class="card-text text-justify">Conversions buzzword trending brand voice B2C curated ideation. Branding user-friendly hashtag tablet click-through rate disrupt iterative. CRM alignment The Cloud visibility taste makers. CRM snackable content engagement millennials. Content marketing integrated big data. Tablet shoptimization intuitive top influencers reaching out.</p>
</div>
</div>
</div>
</div>

You have used fixed hieght 200px, I just run the code in fiddle. If you remove height:200px; and use .text-center class with card-block section then everything will be centered. But the main issue is you have used bootstrap-3.0.0, use the latest version of Bootstrap the issue will be fixed instantly....

i think it's a duplicate of this link
Since v3.0.1 Bootstrap has a built-in class named center-block that uses margin: 0 auto but is missing float:none. You can add that to your CSS to make it work with the grid system.
check the above link may be helpfull for you.

Related

Alternating bootstrap grid columns and mobile view

I have this design:
Desktop or large screens.
Notes* Red or green boxes are not the same height.
Mobile or lower screens.
How to create this layout type on bootstrap 4 grid or custom CSS grid, what is better?
Thanks, have a nice day.
First of all, if you would like to build a masonry-like columns, a regular Bootstrap grid system won't give you what you want. Instead, you will have to use the card columns or JavaScript masonry plugins.
Layout with Card Columns
<div class="container">
<div id="example" class="card-columns">
<div id="card1" class="card border-success">
<div class="card-body">
<p class="card-text">Card #1 - height: 20rem;</p>
</div>
</div>
<div id="card2" class="card border-success">
<div class="card-body">
<p class="card-text">Card #2 - height: 12rem;</p>
</div>
</div>
<div id="card3" class="card border-danger">
<div class="card-body">
<p class="card-text">Card #3 - height: 18rem;</p>
</div>
</div>
<div id="card4" class="card border-danger">
<div class="card-body">
<p class="card-text">Card #4 - height: 14rem;</p>
</div>
</div>
</div>
</div>
With little bit of styling to setup 1 column for mobile view and 2 for others:
#example.card-columns {
column-count: 1;
}
#media(min-width: 576px) {
#example.card-columns {
column-count: 2;
}
}
You can get something closed to what you would like to achieve:
demo: https://jsfiddle.net/davidliang2008/n3fhyp8c/60/
Problems on Mobile View
Now there is a problem on the mobile view. Since the order of the CSS columns is top to bottom, left to right, and there is no way to change it, the only way to get what you want on the mobile view is to duplicate contents and hide/show them based on the breakpoints...
Again, your second screenshot is missing a red box so I don't know which one you want to put in the middle. Here I am assuming you want to put both red boxes there:
<div id="card1" />
<div id="card2" class="card border-success d-none d-sm-block">
...
</div>
<div id="card3" />
<div id="card4" />
<div id="card2" class="card border-success d-block d-sm-none">
...
</div>
Then you can get something you want to achieve on mobile view:
demo: https://jsfiddle.net/davidliang2008/n3fhyp8c/63/

Add Bootstrap panel with border and centralized text

I'm trying to create two panels in bootstrap with centralized text and different fonts in the text. Like the image below
My code:
<div class="row">
<div class="col-lg-6">
<div class="panel panel-default text-center">
<p>
Collected This Quarter <br>
<b>$8084</b>
</p>
</div>
</div>
<div class="col-lg-6">
<div class="panel panel-default text-center">
<p>
Average Property Income YTD <br>
<b>$16,985</b>
</h4>
</div>
</div>
</div>
However, my output is:
How can I have the "shadow" outside the panels and the text inside in similar font to the image above?
Thanks you
I also used to face this problem. So, here is the workaround.
Add the top-level child divs and give them padding (like p-1). This is their only job.
Within each top-level div, add your cell's main content, and style them however you want.
This is the code
<div class="parent row">
<div class="col-lg-6 p-1">
<div class="shadow-sm text-center child">
<p class="p-0 m-0">
Collected This Quarter <br>
<b>$8084</b>
</p>
</div>
</div>
<div class="col-lg-6 p-1">
<div class="shadow-sm text-center child">
<p class="p-0 m-0">
Average Property Income YTD <br>
<b>$16,985</b>
</h4>
</div>
</div>
</div>
Normally I reset padding and margins for .row and .col classes
<style>
.row, .col-lg-6 {
padding: 0;
margin:0;
}
.child {
background-color: white;
}
.parent {
background-color: #F3F4F5;
}
</style>
The result is like this
NOTE: I've used bg-dark and bg-light classes for showing the background colors of parent and child divs. You can give them whichever color you want.
Check out this, used .panel-body wrapper
https://jsfiddle.net/420gwtLz/7/
I will suggest using the latest version of Bootstrap, you will get utility classes for shadow and spacing.

Get divs next to each other, with same height, in md, lg and xl devices and get divs in block in smaller devices

Im using bootstrap and I want that in medium, large and extra large devices get the first layout of the image below, where there is a image at the left and then some information at right. And these 2 areas (the image and the informations div) occupy the full .container div width and have always the same height.
Then in smaller devices I want to get the layout in the image below, where the image is above and the informations below.
Image to demonstrate the layout that Im trying to get:
Image:
enter image description here
But Im getting this: https://jsfiddle.net/ce228caL/
Do you know how to properly get the layouts of the image?
html:
<div class="container py-md-5">
<div class="row justify-content-md-center">
<div class="col-12 col-md-auto">
<img style="width: 100%; height: auto" src="http://via.placeholder.com/1000x400"/>
</div>
<div class="col col-lg-2 d-md-flex">
<div class="d-none d-md-block details-title d-flex flex-column align-items-start">
<span class="font-size-sm font-weight-semi-bold">Title</span>
<h1 class="h5 mb-0 title">Subtitle</h1>
Link
</div>
</div>
</div>
</div>
css
.title{
display: flex;
flex-direction: column;
justify-content: space-between;
border: 1px solid $light-gray;
padding: 1rem;
margin-top: 1rem;
}
.subtitle{
margin-top: 1rem;
}
.link{
margin-top: 1rem;
}
You specified your first div to have all 12 columns on every screen size
<div class="col-12 col-md-auto">
Reduce it to 10 on medium and up, so the 2nd 2-column div will fit in the row. also set the 2nd div to col-md-2, so it occupies 2 columns on medium. Also, I am not sure why you would want to solve this using flexbox instead of the classic grid
<div class="container py-md-5">
<div class="row">
<div class="col-md-10 col-12">
<img style="width: 100%; height: auto" src="http://via.placeholder.com/1000x400"/>
</div>
<div class="col col-md-2 col-12">
<div class="d-none d-md-block details-title d-flex flex-column align-items-start">
<span class="font-size-sm font-weight-semi-bold">Title</span>
<h1 class="h5 mb-0 title">Subtitle</h1>
<span class="subtitle font-size-sm font-weight-semi-bold">Subtitle 2</span>
Link
</div>
</div>
</div>
</div>

Bootstrap card image distorted in Internet Explorer

I'm using bootstrap v4 cards in my layout and unfortunately, the images are distorted in Internet Explorer 11. It seems that IE completely ignores the height: auto attribute given by the img-fluid class. Is it necessary to apply a custom height to the card images? However, the cards render perfectly in Chrome and Firefox. Interestingly, if I change the engine to IE 10 (in the F12 console), the problem is gone.
As images with class img-fluid which are not wrapped by cards are displayed respecting their original ratio, I think the problem corresponds to the card layout.
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="img/1.jpg" alt="Step 1" width="600" height="400" />
<div class="card-block">
<h3 class="card-title">Step 1</h3>
<p class="card-text">Text 1</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="img/2.jpg" alt="Step 2" width="600" height="400" />
<div class="card-block">
<h3 class="card-title">Step 2</h3>
<p class="card-text">Text 2</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<img class="card-img-top img-fluid" src="img/3.jpg" alt="Step 3" width="600" height="400" />
<div class="card-block">
<h3 class="card-title">Step 3</h3>
<p class="card-text">Text 3</p>
</div>
</div>
</div>
</div>
</div>
I had the same problem, but when I wrapped the content of the card in a <a> tag to make it clickable and it fixed itself, but the height of the card was wrong in IE11, I fixed that by adding height: 100% to the <a> tag :
<div class="col-md-4">
<div class="card h-100">
<a href="/document" style="height:100%;">
<img class="card-img-top img-fluid" src="foo.jpg">
<div class="card-block">
<h4>doc number 4</h4>
<p class="card-text">yada yada</p>
</div>
</a>
</div>
</div>
If you don't want your card to be clickable, you could replace the <a> by a <div> (I haven't tested it).
Bootstrap 4 is still in alpha so you should expect various issues.
The issue with image height in IE 11 has already been identified and you can track it here:
https://github.com/twbs/bootstrap/issues/21885
Addding height:100%; can also be done to:
.card img{
height:100%;
}
if you dont want to add another class etc to fix issues in explorer.
the solution is to add d-block (display:block) to the parent div:
<div class="card d-block">
<img class="card-img-top" src="someimage.jpg">
</div>
use overflow: hidden for external container
Inline styling do the magic...
eg.: style="height: 100%; overflow: hidden;"
This is a known issue. You can fix this by adding width: 100%;
According to docs:
SVG images and IE 10 In Internet Explorer 10, SVG images with
.img-fluid are disproportionately sized. To fix this, add width: 100%
\9; where necessary. This fix improperly sizes other image formats, so
Bootstrap doesn’t apply it automatically.
You can check the docs in this link: https://getbootstrap.com/docs/4.3/content/images/
Based on the comments in the issue tracker the only thing that worked for me in IE11 was adding a height: 0.01px to the card image.
To make it IE specific I've added the following rule:
#media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
/* IE10, IE11 */
.card-img-top {
min-height: 0.01px;
}
}
In my case I also have a .card-footer which failed to render properly in IE11 with all previous answers. I'm using Bootstrap 4.1.3.
The best solution for me was to modify IE only.
Turns out it was the card-body text causing the most issues because it wasn't wrapping properly.
The card element did also need display:block; but again, only on IE.
No need to add block for other browsers, as it can affect using the mt-auto class or other utilities.
Here's the full solution:
<div class="card ie-block">
<img src="https://picsum.photos/500/300" class="card-img-top" />
<div class="card-body">
<div class="ie-wrapper"><h5 class="card-title ie-inner">Card title</h5></div>
<div class="ie-wrapper"><p class="card-text ie-inner">Some quick example text to build on the card title and make up the bulk of the card's content.</p></div>
Go somewhere
</div>
</div>
Then, add the following IE10+ only CSS:
/* IE10+ specific styles go here */
#media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
.ie-block {
display: block;
}
.ie-wrapper {
display: flex;
}
.ie-inner {
flex-basis: 100%;
}
}
I had the same issue. Just added the regular old school class "img-responsive" & seems to be working fine in Chrome now.
<img class="card-img-top img-responsive" src="images/your-image.jpg" alt="Description">
Update BS version 4:
img-responsive
is now
img-fluid

Content Positioning trick on bootstrap

Please check this HTML template first Clippinghand
My problem is on what we provide's content below. There is first image then posts and next is first post then image like that 6 Element i took. On Computer type devices things are okay but when I'm going to make it responsive for mobile or tab devices it's making problem and the problem is images and content are getting closer i just always want to make them top of the posts I mean i want my images top of the post all of, How could i do that? Is there any trick ? Also How i can Do complete this section with only one custom post ?
Here is an example on image
You can use Column Ordering via Push + Pull which is built into Bootstrap. You'll have to reorder your content to achieve this. Here a good article also.
Basically stack the column content in the opposite order and apply push and pull classes. (*instead of content 1 then content 2, stack content 2, then content 1 inside your col-md-6). See working example and it will be illustrated.
.red {
background: red;
}
.blue {
background: lightblue;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<section id="our-services">
<div class="container">
<div class="row red">
<div class="col-md-6">
<div class="img-one">
<img src="http://placehold.it/350x150" class="img-responsive" alt="">
</div>
</div>
<div class="col-md-6">
<div class="content-one">
<h4>Background Remove</h4>
<em>Price starts from $0.49</em>
<p>This is the most demandable and most used image editing service all over the world for ecommerce product selling. Don’t worry, we make it easy to ensure all your images fit your ecommerce image editing guideline. We resize, crop, remove borders,
and remove image background turning it to pure white, transparent or color background as per your guideline.</p>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row blue">
<div class="col-md-6 col-md-push-6">
<div class="img-two">
<img src="http://placehold.it/350x150" class="img-responsive" alt="">
</div>
</div>
<div class="col-md-6 col-md-pull-6">
<div class="content-two">
<h4>Web-ready Images</h4>
<em>Price starts from $1</em>
<p>For web-shop owners, we introduce our additional delivery of web-ready images. Add the image specifications (like crop size 800×800 px ) and we will deliver web-ready image. We can fulfill the image size requirements for any ecommerce platform
like Amazon, eBay, Shopify, bigcommerce, volusion, squarespace etc.</p>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row red">
<div class="col-md-6">
<div class="img-three">
<img src="http://placehold.it/350x150" class="img-responsive" alt="">
</div>
</div>
<div class="col-md-6">
<div class="content-three">
<h4>Multiple Mask</h4>
<em>Price starts from $2</em>
<p>If you want to separate and modify different parts and features of the same product including its color, shape, and size, Multipath is the service you can take from us. We can deliver images with Alpha Channel, Layer Mask or Only Path.</p>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row blue">
<div class="col-md-6 col-md-push-6">
<div class="img-four">
<img src="http://placehold.it/350x150" class="img-responsive" alt="">
</div>
</div>
<div class="col-md-6 col-md-pull-6">
<div class="content-four">
<h4>COLOR MATCHING</h4>
<em>Price starts from $2</em>
<p>You have many different color variations of the same product but do not want to spend time taking photos of each one of them? No worries! You don’t have to. We can change the color and size of the same product as per your instruction to save
you from investing more of your time and money in taking photos.</p>
</div>
</div>
</div>
</div>
</section>
<!-- End of Our services area -->
Look here: http://jsfiddle.net/mwvfteuq/
In two words, you need to place your divs in the order you want them to appear on the smallest screen. For wider devices you can play with float set to left or right (pull-left and pull-right in bootstrap) to customize elements position in a row. Revoke those properties using media queries for the smallest screen.
For example:
HTML (using bootstrap)
<div class="row">
<div class="col-sm-6 pull-right right">img</div>
<div class="col-sm-6">some stuff</div>
</div>
<div class="row">
<div class="col-sm-6">img</div>
<div class="col-sm-6">some stuff</div>
</div>
CSS
#media (max-width: 768px) {
div.right{
float:none !important;
}
}
Do not forget to use !important to override bootstrap pull-right property

Resources