Absolute position, can someone explain this - css

Here is a snippet of CSS that I need explained:
#section {
width: 860px;
background: url(/blah.png);
position: absolute;
top: 0;
left: 50%;
margin-left: -445px;
}
Ok so it's absolute positioning of an image, obviously.
top is like padding from the top, right?
what does left 50% do?
why is the left margin at -445px?
Update:
width is 860px.
The actual image is 100x100 if that makes a difference??

Top is the distance from the top of the html element or, if this is within another element with absolute position, from the top of that.
& 3. It depends on the width of the image but it might be for centering the image horizontally (if the width of the image is 890px). There are other ways to center an image horizontally though. More commonly, this is used to center a block of known height vertically (this is the easiest way to center something of known height vertically):
top: 50%
margin-top: -(height/2)px;

This has probably been done in order to center the element on the page (using the "dead center" technique).
It works like this: Assuming the element is 890px wide, it's set to position:absolute and left:50%, which places its left-hand edge in the center of the browser (well, it could be the center of some other element with position:relative).
Then the negative margin is used to move the left hand edge to the left a distance equal to half the element's width, thus centering it.
of course, this may not be centering it exactly (it depends how wide the element actually is, there's no width in the code you pasted, so it's impossible to be sure) but it's certainly placing the element in relation to the center of the page

top is like padding from the top right?
Yes, the top of the page.
what does left 50% do?
It moves the content to the center of the screen (100% would be all the way to the right.)
why is the left margin at -445px?
After moving it with "left: 50%", this moves it 445 pixels back to the left.

The snippet above relates to an element (could be a div, span, image or otherwise) with an id of section.
The element has a background image of blah.png which will repeat in both x and y directions.
The top edge of the element will be positioned 0px (or any other units) from the top of it's parent element if the parent is also absolutely positioned. If the parent is the window, it will be at the top edge of the browser window.
The element will have it's left edge positioned 50% from the left of it's parent element's left edge.
The element will then be "moved" 445px left from that 50% point.

You'll find out every thing you need to know by reading up on the CSS box model

When position is absolute, top is vertical distance from the parent (probably the body tag, so 0 is the top edge of the browser window). Left 50% is distance from the left edge. The negative margin moves it back left 445px. As to why, your guess is as good as mine.

At the risk of sounding like Captain Obvious, I'll try explaining it as simply as possible.
Top is a number that determines the number of pixels you want it to be FROM the top of whatever html element is above it... so not necessarily the top of your page. Be wary of your html formatting as you design your css.
Your left to 50% should move it to the center of your screen, given that it's 50. Keep in mind people have different screen sizes and is allocated to the (0,0) top left of your image, not the center of the image, so it will not be perfectly allocated to the center of one's screen like you may expect it to.
THIS is why the margin-left to -445 pixels is used-- to move it further over, fixed.
Good luck, I hope that this made sense. I was trying to word my explanation differently in case other answers were still giving you a hard time. They were great answers as well.
(If you have two different sized monitors, I suggest toying around the with the code to see how each modification affects different sized screens!)

Related

Why is CSS Left 75% and Right 25% working differently?

I have the following CSS
.bloodparam > .highvalue { bottom: 12px; right: 25%; }
and
.bloodparam > .highvalue { bottom: 12px; left: 75%; }
ideally the position of the element must be the same regardless of which one I use, but I see different positions. Any ideas why?
Please see this fiddle http://jsfiddle.net/956y5/1/ - You'll see the right indicator is different for the first and the second line
left position values are calculated starting at the specified left value given and puts the width to the right of the location
right position values are calculated starting at the specified right value given and puts the width to the left of the location
Visually you can see this in your example, the right part of the top 100 matches up with the left part of the bottom one.
This is for good reason. Take for example the comparison of left:0 and right:0. If they were to be equivalent, the left version would be positioned at the far left of the screen with the full width showing, but the right version would be positioned at the very far right of the screen and not be visible. As it is, though, it positions the element where the full width is shown but it is at the right most value possible
And technically speaking, both of your values are wrong because left and right take into account the width of the element it is positioning. To be perfectly accurate you would need to also have a negative margin on one side of half the width of the element being positioned. The error can be easily seen by giving the left value 100%, the marker then moves outside of the range entirely
Of course it's different, you have an element with an arbitrary width, and the left and right is being calculated from either the left or the ride side of that. 75% calculated from the left edge is different from the 25% calculated from the right edge that is how ever many pixels away from the left.
The 25% and the 75% are the distance between the right element border and the right page borden / the left element border and the left page border. Adding some backgrounds to your fiddle may clarify this (I just added background-color: blue to the two elements):
http://jsfiddle.net/956y5/3/

How do you make a div vertical but aligned to the bottom left coords of a horizontal div?

A rough sketch is on a fiddle here. Is there a way to make that happen without hardcoded values for the height and width of the div's?
This is what transform-origin is supposed to help with.
I've looked at your fiddle. Do you need the div's to be absolutely positioned? If not, then setting transform-origin to 0 0 is enough to make it work. (I'm supposing this is how you want it: http://jsfiddle.net/Actss/17/ )
Removing the position: absolute from the divs guarantees the second div's top-left corner is touching the first div's bottom-left corner. Make the rotation on the second div happen at that same point (by using transform-origin) and you'll get the effect you want.
You can define the point you are rotating around by using transform-origin.
-webkit-transform-origin: 0% 100%;
This will rotate the element around the bottom left point. By default this is set to the centre of the element (50% 50%).
Demo

css liquid layout with div overflow inside

I'm having trouble in design layout css with div element.
Basically my main page layout design is look like the following picture :
The red box is the browser screen area.
The black box is the content area where the data will included / or loaded via ajax.
The green box is the data list which is the response result and contain about hundred rows inside. The data list contain header div and rows divs.
What i intend to do is set the overflow on the blue area which is the data rows so the scrollbar will appear on the right side of the blue box not on the right side of the red or black box.
Then when the browser area (red) resized all the div inside will also resized to the best size.
I've managed to make the scroll bar appear on the blue box when the data inside is overflow by set css overflow : auto /scroll for blue box div. But the problem is the overflow : auto properties seems only work when i set a certain height for the blue box div let's say about 400px. When i resize the browser the blue box div keeps stay with 400px height.
How to make it auto resize? Thanks in advance for any help.
You need to fix the heights of the HEADER, "Table Heading Row", FOOTER and the "subfooter" row
from there you can calculate the top and bottom position offsets for the middle box, which should be absolutely positioned, the becasue it's positioned you will also need to absolutely position the two bottom footers, in my example I have wrapped them two rows and positioned them as one, this may seem excess but there are in fact a lot of your containers which are no longer required (though I didn't weed them out)
also your float code is too excessive, you don't need to relatively position every float to left: 0.0% so I chopped all tham out, you only need top relatively position something if you want to do absolute positioning inside it.. except for the body element which is all we need to use for this style layout (note I did change the end of your HTML slightly)
refiddle: HERE
and btw, I think this one those internal rows would be better as an actual <table>, it seems like rows of Data to me ;) - and the whole thing would likely mean a lot less code..
What your looking for is a positioned div for the blue box.
.blueboxdiv{
position: relative;
top : 100px; // height of header - Top stays 100px away from header thus grows on resize!
bottom : 0px; // Bottom sticks to bottom
left : 0px; // Left sticks to left
right : 0px; // Right sticks to right
}

can a background image be negatively positioned to the bottom or right, or only a portion of a picture repeated?

I'm putting together a sprite and have two questions.
Something I've always wondered whether possible is negatively positioning a background picture to the right or bottom. A negative position is bread and butter stuff on the left of an element or top but what about the right and bottom?
If I have a 500px by 500px div can I then position the left edge of a background image to appear 5px in from the right using a negative value rahter than 495px to push it over?
The second question is whether I can use just a small portion of an image and repeat it without the rest of the image showing.
For example, I may have a sprite thats 300px square and filled with all kinds of things. Is it possible to take a 50px square portion of that image and repeat it in the background of an element?
I very much doubt either is possible but must put the monkey to sleep!
To "negatively positioning a background picture to the right or bottom", you can use percentages less than 0%. e.g.:
background-position: -11% -7%;
...positions the image cropped on the bottom right if the image is of similar size to the element. You might need more negative percentages if they aren't the same size.
To "negatively positioning a background picture to the left or top", you can use percentages greater than 100%. e.g.:
background-position: 105% 110%;
...positions the image cropped on the top and left assuming again that they are of similar sizes. You might need larger percentages if they aren't the same size.
Finding the exact percentages you need is not very intuitive, however. CSS uses percentages a little differently with background-position. The value is the percentage along both the image and element (aka viewport) where they are the same. This is why 0% is equal to left, 50% is equal to center, and 100% is equal to right. Outside of this range it is even less intuitive as "less than zero" matches a point before the top/left edge on both the image and the element (effectively shifting the image away from the top/left), and "greater than 100%" matches a point greater than the far bottom/right edge which has the effect of moving the image away from the bottom/right.
These equation helps to determine where you want the image to sit. Simple algebra will get you the variable you want to know (e.g., solve for percentage).
effectiveLeft = (elementWidth - imageWidth) * percentageLeft;
effectiveTop = (elementHeight - imageHeight) * percentageTop;
As for your second question (repeating part of an image), I don't believe this is possible unless you were to repeat multiple cropping elements, which is different than what you were wanting.
If you use percentage based background positions you can. So you would want something like:
background-position: 190% 0;
They've already answer that you can't use negative values, but maybe you could try:
Give the element background-position:right bottom
and the image for the background should have only the part you want to show.
The background-position property has a few different values:
left top
left center
left bottom
right top
right center
right bottom
center top
center center
center bottom
You can also specify X and Y positions for it. For more information on it, take a look at this link:
https://developer.mozilla.org/docs/CSS/background-position
As for your second question, that's not possible using simple CSS.
For negative right bottom position to work right, this DIV must necessarily be property background-repeat: no-repeat;
For your first question, no you cannot.
For your second question, no you cannot.

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