Bootstrap, making responsive changes to layout - css

I'm using a fluid Twitter Bootstrap layout for my design and am about to make it responsive. Consider a grid such as this:
<div class="row-fluid">
<div class="span4"></div>
<div class="span8"></div>
</div>
What is the best way to hide span4 and let span8 take up the entire width, to be used when the screen gets smaller?

With bootstrap 2.0.2 and up you can:
Change the html to:
<div class="row-fluid">
<div class="span4 hidden-phone hidden-tablet"></div>
<div class="span8 span12-tablet"></div>
</div>
(I interpreted 'smaller' with tablet and phone sizes, use your own definitions for other sizes)
.hidden-phone and .hidden-tablet hide the span4 for smaller screens.
To reclaim that space and re-span the span8, add this to your css:
#media (max-width: 979px) {
.span12-tablet {
width: 91.48936170212765% !important;
*width: 91.43617021276594% !important;
}
}
If you happen to be using less you can use bootstrap's grid mixins:
.span12-tablet {
#media (max-width: 979px) {
#grid > .fluid > .span(12) !important;
}
}

Using a media query with whatever min/max width set .span4 to display: none;
Then, add .span8 to the rule for .span12 for everything below whatever width you hide .span4 as all that work is already done for you by bootstrap, so no need to duplicate. It will look something like this:
#media (min-width: 320px){
.span12,
.span8 {
width: 300px;
}
}
(That last bit of code is just an example, but there will be something like it in bootstraps scaffolding.)
Hope that helps :)
EDIT:
This could work, I tested it using dev tools on the bootstrap site and it seemed to work. Again, in a media query:
#media (min-width: 320px){
#special .span4 {
display: none;
}
#special .span8 {
float: none;
width: auto;
}
}

If using bootstrap 2.2.1 you can:
Change the html to:
<div class="row-fluid">
<div class="span4 hidden-phone hidden-tablet"></div>
<div class="span8"></div>
</div>
Now add this to your css overrides:
#media (min-width: 768px) and (max-width: 979px)
{
[class*="span"],
.row-fluid [class*="span"] {
display: block;
float: none;
width: 100%;
margin-left: 0;
}
}
This will also work for any other span widths you have specified in your html.
the effect of these changes makes all span widths 100% causing the iPad to always use 1 column fluid mode in portrait mode.

This would be the best option to keep it dynamic. In my example I have width set to 6 columns next to fluidGridColumnWidth
[class*="span"] {
width: 100%;
.row-fluid {
[class*="span"] {
width: (#fluidGridColumnWidth * 6) + (#fluidGridGutterWidth * (6 - 1)) - (.5 / #gridRowWidth * 100 * 1%);
float: left;
margin-left: #fluidGridGutterWidth;
&:first-child {
margin-left: 0;
}
}
}
}

Write Like this
in phone device this div will hide<div class="span4 hidden-phone"></div>
and this div will show <div class="span8 visible-phone"></div>
Update
Previous Answer for Bootstrap 2.3
Now bootstrap 3 come in market..
so i update my answer for new user → bootstrap3
in phone device this div will hide<div class="col-md-4 hidden-xs"></div>
and this div will show <div class="col-xs-4 visible-xs"></div>

TLDR: Use the 2nd code snippet
Bootstrap is a mobile first framework so I'll explain from the smallest screen-size up. The layout is always 12 columns wide regardless of breakpoints/screen-size.
Starting from the smallest breakpoint (xs - extra small), the span4 is hidden and the span8 takes all of the width (all 12 columns)
<div class="row-fluid">
<div class="span4 hidden-xs"></div>
<div class="span8 col-xs-12"></div>
</div>
We are not quite done yet as we haven't defined behavior when the next breakpoint up is hit (sm/small/screen width is over 767px), so we'll make span4 take a third of the width (12 columns/3 = 4 columns) and the span8 will take the rest of the width (12-4= 8 columns)
<div class="row-fluid">
<div class="span4 hidden-xs col-sm-4"></div>
<div class="span8 col-xs-12 col-sm-8"></div>
</div>
The above assumes you wanted the change to happen on the change between the xs - sm breakpoints.
Further reading:
If you wanted the change between sm-md (md = medium) then I might use the visible-md class which will show the span4 on breakpoints medium and up (>992px)
<div class="row-fluid">
<div class="span4 visible-md col-md-4"></div>
<div class="span8 col-xs-12 col-md-8"></div>
</div>

I came up with a small variation of that.
Add stack-tablet class to a row-fluid to make the spans stack on tablet width, not only on phone width (bootstrap default):
#media (max-width: 979px) {
.row-fluid.stack-tablet [class*="span"] {
width: 100%;
display: block;
float: none;
margin-left: 0;
}
}
Can be used together with the display- and hidden- classes.

just:
<div class="row-fluid">
<div class="span4 hidden-desktop"></div>
<div class="span8"></div>
</div>

Related

How to remove padding from rows on mobile devices in bootstrap 4.5?

I'm using bootstrap 4.5 in my project, i would use an edge to edge view when the website is opened on a mobile device.
I have a layout as the following
<div class="container">
<div class="row">
<div class="col-md-9>...</div>
<div class="col-md-3>...</div>
</div>
So when it's on a desktop it looks fine but on mobile there is too many padding and i would make the content looks like edge to edge
i've tryed to use no-gutters on row and to remove padding from container but it makes the desktop version look bad..
<div class="container mx-0 mx-sm-3">
<div class="row">
<div class="col-md-9">...</div>
<div class="col-md-3">...</div>
</div>
</div>
mx-0 sets the container margin to 0 on the x-axis and then mx-sm-3 sets it back to 3 (1rem) for small and up (sm, md, lg, and xl)
Spacing in Bootstrap
I've solved the problem by writing my own media-queryes as the following:
#media screen and (min-width: 600px) { // setting default row and cols values for non mobile screens
.row {
margin-left: -15px;
margin-right: -15px;
}
.row [class*='col']{
padding-left: 15px;
padding-right: 15px;
}
}
.row { // setting rows margin and cols padding to 0 for mobile screens
margin-left: 0;
margin-right: 0;
}
.row [class*='col']{
padding-left: 0;
padding-right: 0;
}
And by adding this to container class class="container px-0 px-sm-3"
Here is the result:
You can either write your own media-query for this or use the spacing classes provided by bootstrap https://getbootstrap.com/docs/4.0/utilities/spacing/
class="p-0" == padding: 0 !important;
You can add in your html a class named p-sm-0 and p-0
or you can add a media query just for mobile and add to container class in css padding: 0

ASP.NET:How to make webform mobile responsive?

I have ASP.NET project.I want it to change its view automatically when device is detected as mobile or as desktop.what are the ways available?please suggest some example projects.And i tried studying many tutos.But still i am unable to grasp them.So can any body suggest step by step method to do so?
For making your webpage responsive as per device
--> first you need to have bootstrap in your project.
--> you will be having container in your bootstap.css file(assets-->css-->bootstap.css).
--> container will be having different kind of screen types declared by default.like
EXAMPLE:
#media (min-width: 768px) {
.container {
width: 750px;
}
}
#media (min-width: 992px) {
.container {
width: 970px;
}
}
#media (min-width: 1200px) {
.container {
width: 1170px;
}
}
.container-fluid {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
--> you have to use these container in your cs file.In asp.net it is .aspx extention file.for
EXAMPLE:
<div class="below-slideshow" style="padding-bottom: 0px">
<div class="container"> <!-- NOTICE HERE:inside div declare class as container/container-fluid-->
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div>
<h1 class="head-line">Dashboard</h1>
</div>
</div>
</div>
</div>
</div>
--> To know more about container and container fluid http://www.w3schools.com/bootstrap/bootstrap_get_started.asp
HAPPY CODING :-)

How to change the ordering of two divs when on mobile?

I am trying to change the order of two divs in a container when they get to mobile size.
<div class="container">
<div class="div1"></div>
<div class="div2"></div>
</div>
Is this possible to change it so that it will change to
<div class="container">
<div class="div2"></div>
<div class="div1"></div>
</div>
Depending on which browsers you need to support, you can use flexbox to switch the order of the divs.
.container {
display: flex;
}
.div1 {
order: 0;
}
#media screen and (min-width: 780px) {
.div1 {
order: 1;
}
}
JS Fiddle
You can do it using flexbox by reversing the order.
.container {
display: flex; /* or inline-flex */
flex-direction: column;
}
#media(min-width:768px){
.container {
flex-direction: column-reverse;
}
}
http://codepen.io/partypete25/pen/oxJNqz
You can use pure css to do that
.container{
display:table;
}
.div1{
display:table-footer-group;
}
.div2{
display:table-header-group;
}
make sure you put it in a #media query.
You can use two separate classes and make only one visible at a time depending on the media width.
First create you CSS
//This container is only visible on devices with 1025px or more such as iPad in landscape or portrait view
#media screen and (min-width: 1025px) {
.desktop-container {
Display:none;
}
}
// This container is only visible on smaller devices with screen size 360px - 1024px
#media screen and (min-width:360px) and (max-width:1024px) {
.mobile-container {
Display:none;
}
}
Now create your markup...
<div class="desktop-container">
<div class="div1"></div>
<div class="div2"></div>
</div>
<div class="mobile-container">
<div class="div2"></div>
<div class="div1"></div>
</div>

Even spaces between bootstrap columns

Using this markup...
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<div style="background-color:gray">1</div>
</div>
<div class="col-md-6">
<div style="background-color:gray">2</div>
</div>
<div class="col-md-3">
<div style="background-color:gray">3</div>
</div>
</div>
</div>
Here's a jsfiddle (run it in full screen):
http://jsfiddle.net/MojoDK/swKyX/
Here's a screenshot:
The spacing between the divs at the red arrows are double of the space of the divs at the orange arrows.
How can I make the div space at the red arrow the same space (10px) as at the orange arrows and still maintain aligned divs/blocks when the three divs wraps as the browser window becomes smaller?
.row > div:not(:first-child):not(:last-child) {
padding: 0;
}
#media screen and (max-width: 768px) {
.row > div:not(:first-child):not(:last-child) {
padding: 0 15px;
}
}
This will work fine in a three columned layout.
A few ways to do this, but here's one:
#media (min-width: 992px){
.container-fluid{
margin-left:15px;
margin-right:15px;
}
}
Here is a fiddle (note you can import the bootstrap CSS for future reference):
http://jsfiddle.net/swKyX/5/

Fixed sidebar navigation in fluid twitter bootstrap 2.0

Is it possible to make sidebar navigation stay always fixed on scroll in fluid layout?
Note: There is a bootstrap jQuery plugin that does this and so much more that was introduced a few versions after this answer was written (almost two years ago) called Affix. This answer only applies if you are using Bootstrap 2.0.4 or lower.
Yes, simply create a new fixed class for your sidebar and add an offset class to your content div to make up for the left margin, like so:
CSS
.sidebar-nav-fixed {
padding: 9px 0;
position:fixed;
left:20px;
top:60px;
width:250px;
}
.row-fluid > .span-fixed-sidebar {
margin-left: 290px;
}
Demo: http://jsfiddle.net/U8HGz/1/show/
Edit here: http://jsfiddle.net/U8HGz/1/
Update
Fixed my demo to support the responsive bootstrap sheet, now it flows with the responsive feature of the bootstrap.
Note: This demo flows with the top fixed navbar, so both elements become position:static upon screen resize, i placed another demo below that maintains the fixed sidebar until the screen drops for mobile view.
CSS
.sidebar-nav-fixed {
position:fixed;
top:60px;
width:21.97%;
}
#media (max-width: 767px) {
.sidebar-nav-fixed {
width:auto;
}
}
#media (max-width: 979px) {
.sidebar-nav-fixed {
position:static;
width: auto;
}
}
HTML
<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<div class="well sidebar-nav sidebar-nav-fixed">
...
</div><!--/.well -->
</div><!--/span-->
<div class="span9">
...
</div><!--/span-->
</div><!--/row-->
</div><!--/.fluid-container-->
Demo, edit here.
minor note: there is about a 10px/1% difference on the width of the fixed sidebar, its due to the fact that since it doesn't inherit the width from the span3 container div because it is fixed i had to come up with a width. It's close enough.
And here is another method if you want to keep the sidebar fixed until the grid drops for small screen/mobile view.
CSS
.sidebar-nav-fixed {
position:fixed;
top:60px;
width:21.97%;
}
#media (max-width: 767px) {
.sidebar-nav-fixed {
position:static;
width:auto;
}
}
#media (max-width: 979px) {
.sidebar-nav-fixed {
top:70px;
}
}
Demo, edit here.
The latest Boostrap (2.1.0) has a new JS "affix" feature specifically for this type of application, FYI.
this will screw up the responsive Webdesign.
Better wrap the fixed sidebar in a media query.
CSS
#media (min-width: 768px) {
.sb-fixed{
position: fixed;
}
}
HTML
<div class="span3 sb-fixed">
<div class="well sidebar-nav">
<!-- Sidebar Contents -->
</div>
</div>
Now the sidebar is only fixed, if the viewpot is bigger then 768px.
This isn't possible without javascript. I find affix.js too complex, so I rather use:
stickyfloat
I started with Andres' answers and ended up getting a sticky sidebar like this:
HTML:
<div class="span3 sidebar-width">
<div class="well sidebar-nav-fixed">
Sidebar
</div>
</div>
<div class="span9 span-fixed-sidebar">
Content
</div> <!-- /span -->
CSS:
.sidebar-nav-fixed {
position:fixed;
}
JS/jQuery:
sidebarwidth = $(".sidebar-width").css('width');
$('.sidebar-nav-fixed').css('width', sidebarwidth);
contentmargin = parseInt(sidebarwidth) + 60;
$('.span-fixed-sidebar').css('marginLeft', contentmargin);
I'm assuming I also need JS to update the 'sidebarwidth' variable when the viewport is resized.
Very easy to get fix nav or everything tag you want. All you need is to write your fix tag like this, and put it in your body section
<div style="position: fixed">
test - try scroll again.
</div>
With the current Bootstrap version (3.3.2) there is a nice way to achieve a fixed sidebar for navigation.
This solution also works well with the re-introduced container-fluid class, meaning it is easily possible to have a responsive full-screen layout.
Normally you would need to use fixed widths and margins or the navigation would overlap the content, but with the help of the empty placeholder column the content is always positioned in the right place.
The below setup wraps the content around when you resize the window to less than 768px and releases the fixed navigation.
See http://www.bootply.com/ePvnTy1VII for a working example.
CSS
#media (min-width: 767px) {
#navigation{
position: fixed;
}
}
HTML
<div class="container-fluid">
<div class="row">
<div id="navigation" class="col-lg-2 col-md-3 col-sm-3">
<ul>
<li>Link 1</li>
<li>Link 1</li>
<li>Link 1</li>
</ul>
</div>
<div class="col-lg-2 col-md-3 col-sm-3 hidden-xs">
<!-- Placeholder - keep empty -->
</div>
<div id="main" class="col-lg-10 col-md-9 col-sm-9 fill">
...
Huge Content
...
</div>
</div>
</div>

Resources