In the following HTML, the text input and the buttons are supposed to be positioned 380px from the top but it's further down the page. I noticed if I remove the image it's positioned in the right place or closer to it, I haven't pulled out a ruler yet. Why is this happening and how do I fix it?
<div style="position:relative;width:800px;height:792px;font-family:Arial;font-size:12px;margin:0 auto;left:8px;top:14px;background-color:#FFFFFF;">
<img name="Image712" src="https://www.google.com/images/srpr/logo4w.png" style="position:relative;display:block;margin:0 auto;top:217px;width:269px;height:95px;" />
<div name="HGroup714" style="position:relative;display:block;margin:0 auto;top:380px;width:220px;height:23px;">
<div style="display:inline-block;padding-right:2px">
<input name="Button715" type="button" style="position:relative;vertical-align:middle;width:101px;height:21px;font-family:Arial;font-size:12px;" class="buttonSkin" value="Google Search" />
</div>
<div style="display:inline-block;">
<input name="Button718" type="button" style="position:relative;vertical-align:middle;width:113px;height:21px;font-family:Arial;font-size:12px;" class="buttonSkin" value="I'm Feeling Lucky" />
</div>
</div>
<input name="TextInput721" type="input" style="position:relative;display:block;margin:0 auto;top:330px;width:50%;height:22px;font-family:Arial;font-size:12px;padding:0;border:1px solid #696969;" />
</div>
<style>
*,
*: before, *: after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
</style>
<style>
*,
*: before, *: after {
outline: 1px dotted red;
}
</style>
UPDATE: I know it's inline CSS. Please ignore that for now.
Link to live example (thanks Mr Alien).
Instead of using position relative, which places you relative towards the location where you should be rendered, you want to use position absolute. Absolute is rendered in relation to it's parent, which is what you're actually looking for.
Position fixed does the same thing as absolute, except the 'parent' is the browser window.
I think this is what you're looking for: http://jsfiddle.net/fr6AY/1/
position:absolute;
margin:0 auto;
left: 0;
right: 0;
The problem is that as the image is a relative positioned element, the 'top' css tag applied to the 'HGroup714; element is from the bottom of this image, therefore the height of the image moves it down the page. If you wanted this to not affect the positioning of the lower elements you could simply remove the 95px height of the image from the 'top' tag on the Hgroup element.
Any relative positioning will cause your elements to be positioned relative to each other, so you much take them out of the page flow by using position:absolute, and removing any relative positioning from parent elements.
Suggestions to use position:fixed; would only work if the page were never to be scrolled - fixed positioning fixes it within the browser window, and scrolling keeps it at that same positioning, whereas absolute is not reliant on the position of other elements, but maintains it's place in the page.
If you want it 380px from the top of the window (not the page), you are looking for position: fixed.
If you want it 380px from the top of the page, try using position: absolute in the HGroup714 element.
Related
I have an image/link that I'm trying to absolutely position with the following:
HTML:
<div id="content">
<img id="calendar" src="/bundles/majorproductionsadisite/images/calendar.jpg" alt="calendar" />
<div class="blogEntry">
<h2>Blog title</h2>
<div>Blog text</div>
</div>
</div>
CSS:
#calendar {
position: absolute;
top: 210px;
left: 420px;
z-index: 9000;
}
In Chrome, this puts the calendar image flush to the left of the blog:
In contrast, in Firefox and IE, the calendar is positioned much further to the left:
Any suggestions on how I can get either Chrome to fall in line with the other two, or vice versa?
You need a common reference point on the page for the absolutely positioned element. Otherwise, the element is positioned relative to the browser window, so the position can change depending on the browser size, etc. Try positioning the content div relatively.
http://css-tricks.com/absolute-positioning-inside-relative-positioning/
Did you try using a CSS reset to zero out all of your margins and paddings? Resets are meant to prevent cross-browser differences as much as possible.
Why Use CSS Resets
I am constructing a website based off of the Fluid 960 GS System. I want to overlay an image in the header so that it stays put relative to the header image without disrupting the header itself. If I use the following CSS, I get halfway there:
.imgFloat {
position:absolute;
left:400px;
top:-2px;
z-index:1;
}
<div class="grid_16">
<h1 id="branding">
<img src="img/logo.png" />
<img src="img/float.png" class="imgFloat" />
</h1>
</div>
The only "issue" (not really since the CSS is doing what it is supposed to) with this code is that the image stays put rather than being staying x pixels away from the header image like I want.
If I change the position to relative, it breaks the size of the header and thus the layout of the header itself, but it will position the image like I want.
Is there a happy medium CSS "trick" that I can apply to achieve this result?
Thanks in advanced!
You just need to make sure that the absolute positioned element is inside the element you want it to be relative to. Then just add position: relative to that element and it should work.
So:
tag(position:relative)
ag(position:absolute; left:2px)
Should work
I'm just guessing here since the code you're showing is not detailed enough ;)
Update:
#branding {position:relative}
.imgFloat {
position:absolute;
left:400px;
top:-2px;
z-index:1;
}
set position: relative on the header element that wraps the .imgFloat element that should lock an absolutely positioned element to the wrapper.
Also you'll need to change your positions since the left will be relative to the 0,0 coordinate of the wrapper element.
<div id="main-solutions">
<div id="main-solutions-top-left"></div>
<div id="main-solutions-top-right"></div>
<div id="main-solutions-body">
blah blah blah
</div>
</div>
css
#main-solutions {
}
#main-solutions-top-left {
position: absolute;
top: 0px;
left: 0px;
background: url('../images/Top-Left-Gray-Corner.gif') no-repeat top left;
width: 434px;
height: 15px;
}
#main-solutions-top-right {
position: absolute;
top: 0;
right: 0;
background: url('../images/Top-Right-Gray-Corner.gif') no-repeat top right;
width: 434px;
height: 15px;
}
#main-solutions-body {
background: url('../images/Gray-Gradient.jpg') repeat-x;
}
I'm expecting to see that main-solutions has two absolutely positioned divs at the top left and right with my rounded corner image, and then followed by the body with the gradient, but when I use HTML element browsers, the top-left and top-right div are not appearing at all, very confused, why are those divs being disregarded?
UPDATE (for others confused by answer):
At the root of my issue is I didn't understand that both absolute and relative define a new coordinate system for their contents, in addition to specifying the posision of the element itself. Found a good explanation here:
http://www.w3.org/TR/WD-positioning-970131#Positioned
from section 2.2
Like 'absolute' positioned elements,
'relative'ly positioned define a new
coordinate system for child elements,
with the origin located in the
position where the first child element
is rendered
Far as i'm seeing, the corners should be appearing at the top left and right of the page, since your container div doesn't have a CSS position property. Absolute-positioned elements' positions are relative to the innermost parent that has a position other than static (the default).
Try adding position: relative to the container div's CSS. It works much like the default, but (1) if you want, you can shift the div's position by some amount (which isn't extremely useful here, but still), and (2) since the position's not static anymore, absolute-positioned stuff inside the div should position itself relative to the container, rather than to the body/page.
Also, some browsers won't even display a div that has no content -- so the background for said div might not show. You'll probably want to have something in the divs. Even a single will work.
Have you considered using CSS border-radius to achieve this rather than messing around with images?
border-radius is supported by all browsers except IE, but even IE can be made to work with it with the use of a clever little thing called CSS3Pie.
(plus as a bonus, CSS3Pie also gives IE CSS gradient backgrounds, so you could kill two birds with one stone)
I have a div with border-radius set to some value (let's say 10px), and a nested div that is the full width and height of its parent.
<!-- ... -->
<style type="text/css">
div.parent {
display: block;
position: relative;
width: 100px;
height: 100px;
border-radius: 10px;
background: #0000ff;
overflow: hidden;
}
div.inner {
display: block;
position: relative;
width: 100%;
height: 100%;
background: #ff0000;
}
</style>
<!-- ... -->
<div class="parent">
<div class="inner"></div>
</div>
<!-- ... -->
I noticed that the parent does not clip the child around the rounded corners, in spite of overflow being set to hidden. Another stackoverflow thread indicates that this behavior is "by design":
How do I prevent an image from overflowing a rounded corner box with CSS3?
However, upon digging up the working draft for CSS3 backgrounds and borders...
http://www.w3.org/TR/css3-background/#corner-clipping
...I couldn't help but notice the following description under the "corner clipping" section:
Other effects that clip to the border
or padding edge (such as ‘overflow’
other than ‘visible’) also must clip
to the curve. The content of replaced
elements is always trimmed to the
content edge curve
So what am I missing? Is the content supposed to be clipped to the corners? Am I looking at outdated information? Am I doing it wrong?
If you remove position: relative; on both elements the outer element clip the child around the rounded corners. Not sure why, and if it is a bug.
It's not by design, there's an outstanding defect in Firefox about this. Should work OK in any WebKit browser. In Firefox you either have to add border radius to the contained element too, or use some sort of hack.
I came here looking for an answer because I had a similar problem in Chrome 18.
I was trying to have a rounded box with two elements inside of it - title and index number - with index number positioned absolutely at the bottom left corner of the box.
What I noticed was if I had the HTML like this, the title element would bleed outside the rounded corners (border-radius) even though overflow was set to hidden on the parent element:
<a>
<div style="overflow:hidden; border-radius:15px; position:relative;">
<div id="title" style="text-align:center;">Box Title</div>
<div id="index" style="position:absolute; top:80px;">1</div>
</div>
</a>
But if I moved the relative positioning up one parent element everything looked good:
<a style="position:relative;">
<div style="overflow:hidden; border-radius:15px;">
<div id="title" style="text-align:center;">Box Title</div>
<div id="index" style="position:absolute; top:80px;">1</div>
</div>
</a>
Just wanted to chime in on this one since I found this with a similar problem.
In a div with its overflow set to scroll, the border-radius didn't clip the content while scrolling unless the content was scrolled to the absolute top or bottom. Even then, the clipping only sometimes reappeared if I scrolled the document to the absolute top or bottom as well.
On a lark I added a transparent border to the element and that seemed to enforce the clipping on the corners. Since I already had some space around the element, I just cut that in half and applied the remainder to the border thickness. Not ideal, but I ended up with the result I wanted.
Tested in Chrome, Safari and Firefox on Mac.
I have a DIV which I want to insert into my HTML content. Whenever I do, the content moves down from where the DIV WOULD have appeared in the content. It moves down the height of the div. I sat 'WOULD have appeared' because its position is set to relative.
Here is some code: (THE DIV I MEAN IS THE ONE WITH ID="pop"
<table border="0" align="center">
<tr>
<td><div id="pop" style="position:relative; z-index:20; top:100px; left:480px; width:208px; height:52px;"><img src="../Graphics/valj_oxo_komm.png"></div>
<div class="nav_container" id="nav_container">
<div id="nav_container2" style="position: relative; left: 0%; top: 13%;">
HERE IS ALL THE CONTENT
and some css:
nav_container{
width:720px;
height:180px;
background-image:blablabla;
}
If you need more input tell me and I will update this Q.
The content moves down although I can actually position the div ('pop') where I want, But I dont want the content moved down.
I mean, everything looks good, except content is ALL moved down 52px.
Thanks
If I understand correctly you want DIV to appear on top of the rest of the content, being completely independent of the rest of your site. To do that you need to set position: absolute rather than position: relative. relative basically allows moving element from its original position, but the space that element was occupying is still there and that's why all the content moved down in your case.
If you apply position: absolute element will be taken out of the flow of the page and the following elements shouldn't move down. But then you might need to make sure that DIV#pop moves in correct context but that's something to worry about later ;)
Hope my explanation makes sense, but you also might find this this link useful
What you describe is the intended behaviour of position: relative.
I'm not sure about what you want to achieve but try putting a wrapper outside pop with position: relative and give pop position: absolute. That should make you able to move pop without moving the rest of the content.
If you use only position: absolute the positioning will be relative to the viewport's top left corner and not the table's which probably is not what you want.
You will have to set the position to 'absolute' to get the ability to place the div wherever you want (and also the ability to use z-indexes).
Try to add below css or try to decrease top size you have specified for the div eg top:100px
<style type="text/css">
#pop
{
display:inline;
}
</style>