I'm trying to achieve a fixed left sidebar with fluid right content within a container.
I've checked out answers that tell me to set a margin-left on the content which is not what I want to do.
The closest I could get was through this answer:
A `position:fixed` sidebar whose width is set in percentage?
<div class="main-container">
<div class="sidebar">
<div class="sidebar-content-container">
<div class="sidebar-content">
<!-- Sidebar content here -->
</div>
</div>
</div>
<div id="content">
<!-- Scrollable Content Here -->
</div>
</div>
This is the CSS
.main-container {
height: 100vh;
max-width: 1366px;
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
padding-top: 100px;
width: 100%;
position: relative;
.sidebar {
float: left;
width: 20%;
height: 100%;
position: relative;
.sidebar-content-container {
height: 100%;
position: fixed;
.sidebar-content {
display: block;
height: 100%;
width: 100%;
overflow: hidden;
}
}
}
#content {
height: 100%;
width: 80%;
display: inline-block;
float: right;
}
}
The way I have it done right now, once you inspect the sidebar, the width it calculates is not within the main-container but the rest of the viewport. What I'm trying to achieve is to keep the 20% calculation within the main-container without setting a fixed width.
So the yellow part represents the problem for me. Once I set it fixed, it doesn't stay within the parent container. My goal is after I set it to fixed, it stays within the red part.
I know I could do something like sidebar width 100px content margin-left 100px but that's not the objective and do not like any javascript alternatives.
Thoughts?
Thanks!
Your are using width:100% in this div and also padding which increases its width from 100% because padding adds to the width. So for giving the padding within the 100% width you can use property box-sizing:border-box as below:
.main-container {
height: 100vh;
max-width: 1366px;
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
padding-top: 100px;
width: 100%;
position: relative;
box-sizing:border-box;
}
Well, I guess that's the thing with fixed elements. They kind of have a mind of their own. Once an element is fixed, it doesn't seem to obey the overflow and dimension rule of its parent containers. They take their dimensions (if you are using %) from the browsers width. So I think what you should do is to give the fixed element the same dimension as its immediate container.
<div id="parent-box">
<div id="fixed-elem"></div>
</div>
//css
#parent-box{
position: relative;
width: 40%;
height: 100%;
float: left;
overflow: hidden;
}
#fixed-elem{
position: fixed;
width: 40%;/*this is 40% of the browser width not the container*/
height: 100%;/*this is 100% of the browser height*/
}
Related
So lets imagine I have 6 div elements with different content.
3 divs are header
1 div is main container (scrollable)
last 2 divs are footer
How to make all of this scallable just with pure css? Because now when I'm resizing my browser my footer divs are just disappearing and I can't reach them and when I make my browser even smaller my main container div is cut in half (lower part disappears) and header divs height gets smaller
The best scenario would be to make header and footer divs somehow fixed height(don't know how) and main container to resize on broswer is resized.
html
<div ng-controller="ListController">
<div class="header">
</div>
<div class="price_found">
</div>
<div class="settings">
</div>
<div class="main_container antiscroll-wrap">
<div class="container antiscroll-inner">
</div>
</div>
<div class="total_select">
</div>
<div class="menu_footer">
</div>
</div>
scss
.cheap-watcher { //this is main container properties in which everything is injected
position: fixed;
top: 0px;
right: 0px;
width: 360px;
height: 100%;
max-width: 360px;
max-height: 100%;
min-height: 100%;
background-color: #f1f3f4;
float: right;
.header {
height: 7.57%;
background-color: #00a8e8;
}
.price_found {
padding-top: 16px;
height: 10.169%;
background-color: #fff;
text-align: center;
}
.settings {
height: 4.971%;
width: 100%;
display: inline-block;
}
.main_container {
width: 360px;
background-color: #fff;
.container {
width: 360px;
height: 57.856%;
}
}
.total_select {
height: 7.57%;
width: 100%;
background-color: #fff;
text-align: center;
border-top: solid;
border-top-color: #e8e8e8;
border-top-width: 2px;
}
.menu_footer {
height: 11.864%;
width: 100%;
}
}
You can assign to all of your containers a min-height for example in px or vh. Min. Height means that the container will always have that minimum height but will grow as more content comes in. For your main container you obviously want a fixed height, so use normal height and use also px or vh and overflow so if you have more content then the height allows, the main container becomes scrollable.
See a working Fiddle
Hope this helps you a bit.
I would like to center a div inside of the header which is 100% width. The div that needs to be centered has a physical width and height. I have tried using position absolute, but when resizing the screen, the div will stay centered. I want the div to stay in the center of the design, not the screen. How do I accomplish this? I also do not want to use position fixed for this.
css:
header{
position: relative;
width: 100%;
height: 65px;
background-color: #662D91;
}
#banner{
position: absolute;
left: 50%;
margin-left: -240px;
width: 480px;
height: 115px;
border-radius: 20px;
}
HTML:
<header>
<div id="banner">
<a href="/index">
<img src="/_images/layout/PentaOneStop_headerLogo.png" class="img-center" />
</a>
</div>
</header>
EDIT: I have solved my issue by adding a div between the full width header and the logo that is the width of the design. It appears to have fixed my problem.
JSFiddle of my solution to my problem: http://jsfiddle.net/U3Hpa/2/
The simple solution is to do text-align: center.
#banner{
text-align: center;
}
http://jsfiddle.net/U3Hpa/
I solved my issue by adding another div that was a child of the header element but the parent of #banner. I then set it at a width of 980px (same as width of content container), and set it to margin: 0 auto;. That seemed to fix my issue. I was looking for a CSS solution without changing the HTML markup, but I could not find one. Thanks for all the feedback.
The Fiddle does not illustrate the solution the best because of the small viewport, but the code is accurate.
JSFiddle: http://jsfiddle.net/U3Hpa/2/
#banner{
margin-left: auto;
width: 480px;
height: 115px;
border-radius: 20px;
}
by manually defining a left position, and margins, you make the browser do that no matter what the size of your other divs.
the auto feature in your margins will automatially center the div relative to the header
You should try this:
#banner{
position: fixed;
left: 50%;
margin-left: auto;
margin-right: auto;
width: 480px;
height: 115px;
border-radius: 20px;
}
Try this:
header{
position: relative;
width: 980px;
height: 65px;
background-color: #662D91;
}
#banner{
position: relative;
margin: 0 auto;
width: 480px;
height: 115px;
border-radius: 20px;
}
I'm having an issue with a fluid sidebar and a content box next to it.
I designed my left #sidebar to my liking, but not I'm having trouble making a content box that fills up the remaining space next to it.
I'd like to have the whole project take up 100% of the page width. The problem is coming from the min/max widths on my sidebar.
Been goin' hard on this all day and still having problems, void space between, overlapping ,ect.
http://jsfiddle.net/DrDavidBowman01/PjLgE/
CSS
#container {
width: 100%;
display: block;
height: 100%;
}
#sidebar {
display: block;
width: 22%;
float:left;
min-width: 236px;
max-width: 332px;
height: 100%;
position: fixed;
border: 2px solid #0C6;
background-color: #000;
}
#content {
width: 88%;
height: 400px;
border: 6px solid #F00;
display: block;
color: #fff;
float: left;
position: relative;
max-width: calc(88% - 236px);
min-width: calc(88% - 332px);
}
HTML
<div id="container">
<div id="sidebar"></div>
<div id="content"></div>
</div>
It's a combination of two things. First, if you want to have divs take up 100% height, then you'll need to set the body and html to that as well.
html, body {
height: 100%;
}
Second, you have set the sidebar as position: fixed. This is just like having position: absolute set on it. If you want the sidebar to remain visible at all times, you can do a margin-left: 22%; (or whatever the width of the sidebar is) on #content. If you want the sidebar to flow with the rest of the page, just remove the fixed position.
This is because your sidebar is position: fixed. The best route would be to relatively position/float the sidebar at 100% height and position a fixed wrapper within it.
basic demo
I have the following (jsfiddle):
<div class="content_wrap">
<div class="left_container">
</div>
<div class="right_container">
<div style="background-color: blue; margin:20px; height: 1500px;"></div>
</div>
<div class="clearfix"></div>
</div>
</body>
</html>
CSS:
body,html {
height: 100%;
width: 100%;
background-color: #f8f8f8;
}
div.content_wrap {
width: 100%;
height: 100%;
}
div.left_container {
float:left;
width: 220px;
height: 100%;
background-color: red;
}
div.right_container {
position: relative;
padding: 15px;
padding-top: 100px;
width: 1000px;
}
.clear { clear: both; }
​What I'm trying to do is line the divs side by side and have the left side bar (red) stretch to either the height of the page, or the height taken up by the content (which is in blue), which ever is greater (like the layout shown here)
My problems at the moment are:
The content of the right container (The blue box is just to illustrate content) does not align properly next to the left container
The left container doesn't adjust its height according to the content of the right container.
I've put in a clear fix, although to be quite honest, I don't completely understand how that works.
Would appreciate some guidance.
See this demo: http://jsfiddle.net/PaJ3r/9/
Here is updated CSS for it:
body,html {
height: 100%;
width: 100%;
background-color: #f8f8f8;
}
div.content_wrap {
width: 100%;
position: relative;
}
div.left_container {
float:left;
position:absolute;
width: 220px;
height: 100%;
background-color: red;
}
div.right_container {
position: relative;
padding: 15px;
margin-left: 220px;
padding-top:100px;
width: 1000px;
}
.clear { clear: both; }
position:relative in div.content_wrap is needed in order to get left sidebar stretched to the height of content.
position:absolute; in div.left_container allows left container to fit height of wrapper div.
In div.right_container there is margin-left: 220px; which leave left sidebar visible
Please take a look at this jsfiddle.
So few remarks: I used float: left; on both divs and also, as bart said, you didn't use the correct name for clearfix.
First you should nest your div's so that the left div can grow with the right one.
See updated fiddle
For the left div not set the height, but the min-heigth to 100%. Furthermore you need to play around with margins and paddings to get the last bit ok
My site has a 900px div #content that is centered with margin-left: auto and margin-right: auto. I have an image that I need to display behind the div which will partially overlap #content.
The image is set to display as block at present and I can get it to where it needs to be, but it doesn't allow #content to draw over the image. I can get #content to display over the image with position: absolute however this prevents the use of margin-left / margin-right auto to center.
My current positioning, which gets it where it needs to be is:
img#watermark
{
margin-left: auto;
margin-right: auto;
display: block;
padding-left: 900px;
}
#content just needs to appear over the watermark.
Help greatly appreciated.
html:
<img src="http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png" />
<div></div>
css:
div {
margin:auto;
width: 512px;
height: 512px;
background: rgba(0,0,0,.4);
position: relative;
}
img {
position: absolute;
left: 50%;
margin-left:-256px;
}
http://jsfiddle.net/Db2cw/
the solution is to have a surrounding div on the #content div, and that surroinding div positioned absolutely and with a defined width and height.
Ex:
html:
<div id="outter">
<div id="image"><img src="something.jpg" /></div>
<div id="contentOutter">
<div id="content">the content here</div>
</div>
</div>
CSS:
#outter {
width: 1000px;
height: 300px;
position: relative;
}
#image {
width: 1000px;
height: 300px;
position: absolute;
}
#contentOutter {
width: 1000px;
height: 300px;
position: absolute;
}
#content {
margin: 0 auto;
width: 900px;
}
Example here: http://jsfiddle.net/qwEhv/
"I can get #content to display over the image with position: absolute however this prevents the use of margin-left / margin-right auto to center."
What you might need to do here is to have an additional div - call it #contentWrapper for example and center it using margin-left and right, set position to relative. Put div #content inside the wrapper div and position absolute. This should allow you to make #content look centered.