I have two divs on my page, the first one is a small one used to navigate, and the second one is the main content. The desired result is that they will line up side by side.
Because the main content takes up a lot of horizontal space, I want it to fill 75% of it's container, so that it will get larger or smaller depending on the screen width. Obviously this is a simple width:75% in CSS but when I do this, the main content div refuses to align to the right of the navigation div. However, if the div at 75% was taking up 400px and I changed the width:75% to width:450px then the divs align exactly how I want them to, despite being the same size. When I look at the box model for the main content div I can see the content is 75% as it is supposed to be, but there is a margin on the right of it which takes up the rest of the container. I have tried setting it to margin-right: 0px; but it doesn't change at all and the margin is still there.
The only CSS I have defined for this is:
#mainArea_pnlErrorLog {
width: 50%;
position: relative;
margin-right:0px;
}
Setting position:absolute; fixes the problem however it creates a new one, because the container will not vertically expand to fit the main content and it goes outside of the container
Here's a screenshot of the web page to better illustrate the problem:
!1
You problem is that, in your HTML code, you've got two nested <div>s styled with display: inline-block:
<div id="mainArea_pnlControls">
<div id="mainArea_pnlErrorLog">
...lots of content here...
</div>
</div>
#wrapper {
width: 420px; /* fix surrounding element width */
padding: 10px;
background: #ddd; /* gray */
}
#controlsList {
display:inline-block;
width:145px;
height:50px;
background: #faa; /* red */
}
#mainArea_pnlControls {
display:inline-block;
vertical-align:top;
background: #afa; /* green */
}
#mainArea_pnlErrorLog {
width: 50%; /* set to 200px to see intended rendering */
position: relative;
background: #aaf; /* blue */
}
<div id="wrapper">
<div id="controlsList">
SIDEBAR
</div>
<div id="mainArea_pnlControls">
<div id="mainArea_pnlErrorLog">
CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT CONTENT
</div>
</div>
</div>
The outer one, mainArea_pnlControls, has no explicit width defined in the CSS, so it expands as necessary to accommodate its content. When you give the inner one a width: 50%, you tell the layout engine to make the inner <div> half as wide as the outer one.
This is kind of confusing for the layout engine. On one hand, by using display: inline-block for the outer div, and not specifying an explicit height, you've implicitly told the browser to make it as wide as the inner div inside it. On the other hand, by specifying width: 50% for the inner div, you told the browser to make it only half as wide as the outer div.
Obviously, these requirement conflict (at least unless both divs have zero width), so one of them has to fail.
Firefox, at least, chooses to fail the implicit requirement of equal width, and instead to make the inner div half as wide as the outer one, to honor the explicit width: 50% requirement. But that still leaves open the question of exactly how wide should the outer div be in the first place. Since it's an inline block, it shouldn't be wider than the block enclosing it (unless it's forced to overflow its container), but the only lower bound on its width is that it needs to accommodate its content. But in this case, the content will always shrink to be narrower than the outer block, so... there's just no meaningful lower bound available there.
Apparently, what Firefox ends up doing is (something like) this:
Figuring out how wide the content of the inner div would be, if it could expand freely to be as wide as it wants.
Set the width of the outer div to be the minimum of the width the content "wants" and the width of the enclosing block.
Set the width of the inner div to be 50% of that.
Render the content of the inner div at that width (which guarantees that it will either wrap or overflow).
You can see the effect clearly if you make the content narrower, so that it would actually fit cleanly on one line: it will still wrap, since the inner div ends up being exactly half as wide as it needs to be to accommodate it, as in the snippet below:
#outer {
display:inline-block;
background: #afa; /* green */
}
#inner {
width: 50%;
background: #aaf; /* blue */
}
<div id="outer">
<div id="inner">
BLAH BLAH BLAH BLAH BLAH BLAH
</div>
</div>
Anyway, the solution is simple: don't do that. Either get rid of one of the nested divs entirely, or just set the width: attribute on the outer one, not the inner one.
Related
I have a set of forms on my webpage, they are dynamically generated and the width ends up being wider than the screen (this isn't a problem as it is designed to replace an exel spreadsheet with the same issue).
<body>
<div class='indented-form'>
<table>
... table elements here
</table>
</div>
</body>
css
body {
margin-right: 0;
padding: 0;
}
.indented-form {
margin: 10px;
}
I have "margin:10px" on the div that contains the table.
This adds a margin at the left of the page as I expect, but scrolling right, the table reaches the edge without any margin. (The div seems to be taking its width from the body, which in turn seems to be based on browser width.)
How do I make it include a margin at the right, even though the table is wider than the browser?
If you make your div an inline-block element, it will stretch to fit the table inside it:
.indented-form {
display:inline-block;
padding: 10px;
}
Example
In a page I work on I have a div that contains an animation with changing images (with CSS) and below it is a wrapper div for the content, like that:
<div id="animation">
<img ...>
...
<img ...>
</div>
<div id="content">
Some content
</div>
I've tried different methods: floats, clear, position, but I cannot make the content div to stay where it should, below the animation div - it overlaps it. The only solutions I found to partially work are to give the first div the height of the images (they all have equal width and height), but when I do that it breaks on different resolutions, or to give the images height of 100% and apply the above, but then the images look incredibly ugly on different resolutions.
How can I achieve my goal, preferably using CSS only?
Edit: JSFiddle
Edit 2: I used this tutorial for the changing images.
the problem is that you have all the content in the top div as position:absolute. That way, the top div doesn't know how high it needs to be (i.e. it will be 0px high).
So the solution is to have one img not positioned; then the div is as high as this img and the content div will move down below it.
#cf4a img {
position: absolute;
left:0; top:0;
width: 100%;
}
#cf4a img:first-child { /* one non-positioned child */
position: static;
}
Updated fiddle
I read this and wrote this code:
.wholePageDivForCentering
{
width: 80%;
white-space: nowrap;
display:inline-block;
margin: 0 auto;
border: 4px solid red;
/* other stuff I tried........*/
/*padding-left: 10%;*/
/*margin-left: 10%;*/
/*padding-right: 10%;*/
/*margin-right: 10%;*/
}
<body>
<div class="wholePageDivForCentering">
<h2>Hello from the page</h2>
<!-- stuff such as 2 nested divs contained text labels, and a small image -->
</div>
</body>
I put a solid-red, 4-pixel border around my outermost div for a reason.
I wanted to see if that thick red border rectangle around that outermost div would
horizontally center itself on the page.
IT DID NOT.
EDIT: My outermost div stays on the left when the browser is maximized.
You can see I tried more than one thing. In my opinion, I should be able to:
tell this outermost div, the one with the thick red border, to take up 80% of
the browser window
then using the advice from the above SO post (again, here) -- get this
outermost div always taking up 80% of the browser window but HORIZONTALLY CENTERED
on the browser window.
Me personally? I think my margin-left = 10%, margin-right=10% should do it but no.
To see what I want -- open Craigslist at http://sfbay.craigslist.org/
and maximize the browser window (the main page, not a nested page, of the CL site).
The horizontal width of the whitespace on either side of the Craiglist main
page is the same when you maximize the browser. The main page's columns are
horizontally centered.
Because (perhaps) that page has a centered div that surrounds everything
else on the main page.
How do I do it?
Take out display:inline-block and it should work.
Here is a Jsfiddle (click Run): http://jsfiddle.net/zKm6b/
I also recommend using an id instead of a class for that div. I hope that helps!
Why are you settings display: inline-block. It will work if display is block (which a div is by default, so you can solve the problem by removing display).
I have a centered wrapper with following CSS:
div.wrapper {
width: 1170px;
padding-left:30px;
margin-top: 80px;
margin-bottom:20px;
margin-left: auto;
margin-right: auto;
position:relative;
background-color:black; }
inside i have a div with following css:
position:absolute;
top:-26px;
left:517px;
height:63px;
z-index:3;
inside of this div is an image which has 759px width, that makes the wrapper grow larger and makes the browser show a v-scrollbar on lower display resolutions.
what i want is to make the image go outside the wrapper but prevent the browser from showing the scrollbar, so that the right side of the image is only shown if your browser window is large enough and the wrapper keeps its 1200px width. i can't make it a background image because it goes over some of the other content.
something that is compatible with >= IE7 would be nice.
i uploaded a pic of the page to show what i mean:
http://img153.imageshack.us/img153/6070/hpx.jpg
the blue box is the wrapper, it has 1200px width and is ALWAYS centered in the window (unless then window is smaller than 1200px, then it scrolls)
the red box is the image (the green bar is not part of it)
You can set overflow: hidden to the wrapper so that content that exceeds the dimensions of wrapper will not be shown.
see overflow
You are looking for #your_div { overflow: hidden; }, if you want your content to be hidden. Or #your_div { overflow: visible; } if you want your content visible outside the div.
The only method that springs to mind given your requirements is to move the inner element out of that wrapper div and position it in relation to the entire window:
<body>
<div class="abs">the div with the image</div>
<div class="wrapper">the wrapper div</div>
</body>
Unfortunately, this probably means you can't position it very well. You may need to use Javascript to get the width/height of the page and/or the position of the wrapper div, and calculate the offset accordingly. (You'll find questions on Stack Overflow for these bits.)
The problem lies with the img being inline. Not tested but you should 'display:block' the image and then float it or absolutely position it.
I have a div with two nested divs inside, the (float:left) one is the menu bar, and the right (float:right) should display whatever content the page has, it works fine when the window is at a maximum, but when i resize it the content is collapsed until it can no longer has any space, at which it is forced to be displayed BELOW the left menu bar, how can I make the width fixed so that the user may scroll when resized?
(css width didn't work, i alternated between floating the right content and not), here is the code:
<div style="width:100%">
<div style="float:left; background:#f5f5f5; border-right:1px solid black; height:170%; width:120px;"></div>
<div style="margin-right:2px;margin-top:15px; margin-bottom:5px; width:100%; border:1px solid #f5f5f5"></div>
</div>
I only need to have this working on Interner Explorer for now.
This should do it (container is the parent div containing that 2 divs):
.container {
width: 1024px;
display: block;
}
You may want to set a width on the containing div and set your overflow property
#containing_div {
width: 200px;
overflow: auto;
}
Also use the min-width property on the page if that makes sense, however that CSS property doesn't really work with IE6, this is usually what I do in that situation (supporting Firefox, IE7, IE6, etc)
#container {
min-width: 1000px;
_width: 1000px; /* This property is only read by IE6, which gives a fixed width */
}
Well, putting a width or min-width property is the way to go.
Now, without an example, or a link of the actual page, it's a bit tricky to answer.
Simply don't make the right div floating. Menu is already floating left of any other content. Just set a left-margin for the right div so the content in that div won't be wrapped around the floating div.
if the two divs are taking up 100% of the available width, could try to use percentage width and display: inline with a further div with a fixed min-width/width (boo IE) inside where required.
this is rather difficult without some HTML to go on
Your containing div should have a width wide enough to contain both inner div's
So if your two inner div's are 300px each and assuming you have no margin/padding on them then you should set the outer div to be 600px;
I'm a bit confused:
Fixed width means the width of a node will not change. Never.
You say you want to scroll when the screen gets too small for your content, so I think you mean the exact oposite of fixed width.
If my assumption is right, you could as mentioned before go for the percentual widths.
Watch out width the suggested "min-width" solution because it is not supported all that well.
<div id="container" style="width:100%">
<div id="primaryNav" style="float:left; width:150px; background-color: Orange">someNav</div>
<div id="content" style="margin-left: 10px; background-color: Red; overflow: auto;">
loadsOfSuperInterestingContentI'mSuperSerious<br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
Seriously
</div>
</div>
This should be pretty cross browser