Here's my jsFiddle
In my Fiddle link i have floated my left-sidebar and right-sidebar. As per my knowledge floated element are removed from html normal flow. i just did not understand if it is so why it is displacing my right-sidebar.
<div class="content-area">
<div class="left-sidebar"></div>
<div class="main-area">hi</div>
<div class="right-sidebar"></div>
my css:
.content-area {
background-color: #bbb;
height: 310px;
}
.left-sidebar {
float: left;
width: 50px;
height: 100%;
background-color: #abcdef;
}
.right-sidebar {
float: right;
width: 50px;
height: 100%;
background-color: #abcdef;
}
http://www.vanseodesign.com/css/understanding-css-floats/
Elements are placed in the order per a normal flow, removed from the normal flow and then moved right or left based on the float. Your right side bar is shifted down because it comes after the main content in a normal flow. If you don't want want it to be shifted you need to change the order of your elements.
http://www.w3.org/TR/CSS2/visuren.html#positioning-scheme
Per the spec:
In the float model, a box is first laid out according to the normal
flow, then taken out of the flow and shifted to the left or right as
far as possible. Content may flow along the side of a float.
You are confused with position: absolute. Floating will move an element to the left or right, encouraging other elements to 'float' around it, but they will still be in the flow and other elements are influenced by it.
When you use position: absolute, you remove the element from the flow, placing it on top or below other elements.
Here is your updated fiddle. The most important changes are in this part:
.content-area {
background-color: #bbb;
height: 310px;
position: relative;
}
.left-sidebar {
position: absolute;
top: 0;
left: 0;
width: 50px;
height: 100%;
background-color: #abcdef;
}
.right-sidebar {
position: absolute;
top: 0;
right: 0;
width: 50px;
height: 100%;
background-color: #abcdef;
}
The parent is given a position too, so the sidebars can be positions in it. The left and right sidebar are positioned at the top of the parent and on the far left and far right respectively.
If you look closely, you see that the contents of the main content block are now behind the side bars. I've made the text a bit longer, so you can see it appearing from underneath the left bar.
Of course this is probably not what you want, but you can solve it quite easily by changing the order in your markup. First add the sidebars in whichever order you like, and then add the main content. The results can be seen here.
The way this works: at the top of your content-area, the left side bar is started first. It floats to the left, encouraging the next element to float next to it, having the same top position. That element would be the right side bar, which float to the right, and also encourages the next element to float left of it. The main area doesn't have an explicit width, so it figures that it would fit snuggly inbetween, still starting at the same top position.
Since the main element itself doesn't float, the next element will start (top) where the main area ends. So if the right bar would come after the main area, it shifts down, like in your example. The right bar would move even further down if the main area contents grow.
<div class="content-area">
<div class="left-sidebar"></div>
<div class="right-sidebar"></div>
<div class="main-area">hi</div>
You need to change main-area to come after the other two floated elements. I may seem weird to do it this way, but it means that main-area will not push right-sidebar down because the content is coming after it in the markup. I hope this helps.
Related
I'm creating a web page with some content which is divided into two sides: a left side and a right side. The right side contains various forms and needs to be scrollable, the left side instead contains only an image and some buttons/checkbox and it needs to stay fixed. Therefore the left side itself cannot be scrolled, but when I scroll the right side, the left side stay fixed and I still can see its content if I am at the top or at the bottom of the page.
In figures, what I am tring to achieve is something like the following:
In the figure above you can see my main page. The right content is scrollable and when you scroll it the left content stay fixed. At the bottom of the page I can still see the left content as in the following image:
Is there any way I can achieve this? I'm developing using Angular and Bootstrap, but I'm using also custom CSS.
You can use flexbox/css grid or bootstrap grid for drawing basic layout and can use code
.left-container{
position: fixed;
top: 0;
right: 0;
}
for better understanding please go through css position properties
In case if your right panel gets distorted once you place fixed position on left div you can do something like this:
.side-bar{
width: 20%;
height: 550px;
border-right: 2px solid black;
position: fixed;
}
.main{
width: 80%;
height: 550px;
margin-left: 20%;
}
place margin left of main /right div to the width of left panel, like I have width of side panel as 20% so I have placed margin-left of main div to 20%
Try this:
HTML
<div class="image-container">
//add your html code here
</div>
CSS
.image-container {
position: fixed;
top: 0;
right: 0;
display: block;
}
To fix your container use position:fixed
here is a project that i did that has fixed position on left.
just got a question regarding relative & absolute positioning and applying clearfix to the main container cos I've written the code and it's not behaving as I expected.
Structure-wise this is a simple page about product history. nav-bar with drop-down menu at the top across the screen, then a big hero image across the screen, followed by a few paragraphs and a simple footer, that's it.
here's my problem:
I need to put 3 components in the hero image area - the hero image itself, one title word on the top left corner and one logo on the top right corner. What I've done is: I created a div and used the hero image as background image. I set the position value of the div to relative. I created another div to hold the title word and set the position to absolute, using top and left to give it a location. Following the same logic, I created another div to hold the logo and set it to float right, with position set to absolute and top and right to give a location. I've applied clearfix to the main div and everything looks ok on my screen (resolution 1280 x 1024) until I saw it on the wide screen(1680 x 1050) --- the logo is not on the hero image! It's to the right side of the hero image.
What caused this? I thought by putting 2 divs inside the main div and applying clearfix, the three will "get together" and act as one and won't separate... Is it because I haven't written any code for responsive layout? Or was it because I shouldn't have used the hero image as the background? Would this problem be solved if I used z-index instead to specify the stack order of hero image, logo and title word?
Below is my code and any help would be much appreciated!
<div id="history-content" class="clearfix">
<div id="history-image-text">HISTORY</div>
<div id="stamp">
<img src="./images/logo.png">
</div>
</div>
#history-content {
background-image: url('./images/heroimage.jpg');
min-height: 307px;
background-repeat: no-repeat;
position: relative;
}
#history-image-text {
color: #fff;
position: absolute;
top: 20px;
left: 50px;
font-size: 20px;
font-weight: bold;
}
#stamp img {
width: 10%; /*not sure I'm doing the right thing here either*/
height: 40%; /*not sure I'm doing the right thing here either*/
float: right;
position: absolute;
right: 100px;
top: 20px;
}
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
height: 0;
line-height: 0;
}
Few things:
Absolutely positioned elements are taken out of normal flow, hence doesn't affect the size of their parent.
Since they're out of normal flow, float has no effect on them (as far as i know)
Absolutely positioned elements shrink wraps to fit it's contents unless width and height is set explicitly or stretched using the top, right, bottom & left properties.
Now your parent div #history-content doesn't have any height set, and all of it's content of are absolutely positioned, So it's not visible (height 0)
applying a proper height for the parent seems to fix the issues for me.
Side note: unlike what you think, you don't have two absolutely positioned<div>'s, #stamp img absolutely positions the <img> inside div#stamp, for the same reason mentioned above, div#stamp is also invisible (height 0) you'll get the same result with and without it. And without floats
As others have said, float doesn't have an effect on absolute positioned elements, and so technically you don't need clearfix in this case.
I'm not exactly sure why your logo is positioned outside the outermost container #history-content, but you could try to put a border around the #history-content to further troubleshoot.
EDIT: Maybe check your hero image dimension, is it smaller than 1608px in width?
<div id="history-content">
<div id="history-image-text">HISTORY</div>
<div id="stamp">
<img src="./images/logo.png">
</div>
</div>
I've changed your CSS below
#history-content {
background-image: url('./images/heroimage.jpg');
min-height: 307px; /*set whatever minimum height you wish*/
background-repeat: no-repeat;
position: relative;
}
#history-image-text {
color: #fff;
position: absolute;
top: 20px;
left: 50px;
font-size: 20px;
font-weight: bold;
}
#stamp {
display: block;
position: absolute;
right: 100px;
top: 20px;
width: 10%; /*set width of image in containter instead*/
height: auto;
}
#stamp img {
max-width: 100%; /*image width will not stretch beyond 100% of container*/
height: auto;
}
JSFiddle: http://jsfiddle.net/5L9WL/3/
I want to place a div fixed on the left and near I want to place other div.
Imagine a twitter webpage, I want to fixed the left panel (where you write yout tweets) and near I want to place the panel where you read tweets.
Now I have the following code:
<div id="container">
<div id=fixed-menu>
</div>
<div id="content">
</div>
</div>
#fixed-menu {
position:fixed;
background: #fff;
padding: 10px;
top:60px;
left: 10px;
width:300px;
max-width: 300px;
}
#content {
background: #fff;
padding-top: 10px;
}
In this way, the div with id="content" appear on left so, the fixed-menu doesn't appear, because it is under content div.
If I use margin-left in #content the error is solved, but I don't want use that, any other solution?
Thanks.
One of the first things to note is that by putting a position Fixed on div#fixed-menu breaks it out of the normal document flow. What this means is that the other block/inline level elements do not know about it. Also by making it fixed, you make it fixed relative to the window. If you want it "fixed" within the container and not to a certain point on the screen I would go with position:absolute and then a position:relative on it's parent container.
Either way, the problem you're experiencing where div#content doesn't respect the position of the fixed element, is due to the fact that the fixed element is no longer part of the normal document flow. Adding a z-index to div#fixed-menu should bring it above the content. However, you will see overlapping and will have to account of the offset of div#content with either margin on div#content or padding on the parent container.
If you look at this fiddle: http://jsfiddle.net/f38aj/
css:
#container {
position: relative;
height: 700px;
padding: 0 0 0 320px;
}
#fixed-menu {
position: fixed;
background: red;
padding: 10px;
top:8px;
left: 8px;
width: 300px;
max-width: 300px;
}
#content {
background: blue;
padding-top: 10px;
}
If you notice we create padding in the container, where we end up overlaying the div#container object.
we have a fixed container on the left while the right side content will scroll with the page. If you can come up with a non fixed solution it might be better, as there are phone browsers like older versions of iOS that will take anything that is position fixed and replace it with position absolute.
A side note, working with fixed/absolute positioning is useful especially in some crazy cases, but it does require a little more due diligence on your/your teams parts to maintain. If you start getting into z-indexes you might want to look at a library like less or sass just to create global css variables, which will make it easier to manage what can turn into an almost unmanageable experience.
hope that helps.
I have a page with a float: right'ed div at the top, and a text-align: center'ed div right underneath it. Is it possible to make the floated div not alter the flow of the other div (the one right below it)?
Here are two fiddles that show what I'm seeing (I like neither, they are explained in the next paragraph):
off-center -> http://jsfiddle.net/5XMVt/
centered but gaping hole -> http://jsfiddle.net/CSGQn/
If left alone, the bottom div is pushed left (out of the center) by the floated div. I could do clear: both on the bottom div but that would push it down below the floated div, and even though this is better than being off-center, it's suboptimal because it creates a giant hole right above it. I need the floated div not to alter the flow of the div below it at all, like it's not even there.
I was also thinking about doing position: absolute; but that would only work if the div were supposed to be on the left side, when it's supposed to be on the right side.
What I would like is like the first fiddle except with the 'should be centered' text actually centered.
You can set the right-floated div with position:absolute
And set the right:0, that will give it the same behavior as floating it to the right.
Note that this will only work if the div has to be at the right of the page, not a container.
i think you can do it this way http://jsfiddle.net/5XMVt/4/
or you can use position absolute for the .floating class, and set the right:0px
Yes, why not you have something like that:
<div id="container">
<div id="rightFloat"> you right floated here</div>
<div id="content"> your text here</div>
</div>
With the following style:
#container {
}
#rightFloat {
float: right;
width: 200px;
height: 200px;
border: 1px solid black;
margin-right: 10px;
}
#content {
border: 1px solid black;
float: right;
clear: none;
margin-right: 10px;
}
That ought to do it :)
I have a page where I need a piece of text to appear aligned to the upper left of an absolutely positioned element (a span, if it matters), and a button to appear aligned to the upper right of the same element. edit: Problem with this is even when I use float: right; and display: inline; the button still likes to drop the next line.
Currently my solution is to wrap the button with a span element, float the span to the right, and then set the button to absolute position. Problem with this is it doesn't appear unless I manually specify the width of the wrapper span to fit whatever size the browser renders the button. Which is kinda dumb.
What's the proper way to do this?
edit 2: Here was my original code:
#header
{
position: absolute;
top: 0;
bottom: auto;
left: 0;
width: 100%;
height: 24px;
overflow: hidden;
}
/* Header's buttons. */
#header > span
{
float: right;
width: 100px;
}
#header > span > button
{
position: absolute;
}
And the HTML:
<span id="header">
Trigger editor
<span><button type="button" id="h_output">Output Triggers</button></span>
</span>
Hope I understood correctly.
<div class="clearfix">
<div style="float:left">Text</div>
<div style="float:right">Button</div>
</div>
Where clearfix is the famous one (http://www.456bereastreet.com/archive/200603/new_clearing_method_needed_for_ie7/). In this way I don't think you need to explicitely set the width of the text or the button.
I believe 'the proper' way it to use DIVs (not SPANs) for element positioning. But yes, you'd have to set the width explicitly on the floated DIV on the left, otherwise it'll take up the whole line (being a block element and all). Just make sure that your width is enough to show all contents of the DIV without overflowing.