bootstrap left side bar - html

I have created a left side bar. Is it possiable to make the side bar fixed on the screen. Whenever i make the screen smaller, the left side bar disappears.
<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar">
<li class="active">Categories <span class="sr-only">(current)</span></li>
This is my code for the side bar. it gets the categories name using php from mySql.

You'll need to use media queries to avoid the sidebar go all weird and stuff in smaller screens. For example let's say that we have this:
<div class="container">
<div class="leftSidebar">Left Sidebar</div>
<div class="mainContent">Main Content</div>
<div class="clear"></div>
</div>
I can give my mainContent class a float:right; to be floating on the right side, and then a float:left; to the leftSidebar to be floating on the left side. But we want the sidebar to be fixed so, float:left; doesn't really do nothing here anymore as soon as we say the leftSidebar to be position:fixed;. So here's how we gonna style these both divs:
.leftSidebar {
width: 100px;
height: 300px;
/* Making sidebar fixed */
position:fixed;
}
.mainContent {
width: 400px;
height: 300px;
float: right;
}
Allright!!! We have now a fixed sidebar and a div with the main content align to the right.
What about now? I don't want the sidebar acting all weird and being above the main content when I resize the window or visit the website in smaller screens!
Well this is where the media queries comes in. With media queries we can tell the sidebar to not be fixed anymore
/* Media query to create a fix for smaller screens */
#media (max-width: 450px){
.leftSidebar {
width: 80%; /* Meet your new width Sidebar! */
margin: 0 auto;
position:inherit; /* No more Fixed position for you sidebar!! */
}
.mainContent {
width: 80%; /* Meet your new width mainContent! */
margin: 0 auto;
float:none; /* What are you doing float right? I don't want .mainContent to float right anymore! Now it's float:none; !! */
}
}
An here's the final result and online example
At last we can change the order of the sidebar, either if we want it to appear first than the .mainContent or after .mainContent. For the sidebar appear first:
<div class="leftSidebar">Left Sidebar</div>
<div class="mainContent">Main Content</div>
For the sidebar appear after the .mainContent, just reverse order of the classes:
<div class="mainContent">Main Content</div>
<div class="leftSidebar">Left Sidebar</div>

If I got you right, the sidebar is always visible? If so, add it to your whole grid system.
<div class="container-fluid" id="page">
<div class="row">
<div class="col-xs-9" id="content">MAIN CONTENT</div>
<div class="col-xs-3" id="sidebar">SIDEBAR</div>
</div>
</div>
Based on bootstrap documentation, col-xs is the lowest col size and should also be defined. I've assumed, that you have a 12 column grid. Else you have to change the column settings.

Sidebar doesn't disappear. It falls underneath main content which is good for smaller screen sizes. If you don't need responsive layout you can find more information at bootstrap documentation page.

Related

CSS/Bootstrap: fixed max-width for content layout

I have noticed, that many websites (SO included) don't shrink to the whole width of the screen, preferring to render content column either of fixed-width or setting max-width property for it. Merriam-Webster dictionary website is a good example for the latter.
Is it possible to create such a layout using Bootstap? I have managed to limit content column width inside it's col-8-md div, but there is a huge gap between content and right sidebar on big displays now.
Live demo: https://codepen.io/anon/pen/dNprzm
HTML:
<div class="row">
<div class="col-md-8">
<div class="content-block">
CONTENT
</div>
</div>
<div class="col-md-4 right-bar">
RIGHT_BAR
</div>
</div>
CSS:
.content-block {
height: 1000px;
max-width: 1000px;
background-color: lightgreen;
margin-left: auto;
margin-right: auto;
}
.right-bar {
background-color: pink;
width: 400px;
}
If I'm understanding your question correctly, you just want to be sure to have a fixed width for your content but get rid of the space that's happening to the right of it on large screens?
Remove your margin-right: auto;. Once you get to a screen size where it's larger than 1000px, it's trying to "center" your .content-block

Fixed size right panel, fluid left and semantic element order

On a portion of a web site I have a container which contains two blocks. One panel that has a static width and a left panel that should fill the space to the left of the right panel. I have solved it today by having the following markup:
<div class="container">
<div class="rightpanel"></div>
<div class="leftpanel"></div>
</div>
.leftpanel {
overflow:hidden;
}
.rightpanel{
width: 200px;
float: right;
}
The problem here is that I need to declare the rightpanel before the leftpanel. I would like to be able to declare it like this:
<div class="container">
<div class="leftpanel"></div>
<div class="rightpanel"></div>
</div>
Any suggestions on what I should do? Help gladly appreciated.
Simply change the element being floated, then set the width of the flexibly sized element to that of the page minus the known width of the sidebar, by using calc
Demo Fiddle
Change your CSS to:
.leftpanel {
float:left;
width:calc(100% - 200px)
}
.rightpanel {
width: 200px;
overflow:hidden;
}

How Can I Make The HTML Page More Narrow?

I want to change the width of the area that i present things (the body), i add picture for better understanding of what i'm looking for:
(the yellow mark is the width that i'm looking for to my HTML body, How can i do it ?
I'm using MVC 4 w/ bootstrap.
Additional info: I have a navbar that require from me to add style configures in the '_layout' in order to display the page in a proper way and not to allow the navbar to be over the body content, here is it:
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
.aligntobottom {
bottom: 0;
position: absolute;
}
</style>
What i need to add to the style markup in order to get what i want?
Update This is how i render the body (in the '_layout'):
<div class="container">
#if (IsSectionDefined("featured"))
{
<div class="hero-unit">
#RenderSection("featured", required: false)
</div>
}
#RenderBody()
</div> <!-- /container -->
So its inside a container at the first place.(i render the body into the container)
update
**I notice that the problem is because the '#media' :
But now i see that i have a couple of #media section in my CSS:
#media (min-width: 768px) {
.container {
width: 550px;
/*width: 750px;*/
/*MAYBE THIS*/
}
#media (min-width: 992px) {
.container {
width: 550px;
/*width: 970px;*/
/*MAYBE THIS*/
}
#media (min-width: 1200px) {
.container {
/*width: 1170px;*/
width: 550px;
/*MAYBE THIS*/
}
what i need to change in order to keep the suitability for all type of browser and screens ??
I change all of them(1200px, 992px, 768px) like the code i post, how can i know which one i'm currently using ? hence what my media width?
If I read your post correctly, I think you're trying to make the content narrower keeping the nav bar to the full width.... then I suggest changing the following
<div id="navbar">
code of the navigation bar
</div>
<div id="content" >
content
</div>
and then change the stile to look like this
#navbar{
width:100%;
/*other styles*/
}
#content{
width:920px;/*change the value to suite ur need*/
margin:0 auto;/*this will center ur content*/
}
If you want to make the nav bar the same width as the content then add the #navbar div inside the content div as follows:
<div id="content">
<div id="navbar">
code of the navigation bar
</div>
content
</div>
create a new div, just after the body tag, put everything inside it, give width (eg 1200px) and margin:0 auto; to keep it at the center of the screen.
if you are using bootstrap, put everything inside of "container".
There's no need to modify the CSS, jsut put the content of your page inside a
<div class="container">
<!-- More HTML here -->
</div>
If you want to make is narrower you could use:
<div class="container">
<div class="row">
<div class="col-lg-offset-1 col-lg-10">
</div>
</div>
</div>

Moving up the div at window resize without using absolute

Lets say I have a structure like this.
<div class="container>
<div class="left-col>
some content
</div>
<div class="right column>
<div class="login-form">Login Form</div>
</div>
</div>
Above is the structure of my webpage at 700px width and above.. What I wanted to do is whenever the browser width decreases to 480px and below.. the div class login-form would be at the top and then below of it is my left-col.. is there any way for this? I would love to do it without using the position: absolute
if browser width is 480px and below structure would be like this
<div class="container>
<div class="right column>
<div class="login-form">Login Form</div>
</div>
<div class="left-col>
some content
</div>
</div>
Does your HTML have to be like that or can you swap it around? Meaning the html looks different, but you still have the login form on the right on big screens. It's much simpler if the HTML is:
<div class="container">
<div class="right-col">
<div class="login-form">Login Form</div>
</div>
<div class="left-col">
Some Content
</div>
</div>
Then the CSS:
.left-col {
background: green;
}
.right-col {
background: red;
}
#media (min-width: 481px) {
.container {
overflow: hidden;
}
.left-col {
overflow: hidden;
}
.right-col {
float: right;
margin-left: 10px;
}
}
Working Fiddle: http://jsfiddle.net/jhummel/4aNjd/
There is a way to do this and it is called responsive web design. You would use media queries that will tell the page how to behave at certain sizes.
Lucky for you there are more functions to the position attribute than position:absolute;.In this case, I would use position:relative; and move "left-col" down.
Here is a great article to get you started with Responsive web design. CLICK HERE
Do those divs show up left and right when the browser is 700px? If so...
Just have the "right column" div before the "left-col" in your html. In the css use floats so "right-column" shows up on the right and "left-col" still shows up on the left. I am assuming their widths fit in the container. Then, within your media query give "right column" float:none and width:100% so that when the browser is less than 480px the form div will show up on top.
note: I think you meant "right-column" and not "right column".
<style>
.left-col{
float:left;
}
.right-column{
float:right;
}
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
.left-col{float:none; width:100%;}
.right-column{float:none;}
}
</style>
<div class="container>
<div class="right-column>
<div class="login-form">Login Form</div>
</div>
<div class="left-col>
some content
</div>
</div>

Centering a span12 div in a container in Bootstrap

I have been through a number of similar questions, and tried to adapt the solutions to my case, but haven't had success in doing so.
I am trying to implement something of a reader, so I have a reading pane which I want to center on my page. I want to limit the size of the pane so that the user is no reading lines spanning the full width of a large browser window, but I also want to have that pane centered in the window. Above the pane I have a header which spans the full width of the page.
Originally I tried to use "span8 offset2" for the reading pane, but as the size of the window is reduced, I want the margins to disappear before the pane shrinks, and using this setup, the reading pane shrinks unnecessarily, squeezing content, as the window is made thinner.
I get the correct behavior just using "span12" with "max-width: 700px" set, in terms of the reading pane shrinking as I want it to, but I cannot get the div to center on the page.
Here is what I have that I'm working with:
<body>
<div class="container">
<div class="row-fluid">
<div class="span12 reading-pane">
<div class="row-fluid">
<div class="nav">
<div class="span6 offset3">
Main Navigation
</div>
<div class="span2 offset1">
Nav2
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div class="body-text">
Text Area
</div>
</div>
</div>
</div>
</div>
</div>
</body>
The style for the reading-pane is as follows:
.reading-pane {
border: solid;
border-color: #ccc;
border-width: 3px;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
min-height: 40px;
line-height: 40px;
max-width: 700px;
}
I have tried adding the following to the .reading-pane style (individually):
float: none;
margin-left: auto;
margin-right: auto;
 
margin: 0 auto;
 
float: none;
margin: 0 auto;
I've also tried centering text in the container which centers my header text, but not the reading-pane.
So how do I get the span12 div to center on the page?
I'm assuming since you're using row-fluid that you're using bootstrap 2.0. Bootstrap 3.0 handles responsive grids a bit more cleanly, so if you can I'd recommend using 3.0.
Then move your max-width to the container:
.container {
max-width: 700px;
}
Note that 700 includes the gutters so you may want to use 730.
Or better than using max-width, you can customize (http://getbootstrap.com/customize/) your twitter bootstrap download and define your own widths there if 700 is critical to you. And you can then also remove the larger #media queries then.
There's a few other tweaks to how grids are done on 3.0, which I included in this fiddle: http://jsfiddle.net/PQM34/2/
Hard to gauge without an example of your code and Bootstrap's source...
Note, it sounds like your using the framework incorrectly though. Why not just use span10, span8, etc. and center that?
In order to center divs, using margin:0 auto a fixed width is required (%, px, em, etc.).
Try adding this css to .reading-pane:
position:relative;
margin: 0 auto;
width: 700px;
float:none!important;