I'm trying to create a layout for my site for a chat app. It needs to look something like this:
The point is that I don't want to body to scroll at all, just the chat area. My markup is as follows:
<div class="container">
<div class="header"></div>
<div class="chat"></div>
<div class="footer"></div>
</div>
Can anyone help?
Using fixed positioning, you can do something like this in your CSS.
Let's say the chat section division has "chat" as its ID and that the header and footer have an height of 200px.
.container .header {
position: fixed;
top:0;
left:0;
right:0;
height: 200px;
}
.container .chat {
position: fixed;
left:0;
right:0;
top:200px;
bottom:200px;
/* For Scrollbars */
overflow: auto;
}
.container .footer {
position: fixed;
bottom:0;
left:0;
right:0;
height: 200px;
}
Suggestion: you should use a "header" tag and a "footer" tag instead of "div" tags affected by css classes. Also, you should consider using absolute positioning instead of fixed positioning if you wrap the header, chat and footer inside a wrapper div so that the positions are relative to the wrapper and not the document. If you do so, don't forget to add a "position: relative" to the wrapper div.
The easiest thing to do is position everything absolutely, and have your main content area have an
header {
position: absolute;
height: 50px;
top: 0;
left: 0;
right: 0;
}
footer {
position: absolute;
height: 50px;
bottom: 0;
left: 0;
right: 0;
}
#main {
overflow: auto;
position: absolute;
top: 50px
left: 0;
right: 0;
bottom: 50px;
}
Like this: http://jsfiddle.net/cM277/
Related
I am wondering how I should organize things. I want my screen to be organized like this, and to be responsive:
So here is what I did:
.container-map {
position: relative;
}
.map-background {
z-index: 10;
width: 100%;
height: 50%;
position: absolute;
top: 0;
left: 0;
}
.map-filter {
z-index: 100;
margin-left: 10%;
margin-top: 5%;
position: absolute;
}
.map-search-results{
position: absolute;
margin-top: 50%;
width: 100%;
}
<div class="container-map">
<div class="map-background"></div>
<div class="map-filter"></div>
<div class="map-search-results"></div>
</div>
It is working for the map and the filter, but for the search-results section, this seems very dirty to me.
It seems like adding a div around map-background and map-filter should be the solution, but how do I make its position "more important" than the absolute positions of the two other divs?
It's not clear what you mean by "more important" but I think I know what you mean. One of the main issues is the fact that the top map background and map filter are not positioned together but independently, and then just aligned with absolute positioning. This makes the style brittle and prone to errors from changes - whether that be changes in code or change in viewport etc.
Instead this might be the kind of thing you are after:
.top-container{
height:50vh;
position:relative;
}
.map-background {
height: 100%;
background-color:yellow;
outline:2px solid yellow;
}
.map-filter {
position: absolute;
top:15%;
left:10%;
min-height:50px;
min-width:200px;
background-color:lightblue;
outline:2px solid lightblue;
}
.map-search-results{
height:50vh;
background-color:red;
outline:2px solid red;
}
<div class="container-map">
<div class="top-container">
<div class="map-background">
Background
</div>
<div class="map-filter">
Filter
</div>
</div>
<div class="map-search-results">
Search Results
</div>
</div>
Now the top section is held in it's own container and only the filter is positioned absolutely, but that's absolutely relative to the wrapping container. Remember that position: absolute will position an element relative to the nearest ancestor with position: absolute or position: relative.[1]
This means that the top section is effectively 'grouped' and if the container is repositioned, whether that be with new CSS rules, changes to the DOM, changes to the the outer dimensions etc etc, then all the children should also be naturally repositioned as well (barring any other complications).
I have also cleaned up the code somewhat.
Your height definitions weren't working because a percentage height needs a parent with absolute height to work. Instead I have defined the two main blocks as having height: 50vh but you can set it to whatever you need.
There's also no need for z-index in this case (and z-index with absolute positioning is a recipe for confusion). The map-filter is the only thing 'on top' of something else and that will appear on top anyway since it is absolutely positioned and the map-background is not.
So if you take out the code I created for demonstration this is the core CSS:
.top-container{
height:50vh;
position:relative;
}
.map-background {
height: 100%;
}
.map-filter {
position: absolute;
top:15%;
left:10%;
}
.map-search-results{
height:50vh;
}
You don't need position: absolute for any of these:
<div class="container-map">
<div class="map-background">
<div class="map-filter"></div>
</div>
<div class="map-search-results"></div>
</div>
.container-map {
width: 400px; /*set as much as you like */
}
.map-background , .map-search-results {
display: block;
height: 50%;
}
.map-background {
padding: 15px; /* set as much as you want - to affect the height/position of .map-filter */
}
.map-filter {
width: 200px;
height: 100%; /* top/bottom padding of [.map-background] will create the height differential here */
}
First thing you need to know is when dealing with absolute it's better to use left, right, top & bottom,
Second thing you need to know is the relatively positioned element should have width and height in order to place the absolute positioned item inside it
Consider reading this article to know what is the difference between this properties ( relative & absolute )https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/
I tried to make an example like the image in your question :
.container-map {
position: relative;
background:#000;
width:100vw;
height:100vh;
}
.map-background {
z-index: 10;
width: 100%;
height: 50%;
position: absolute;
top: 0;
left: 0;
background:#ff0000;
}
.map-filter {
z-index: 100;
left: 5%;
top: 5%;
width:130px;
height:40%;
background:orange;
position: absolute;
}
.map-search-results{
position: absolute;
top: 50%;
width: 100%;
height:50%;
background:#00ff00;
}
<div class="container-map">
<div class="map-background"></div>
<div class="map-filter"></div>
<div class="map-search-results"></div>
</div>
I have here plunkr which shows the issue I am facing.Basically I am trying to do create a scroll bar for middle div element, have all three div's fit inside the page.
This approach works fine if I use it as html page, but when I use it inside ui-view, the content inside '#content-scroll' does not show up, if I remove position:absolute from CSS, it displays, but doesn't display scroll bars.
Basically I just want to know how can I have the content display?
How can this be resolved?
http://plnkr.co/edit/ZkGafJ8cS6VG6lfS9SJY?p=preview
#content-scroll {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
overflow: auto;}
Here is the example of same page that works inside html
http://jsfiddle.net/sA5fD/8/
absolute positioned elements behave according to it's parents.
If we give bottom:0; top:0; to an absolutely positioned element it will select the bottom and top of it's parent. So:
Your previous example it is working since the parent have 100% height.
Inside ui-view it will not work it doesn't have an explicit height to it.
There's more than one solution to this problem. For example I applied a min-height to the parent #content and changed it's display property. I suppose you have #content only used for this scrollbar content. Othewise you have to apply special class to the element.
#content {
display:block;
height:100%;
background:#8f8;
min-height: 90vh;
}
Here's a plunker: http://plnkr.co/edit/5vl0AN3WwtUJmDiNtNdL?p=preview
I guess This would solve your problem.
html { height: 100%; }
body {
padding:0 0;
margin:0 0;
height: 100%;
}
#main {
display:table;
height:100%;
width:100%;
}
#header, #footer {
display:table-row;
background:#88f;
left:0;
right:0;
}
#content {
display:table-cell;
height:100vh;
background:#8f8;
}
#content-scroll-wrap {
position: relative;
height: 100%;
}
#content-scroll {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
overflow: auto;
}
I have a question regarding the absolute position div.
I have something like
<div id='body-container'>
<div id='content-wrapper'>
contents....
</div>
</div>
I need to make my body-wrapper with absolute position because of other issues.
my css
#body-container{
position: absolute;
right: 0;
left: 0;
height: 100%;
overflow-y: hidden;
}
I have encounter a problem. Inside my content-wrapper, I have dynamic contents that will be added inside. It will create a unwanted scrollbar and the div become scrollable in that div. Is there anyway to kill the scrolling behavior? Thanks so much.
Remove the height property from your #body-container, and it will fit the content's height. No scrollbar.
http://jsfiddle.net/QeFe5/
EDIT
If you need the wrapper to be full height, then don't forget to set the top: 0 property, so the body won't create a scroll:
#body-container{
position: absolute;
right: 0;
left: 0;
top: 0;
background: silver;
height: 100%;
overflow: hidden;
}
Updated: http://jsfiddle.net/QeFe5/1/
This might work...
#body-container{
position: relative;
}
#content-wrapper{
position: absolute;
right: 0;
left: 0;
height: 100px; /* Need to put an exact height */
overflow: hidden;
}
If you need the content-wrapper to extend the full page you can use top and bottom with absolute positioning and make sure that your body-container (and it's enclosing containers) have height 100%.
#body-container{
position: relative;
height: 100%;
}
#content-wrapper{
position: absolute;
top: 0;
bottom:0;
right: 0;
left: 0;
}
not actually sure this is only a chrome problem, but i've got this entire page set up with absolutely positioned elements within a container that's position relatively. normally, i wouldn't do this, but we needed rotating images, so we couldn't use the css background property.
anyways, for some reason, when i position the footer absolutely with a position of bottom: 0; right: 0; it doesn't show up.
HTML:
<div id="wrapper">
<div id="content">
</div>
<div id="footer">
<ul>
</ul>
</div>
</div>
CSS:
#wrapper {
position: relative;
margin: 0 auto;
width: 940px;
}
#content {
position: absolute;
top: 0;
left: 0;
height: 350px;
}
#footer {
position: absolute;
bottom: 0;
right: 0;
z-index: 10;
}
EDIT: We cannot set the height of the wrapper, as this is going to be a wordpress site, and the user could put in a ton of text. we can, however, set the height of footer.
If you're unable to set a height then just remove the footer from the wrapper so it has no parent. Absolute positioning height is determined from its parent. You parent "wrapper" has no height; therefore, it won't float to the bottom.
Luckily body will go to the bottom.
here's the code in jsFiddles
#wrapper {
position: relative;
margin: 0 auto;
width: 940px;
}
#content {
background:#ccc;
position: absolute;
top: 0;
left: 0;
height: 350px;
}
#footer {
position: absolute;
bottom: 0;
right: 0;
z-index: 10;
background:papayawhip;
}
<div id="wrapper">
<div id="content">
sdfasdf
</div>
</div>
<div id="footer">
<ul>
<li>aslfaskdjf</li>
</ul>
</div>
The height of #wrapper is 0px so the #footer does show up - but its 0px height .... see here you will notice that the #footer is a red border on the right
If you give the #wrapper a height and apply a height to #footer you can see it or to make the footer the same height as its contents remove its height property
Actually it above code true but, your #wrapper has relative position and your #footer inside #wrapper. the position of #footer is aligned according to wrapper.If you give height to wrapper you can see it ;See below;
HTML:
<div id="wrapper">
<div id="content">
</div>
<div id="footer">
<ul>
your_content
</ul>
</div>
</div>
CSS:
#wrapper {
position: relative;
margin: 0 auto;
height: 200px;
width: 940px;
}
#content {
position: absolute;
top: 0;
left: 0;
height: 350px;
}
#footer {
position: absolute;
bottom: 0;
right: 0;
z-index: 10;
}
You can see example here http://jsfiddle.net/8DZ5R/
Another way to solve this, simply change "#footer" position "absolute" to "static".You dont need to change wrapper height: You can see here http://jsfiddle.net/8DZ5R/1/
My code looks like this:
css:
.top {
position: absolute;
top:0;
left:0;
height: 1600px;
width: 100%;
z-index: -100;
}
.bar {
position: relative;
z-index: -200;
width: 100%
height: 100px;
}
.inner-bar {
position: relative;
z-index: 100;
width: 100%
height: 50px;
}
html:
<body>
<div class="top">some content</div>
<div class="bar">
<div class="inner-bar">some content</div>
<div>
</body>
As you can see I am trying to make inner-bar appear in front but this does not work. Once I set bar to be behind of everything ( which works) this also sets inner-bar to be behind of everything no mater what styling I do for inner-bar. My layout requires that inner-bar must be a child of bar. So is there a solution and what it is?
To make it clear my objective is to make bar behind top (content in top appears on bar) and to make top behind inner-bar ( content in top is hidden if it overlaps inner-bar so that the links in inner-bar are active).
first off there is an error in the html you posted:
<body>
<div class="top">some content</div>
<div class="bar">
<div class="inner-bar">some content</div>
</div>
</body>
you didn't close the last div :)
as for the rest:
here you go good sir! http://jsfiddle.net/8AJnD/31/
.top {
position: absolute;
top:0;
left:0;
height: 1600px;
width: 100%;
top:0;left:0;z-index:0;
}
.bar {
position: absolute;z-index:-1;
width: 100%;
height: 100px;top:0;left:0
}
.inner-bar {
position: absolute;
z-index:-2;
width: 100%
height: 50px;top:0;left:0
}
Use absolute instead of relative and make the parent relative to be able to position the elements however you want them to be positioned
Negative z-index values have strange behavior. I don't believe that they work in "layers" like you would expect, rather they all wind up on the same "layer". Try using positive z-index values instead:
.top {
position: absolute;
top:0;
left:0;
height: 1600px;
width: 100%;
z-index: 1;
}
.bar {
position: relative;
z-index: 2;
width: 100%
height: 100px;
}
.inner-bar {
position: relative;
z-index: 3;
width: 100%
height: 50px;
}