CSS Layout Question - html

I'm having trouble defining the CSS styles necessary in order to achieve the following layout:
Ideally, I'd like to have the left two divs be of width 200px. div#image will always have a height of 100px. However, I would like div#sidebar and div#mainContent to have lower borders which lie on the same horizontal level. Their sizes should be large enough to contain their respective content, which is determined when the page is being served. Hence, the one with more content will cause the other div to extend down to the same distance.
The problem is that with absolute positioning, the div#sidebar and div#mainContent elements don't seem to acknowledge the flow of their child elements. Perhaps I don't fully understand absolute positioning. Also, it seems like bad form to use Javascript in order to set the inline style of elements on the page. Is there a way of accomplishing this solely with CSS?
I've also tried floating the div#image and div#sidebar, and setting a margin-left property on div#mainContent, but wasn't able to get it to work...
Any help will be much appreciated!
Andrew

demo: http://jsfiddle.net/TRa35/
html
<div id="wrapper">
<div id="div-image">div image</div>
<div id="div-maincontent">
<div id="div-sidebar">
div sidebar
</div>
div maincontent
<button>click to add content</button>
<br />
<span></span>
</div>
</div>
css
html, body {
margin:0;
padding:0;
}
#wrapper {
position:relative;
}
#div-image {
position:absolute;
left:0;
top:0;
width:200px;
height:100px;
background-color:#cef;
}
#div-sidebar {
position:absolute;
left:-200px;
top:100px;
bottom:0;
width:200px;
background-color:#efc;
}
#div-maincontent {
position:absolute;
left:200px;
right:0;
top:0;
background-color:#fce;
min-height:300px;
}

This almost solves the problem. In fact, to be more precise, it does solve the problem in Google Chrome and Firefox, but IE 9 seems to have problems recognizing the height of cells and/or rows. I can't really mark it as an answer because of this, but I'm just posting it in case anyone can use something from it. It uses an html table element.
CSS:
#mainContentCell
{
background-color: Blue;
}
#imageCell
{
width: 200px;
height: 100px;
background-color: Yellow;
}
#sidebarCell
{
background-color: Red;
}
HTML:
<table id="layoutTable" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td id="imageCell">
Image
</td>
<td id="mainContentCell" rowspan="2">
Main Content
</td>
</tr>
<tr>
<td id="sidebarCell">
Sidebar
</td>
</tr>
</table>
Also, if anyone can make this work in IE 9, I'll gladly mark their response as the answer.

Note: This has been abandoned as "unsolvable", a reasonable answer is given below, but the original problem question remains without a definite solution.
JS Fiddle: http://jsfiddle.net/steve/gHrce/
Does almost everything, may help put you on the right track.
CSS:
#img {
background:green;
float:left;
height:100px;
width:200px;
}
#sidebar {
background:red;
clear:both;
float:left;
width:200px;
}
#mainContent {
background:yellow;
margin-left:200px;
}
HTML:
<div id='img'>
IMG
</div>
<div id='sidebar'>
SIDEBAR
<br />
<br />
<br />
</div>
<div id='mainContent'>
MAIN CONTENT
<br style='clear:both' />
</div>
That extra <br /> at the bottom forces the height of the main content to whatever the sidebar height is.
The <br /> tags in the sidebar are just to provide some extra height for demonstration purposes.
Of course, you can add Javascript pretty easily to expand the sidebar's height, but it smells strongly of hack:
if($('mainContent').offsetHeight > $('sidebar').offsetHeight + 100) {
$('sidebar').style.height = $('mainContent').offsetHeight - 100 + 'px';
}

#Andrew
for my suggestion, you should use 960 grid system CSS. pls check on this link http://960.gs/
I think you may more easier to develop and maintain.

Whew! After searching tons of articles. I hope this could help you.
<style type="text/css">
#wrapper{
background:blue;
position:relative;
min-height:400px;
}
#img{
background:green;
position:absolute;
left:0;
height:100px;
width:200px;
}
#some-panel{
background:orange;
position:absolute;
width:200px;
top:100px;
left:0;
bottom:0;
}
#main-content{
background:yellow;
margin-left:200px;
}
</style>
<div id="wrapper">
<div id="img">img img</div>
<div id="some-panel">some panel</div>
<div id="main-content">
main content main
</div>
</div>

Related

Featured Article Container with Image overlayed by Post info

What I am trying to achieve seems relatively simple, but I can't seem to get it to work.
I want to have my article previews on my website appear in tiled form.
The tiles, for the sake of the argument would be a fixed height and width. Lets say 300px by 300px.
I want then for the title of the article and perhaps even a short excerpt to appear, overlaying the image. Kind of like what theverge.com have.
What I need help with is that Im just trying to do a proof of concept mock up. I can do the specific styling fine myself but its literally just the structure I cant seem to figure out.
I cant seem to get the h1 to overlay the img.
I've tried creating a parent container div, and then containing both elements within separate div containers and giving the container with the h1 or "post info" absolute positioning.
But It never seems to work out quite right.
HTML:
<div class="container">
<div class="feat-img">
<img src="www.sample.com"/>
</div>
<div class="post-info">
<h1>Post Title</h1>
</div>
</div>
CSS:
.container: {width: 300px; height:300px;float:left;}
.feat-img img: {width:300px; height:300px; float:left;}
.post-info: {position:absolute;bottom:0px;}
Ok so I know there is a lot wrong with that style but I just did it off the top of my head there. It has the general jist of my train of thought.
Any help would be greatly appreciated as I havent found anything (Probably becuase I dont really know what Im searching for)
First, you need to know how an absolute div relates to a relative one.
Add
.feat-img {
position:relative;
height:300px;
width:300px;
}
to your CSS,
and place the .post-info div inside the .feat-img div:
<div class="feat-img">
<div class="post-info">
<h1>Post Title</h1>
</div>
<img src="image.jpg"/>
</div>
apply this CSS:
.post-info {
position:absolute;
bottom:0px; /* or whatever position */
left:0px; /* or whatever position */
}
Please have a look at this jsFiddle for a quick mockup: http://jsfiddle.net/ZJT6f/
Cheers,
Jeroen
look on this:
demo
html code:
<div class="container">
<div class="feat-img"><img src="http://lorempixel.com/300/300/"/></div>
<div class="post-info"><h1>Post Title</h1></div>
</div>
css code:
*{margin:0; padding:0}
.container: {width: 300px; height:300px; display:block; position: relative;}
.feat-img img: {width:300px; height:300px; position:absolute; top:0; left:0; display:block;}
.post-info{position:absolute; top:130px; left:0; display:block; width:300px; height:300px; text-align: center; color:#fff;}

Three floated elements with one that shrinks to available space

I'm fairly confident this is one of those things that has been discussed endlessly out there in the internet, but I can't find a solution.
I need to float 2 divs on the same line as a paragraph. Both of the divs have variable width and I need the paragraph to shrink into the available space and wrap its contents so that none of the elements themselves wrap off the line.
I've set up a JSFiddle
HTML here:
<div class="icon"></div>
<p>This is a really long line of text that will need to wrap</p>
<div class="count"></div>
CSS here:
.icon {float:left; width:50px; height:50px; background-color:#4d4d4d; margin-right:10px}
p {margin:0; overflow:auto; display:inline-block}
.count {float:right; width:250px; height:50px; background-color:#ff0000; margin-left:10px}
I know that I can use Javascript to achieve this, but I'd much rather find a pure CSS solution.
Thanks.
Floats do not shrink or expand to fit the available space. A floated item always uses the required space of any children.
That is what flexbox was invented for.
.flex-column-container {
flex: 1 auto 1;
}
Alternatively you could use a table layout.
Please check this fiddle if this is what you are looking for http://jsfiddle.net/Mohinder/dEwuU/
here is HTML
<div class="main">
<div class="w_200"></div>
<div class="middle"></div>
<div class="w_200"></div>
</div>
here is css
body,div{
margin:0px;
padding:0px;
}
.main {
width:100%;
text-align:center;
min-width:404px;
background:black;
float:left;
}
.w_200 {
width:200px;
background:red;
height:100px;
float:left;
}
.middle {
height:100px;
background:red;
float:left;
width: 49.2%;
width: -webkit-calc(100% - 404px);
width: -moz-calc(100% - 404px);
width: calc(100% - 404px);
margin:0px 2px;
}
Got it. If I change the paragraph to display:block instead of inline-block and change the order of the elements so that the paragraph is the last in the markup it works perfectly.

css middle div scrolling and 100% height

I'm sure this has been asked before but I couldn't find it.
Please see http://jsfiddle.net/Bw5j4/1/
I want to make #room div to fit 100% between #top and #commands even if there is no content in it.
And, if the content overlaps (as in current example) I want to fit it within borders of #room with scroll.
I need to keep #commands stuck to the bottom of page. I've tried height, max-height but it didn't work.
This should get you started to lock in the middle section
.room {
background-color:#fff;
border:1px solid #d8d8d8;
overflow:auto;
position:fixed;
top:80px;
bottom:150px;
left:0;
right:0;
}
You'll need to use JavaScript for this, unless the page is guaranteed to always be the same size and can't be resized. If that is the case, you can just explicitly set the height on .room. Otherwise:
function setRoomHeight() {
$(".room").height(
document.documentElement.clientHeight
- $(".top").height()
- $(".commands").height()
- 20);
}
$(setRoomHeight);
$(window).resize(setRoomHeight);
http://jsfiddle.net/gilly3/5TzFm/
(is jQuery ok, or would you prefer a non-jQuery example?)
This is what lazy* developers use tables for. It's very easy to get these fluid layouts like this. Without tables, it's more difficult.
I think perhaps this is something like what you want: http://jsfiddle.net/thomas4g/6u7ry/13/
<div id="wrapper">
<div id="top">Top Stuff</div>
<div id="content">
My Epic Content
</div>
<div id="bottom">Bottom Stuff</div>
</div>
#wrapper
{
height:700px;
background-color:teal;
position:relative;
padding-top:50px;
padding-bottom:50px;
}
#content {
height:700px;
background-color:red;
overflow:auto;
}
#top {
position:absolute;
top:0;
left:0;
height:50px;
width:100%;
background-color:yellow;
}
#bottom {
position:absolute;
bottom:0;
left:0;
height:50px;
width:100%;
background-color:yellow;
}
*granted, just because you use tables doesn't mean you're lazy. It's just often true. No offense intended.

Vertical alignment and spacing for two divs

I'm stuck with a vertical align issue. I have 2 divs. First one has auto height (depends on the browser size), the other one has fixed height and is positioned at the bottom of page. Also, the second div needs margin.
An exact example of what I want to do:
http://img199.imageshack.us/img199/9569/79106387.jpg
I tried:
<html>
<body>
<style>
* { margin: 0; padding: 0; }
body { background: #a7daf6; }
</style>
<div style="width:200px; height:100%; position:absolute; background:#000; opacity:0.6"> </div>
<div style="width:200px; height:40px; position:absolute; background:#eee; bottom:0; opacity:0.6"> </div>
</body>
</html>
but I can't give margin to second div. Any ideas?
try to add this for first div:
<div style="width:200px; position:absolute; top:0px; bottom: 42px; background:#000; opacity:0.6"> </div>
and remove margin-top from second one
If I understand correctly, you can simply apply to the first <div> this style: top:-42px.
If you need content inside the <div>, you can add another <div> with padding-top: 42px.
Like this:
Live Demo
<div style="width:200px; height:100%; position:absolute; background:#000; opacity:0.6; top:-42px">
<div style="padding-top:42px; color:#fff">hello</div>
</div>
Giving any element an absolute position will remove it from the flow of the document.
Not matter what the margin is other elements will not be affected.

relative positioning items with overlapping z-indices

I have a curious issue that's proving difficult. I have five divs stacked vertically in a table cell. I'd like the even-numbered divs to fold behind the middle div but in front of the others with z-indexing so that the stack appears as 1-3-5 by default (and all touching, no whitespace), with the even divs' placement and movement not affecting those of the odd-numbered divs. However, if I put the even divs into the middle div, the z-indexing of the evens is completely ignored and they appear on top of the middle guy instead of under it.
I need everything here positioned relative to the containing table cell. Absolute positioning sends any one of these elements travelling to places they shouldn't go. The cell alignment specs are needed as well. Ultimately I want to be able to expand out and contract in the even items with a mouseover (javascript) without moving the odd ones.
<style type="text/css">
.oddStationary {
position:relative;
width:100px;
height:120px;
z-index:1;
border:solid red;
}
.evenMover {
position:relative;
width:100px;
height:120px;
z-index:2;
border:solid yellow;
}
.middleStationary {
position:relative;
height:300px;
width:200px;
z-index:3;
border:solid orange;
background-color:pink;
}
</style>
<table width="600">
<tr>
<td valign="top" align="center">
<div class="oddStationary"></div>
<div class="evenMover"></div>
<div class="middleStationary"></div>
<div class="evenMover"></div>
<div class="oddStationary"></div>
</td>
</tr>
</table>
You need to establish a common reference point for your absolute positioning. By default absolutes go up the HTML tree until they encounter the first "position:relative", which becomes the new origin. If you don't have one defined, the "origin" becomes the BODY tag. You can either set TD as "position:relative" or wrap the whole thing in a DIV that has "position:relative". That's a good start.
set evenMover position to absolute and then put the evenmover tag inside the div tag of those divs where u want it.
<td valign="top" align="center">
<div class="oddStationary">
<div class="evenMover"></div></div>
<div class="middleStationary">
<div class="evenMover"></div></div>
<div class="oddStationary"></div>
</td>
I didn't get your question properly:
while this is the answer to your question whatever I understand from this article:
May be it's helpful:
<style type="text/css">
.oddStationary {
position:relative;
width:100px;
height:120px;
z-index:1;
border:solid red;
}
.evenMover {
position:absolute;
width:100px;
height:120px;
z-index:2;
border:solid yellow;
}
.middleStationary {
position:relative;
height:300px;
width:200px;
z-index:3;
border:solid orange;
background-color:pink;
}
</style>
<table width="600">
<tr>
<td valign="top" align="center">
<div class="oddStationary">
<div class="evenMover"></div></div>
<div class="middleStationary">
<div class="evenMover"></div></div>
<div class="oddStationary"></div>
</td>
</tr>
</table>