Div goes under Div - css

I have a layout with two divs on the same line. I need one div floated left and the other floated right so that the divs will stay on their respective sides no matter the browser size and the div on the right won't fall below the left floated div when the browser size is smaller than the two divs.
I need the browser to cut off the 2nd div after the 2 divs touch each other when the browser shrinks.
I had a picture to illustrate my question but I don't have enough reputation points to post it.

Do you really need to float the divs? If not you can always create a wrapper for the divs with a minimum width set to allow the divs and a position of relative or absolute to stay next to each other,
#wrapper {
min-width: 250px;
position: relative
}
then set the inner divs display property to "inline-block".
.blocks {
display: inline-block;
position: absolute;
}
All together, this will create a "safety bubble" in which your divs can rest side by side without jumping to the next line, even after window resizing.
Check it out here.
EDIT
After a couple of trial and errors I believe we have an answer.
Javascript.
So in interest of time, I will post the code on jsFiddle here. Breifly, what I added to the previous code was a script that ( on window.onload) you get the id's of both inner divs. You then create two objects to hold the position of their facing borders which are then compared to see if the second div ( one on the right ) has crossed into the first. The numbers in the div are there as a place marker to show that the div does not slide onto/under the other.
*PS the 200px is just a demo number, they can be changed)

I'm assuming that what you mean by "cut off" is that you want the left div to appear "in front of" the right div. To do this, you want to give the left div the css
{
position:absolute;
left:0px;
z-index:2;
display:inline-block;
width:[number]px;
height:[number]px;
}
and the right div
{
position:absolute;
right:0px;
z-index:1;
display:inline-block;
width:[number]px;
height:[number]px;
}
and to their shared parent div add the css "position:relative;".
Alternately, giving both divs "position:fixed;" might work, that makes them relative to the browser winder instead of the parent div, though that will give you very different outcomes when people try to scroll. You'll also need to give the left div a background that isn't transparent, or they'll just overlap. Also note that "position:absolute;" puts the divs 'outside the flow' as it were, in the sense that they will also overlap anything else you put in, and your design has to account for this.
Working example http://jsfiddle.net/RdX5P/
If by "the browser needs to cut off the second div" you mean you want the second div to just disappear when the window gets too small, just float the two divs and put them in a container div with set height and with "overflow:hidden;"

Related

Keep element inside visible part of window

I have positioned a sidebar element at the right side of the visible part of the screen (this part already works!). The sidebar consists of several DIVs. A few of them I don't care about, but the last (lowest) one should not "disappear" when the user scrolls down the page. And here is where I'm stuck. In a way, the top position should not be < 0 of the visible top of the browser window. Is that even possible with CSS and the better browsers?
Here is my definition of the sidebar:
div#sidebar{font-size:95%; float: right;width: 150px; margin-top:20px;}
div#sidebar div{padding: 5px 0;margin-bottom: 5px}
Here is the element I would like to keep inside the visible part of the screen:
div#navtop{background:#B3B3E3 url(/css/blue.jpg); margin-bottom:0px;}
div#navsoc{background:#B3B3E3 url(/css/blue.jpg); margin-bottom:0px; top:!important 0px;}
The second element, "navsoc", should remain in the visible part. But it moves exactly like "navtop" and the others I have defined there.
<div id="sidebar">
<div id="navsoc">
keep me inside the window!
</div>
</div>
I think you need
position:fixed;
So the element will be positioned relatively to the window, so if you scroll it is always visible.
Demo: http://jsfiddle.net/mJubt/
Moreover, if you want to set !important to a value, use top:0!important instead of top:!important 0.
And when you have 0px you don't have to write the unit, you can use just 0.
if you use top in CSS you should make sure that the element's position is not static (default) but absolute, relative or fixed. So, top:0 in this case is not working. And if you do change the position to either of those it would behave in different ways:
if it's fixed the element would be position relative to the window
if it's relative or absolute it would be position zero pixels from the top of the closest element in the DOM with a position different than static.
If the contents of the element above #navsoc has a flexible height you can't make it respect it's position and at the same time not move on scroll.
You need Javascript to achieve that.
The first part is a bit off topic back I think it is good to know it!
Here you have the fiddle.

How to overlap 2 transparent divs?

I have a navigation bar set to "fixed" positioning at the top of my web page with a transparency of 75%. I have another div that holds all of my text/content that also has a transparency of 75%. However, when the "content" div overlaps with the navigation bar, I want the overlapped area of the "content" div to not be there, so to speak. I also want there to be a tiny gap between where the "content" div and the navigation bar would overlap.
When it's scrolled up:
When it's overlapping:
In the 2nd screenshot, I want the part of the "content" div that is overlapping to go away.
to overlap.
1st div
position:absolute;
z-index:2;
2nd div to over lap the 1st div
position:absolute;
z-index:3;
z-index will make elements overlap but it must have the position
you can use fixed or relative also.
I think you could put the "content" div in a transparent "container" div that was fixed just below the navigation and 100% wide/tall. Then put overflow:auto on the "container" and overflow:hidden on the body.
The "container" div will effectively become the body of your page and all scrolling will happen within it, instead of the body. So it should then look like the "content" div disappears before it reaches the navigation while you're scrolling down.
This may or may not be practical depending on the rest of your page, but i think it might work, give or take a few other css tweaks u may need.

CSS: Sizing column to fit all but bottom 100px of a div

I have another CSS problem. A layout that escapes me.
I could, I suppose, get the desired effect by messing about with JavaScript, but that always bugs me - it leads to having the code that controls the layout split between the markup file and the CSS file and the javascript file, and that just makes maintenance a mess.
I've created an example of what I don't quite want: http://jsfiddle.net/4sCKq/1/
The split between header and body is fixed - it's a project-wide thing that I can't change for this page. You'll notice that the header has a fixed height, and the body takes up the rest. This cannot change.
The problem is the left div, within the body.
You'll notice here that the left div has a fixed width, and the main div takes up the rest. This also cannot change.
You'll notice that the left div has a variable height, taking up what is left of the browser window, from the header down. Yep, this is also required.
And at the bottom of the left div I have a fixed-size div, that has the same width as the left div, and a fixed height, and that stays at the bottom, as the window resizes. This is also required.
What is left is the left-main div. I want this to take up all of the left div that is not contained by the left-bottom div. That is, between the two of them, left-main and left-bottom should completely fill the left div, regardless of the size of the browser window. But, as the window resizes, it should be the left-main div that grows and shrinks, the left-bottom div should remain at the bottom of the window, and its size should stay constant.
I've added the show-top and show-bottom divs solely to make it clear where the limits of the left-main div are. If you give left-main a large enough height, it looks like it is filling the div, but in fact it is disappearing behind the left-bottom div. The fact that you can't see the show-bottom div reveals that. I cannot have this - I need all of what is within the left-main to be visible.
So, any ideas?
Rather than define a height you can define the bottom absolute pixel value.
Changing one line in your jsfiddle seems to do it, if i understand the question:
#left-main
{
background-color: #AA9977;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 100px;
}
I was able to get this done pretty easily using some basic jQuery. If you decide to go that route, feel free to use this.
LIVE DEMO http://jsfiddle.net/Jaybles/4sCKq/5/
Here's the magic.
$(window).resize(function(){
var freeSpace = $('#left').height() - $('#left-bottom').outerHeight();
if (freeSpace>=150) freeSpace=150;
$('#left-main').height(freeSpace);
});
UPDATE
After seeing the answer from RSG, I'm not sure I got exactly what you were looking for. My code simply prevents the top div from going behind the bottom one, and does not stretch the div to maximize space. If you wanted to accomplish what RSG did, you can do the following (although RSG's answer is MUCH better as there is no flickering)
$(window).resize(function(){
var freeSpace = $('#left').height() - $('#left-bottom').outerHeight();
$('#left-main').height(freeSpace);
});

Allowing div to expand or move past container

I have an issue (code is dynamic so difficult to print - I hope this is simple) whereby when a parent container div contains 3 div elements floated left, yet the if the 3rd div goes beyond the body of the page (i.e. the browser's width) it line breaks to go underneath.
I want it to float: left whatever, whether it goes past the 'end of the browser' or not. Is this possible?
Example code:-
<div id="container"><div id="divLeft"></div><div id="divCenter"></div><div id="divRight"></div></div>
Where all the divs left, center and right are float: left;
Yet #divLeft will break to go under divCenter if it's width goes outside the browser width.
Any help much appreciated!
The best way to be sure is to set a fixed width to your div here.
An example here
#container{width:306px;display:block;border:1px solid black;overflow:auto;}
#divLeft, #divCenter,#divRight{float:left;border:1px solid red;width:100px;}
Don't forget the overflow:auto on your container if you want to apply a background or a border, else it won't be under your divs.
it seems the divs don't fit in container div, and the last one floats under them. this is how float works. you must arrange the widths of them.

Sidemenu overlaps when browser window is restored

Check my website, and see the Divisions left menu. When you have maximized your broswer there is no problem, but when you restore it to half of screen, the left menu overlaps to the right.
Here is the CSS code. Can someone help me?
It's because your "divisions" div is absolutely positioned.
You can remove "position: absolute" and increase the width of the "divisions" div to 300px.
Your left menu is absolutely positioned that's why it overlaps other content when window size is too narrow. But the solution for this problem is quite tricky and actually depends on what you want to achieve.
Percentage
One possible solutions would be to set width on "divisions" and "content" div in percentage. This way they'll never overlap. But it depends if you can afford to have dynamic width for your "content" div.
Repositioning
If your content must be fixed width... You'll first have to decide how would you like your content/menu to appear when window is too narrow (maybe even narrower than content width)... And work from there.
Body element width
Set minimum window content (as in <body>) width. Either by using:
transparent image at the beginning of your document <img src="t.gif" width="1250">
set body's minimum width css as min-width: 1250px; has to be 1250px wide, because content is centrally positioned, so it must have equal space on the left and on the right (right one being useless empty space just allowing non overlapping space on the left of content)
The last one is actually the simplest and works. It only makes it a bit wide for smaller screen sizes, but your content width (including menu on the left) already exceeds 1030px anyway...
A very straight-forward and simple
and quick-fix solution would be with CSS :
#content {style.css (line 17)
left:-270px;
margin:0 auto;
padding:30px 10px 0 550px;
position:relative;
width:780px;
}
I tried this in my Firebug and it worked fine. hope it'll suit you're needs :)
next time just use css floats:
put the side menu and the content div in a wrapper,
float:left for the menu, and give the wrapper a fixed width, and center align it.
you can also make the navigation menu go "out" from the left with negative left positioning it.

Resources