Looking for a responsive layout using CSS only that can behave like the images shown. There are 5 basic areas of the site. Header, Lefthand nav, Content, a sidebar and a footer. So far i have everything done besides the content/sidebar relationship. For the desktop site the green sidebar floats inside the content area and to the right and when the sidebar ends the content wraps around it. On the mobile site the sidebar moves below the content. The issues i am having are getting this to happen. I can get it to float left without the wrap and then move below the content but getting the content to wrap the sidebar as well is proving troublesome. Any pointers?
what about this approach;
Please note that the following excerpt is not completed, but it gives you hopefully a good idea:
For the desktop-version:
In order to display the sidebar in the content section properly, the best you could do is to absolute-position the sidebar-section. Like this:
#content {
position: relative;
}
#sidebar {
position: absolute;
right: 0px;
top: 0px;
}
Above code requires that "sidebar" is wrapped by "content" which will cause problems with the following piece of code:
Smartphone / tablet support: To stack all sections nicely on a smartphone:
#media screen and (min-width: 0px) and (max-width: 900px) {
#navigation, #content, #sidebar, #footer {
width: 100%;
float: left;
clear: both;
border-top: 1px solid #ccc;
margin-top: 20px;
background: none;
position: relative;
}
}
Now, our final problem is the fact that "sidebar" is still wrapped by "content".
Couple of approaches to solve this issue;
Use JavaScript to move the #sidebar to and from another block based on the screen width.
What about introducing a second sidebar-section such as #sidebar-mobile which is only visible when you are on a mobile device. Obviously, in that case, the first sidebar will be invisible.
This is your code for content area.
<div id="content">
<div id="sidebar">
sidebar
</div>
content
</div>
<div id="footer">footer</div>
Here is the css for desktop and device as well.
#content {background:yellow; padding:20px;}
#content p{ margin:0;}
#sidebar {background:green; float:right; padding:10px; margin:0 0 10px 10px;}
#footer{background:#2AABE4;}
#media only screen and (max-width: 420px) {
#sidebar{ float:none; margin:0;}
}
Now we have to use the little bit part of jquery to place the side between #content and #footer in device. Here the code for jquery.
function setlayout(){
//alert($(window).width())
if( $(window).width() < 420 ){
$('#sidebar').insertBefore('#footer');
} else {
$('#sidebar').prependTo('#content');
};
};
$(window).on('resize load', function(){ setlayout() });
Above code will work on window resize and window load as well.
When you will resize you browser below 420 it will move the div. You can modify the width as per your requirement.
For working example visit below link. http://jsfiddle.net/rakeshpersonal/hknBb/
You can (ab)use CSS display:table to effectively rearrange divs using just CSS without any Javascript.
Use your initial formatting for the "desktop" view.
In the mobile view (which you can activate by a CSS3 media query), apply display: table to a wrapper around everything else on the page (or directly to the body tag). Then, use display: table-footer-group to push the sidebar and footer to the bottom, and use float: none to remove their float properties.
The following is based under the presumption that your html is like this.
<div id="content">
<div id="sidebar">
</div>
</div>
For desktop:
#sidebar {
position: absolute;
right: 0;
top: 0;
}
For mobile:
#sidebar {
position: relative;
margin-top: 16px;
}
you probably need to have position relative for content, for it to work, but if not there is likely other solutions
#content {
position: relative;
}
Related
We have a sticky side panel on our page implemented with the following very-simple CSS:
position: fixed;
top:62px;
bottom:10px;
Where the top and bottom properties create the desired margins.
The problem is that this panel contains several accordion-style elements, and expanding some of them causes the content to overflow past the bottom of the screen and become invisible/inaccessible. Adding an overflow:auto; rule to the above css style almost solves the problem, by inserting a scrollbar that allows the user to scroll vertically to see the would-be hidden content. However, this results in two scrollbars - one for the main nav and one for the sidebar - which feels clunky an unintuitive. Instead, I'd like to have the "fixed" element scroll with the main scrollbar when it overflows. I'm aware that this would essentially make it not a fixed element, and thus am afraid I'll have to resort to JS to make this happen - but does anyone have a cleaner, html/css-only way of handling this?
I'm not sure this is what you need, but hope it helps some way.
#container1 {
height: 400px;
width: 200px;
overflow: hidden;
position: fixed;
top: 62px;
bottom: 10px;
background: #888;
}
#container2 {
width: 100%;
height: 99%;
overflow: auto;
padding-right: 20px; /*Adjust this for cross-browser compatibility */
}
#container2 ul li {
height: 300px;
}
html, body {
height: 99%;
overflow:hidden;
}
<div id="container1">
<div id="container2">
<ul>
<li>test1</li>
<li>test2</li>
<li>test3</li>
</ul>
</div>
<div>
JSFiddle
Also in chrome you can try out:
::-webkit-scrollbar {
display: none;
}
But this snippet works only in chrome, so I would rather use the above.
Let me try to help. Use Panel-body class selector to handle this.
First you should do many things, such as, width of the div and the second div.
You can manage to hide the scrollbar as follows:
.panel-body {
height:300px;
overflow:auto;
margin-right:0px; // when it shows scrollbar, you need to set it MINUS.
}
Second, you also take notice when browser window gets resized by user and you need to manage Media Queries in related to the div width.
This is the DEMO.
I'm trying to get a footer to flush to the bottom of the page, but not necessarily be sticky - just be at the bottom in case the user scrolls down there.
This "works" but there seems to be a bit of white space at the bottom after the footer appears which looks a little awkward. Does anyone know the best way with CSS to flush a footer to the bottom and keep it at the very bottom without making it sticky?
Let me know if you want me to post my html/css.
There are a number of good examples on the web of this.
Here is a supposedly updated version: http://mystrd.at/modern-clean-css-sticky-footer/ ; I have no experience with this one.
And this is the classic version that has been around for a long time and well used by many:
http://www.cssstickyfooter.com/html-code.html (waybackmachine archived)
Here's my own slightly edited version of the second link that I've had good luck with.
/* Sticky Footer Stuff
*/
html,body { height: 100%; }
#sticky-wrap { min-height: 100%; }
.footer {
height: 160px;
margin-top: -160px;
}
/* end sticky footer stuff
*/
<div class="wrapper" id="sticky-wrap">
<div class="content-area”>
</div>
</div>
<footer>
</footer>
I've had a similar issue.
I always wanted my footer to be at the bottom of the page, but never overlap the other div's.
The best solution I came up with was :
CSS
#footer {
position: absolute;
bottom: 0px;
left: 0px;
right: 0px;
}
#media screen and (max-height: 700px) {
#footer {
position: relative;
}
}
HTML
<div id="footer"></div>
This will keep the footer always on the bottom of min-height 701px, and below that it will be at the bottom relative to other divs. Of course you can change the parameters and set it to your own pixel measurements.
Hope this helps.
I'm trying to divide a page into 3 sections. A header, body and a footer. I found this blog post which comes close to what I need. But the header and footer with his approach are sticky'd to the window when you scroll up or down.
Further more I want an extra div inside the body section which is centred horizontally and vertically with a fixed width of 600px. And the height should be auto but not overlapping the header or footer. So a padding should be between those 2 sections as well.
I used his approach for the centring part which works pretty good. But I'm using Twitter Bootstrap 3 as well. And I never get the result I want.
Can someone help me out with at least the basics for this to work without TB3 so I can figure it out myself from that point to make it work with TB3?
Thanks in advance!
[EDIT]
To sum it all up what I want is:
A page divided into 3 sections where the footer and header do not stick to the window;
Centring a div inside the body section with a fixed width and a growing height that should not overlap the header or footer;
[EDIT 2]
Sorry, I forgot to mention that the header and footer have a fixed height of 65px.
Without knowing how high the inside div's height will be, and that the height will be variable, it's very difficult to create a one-size-fits-all solution, especially considering that different devices and computers have different screen sizes and resolutions. Added to this problem is that your header and footer may also be of variable height, so that also complicates things.
To get around these quirks, and have better control of your varying div height and where it sits on the page, I'd consider using JavaScript to control its position. This way, you can monitor when its height changes (depending on what you're planning on doing), and move it dynamically depending on its current height.
In the following example, I'm using jQuery to get the JavaScript done easily. You can download from their site here, and just include it in your code.
Here's a basic example (HTML first):
<div id="header"></div>
<div id="container"></div>
<div id="footer"></div>
Then some CSS:
html, body {
height: 100%;
min-height: 400px; /* to cater for no element 'collision'... */
margin: 0;
padding: 0;
position: relative; /* to keep header and footer positioned correctly... */
}
#header {
background-color: green;
height: 65px;
position: absolute;
top: 0;
left: 0;
width: 100%;
}
#container {
background-color: red;
width: 600px;
height: 40%;
position: absolute;
left: 50%;
margin-left: -300px; /* 50% over, then move back 300px to center... */
}
#footer {
background-color: orange;
height: 65px;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
}
And finally, your JavaScript. Put this in the <head>...</head> section of your HTML. I've tried to comment as much as possible, but please let me know if anything's unclear:
<script src="./path/to/jquery.js"></script>
<script>
// this will run this function as soon as the page is loaded...
$(function(){
// bind the resize event to the window...
// this will call the 'centerMyContent' function whenever the
// window is resized...
jQuery(window).bind('resize', centerMyContent);
// call the 'centerMyContent' function on the first page load...
centerMyContent();
});
// the actual centring function...
function centerMyContent(){
// get the body and container elements...
var container = jQuery('#container');
var body = jQuery('body');
// work out the top position for the container...
var top = (body.height() / 2) - (container.height() / 2);
// set the container's top position...
container.css('top', top + 'px');
}
</script>
I hope this helps get you on the right track! Successfully tested in Chrome, Firefox, and Internet Explorer 7, 8, 9 and 10.
Since you're using Twitter Bootstrap 3, you can start with the "Sticky footer navbar" template which is in the examples folder in the zip file you get from http://getbootstrap.com/ homepage.
Getting rid of the sticky navbar:
open the index.html and go to row 31, it has this:
<div class="navbar navbar-default navbar-fixed-top">
Just remove the navbar-fixed-top from the class attribute.
Making container 600px wide:
Create your own css file, include it in your HEAD part of the index.html and add there
#wrap > .container {
width: 600px;
}
You also need to add mediaqueries to overwrite the basic tb3 styles for smaller viewport-sizes and add that 600px there. So below that type:
#media (max-width: 1199px) {
#wrap > .container {
width: 600px;
}
}
#media (max-width: 991px) {
#wrap > .container {
width: 600px;
}
}
#media (max-width: 767px) {
#wrap > .container {
width: 100%;
}
}
#media (max-width: 480px) {
#wrap > .container {
width: 100%;
}
}
those 100% widths are there because you'll lose the responsive width for your site if you force the page to be 600px wide under that width.
And the last part, centering vertically. Are you sure you need it? How much there will be information on the page? Does it vary much? What happens when you view the site for example with mobile device? You can achieve it using HTML & CSS only adding couple of more divs and some css.
In the index.html file wrap the container div like this:
<div class="outercontainer">
<div class="middlecontainer">
<div class="container">
Content
</div>
</div>
</div>
And change the #wrap > .container part of the CSS to:
.outercontainer {
display: table;
height: 100%;
position: absolute;
width: 100%;
}
.middlecontainer {
display: table-cell;
padding: 65px 0; /*For your header and footer*/
vertical-align: middle;
}
.container {
height: auto;
margin: 0 auto;
max-width: 600px;
position: relative;
}
Here's a Codepen of the vertical centering: http://codepen.io/anon/pen/Gposw
You may need some tweaks to get it work like you want it but using these gives you good start.
Try:
JavaScript:
<div class = "row-fluid">
<div class = "header">
</div>
</div>
<div class = "row-fluid">
<div class="container main">
</div>
</div>
<div class = "row-fluid">
<div class = "footer">
</div>
</div>
CSS:
.header, .footer {
height: 65px;
}
.container.main {
width: 600px;
}
I am looking to create a layout for my site where a sidebar is fixed at the right side of the viewport with a 30% width (content is to the left of it) until the browser window reaches a certain width, at which point I want the content and sidebar to be centred and no longer grow with the browser window (since it becomes hard to read at extremely large widths). Here is an idea of the html being used:
<body>
<div id=sidebar>sidebar content</div>
<div id=content>articles, images, etc</div>
And here is some of the basic HTML being used to format it:
#sidebar {
width: 30%;
position: fixed;
right: 0;
top: 0;
background-color: gray;
}
#content {
width: 70%;
margin-right: 30%;
max-width: 49em;
}
At this point, when the content gets wider than 49em, it sticks to the right side of the page creating an ever-increasing gap between it and the fixed sidebar. What I would like is to have it reach a max width of 49em, have the sidebar reach 21em (so they are still 70:30) and remain fixed, but have that whole 70em worth of width centered in the viewport.
I also want the background colour of the sidebar to span the entire way from the edge of the content to the right-hand side of the screen (i.e. a containing div that centers both the sidebar and content with a max width of 70em doesn't work since the background of the sidebar would only go to the edge of the containing div instead of the viewport). That one isn't as important because it might look fine to put some sort of textured background on the body element to make it look like as though the page is "sitting" on some textured surface (not ideal, but fine). I just haven't been able to center the sidebar and content while maintaining the sidebar's fixed positioning.
Thanks!
Update: here's a very rough schematic of what I am looking for:
|A|B|C|D|
B is the content area with a max width of 49em. C is the sidebar with max width of 21em AND it has to have fixed positioning. A and D would be the margins (each half of the difference between the viewport width and 70em). Background of D must be the same colour (gray) as the sidebar. Background of A must be white.
This solution meets most of your requirements, but you need to provide the width of the content+sidebar (in this case, I put 70em)
HTML:
<div id="container">
<div id="content">articles, images, etc</div>
<div id="sidebar">sidebar content</div>
</div>
CSS:
#sidebar {
width: 29%; background-color: gray; border: 1px gold solid;
float: left;
position: fixed; right: 0; top: 0;
}
#content {
width: 69%; max-width: 49em; border: 1px silver solid;
float: left;
}
#container {
max-width: 70em;
margin: 0px auto;
}
jsFiddle here. (You can test by just dragging the middle frame left and right)
Something like this:
<body>
<div id="wrapper">
<div id="sidebar">sidebar content</div>
<div id="content">articles, images, etc</div>
</div>
</body>
With CSS that is similar to this:
body { background:url(imageForSidebar.png) right top repeat-y; }
#wrapper {
max-width:1000px;
margin:0 auto;
background:#FFF url(imageForSidebar.png) -66% top repeat-y;
position:relative;
}
#sidebar {
width:30%;
float:right;
position: fixed;
}
#content { margin-right:30%; }
The background image on the body would take care of it going all the way to the edge of the screen. You would use a background image that was large enough to do this, but small enough so that it gets covered by the #wrapper background. The background image on the wrapper works in a similar way, but in this case it is just making sure that the sidebar image always extends to the bottom of the content.
You can add media queries into your css
//your normal css
#sidebar {
width: 30%;
position: fixed;
right: 0;
top: 0;
background-color: gray;}
//media query (you can add max and min width of the sceen or one of both)
#media screen and (min-width:500px) {
#sidebar{
//css you want to apply when when width is changed
}
}
So, I have this, when the screen is resized, the floater should move to the left. Simple enough - but I want it below the content element - any easy way to accomplish that?
Since DOM order can't change without javascript - is there a way to have it display the same (floater is floated to the right), but have the inner elements in different order?
The floater cannot have a set height, only width.
I tried doing it position absolute and change the DOM order, it mostly worked, but then the floater has to be lower (in px) than the content div, which is not the case for me, it gets over the content below.
Any ideas?
.floater
{
float: right;
width: 300px;
background-color: gray;
}
#media screen and (max-width: 650px) {
.floater
{
float: left;
}
}
<div>
<div class="floater"></div>
<div>
content content content content content content content content content content content content content content content content content content content content
</div>
</div>
Update
Given your comments, we are more or less back to basics now :)
We're setting the main content to take up the entire width but leave 300px on the right side with margin. Width defaults to auto and results in a liquidy feel when resized above the #media treshold; I'm going to float it left too to make it play nicely with the next column:
.content-main {float:left; margin-right:300px;}
Now, for the sidebar. We know that it's 300px wide, and we also know that there is that much space available right next to the main content. That area, however, is taken up by the main content's margin, effectively "pushing" the sidebar down. We'll just handle it with a negative margin:
.content-secondary {float: left; width: 300px; margin-left:-300px;}
Because of the way dimensions worked out for us so far, it doesn't actually matter (for this scenario at least) whether the sidebar is floated left or right. Personally I'd stick with "stacking" it in one direction which would be of benefit if you were to add yet another column.
Updated version here: http://jsfiddle.net/6VpTR/76/
Original Answer
Note that in your original code the right column would interfere with the main content because of the manner in which floating elements interact with non-floating ones. I'd strongly suggest looking into using an existing framework where the kinks have been worked out maybe cssgrid.net or 960.gs
That being said, see this fiddle here: http://jsfiddle.net/6VpTR/
As you can see I'm strongly in favour of semantic class names, and your original concern gets addressed by some html restructuring as well as creative use of floating and margins. Gutters should be added if you do not go with a pre-defined grid framework.
New Answer (works, but with one major caveat)
This new answer works great (IE9, Firefox tested [IE8 renders but does not recognized #media switch]), except it requires you know the height of the right column (the column can be whatever height, but it needs to be known). It does reverse the DOM order within the wrapper. I realize the height issue may be a problem for you, Madd0g (I'm not sure if by "cannot have a set height" also means you do not know the height of any particular usage), but this solution could work for others, so I decided to post it anyway.
HTML
<div class="wrapper">
<div class="content-main">Main Content</div>
<div class="content-secondary">Right column</div>
</div>
<div>Content below</div>
CSS
.wrapper {
padding-right: 300px;
}
.content-secondary {
float: right;
width: 300px;
background-color: gray;
margin-right: -300px;
margin-left: 0;
}
.content-main {
float:left;
background:orange;
min-width: 100%;
margin-right: -300px;
}
.content-main:before {
content: '';
width: 300px;
float: right;
margin-bottom: 1em; /*this needs to be set to height of right column */
}
.wrapper + div {
clear: both;
}
#media screen and (max-width: 650px) {
.content-main, .content-secondary
{
float:none;
margin: 0;
}
.content-main:before {
display: none;
}
.wrapper {
padding-right: 0;
}
}
Original Answer (works if no content below the wrapper)
Based on your comment to o.v., I think what you want is this:
HTML
<div class="wrapper">
<div class="content-secondary">Right column</div>
<div class="content-main">Main Content</div>
</div>
CSS
.wrapper {
position: relative;
}
.content-secondary
{
float: right;
width: 300px;
background-color: gray;
}
.content-main
{
background:orange;
}
#media screen and (max-width: 650px) {
.content-secondary {
float:none;
position: absolute;
top: 100%;
}
}
This works for me, atleast in Firefox...you just need another <div>
.floater
{
float: right;
width: 300px;
background-color: gray;
}
#media screen and (max-width: 650px) {
.floater
{
float: left;
}
}
<div>
<div>
content content content content content content content content content content content content content content content content content content content content
</div>
<div>
<div class="floater">a</div>
</div>
</div>