In a bootstrap 3 project i've wanted some borders around cols so I made the following css Fiddle.
.admin-div{
background-color: #fff;
border-radius: 7px;
border: 1px solid #ccc;
padding:20px;
}
When I use it I place it inside a col like:
<div class="col-md-6 col-sm-8">
<div class="admin-div">
</div>
</div>
This works fine on big screens but on small screens there is no margin (note: the most left doesnt have the custom admin-div class and is a default list-group).
Large screen (nice margins beteween col's)
Small screen (no margin between col's)
One options is add
margin-bottom:20px
to all sm and sx elements but that sounds a bit overkill.
I could use
#media (max-width: 970px) { .spacing-div{ margin-bottom: 20px; } }
But than I have to add an extra class to all divs where I have a col.
So i have two solutions but they both feel like they could be replaced by something much more easy.
What is the best way to make
all col's have a margin-bottom :20px; even on small screens?
The extra admin-div is only to show the problem, without the extra div I would still have the problem but it wouldnt be visible that much.
If you want a margin on absolutely every column in Bootstrap you could use the following CSS selector which will add a margin to every element that contains col-.
[class*=col-] {
margin-bottom: 20px;
}
You could also use [class^=col-] in which the first class on an element needs to began with col-.
To extend this a bit further you could use a media query to have it only apply up to a certain screen size.
[class*=col-] {
margin-bottom: 20px;
}
#media ( min-width: 500px ) {
[class*=col-] {
margin-bottom: 0;
}
}
If you didn't want a margin on every single column then I would scope the above with a modifier selector.
<div class="row modifier">
<div class="col-6-md"></div>
<div class="col-6-md"></div>
</div>
/* fairly general - all columns in .modifier */
.modifier [class*=col-] {
margin-bottom: 20px;
}
/* a little more specific - only .col-md-6 columns in .modifier */
.modifier [class*=col-md-6] {
margin-bottom: 20px;
}
Related
I've been using bootstrap for quite a while now and I'm facing this problem for the first time. I really don't know how to do this. I have found many people suggesting to just remove padding-left on the first-child element and the last one. I also tried this way at first but then I realized that it couldn't work, since the .col class has the box-sizing: border-box; property which makes the div to have padding included in the width. (Which is obviously necessary if you want a clean layout using width: 25%;).
So, if you remove these padding on the left, the first and last div are going to be 15px larger, which breaks the layout... I want every col div to have exactly the same width, I want them to fit 100% of the row and have no padding left or right. Is there a class that I'm not aware of in bootstrap?
Is it possible while keeping the Bootstrap 3 templating system?
Code example:
<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>
I finaly found a way around it by creating my own class, that I believe respect the way bootstrap made their layouting system. Here was the best and minimal way to do it:
CSS
.layout-no-gutter-around.is-4-columns > .col-md-3{
margin: 0 5px;
}
.layout-no-gutter-around.is-4-columns > .col-md-3:first-child{
margin-left: 0;
padding-left: 0;
width: calc(25% - 15px);
}
.layout-no-gutter-around.is-4-columns > .col-md-3:last-child{
margin-right: 0;
padding-right: 0;
width: calc(25% - 15px);
}
HTML
<div class="row layout-no-gutter-around is-4-colums">
<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>
This way, I achieve exactly what I want, it's reusable and respect the Twitter's Bootstrap thinking (I believe). Thanks to Deja Vu who leaded me to the calc thiking, I believe it's a good way to achieve this. But you cannot put 15 margin left on the first child and right on the last child since that would still create a gutter around but using the margin.
I wasn't able to solve your problem but I have 2 ideas and maybe it will lead you to the solution.
Replace paddings with margins
html
<div class="row" id="optionOne">
<div class="col-md-3">first child</div>
<div class="col-md-3">child</div>
<div class="col-md-3">child</div>
<div class="col-md-3">last child</div>
</div>
css
#optionOne > div:first-child {
background-color: red; /* for display purposes only */
padding-left: 0px;
margin-left: 15px;
}
#optionOne > div:last-child {
background-color: yellow; /* for display purposes only */
padding-right: 0px;
margin-right: 15px;
}
Not sure if that would satisfy your design requirements.
recalc width
css
#media (min-width: 992px) {
#optionTwo > div:first-child {
background-color: green; /* for display purposes only */
padding-left: 0px;
}
#optionTwo > div:last-child {
background-color: grey; /* for display purposes only */
padding-right: 0px;
}
#optionTwo > div:not(:first-child):not(:last-child) {
background-color: blue; /* for display purposes only */
width: calc(25% + 15px);
}
}
The problem I faced was - in both cases last child falls onto the separate row:
fiddle.
Hope this will give you some food-for-thought.
I could not comment on Yann Chabot's post, but as an extension on it, I would recommend to use escape values. If you don't use this, Chrome recalculates the width to 10% instead of the correct width.
CSS
width: calc(~"25% - 15px");
Making 3 column responsive design using media queries is simple, but in case I want the middle column to stay in mid with 100% height and the right column goes on the left column it gets tricky.
The problem emerge because mid column height is variable, therefore it will be impossible to give negative margin-top to the transformed right column.
here is css code example:
/* 3 columns css */
.colLeft, .colRight, .colMid{
width: 280px;
float: left;
}
.colMid{
width: 500px;
}
.colRight{
width: 240px;
}
.container{
width: 1020px;
margin: 0 auto;
}
/* Media queries */
#media screen and (max-width: 1020px) {
.container {
width: 780px!important;
}
.rightCol{
width: 280px;
}
}
Html:
<div class="container">
<div class="leftCol">
</div>
<div class="midCol">
</div>
<div class="rightCol">
</div>
</div>
this code works fine, But mid column height push the transformed right column down.
My question: Is there HTML/CSS solution for this issue? or do I have to change HTML rendering when screen re-size?
Thanks,
Looks like your float: left is being applied to your .colRight class. This could be what is causing your right column to display on your left column.
I found the solution, using only CSS/HTML. Here is the explanation:
I created a duplicate rightCol div and placed it inside leftCol div, and named it righCol-left.
Initially the rightCol-left is set to "display:none"
when media screen is lower than 1020px, hide rightCol (display:none), and display rightCol-left (display:block)
here is the CSS:
.rightCol-left{display:none;} /*duplicate div*/
#media screen and (max-width: 1020px) {
.container {
width:780px!important;
}
.rightCol{
display:none;
}
.rightCol-left{
display:block;
width:280px;
}
}
here is the HTML:
<div class="leftCol">
/*Left column content*/
<div class="rightCol-left"> /* will be displayed when screen is below 1020px */
</div>
</div>
<div class="midCol">
/* mid column content */
</div>
<div class="rightCol"> /* gets hidden when screen is below 1020px */
</div>
This works perfect, the only bad thing would be the repeated HTML code. But this is not noticeable and very minor, after all, HTML code produce far less load to the webpage compared to JavaScript or anything else.
I have for divs:
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
<div id="4"></div>
On desktop, they are positioned like this:
1
2
3
4
On mobile, I would like them to be positioned like this:
1 3
2 4
if i do
div{
width: 50%;
float: left;
clear left;
}
#3,#4{
float: right;
clear: right;
}
They end up like this:
1
2 3
4
The divs are dynamically generated, so if I can avoid adding additional markup, that would be very good.
Demo Fiddle
Given the HTML
<div class='wrapper'>
<div id="1">1</div>
<div id="2">2</div>
<div id="3">3</div>
<div id="4">4</div>
</div>
You could use the CSS:
.wrapper > div {
border:1px solid; /* <-- not needed, used to show div locations */
height:100px; /* <-- not needed, used to show div locations */
}
#media screen and (max-width: 300px) { /* <-- apply styles on certian screen size */
.wrapper {
-webkit-column-count:2; /* <--put the divs in two columns */
-moz-column-count:2; /* <--put the divs in two columns */
column-count:2; /* <--put the divs in two columns */
}
}
Changing the 300px for whatever you deem a mobile device screen to be
I know this has been answered, but I thought I'd give you a different perspective and actually a more commonly used one as well. If you apply bootstrap framework (even if not to a global extend) but you can gut out the css the is valid to your problem, you will be able to do this a lot more comfortably. without getting into css specifity. All you have to do is make proper us of col-md-12 and col-xs-6
See DEMO
Another approach would simply use media queries to change the width of the divs. This would allow you to avoid adding more markup.
http://jsfiddle.net/14ecjy7n/1/
div {
box-sizing: border-box;
float: left;
}
#media all and (max-width: 639px) {
div {
width: 50%;
}
}
#media all and (min-width: 640px) {
div {
width: 100%;
}
}
I just started using Twitter Bootstrap, and I have a question about how to best add a border to a parent element?
for instance, if I have
<div class="main-area span12">
<div class="row">
<div class="span9">
//Maybe some description text
</div>
<div class="span3">
//Maybe a button or something
</div>
</div>
</div>
If I apply a border like so:
.main-area {
border: 1px solid #ccc;
}
The grid system will break and kick span3 down to the next row because of the added width of the border......Is there a good way to be able to add things like borders or padding to the parent <div>s like this?
If you look at Twitter's own container-app.html demo on GitHub, you'll get some ideas on using borders with their grid.
For example, here's the extracted part of the building blocks to their 940-pixel wide 16-column grid system:
.row {
zoom: 1;
margin-left: -20px;
}
.row > [class*="span"] {
display: inline;
float: left;
margin-left: 20px;
}
.span4 {
width: 220px;
}
To allow for borders on specific elements, they added embedded CSS to the page that reduces matching classes by enough amount to account for the border(s).
For example, to allow for the left border on the sidebar, they added this CSS in the <head> after the the main <link href="../bootstrap.css" rel="stylesheet">.
.content .span4 {
margin-left: 0;
padding-left: 19px;
border-left: 1px solid #eee;
}
You'll see they've reduced padding-left by 1px to allow for the addition of the new left border. Since this rule appears later in the source order, it overrides any previous or external declarations.
I'd argue this isn't exactly the most robust or elegant approach, but it illustrates the most basic example.
Another solution I ran across tonight, which worked for my needs, was to add box-sizing attributes:
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
These attributes force the border to be part of the box model's width and height and correct the issue as well.
According to caniuse.com ยป box-sizing, box-sizing is supported in IE8+.
If you're using LESS or Sass there is a Bootstrap mixin for this.
LESS:
.box-sizing(border-box);
Sass:
#include box-sizing(border-box);
I have a containing div that is NOT restricting the width of its child divs. The divs are stretching all the way to the full width of the screen, when i have a set width on both the container and the child. Why is this happening. I do NOT have any positioning or floating going on.
Please view my HTML:
<ul class="tabs_commentArea">
<li class="">Starstream</li>
<li class="">Comments</li>
</ul>
<div id="paneWrap">
<div class="panes_comments">
<div class="comments">member pane 1</div>
<div class="comments">member pane 2</div>
<div class="comments">member pane 3</div>
</div>
My CSS, the relevant parts of it at least:
#MembersColumnContainer {
width: 590px;
float: left;
padding-right: 0px;
clear: none;
padding-bottom: 20px;
padding-left: 2px;
}
ul.tabs_commentArea {
list-style:none;
margin-top: 2px !important;
padding:0;
border-bottom:0px solid #666;
height:30px;
}
ul.tabs_commentArea li {
text-indent:0;
margin: !important;
list-style-image:none !important;
padding-top: 0;
padding-right: 0;
padding-bottom: 0;
padding-left: 0;
float: right;
}
#paneWrap {
border: solid 3px #000000;
}
.panes_comments div {
display: ;
padding: px px;
/*border:medium solid #000000;*/
height:150px;
width: 588px;
background-color: #FFFF99;
}
You could set max-width on either, or both, of the div elements to prevent their expansion:
#containerDiv {
min-width: 400px; /* prevents the div being squashed by an 'extreme' page-resize */
width: 50%; /* defines the normal width of the div */
max-width: 700px; /* prevents the div expanding beyond 700px */
}
It might also be that you're allowing the div's overflowed contents to be visible, as opposed to hidden (or auto). But without specific examples of your mark-up and css it's very difficult to guess.
Generally giving elements layout is pretty straight forward (always assuming you have a good understanding of floating, positioning and the box model), and in most cases you wouldn't have to use max- min-width to control elements on the page.
My two cents: If I was you, I'd start stripping out code (starting with the !important rule), and see when the problem is solved. De-constructing the code like that is a good way to find bugs.
Sorry I couldn't help, but I'm reluctant to give advice since the code you provided shows a lot of other stuff going on elsewhere that might be contributing to your problem (like having to use !important).
:D
I figured out the problem. The file that was calling in the css was conflicting with another external css file that had the same element with the same name in it. Thank you all for your help though.