Fill the horizontal space between left and right floated elements - css

I found an answer here that addresses what I need to do, but it doesn't seem to work perfectly for me. I have sidebar divs floated left and right. I want to stick a div in the middle and fill the space. The technique in that answer works for the left div, but it pushes the right div down to the next line. I threw up a quick demo on CodePen to show the problem.
Can anyone suggest a way to fix this so that all the divs are on the same line?
Update: I realize I could do this easily using percentages for all the div widths, but I really need to maintain static widths for the sidebars.

One way to do this would be changing the order of your HTML (placing the right sidebar before the left)
<div class="page-grid">
<div class="sidebar right">
<div>Right Widget 1</div>
<div>Right Widget 2</div>
</div>
<div class="sidebar left">
<div>Widget 1</div>
<div>Widget 2</div>
</div>
<div id="content">Content area</div>
</div>
http://codepen.io/anon/pen/kHmjd

This is using an example from Dynamic Drive's CSS Layouts
I like this one, especially because it preserves the order of the content semantically. Main content is delivered first, followed by both side columns.
HTML
<div class="page-grid">
<div id="contentwrapper">
<div id="content">Content area</div>
</div>
<div class="sidebar left">
<div>Widget 1</div>
<div>Widget 2</div>
</div>
<div class="sidebar right">
<div>Right Widget 1</div>
<div>Right Widget 2</div>
</div>
</div>
CSS
#contentwrapper {
float: left;
width: 100%;
}
#content {
margin: 0 15em;
}
.sidebar {
float: left;
width: 15em;
}
.sidebar.left {
margin-left: -100%;
}
.sidebar.right {
margin-left: -15em;
}

Related

How to put a div to the right of .container in Bootstrap?

Basically, I need to put a back-to-top button at the right side of the footer.
Something like this:
What I get is this:
You can see that there is a blank space between footer and the end of viewport, that space is the height the back-to-top button, if I remove the button the blank space is removed too.
I'm using bootstrap so my html code is similar to:
<footer class="container-fluid">
<div class="container">
<div class="content1>CONTENT 1</div>
<div class="content2>CONTENT 2</div>
</div>
<div class="back-to-top>TOP</div>
</footer>
You can see an example in Bootply. You can see that the footer has to be 20px height (min-height: 20px) but instead it is 40px.
I think that my problem will be solved if I can put the .back-to-top div beside the .container div.
How can I get this?
You can use helper class pull-right and move TOP link before container:
<footer class="container-fluid">
<div class="back-to-top pull-right">TOP</div>
<div class="container">
<div class="content1>CONTENT 1</div>
<div class="content2>CONTENT 2</div>
</div>
</footer>
You need to remove your CSS bloc:
.back-to-top {
float: right;
position: relative;
top: -20px;
}
Doc: http://getbootstrap.com/css/#helper-classes-floats
Having a min-height proxy doesn't mean you footer is going to be 20px. That just mean its height won't be smaller than that. If you want your height to be 20px, use height property. If for some reason you want it to be variable, you can look to the max-height property.
For your "back-to-top" button, here is my suggestion :
http://jsfiddle.net/Bladepianist/38ne021p/
HTML
<footer class="container-fluid navbar-inverse">
<div class="row">
<div class="col-xs-6">CONTENT 1</div>
<div class="col-xs-5">CONTENT 2</div>
<div class="col-xs-1 text-right" id="back-to-top">TOP</div>
</div>
</footer>
CSS
.container-fluid {
color: white;
}
Basically, I change your "back-tot-top" class to an ID in my model but you're free to adapt it to your liking.
Using the col-system and the text-positions classes, you can achieve the same rendering as you show in your question. That way, the back-to-top button is part of the footer.
Hope that's helping ;).

Bootstrap background width padding

I'm using Boostrap 3 and have a problem with backgrounds on elements that appear too wide. The container is in desktop mode 1170px and has 15px padding left and right which makes the content appear with 1140px width.
When I'm adding an element with a different background color (let's say body + .container both has same background), then the element will appear as 1170px wide due to the background showing in the padding area as well.
I could add CSS for each element with deviating background in each screen width (media queries) to solve the problem. But I hope there is a better way to achieve this since I can't be the only person with this problem. Does anyone know some Boostrap class / function to solve this issue, or know some best practise for this?
Try wrapping the rows and/or inner content, you can see how I have done it here; http://jsfiddle.net/w7wowg94/
HTML
<div id="master-wrap">
<div class="container">
<div class="wrap-class-one">
<div class="row">
<div class="col-md-6">Content 1</div>
<div class="col-md-6">Contnent 2</div>
</div>
</div>
<hr />
<div class="row">
<div class="col-md-4">
<div class="inner-wrap">Content 1</div>
</div>
<div class="col-md-4">
<div class="inner-wrap">Contnent 2</div>
</div>
<div class="col-md-4">
<div class="inner-wrap">Contnent 2</div>
</div>
</div>
</div>
</div>
CSS
#master-wrap {
margin: 0 auto;
background-color: #eee;
padding: 15px;
}
.wrap-class-one {
background-color: #ccc;
padding: 15px;
}
.inner-wrap {
padding: 15px;
background-color: #ccc;
}

Using a div as a filler

I appreciate this maybe a simple question but I cannot find the answer.
I have a div. Inside that div I have 3 columns (made up of divs).
The 1st div has some text in it and the 3rd div had some text in it as well. I want the middle div to take up all the remaining space.
I have played around with absolute,fixed and relative positions.
This is what I mean:
<div id="divheader">
<div style="float: left; width: 85px;">Caption to the left</div>
<div style="float: left; width: 100%;">this div has no caption but I want it to take up the remaining space</div>
<div style="float: left; width: 185px;">caption to the right</div>
</div>
<div id="divpage">
stuff
</div>
<div id="divfooter">
footer stuff
You could float the last div right and not worry about having a div in the middle if you are not using it for any purpose.
<div>
<div style="float: left; width: 85px;">Caption to the left</div>
<div style="float: right; width: 185px;">caption to the right</div>
</div>
You can use the table-cell display for this exact purpose.
HTML:
<div class="divided">
<div style="width: 85px;">Caption to the left</div>
<div>this div has no caption but I want it to take up the remaining space</div>
<div style="width: 185px;">caption to the right</div>
</div>
CSS:
.divided {
display: table;
}
.divided > div {
display: table-cell;
}
jsFiddle Demo
Browser compatibility: Works great with modern browsers.
You can make use of the display table (the advantage of doing it this way is that it will also keep all three columns the same height):
<div style="display:table; width:100%">
<div style="display:table-cell; width: 85px;">Caption to the left</div>
<div style="display:table-cell;">this div has no caption but I want it to take up the remaining space</div>
<div style="display:table-cell; width: 185px;">caption to the right</div>
</div><br />
Or if you want it to be more compatible with older browsers then you can use padding and negative margins:
<div style="padding:0 185px 0 85px">
<div style="float: left; width: 85px; margin-left:-85px;">Caption to the left</div>
<div style="float: left; width: 100%;">this div has no caption but I want it to take up the remaining space</div>
<div style="float: right; width: 185px; margin-right:-185px;">caption to the right</div>
</div>
Example Fiddle

divide screen into equal parts with overflow

I recently made a website where I needed 6 columns with the y overflow visible. I couldn't make a clean 6 divisions. The width needed to be wider to adjust for the 6 scrollbars and a bit of padding.
Is there a better way than my attempt?
<div class="col">
<div class="section">
Content that overflows this section.
</div>
</div>
.col {
width: 15.2%;
padding-right: 15px;
float: left;
}
.section {
overflow-y: scroll;
width: 100%;
}
It's very sloppy and the columns don't reach the far right edge.
I don't know jquery well enough to attempt but will like take any advice.
****** I worked it out, so silly. You need to use % for everything including padding. Duh ******* Sorry for wasting anyones time!
I would say that it is better to set padding for the inner div .section, so there will be no need to adjust .col width.
Try this HTML code:
<div id="grid">
<div class="col">
<div class="section">
Content that overflows this section.
</div>
</div>
<div class="col">
<div class="section">
Content that overflows this section.
</div>
</div>
<div class="col">
<div class="section">
Content that overflows this section.
</div>
</div>
<div class="col">
<div class="section">
Content that overflows this section.
</div>
</div>
<div class="col">
<div class="section">
Content that overflows this section.
</div>
</div>
<div class="col">
<div class="section">
Content that overflows this section.
</div>
</div>
</div>
With this CSS:
#grid {
margin-left: -15px;
}
.col {
width: 16.6%;
float: left;
}
.section {
overflow-y: scroll;
margin-left: 15px;
height: 100px;
background: green;
}​
Please note that #grid { margin-left: -15px; } will help you get rid of unnecessary white space before first column
take a look at Live demo

CSS - element positioning using float does not work

I'm trying to position two panels and just can't get it to work...
I have a container-page wrapping two panels, each with it's own page. I want to position the panels side by side using float.
This is my CSS:
.pages {width: 100%; position: absolute;}
.leftPanel {position: relative; width: 25%; min-width:100px; float: left;}
.rightPanel {position: static;}
and HTML
<div class="page">
<div id="lefty" class="leftPanel">
<div class="page">
<p>helloworld</p>
</div>
</div>
<div id="righty" class="rightPanel">
<div class="page">
<p>HELLO WORLD</p>
</div>
</div>
</div>
I have to use position:relative for the left panel and position:static for the right panel. Strangely this works in JSBin but in my actual page, the right panel with position:static always has 100% width covering the whole screen.
Any hints on what I may be doing wrong?
Thanks!
div elements by default have a width of 100% of their parent. Since you floated the lefty div you took it out of the flow so what is happening is that the lefty div is effectively sitting outside the flow of the elements. Also float causes the div to shrink-wrap to the size of it's children. So if you are wanting to set the righty div to but up against the lefty div then you should do two things: first add float:left; position:relative; to the righty styling. Second you should add a div at the bottom of that to clear your floats.
On another note you should only use a class if you are going to be styling multiple elements the same way, otherwise just style the element off of the ID.
.pages {width: 100%; position: absolute;}
.leftPanel {position: relative; width: 25%; min-width:100px; float: left;}
.rightPanel {position: relative; float: left;}
<div class="page">
<div id="lefty" class="leftPanel">
<div class="page">
<p>helloworld</p>
</div>
</div>
<div id="righty" class="rightPanel">
<div class="page">
<p>HELLO WORLD</p>
</div>
</div>
<div style="clear:left;"></div>
</div>
Try floating both of the panels? As of right now only the left one is floated... try floating both of them to the left and then putting the correct amount of margin between them to line them up like you want them. Or even floating one left and the other right would probably work.
Add this to your CSS,
div.clear-both {clear: both;}
And change your HTML to this:
<div class="page">
<div id="lefty" class="leftPanel">
<div class="page"
<p>helloworld</p>
</div>
</div>
<div id="righty" class="rightPanel">
<div class="page">
<p>HELLO WORLD</p>
</div>
</div>
<div class="clear-both"></div>
</div>

Resources