I have two rows a and b,I think the distance between the two rows in vertical is too small.
I want to make the distance more bigger,I know I can change the distance in horizontal by col-md-offset-*.But how to change the vertical distance?
<div class="row" id="a">
<img> ... </img>
<div>
<div class="row" id="b">
<button>..</button>
<div>
Now my solution is insert a tag of h1,I think it is not graceful.
<div class="row" id="a">
<img> ... </img>
<div>
<h1></h1>
<div class="row" id="b">
<button>..</button>
<div>
Does it have more graceful solution?
Instead of adding any tag which is never a good solution. You can always use margin property with the required element.
You can add the margin on row class itself. So it will affect globally.
.row{
margin-top: 30px;
margin-bottom: 30px
}
Update: Better solution in all cases would be to introduce a new class and then use it along with .row class.
.row-m-t{
margin-top : 20px
}
Then use it wherever you want
<div class="row row-m-t"></div>
use:
<div class="row form-group"></div>
If it was me I would introduce new CSS class and use along with unmodified bootstrap row class.
HTML
<div class="row extra-bottom-padding" id="a">
<img>...</img>
<div>
<div class="row" id="b">
<button>..</button>
<div>
CSS
.row.extra-bottom-padding{
margin-bottom: 20px;
}
UPDATE
Bootstrap 4 has spacing utilities to handle this https://getbootstrap.com/docs/4.0/utilities/spacing/
.mt-0 {
margin-top: 0 !important;
}
--
ORIGINAL ANSWER
If you are using SASS, this is what I normally do.
$margins: (xs: 0.5rem, sm: 1rem, md: 1.5rem, lg: 2rem, xl: 2.5rem);
#each $name, $value in $margins {
.margin-top-#{$name} {
margin-top: $value;
}
.margin-bottom-#{$name} {
margin-bottom: $value;
}
}
so you can later use margin-top-xs for example
Use <br> tags
<div class="container">
<div class="row" id="a">
<img src="http://placehold.it/300">
</div>
<br><br> <!--insert one, two, or more here-->
<div class="row" id="b">
<button>Hello</button>
</div>
</div>
There's a simply way of doing it.
You define for all the rows, except the first one, the following class with properties:
.not-first-row
{
position: relative;
top: -20px;
}
Then you apply the class to all non-first rows and adjust the negative top value to fit your desired row space. It's easy and works way better. :)
Hope it helped.
Related
I am learning bootstrap and I want to know how does the offset set on the
parent element gets applied to the child elements and what is its significance over applying the styles directly to the child element? I saw scenario 1 type of bootstrap set up in one of the popular website.
Scenario 1:
<div class = "footer-offset-padding navbar-offset-padding">
<div class="navbar">
</div>
<div class="main">
</div>
<div class="footer">
</div>
</div>
CSS:
.navbar-offset-padding {
padding-top: 40px;
}
.footer-offset-padding {
padding-bottom: 40px;
}
Scenario 2:
<div class = "nothing">
<div class="navbar navbar-offset-padding">
</div>
<div class="main">
</div>
<div class="footer footer-offset-padding">
</div>
</div>
CSS:
.navbar-offset-padding {
padding-bottom: 20px;
}
.footer-offset-padding{
padding-top: 20px;
}
As a beginner, if I wanted padding I would have followed an approach similar to Scenario 2. I am not able to understand, how does first scenario works. Why don't we add the padding "Directly to the Element Itself (Footer and Header in this case)". Is there some other benefit we get when applying Scenario 1. Please guide me a little. I am not even sure why/how does Scenario 1 even works?
I have a grid with multiple columns. All columns are in one row and the number of columns may vary.
Since I needed some vertical space between columns, I added margin-bottom on columns.
However, I want equal space between wrapper and the columns so I need to remove margin-bottom on last few columns in the row.
See sample here: https://jsfiddle.net/aucovic/rhhyu6h2/6/
How would I dynamically remove bottom-margin on those last few columns?
<style>
.wrapper {
background:#000;
padding:10px;
}
.box {
background: #fff;
}
.mb {
margin-bottom:10px;
}
</style>
<div class="wrapper">
<div class="row">
<div class="col-md-4 col-xs-6 mb">
<div class="box">
Box 1
</div>
</div>
<div class="col-md-4 col-xs-6 mb">
<div class="box">
Box 2
</div>
</div>
<div class="col-md-4 col-xs-6 mb">
<div class="box">
Box 3
</div>
</div>
<div class="col-md-4 col-xs-6 mb">
<div class="box">
Box 4
</div>
</div>
<div class="col-md-4 col-xs-6 mb">
<div class="box">
Box 5
</div>
</div>
</div>
</div>
Well, in case you want the wrapper to have some padding, a super quick-fix would be to set the bottom padding to be equal (initial padding - margin bottom of columns). On your example it's just gonna be padding-bottom: 0 for your wrapper. This is definitely not an ultimate fix, but is super simple and works in described case.
Here's a fiddle: https://jsfiddle.net/kLr8sog6/
Assuming you don't change the number of columns, nth child selectors could help:
//mobile and tablet view
#media(max-width:992px) {
.mb:nth-last-child(-n+2) {
margin-bottom:0px;
}
}
//wider views
#media(min-width:993px) {
.mb:nth-last-child(-n+3) {
margin-bottom:0px;
}
}
EDIT: Updated your fiddled to show this in effect: https://jsfiddle.net/rhhyu6h2/8/
Again, only for this specific layout. For variable columns layouts, you have to solve in a wrapper or through JS.
EDIT2: from reading your other comments, you could use a wrapper adjustment class on selective rows: https://jsfiddle.net/rhhyu6h2/9/
This doesn't require any new HTML.
#Antonio, in order to remove the bottom-margin dynamically you gotta use jQuery/JavaScript. On appropriate event add the class which has margin-bottom:0px.
Example:
CSS Code:
.removeBottomPadding{
margin-bottom:0px !important;
}
JavaScript Code: [Ensure to add an id to the wrapper]
window.onload = function() {
document.getElementById('wrapper').className = 'removeBottomPadding';
};
OR
jQuery Code:
$(function() {
$('.wrapper').addClass('removeBottomPadding');
});
That's all.
Good luck & hope that helps.
I'll start off by stating that I know this question has been asked a lot, but none of the answers I saw seemed to work for me.
Basically, I have some divs inside of a larger div. They'll have dynamic text, so I don't know how many lines each will be. The problem is that I can't seem to get the divs to size themselves to the parent's height. I want the column divs to take up the entire height of the row div (basically, I want that blue part to fill all the space between the bars).
HTML:
<div class="container">
<div class="row divOne">
<div class="col-xs-3 divTwo">Some Text</div>
<div class="col-xs-3">
Some text that could wrap to multiple lines
</div>
</div>
<div class="row divOne">
<div class="col-xs-3 divTwo">Different Text</div>
<div class="col-xs-3 divThree">
With some more text
</div>
</div>
</div>
CSS:
.divOne
{
border-top:10px solid black;
}
.divTwo
{
background-color: #32649b;
height:100%;
color:white;
}
jsfiddle:
Now, what I've learned from other versions of this question are that
float:left might be screwing it up
height:100% doesn't work if the parent's height is defined
position:relative might help on the parent
The problem with the float is that I'm using bootstrap, and that's where the float is coming from, so I don't really want to mess with that.
I can't really define parent height, because it'll be dynamic based on the children.
I also tried messing around with position:relative on the parent and absolute on the child, but that seemed to get really screwy. I'm also guessing this won't work because I'm using bootstrap. It's possible that I'm just missing something, though. I'll admit to not being the greatest with CSS.
I don't know if I'm having these issues because I'm using bootstrap, or because I'm just being an idiot right now.
Something else that seems to be throwing a wrench into things: These columns will be laid out differently on smaller screens vs. larger ones. I actually want something along the lines of col-xs-12 col-md-3 for these.
The short answer is that you can't really achieve this within the constraints of the bootstrap framework. There are plenty of articles that explain why div elements can't stretch to the height of their container, and how to get around this problem. One of the solutions I'm most fond of is Faux Columns.
But, let's get a little more creative then that.
I came up with something that might work for your scenario, but requires a bit of change to your markup. Here's a solution that wraps the bootstrap grid with display: table.
http://jsfiddle.net/Wexcode/13Lfqmjo/
HTML:
<div class="table-container">
<div class="table-row divOne">
<div class="col-xs-3 divTwo">Some Text</div>
<div class="col-xs-3">
Some text that could wrap to multiple lines
</div>
<div class="col-xs-6"></div>
</div>
</div>
CSS:
.table-container {
margin: 0 -15px;
}
.table-row {
display: table;
width: 100%;
}
.table-row [class^="col"] {
display: table-cell;
padding: 0 15px;
float: none;
}
Note that for this solution to work, you must include enough col elements to stretch it all 12 columns (see that I added an empty .col-xs-6 div).
You can add
display:flex;
to divOne , and will act like you wanted.
in bootstrap 4 'row' class applies this on div, but in ealier versions you need to add manually if you expect such behavior.
Give .divOne a display: flex and remove the height: 100% from .divTwo:
.divOne
{
border-top:10px solid black;
display: flex;
}
.divTwo
{
background-color: #32649b;
/*height:100%;*/
color:white;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row divOne">
<div class="col-xs-3 divTwo">Some Text</div>
<div class="col-xs-3">
Some text that could wrap to multiple lines
</div>
</div>
<div class="row divOne">
<div class="col-xs-3 divTwo">Different Text</div>
<div class="col-xs-3 divThree">
With some more text
</div>
</div>
</div>
I have this html:
<div class="col-xs-12">
<div class="row">
<div class="col-xs-8">
<h2>Some big header</h2>
</div>
<div class="col-xs-4 select">
<select class="form-control">
<option>TMP</option>
</select>
</div>
</div>
<div class="row">some more text</div>
</div>
and I am getting this result:
There is margin property on tag h1 so there are some blank space above h1. There are no such space above select, so this elements are not centered.
"What you've tried?"
I've tried bad and monstrouse method. I know it can be used to center divs. But not in this case... I have added line-height and height properties to the select:
.select {
height: 80px;
line-height: 80px;
}
.form-control {
display: table-cell;
}
Yes, it worked, but it is not relative at all. What if on left column there will be h2 text, not h1?
jsFiddle example of "what you tried?"
Also, "what you've tried?" 2: I tried to use form-horizontal class. Look this:
And the code:
<div class="col-xs-12 form-horizontal">
<div class="row form-group">
<div class="col-xs-8">
<h2 class="control-label">Some big header</h2>
</div>
<div class="col-xs-4 ">
<select class="form-control">
<option>TMP</option>
</select>
</div>
</div>
</div>
Also, text in the left column is right aligned (as form label, obvious). I don't want this.
jsFiddle example of "what you tried?" 2
So the question is: how I can center both elements vertically? Highly prefer using bootstrap classes and approaches. Don't suggest methods, where you specifying height of div, margins or paddings: it is not relative at all.
To be able to use vertical alignment, I think you need to override the floating used by bootstrap classes, and use inline-block for the columns. It's not exactly a bootstrap approach, but I think it works and is responsive. There is one disadvantage that can be a bit annoying, is that you need to make sure there is no space between the divs...
<div class="col-xs-12">
<div class="row">
<div class="col-xs-8 vcenter-col">
<h2 class="control-label">Some big header</h2>
</div><!-- comment required to prevent line break...
--><div class="col-xs-4 vcenter-col">
<select class="form-control">
<option>TMP</option>
</select>
</div>
</div>
</div>
.vcenter-col {
display: inline-block;
vertical-align: middle;
float: none;
}
Jsfiddle
Did I understand correctly your question?
Try this CSS :
h2 {
margin: 0;
}
BootStrap does a lot off things in CSS, so be careful to correcty set your CSS selector to not override all BootStrap CSS.
But, does your HTML can't change ? Is seems to me that the H2 element should have been a LABEL.
I have a Bootstrap 3 grid.
I need to add a header image that's 960 pixels wide.
However, if I add it to a row, the usual padding of 10px on a column "offsets" my image since it no longer "fits":
I know how to force this to work, but I was wondering if I am missing some modifier class in BS3 to make this work.
And, yes, I know I could use a css background-image but the client wants an image there.
Add a class to your CSS that removes the margin to make full-width image within the column and then add that class after the column.
.bosom-none {
margin-right: -15px; // Removes the right gap
margin-left: -15px; // Removes the left gap
}
<div class="container">
<div class="row">
<div class="colum-md-6 bosom-none">
<img src="your-image.jpg">
</div>
</div>
</div>
You probably have something else messing it up because as you can see, a container, row and then a img gives you no margins.
<div class="container" style="background:green">
<div class="row">
<img src="http://www.placehold.it/150x150">
</div>
</div>
http://jsfiddle.net/dBNwq/1
Here's the whole page
<!DOCTYPE html>
<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container" style="background:green;">
<div class="row">
<img src="http://www.placehold.it/960x150">
</div>
<div class="row" style="background:red;">
Some other content
</div>
</div>
</body>
</html>
This is a problem I've encountered many times with Bootstrap. You basically have three options:
If you're placing the image inside a column (e.g. .col-sm-12) which is inside a .row, you'll have to apply a negative margin to the image or a parent container equal to the column's padding: http://codepen.io/anon/pen/xEGkaQ
You could also instead make the image (or a wrapping div) a direct child of the .row but Bootstrap discouraged use to do this.
You can move the image outside the .col/.row all together, give it it's own .container and rid of that container's padding: http://codepen.io/anon/pen/WGvAap.
I usually go for option #1.
Please note that this does not answer your question. It meant to write this for future reference for other people encountering the same problem and do not know how to force this to work.
The actual answer to your question: no, Bootstrap does not offer such a modifier class.
Simply replace div class="container" with div class="container-fluid"
and that's it !
You can also create a class that will cover the container it is in...
.heroLg {
size:cover;
-webkit-size: cover;
-moz-size: cover;
-o-size: cover;
}
Then add the class to your image.
put the image into a span12
<div class="row">
<div class="span12">
<img src="image.jpg" />
</div>
</div>