How to keep fixed a portion of a web page? - html

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.

Related

How to position div on left side of screen pushing all content to the right

I am experimenting with a few things and I have a navbar that I want to switch from up to to the left side of the screen. I am able to position a basic div to the left side of the screen but what I would like to do is have it push all content on the right of it so that I can implement a hide feature to the menu.
CSS
div {
position: absolute; // also tried fixed
left: 0px;
height: 100%;
width: 100px;
background: #222222;
top: 0px;
bottom: 0px
}
As you can see in the image the div is just on the left side but the content is just over lapping it. Any way to push the content to the right after the div? Any help would be awesome.
Put your right content in a container and then give it margin-left equal to the width of the left column. So:
HTML
<div id='right'>
<!-- all your left right elements go in here -->
</div>
CSS
#right { margin-left: 200px; /* or whatever */ }

How to keep position of image fixed while scrolling?

I have a web site which is made in WordPress. All of the content on my pages is wrapped in a div. What I would like to do now is add banners on both sides of the content and keep them fixed while scrolling.
So, the top of the banners should be aligned with the top.
The right side of the left banner should be right next to the left side of the main content.
The left side of the right banner should be right next to the left side of the main content.
Hence, I would get all elements from left to right: left banner -> content -> right banner. All aligned at the top. And when I scroll the page, only the content will scroll and not the banners.
It's important that the banners are anchored to the sides of the content, so if the width of the browser shrinks then parts of the banners are going out of screen instead of the banners going over the main content.
you can use css. Position fixed actually. Here a fiddle
ex:
.banner {
position:fixed;
width: 100px;
height: 100px;
background-color: mediumaquamarine;
top: 40px;
}
Fix them with
position:fixed;
For aligning them on top you have to work with margin and float I guess.
As example left banner:
float:left;
Now you can easily work with margins to position them right.
I hope that's what you meant ?
Assuming banners have got a class="banner banner-right" or class="banner banner-left" go with
.banner {
position: fixed;
top: 0; /* or the distance from top page edge */
max-height: 100%;
}
.banner-right { right: 0; } /* or the distance from right page edge */
.banner-left { left: 0; } /* or the distance from left page edge */
and the banners will stick in the same position even if you scroll the page!
The max-height is there to prevent banners from being longer than the window itself, thus hiding content;
you might want to add overflow: auto; if the banner content gets too long
Another solution wold be to set
html, body { height: 100%; }
.content { max-height: 100%; overflow: auto; }
making the content element with a scrollbar, and leaving the banners in the same place
You can use position: fixed like:
position: fixed;
top: 0px;
left: 0px;

Are floated element really removed from normal html flow?

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.

Make an object stick to the top-right side of the page

I added the famous "Fork me on Github" ribbon to one of my projects. The tag looks like this:
<a href="https://github.com/Nurdok/htmlify">
<img style="position: absolute; top: 0; right: 0; border: 0;"
src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"
alt="Fork me on GitHub">
</a>
It looks great, but some of the divs on my webpage have minimum length, so when the window is small, one has to horizontally scroll the screen. When that happens, I want the "Fork me on Github" link to stick to the top-right side of the page, not the window. This is how it looks right now:
Scrolled all the way to the left:
Scrolled all the way to the right:
It seems that the ribbon is placed on the top-right side of the initial window, and stays static.
What I want is for it to be out of sight in the first case and top-right in the second case (when I scroll to the right).
Edit: Thanks for the quick answers, people. However, most of the answers made the ribbon scroll horizontally and vertically with the page. What I want is for it to be fixed on the top-right side of the page (not the browser view), and only be seen if I scroll to where its position is.
You can do a little trick and put your image into a div which has minimal-width.
<div style="position:relative;min-width:960px">
<img src="..." style="position: absolute;right:0;top:0" />
</div>
and put that div at the beginning of <body> section.
position:relative makes that all children of that elements that have position:absolute are positioned absolute according to that div, not whole page. When viewport is bigger than min-width, the div is the same width as the viewport. When the viewport is smaller, the div will have the min-width and the image stays at the corner of the div.
Two alternatives
Sticking to the Viewport: To stick it to the viewport you should position your element "fixed" instead of "absolute"
<img style="position: fixed; top: 0; right: 0; border: 0;"
Sticking to a Container: And if you want it to be sticked to a container (so youn dont see it when you browse left) use absolute but do that container position:relative so its containing block is targeted
If you dont want to see the image when scrolling left then use a explicit width for this container I am talking about
Here is a JSFiddle example.
I used a squared div instead of an image. CSS code as follows:
#container {
width: 700px;
height: 700px;
background: #55ff90;
position: relative;
}
#image {
width: 70px;
height: 60px;
background: #ffff90;
position: absolute;
top: 0px;
right: 0px;
}
In case it's supposed to stick to the right top on horizontal scroll only, you can't accomplish this with basic CSS. Your requirement is stick to the right top for horizontal scroll but not vertical scroll. The first part of the requirement can be accomplished using position: fixed; though this breaks the second part.
How about always sticking to the right top of the website using a relative float: Fiddle
<div id='container'>
<div id='sticky'>x</div>
</div>
#sticky {
width: 100px;
height: 100px;
background: red;
float: right;
}
#container {
width: 100%;
height: 200px;
background: blue;
}
You should use float:right, adjusting margin if you need, e.g.: margin-right: 5px. Cheers :)
If I understand what you want correctly, you'd like for the image to stick to the top corner of the window UNTIL the window gets to a certain size (horizontally) and then stick.
If so, here is a plausible solution:
body{
min-width:1000px; /* or whatever you need it to be */
}
#ribbon{
position:relative;
float:right;
}
DEMO FIDDLE
DEMO FULLSCREEN
You can also use a container div with min-width, your choice.
Change position: absolute; to position: fixed.
As side note, put the style on the a instead of the image and add some z-index to make sure it stays on top of everything else:
<a href="https://github.com/Nurdok/htmlify" style="position: fixed; top: 0; right: 0; border: 0; z-index: 999; display: block;">
<img src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"
alt="Fork me on GitHub">
</a>

CSS Fixing divs in place and hiding overflows

So, I've been stuck with this problem for a while now and I can't seem to find a solution.
I'm trying to make a layout consisting of (for now) 4 different content areas like so:
What I'm trying to do
I'm trying to do the following things:
Simple explanation: Content should be the only scrollable thing on the page, with the footer following right behind it if content fits on the page, fixed on the bottom otherwise.
Detailed explanation:
Fix banner and mainMenu so that they never move when page is scrolled.
Make the content scroll with a page so that:
If the content (and footer) fit on a page, no scroll is displayed.
If the scroll is needed, content goes behind the banner (not being shown) and does not appear again above it.
If the scroll is needed, content can scroll until the bottom line of it and the footer are in the visible area.
The footer should do two things:
If content and footer fit on the page, footer should stick at the bottom of the content
Otherwise, footer should be fixed on the bottom.
What I have tried
Fixing banner,mainMenu and the footer are fixed using position: fixed (and positioned accordingly). Parent div has overflow: hidden (which doesn't seem to work).
<div id='main'>
<div id='banner'>banner</div>
<div id='mainMenu'>mainMenu</div>
<div id='content'>.. some long content ..</div>
<div id='footer'>footer</div>
</div>
And
#main {
width: 960px;
height: auto;
margin: 40px auto;
overflow: hidden;
}
#main #banner {
width: 960px;
height: 100px;
position: fixed;
}
#main #mainMenu {
width: 230px;
height: auto;
display: inline;
float: left;
position: fixed;
top: 140px;
}
#main #content {
width: 720px;
height: auto;
display: inline;
float: right;
margin-top: 100px;
}
#main #footer {
width: 960px;
height: auto;
clear: both;
position: fixed;
bottom: 0;
}
The Problem
Footer does not follow content if it fits within the area
Content overflows on the top of banner
I would really prefer to do this just in CSS (if possible) and as compatible as possible (IE7+, all other major browsers).
It's really getting frustrating now.. Any help would be appreciated.
There is no conceivable way I can think of that would solve your problem by just using css. Once you have set your elements to a fixed position they are out of the flow and thus your other elements cannot conform around them.
However I did find a solution by doing two different things. For the header issue I simply added another fixed element above the main banner and set it to the color of the background. This way the content will scroll behind it and look as if it is hidden. For the footer, I set up some javascript using jQuery to see if the content overflows or not. If it does then the footer's position is set to fixed, otherwise the position is set to relative.
You can check it all out here in this demo: http://jsfiddle.net/mrQGh/4/
To test out the javascript simply delete the text until there is no more overflow and run it again.