I'm trying to teach myself how to effectively center things using bootstrap. Centering is something I really struggle with, even after reading a ton of other SO posts asking the same question.
Why do we have to wrap the cols in a row, and then wrap that row in a container? What does this actually do?
body {
background-color:pink;
}
.container {
overflow: hidden;
background-color: yellow;
}
.col-sm-6 {
float: left;
height: 100px;
width: 100px;
background-color: blue;
padding: 10px;
margin: 1%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<div class = "container">
<div class = "row">
<div class = "col-sm-6"></div>
<div class = "col-sm-6"></div>
</div>
</div>
JSFiddle
1 row have 12 cols for you're first code :
<div class = "container">
<div class = "row">
<div class = "col-sm-6"></div>
<div class = "col-sm-push-6"></div>
</div>
</div>
That means you're second div is after the first who takes 6 cols. So you push the second after the first (col-sm-push-6)
This is for the web-responsive, when you're website is on a computer or in a mobile phone, the screen have different size. Bootstrap adapt you're div to the screen.
They're is sm : for small screen, lg : for large screen and md : for middle screen like a tab for exemple.
Let me explain for you. I love to teach beginners :-)
Firstly when we use column of bootstrap it adds '15px' padding from
right and left. in this way our column looks like '15px' inside from
wall of container.
Secondly we use row to overcome the first situation given upper paragraph.
Thirdly if you are trying to align Centre 2 columns of 6 and 6 then it is impossible to do this because 2 col-sm-6
occupy whole container space.
Fourthly do r&d about bootstrap offset and push properties. also check bootstrap row and columns properties by using code inspector. thanks
If you use bootstrap, there is class ready made to build your layout :
https://getbootstrap.com/docs/4.0/utilities/flex/
Flex
Quickly manage the layout, alignment, and sizing of grid columns, navigation, components, and more with a full suite of responsive flexbox utilities. For more complex implementations, custom CSS may be necessary.
If you need cols of a fixed width, you might need to create your own class here .
example:https://jsfiddle.net/cLw2ajro/7/
body {
background-color: pink;
}
.container {
overflow: hidden;
background-color: yellow;
}
.mycol {
background-color: blue;
height:100px;
flex:0 0 100px
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row d-flex flex-nowrap justify-content-center">
<div class="mycol m-2 p-4"></div>
<div class="mycol m-2 p-4"></div>
</div>
</div>
Try this
CSS:
body {
background-color:pink;
}
.row-centered {
display:block;
text-align:center;
}
.col-centered {
display:inline-block;
float:none;
/* reset the text-align */
text-align:left;
/* inline-block space fix */
}
.container {
overflow: hidden;
background-color: yellow;
}
.col-sm-6 {
height: 100px;
max-width:100px;
background-color: blue;
padding: 10px;
margin: 1%;
}
HTML
<div class = "container">
<div class = "row row-centered">
<div class = "col-sm-6 col-centered">-</div>
<div class = "col-sm-6 col-centered">-</div>
</div>
</div>
Fiddle
Source: This question
Related
I'm trying to work out how to achieve the following in Bootstrap 3:
I have a HTML page which is primarily based around bootstrap's fixed container grid.
Half way down the page I want a row with columns of different sizes.
I want the content (text / images) inside these columns to line up with the content inside the columns in the fixed container grid.
I want the background colours of the left and right furthest columns to bleed right to the edge of the page.
It may help if I illustrate what I'm trying to achieve:
Any help would be greatly appreciated!
Update: as requested here's some code examples of what I currently have: http://www.bootply.com/ZzOefJGRRq As you can see the text and columns in the fluid container are not lining up correctly.
Bootstrap 4
Use position absolute before or after elements with width: 50vw
Codepen
HTML
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
...
</div>
</div>
<div class="row">
<div class="col-lg-6 c-col-bg--red">
...
</div>
<div class="col-lg-6 c-col-bg--blue">
...
</div>
</div>
</div>
CSS
.container-fluid {
max-width: 1000px;
}
#media (min-width: 992px) {
div[class*="c-col-bg"] {
position: relative;
}
div[class*="c-col-bg"]:after {
content: "";
position: absolute;
top: 0;
bottom: 0;
z-index: -1;
width: 50vw;
}
.c-col-bg--red:after {
right: 0;
background: red;
}
.c-col-bg--blue:after {
left: 0;
background: blue;
}
}
You can use :before elements and some classes
https://jsfiddle.net/ex3ntia/wa8myL9v/2/
.bg:before {position:absolute;left:0em; content:'';height:100%;width:800em;z-index:-1}
UPDATE
added media query for small devices
https://jsfiddle.net/ex3ntia/wa8myL9v/3/
UPDATE 2
I have added the following line to fix the big horizontal scroll on chrome browsers.
body, html {overflow-x: hidden;margin: 0;padding: 0;}
TLDR; no framework has this out of the box, because covering all possible use cases is both very complex and would result in a huge amount of code.
It is doable but requires some amount of manual coding. The approach below works for 2 columns. More columns and breakpoints will require a more complex solution.
Sample markup:
<div class="container">
<div class="row">
<div class="col-5">default column</div>
<div class="col-7">default column</div>
</div>
</div>
<div class="container container--fluid">
<div class="row">
<div class="col-5">fluid column, aligned with above</div>
<div class="col-7">fluid column, aligned with above</div>
</div>
</div>
<div class="container container--bleed">
<div class="row">
<div class="col-5">
<div class="content">like fluid, but content is aligned with default</div>
</div>
<div class="col-7">
<div class="content">like fluid, but content is aligned with default</div>
</div>
</div>
</div>
scss for brevity
// assuming you have these values or able to set them
$max-width: 1140px;
$gutter: 8px;
$grid: 12;
$customColumns: [5, 7]; // columns you want to align
// sample grid
.container {
max-width: $max-width;
margin: 0 auto;
padding-left: $gutter;
padding-right: $gutter;
}
.row {
display: flex;
margin-left: -$gutter;
margin-right: -$gutter;
}
div[class^='col'] {
max-width: 100%;
padding-left: $gutter;
padding-right: $gutter;
position: relative;
}
#for $i from 1 through $grid {
.col-#{$i} {
width: calc(100% * #{$i} / #{$grid});
}
}
.container--bleed, .container--fluid {
max-width: none;
}
// custom grid rules for alignment
#media(min-width: #{$max-width}) {
#each $i in $customColumns {
.container--bleed, .container--fluid {
.col-#{$i}:first-child, .col-#{$i}:last-child {
width: calc(#{$max-width * $i / $grid} + ((100% - #{$max-width}) / 2));
}
}
.container--bleed {
.col-#{$i}:first-child {
padding-left: calc((100% - #{$max-width}) / 2 + #{$gutter});
}
.col-#{$i}:last-child {
padding-right: calc((100% - #{$max-width}) / 2 + #{$gutter});
}
}
}
}
I created a codepen POC for a similar layout here: https://codepen.io/bariscc/pen/BajKpMP
You can implement the container-fluid to achieve this.
Basically your webpage will have the following structure:
<div class="container">
<p>Content here</p>
</div>
<div class="container-fluid">
<p>"Bleeded" content here</p>
</div>
<div class="container">
<p>And it continues with the fixed width!</p>
</div>
If you need to adjust the spaces between those containers, you can add your own classes or ID:s to each and kind of take it from there. Since containers in Bootstrap don't have much of a default styling, this is very efficient way of creating what you're looking to do in here.
EDIT: After inspecting the comments section and looking at the code you provided, I assume you want to have the fluid container, but keep the contents within it lined up with the fixed container, right?
For this, you can just put another <div class="container">...</div> in your container-fluid. Check the updated fiddle.
Where you have the special row, you need a div with container-fluid class encapsulating a div with container class (this is a fixed width class).
Then you need to account for the background colours either side. Either add additional divs within container-fluid each side of container and set background colour, or perhaps use a three column table.
It's my first time building a bootstrap website. I thought I knew how the columns worked, but when I try to use them they take up 100% width of the container (as opposed to taking up 1/4 of the container as they are supposed to). I've tried googling for people with the same problem but can't find anything. So, here's what I have...
HTML:
<body>
<div class = "hero container"></div>
<div class = "container-fluid how-it-works">
<div class = "row">
<div class = "col-md-3"></div>
<div class = "col-md-3"></div>
<div class = "col-md-3"></div>
<div class = "col-md-3"></div>
</div>
</div>
<div class = "featured container"></div>
<div class = "testimonials container"></div>
<div class = "about container"></div>
</body>
CSS:
.how-it-works {
height: 550px;
background-color: #f7f7f7;
}
.how-it-works .row .col-md-3 {
height: 550px;
background-color: blue;
width: 100%;
}
If anyone can figure out what I'm doing wrong, I would really appreciate some help. Thanks!
Remove following CSS (setting width on bootstrap columns):
.how-it-works .row .col-md-3 {
width: 100%;
}
For instance col-md-3 is defined as:
.col-md-3 {
width: 25%;
}
and your code changes that. Remove it and it will again take 1/4 of space.
width for .col-md-3 is already set by bootstrap so you are overwriting it with your css. you can check how it works here: http://getbootstrap.com/css/#grid
I have a div inside a Bootstrap container-fluid like so:
<div class="container-fluid">
<div class="col-md-12 myDiv">
</div>
</div>
My CSS:
.myDiv {
height: 200px;
background-color: red;
}
But of course, the background of myDiv doesn't go all the way to the edges of the view, since container-fluid has padding/margin on it. Anyway to over come this?
If you want to use bootstrap element then use class row so it will overcome the issue of container-fluid or else you can use custom class(in my case .pd_none).
With row demo
.myDiv {
height: 200px;
background-color: red;
}
HTML:
<div class="container-fluid">
<div class="col-md-12 myDiv row"></div>
</div>
With custom class
.myDiv {
height: 200px;
background-color: red;
}
.pd_none{padding:0px !important;margin:0px !important;}
HTML:
<div class="container-fluid pd_none">
<div class="col-md-12 myDiv ">
</div>
</div>
For more detail here is link
You can simply override your div (not a good practice to override the divs of bootstrap, but if requirement says then we need to )
.col-md-12.myDiv{
margin:0px;
padding:0;
}
And this shall work.
{By default 15px pad is added on each side of the col by default}
I have this site:
http://avocat2.dac-proiect.ro/?page_id=25
At this point my items are centered as they wish. The only problem is that it is not responsive.
This is code HTML:
<div class="parentVerticalCenter">
<div class="childVerticalCenter">
<div class="row sss">
<div class="col-sm-4 col-md-4 col-lg-12 col-lg-offset-0" style="font-size:17px;color:white;">
<div class="container3">
<div class="centered">[Contact_Form_Builder id="10"]</div>
</div>
</div>
</div>
</div>
</div>
This is code CSS:
.container3 {
background-color: green;
}
.centered {
display: table;
margin: 0 auto;
background-color: red;
}
div[wdid="4"] {
width: 40%;
display: inline-block;
}
div[wdid="22"] {
width: 40%;
display: inline-block;
margin-left:-40px;
}
div[wdid="2"] {
width: 40%;
display: inline-block;
}
div[wdid="6"] {
width: 40%;
display: inline-block;
margin-left:-40px;
}
If you delete this code, my elements are responsive but are not aligned properly.
.contactform10 .wdform_column
{
width:50% !important;
}
I tried to use min-width and max width for this but does not work so take one above the other elements
Basically, my div red is divided into two equal parts, each having a width of 50%.
Can you please help me solve this problem?
Items to be displayed as they are now and be responsive.
Thanks in advance!
Using "Push/Pull" with bootstrap will help you reposition elements in a stacked order when the screen resizes... perhaps this is what you meant?
full size:
| ITEM 1 | ITEM 2|
Smaller size:
| ITEM 1 |
| ITEM 2 |
Bootstrap discusses it here: http://getbootstrap.com/css/#grid-column-ordering
If this is the case, this question/answer may also assist you:
Bootstrap 3: Push/pull columns only on smaller screen sizes
I use foundation (not bootstrap) and to do it in that framework you simply set up as similar to this pseudo-code:
<div id="item1" class="small-12-pull medium-12-pull large-6">lorem ipsum</div>
<div id="item2" class="small-12-push medium-12-push large-6">qwerty colec</div>
I'm working on a site which will use Bootstrap 3. The site will mainly use dark background-color but for a header-row containing a logo and a menu the background-color should be white. However! I do not want the white color to fill the entire width (all the way to the "container" boundries). The attached image illustrates what I would like to do. The row contains 2 columns, one is 2 cols wide and the other one is 10 cols wide. I would like to fill the entire row with white but I do NOT want it to span the "padding" or margins marked with green (those shall remain blue). What's the best suggestion for doing this?
OK, maybe I'm bad on explaining but I'll give it another shot ;)
I've expanded the example given to better show what I want: http://jsfiddle.net/RedRockerSE/47Fsr/
I can't see how: background-clip: content-box; do this?
I want the outer "padding/margin/gutter"-areas (marked with arrows below) to be filled with the background-color (or NOT to be filled with the rows background-color if you will). The "padding/margin/gutter"-areas between the cols SHOULD be filled with the row background-color.
try this
<div class="container">
<div class="row">
<div class="col-xs-4 bg-color"></div>
<div class="col-xs-4 bg-color"></div>
<div class="col-xs-4 bg-color"></div>
</div>
</div>
css code
body {
background-color: #f00;
}
.row {
background-color: #fff;
}
.col-xs-4 {
background-clip: content-box;
height: 100px;
}
.bg-color {
background-color: #ccc;
}
try to user .row .no-gutter
HTML
<div class="container">
<div class="row no-gutter">
<div class="col-xs-4"></div>
<div class="col-xs-4"></div>
<div class="col-xs-4"></div>
</div>
CSS
body {
background-color: #f00;
}
.col-xs-4 {
background-color: #fff;
height: 100px;
}
http://jsfiddle.net/47Fsr/7/
you can do this;
[class^=col-].no-space-left {
padding-left:0;
}
[class^=col-].no-space-right {
padding-right:0;
}
and add inner div padding;
[class^=col-].no-space-left > div {
padding:0 15px
/*padding-right:15px;*/ /*if you want no space between col- */
}
[class^=col-].no-space-right > div {
padding:0 15px
/*padding-left:15px;*/ /*if you want no space between col- */
}
http://jsfiddle.net/47Fsr/32/