简体   繁体   中英

IE7 - absolute positioning offset different to Firefox?

I'm tidying up another developer's work who seems to have done a shoddy job with the CSS.

There is the main "wrapper" div on the page, and inside this is a logo and images for the navigation. The images are using "position: absolute" and using the CSS "top" property to offset them. However, Firefox and IE seem to start their offset from a different point, meaning the logo is about 100px above where it should be in IE.

Is this an IE CSS bug or known thing?

Example in question: http://barry.cityjoin.com/mccamb/

If you want to position elements absolutely within a wrapper using top, right, bottom and/or left, the position of the wrapper has to be set as relative explicitly. Otherwise the absolute elements will get positioned within the view port instead.

A little working example:

<style>
    .wrapper
    {
        position: relative;
        height: 100px;
        width: 800px;
    }
    .absoluteLogo
    {
        position: absolute;
        top: 10px;
        left: 10px;
        height: 60px;
        width: 80px;
    }
    .absoluteElement
    {
        position: absolute;
        top: 80px;
        left: 320px;
        height: 20px;
        width: 80px;
    }
</style>

<div class="wrapper">
    <div class="absoluteLogo">Logo</div>
    <div class="absoluteElement">Element</div>
</div>

Another possibility would be to position the absolute elements using margins:

<style>
    .wrapper
    {
        height: 100px;
        width: 800px;
    }
    .absoluteLogo
    {
        position: absolute;
        margin: 10px 0 0 10px;
        height: 60px;
        width: 80px;
    }
    .absoluteElement
    {
        position: absolute;
        margin: 80px 0 0 320px;
        height: 20px;
        width: 80px;
    }
</style>

<div class="wrapper">
    <div class="absoluteLogo">Logo</div>
    <div class="absoluteElement">Element</div>
</div>

The result is the same and should be working across all browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM