SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    CSS Hotspots on Centered Background Image

    Hello, this issue is driving me nuts. I'm wondering if one of you good folks might be able to help me out.

    The Problem:
    I am working on a banner that has css hotspots over a div's background image (the banner image) . I originally had the banner aligned to the left, and all worked fine. But then I decided I want to center the div and its' background image in the div it was wrapped by, so the hotspots will need to move to follow the image when the browser window is resized. And that's where it went wrong. No matter what I do, the hotspots stay in the same place.

    I would love to link to the actual page I am working on, but it's a client's page in progress, and I'm not allowed. But, I can show you the styling and the structure.


    here is the structure code:


    <div class="banner-wrap">
    <div class="banner">
    <a href="1.html" class="hotspot1">
    <a href="2.html" class="hotspot2">
    <a href="3.html" class="hotspot3>
    </div>
    </div>


    Here is the styling:

    This div takes up the entire width of the page. It wraps the banner div.:

    .banner-wrap {
    width:100%;
    height:388px;
    float:left;
    position:relative;
    background-repeat: repeat-x;
    }


    This div is 970px and is centered in in the banner-wrap. It contains the image that moves when the browser is resized, and wraps the hotspots:

    .banner {
    height:388px;
    background-image: url(images/banner.jpg);
    background-repeat: no-repeat;
    background-position: center top;
    }


    This is a sample hotspot div. that contains the position and size specs for the hotspot. It wraps the hotspot links:
    .banner .hotspot1 {
    width: 90px;
    height: 90px;
    position: absolute;
    top: 30px;
    left: 30px;
    }


    What I've Tried:

    I've tried the following to no avail:

    (1) Set the .banner class to a fixed width of 970px.

    (2) Set .banner .hotspot positioning to percentages instead of fixed coordinates. Made no difference.

    (3) set .banner .hotspot position to relative. No effect

    Can anyone tell me what I did wrong?

    Thanks a bunch,
    MIke

  2. #2
    Ripe Tomatos silver trophybronze trophy Rayzur's Avatar
    Join Date
    Jun 2007
    Location
    Texas
    Posts
    4,174
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Can anyone tell me what I did wrong?
    Hi Mike, Welcome to the forums

    You have placed position:relative; on the wrong div, it belongs on the .banner div.

    AP elements always position themselves from the nearest positioned ancestor, by using position:relative; on the div you want to position from you will establish the containing block.

    You should also be able to loose that 100% wide float on the .banner-wrap and just let it be a static block with a default width of 100%.
    Looks like the hotspots have unique positions so I would opt for an ID on those.

    Try this -

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Test Page</title>
    
    <style type="text/css">
    body {
        margin:0;
        padding:0;
    }
    .banner-wrap {
        /*width:100%;*/
        height:388px;
        /*float:left;*/
        /*position:relative;*/
        background-repeat: repeat-x;
    }
    .banner {
        width:970px;
        margin:0 auto;
        position:relative;
        height:388px;
        background:red url(images/banner.jpg) no-repeat 50% 0;
    }
        #hotspot1 {
            width: 90px;
            height: 90px;
            position: absolute;
            top: 30px;
            left: 30px;
            background:lime;
        }
        #hotspot2 {
            width: 90px;
            height: 90px;
            position: absolute;
            top: 90px;
            left: 230px;
            background:yellow;
        }
        #hotspot3 {
            width: 90px;
            height: 90px;
            position: absolute;
            top: 150px;
            left: 430px;
            background:orange;
        }
    </style>
    
    </head>
    <body>
    
    <div class="banner-wrap">
        <div class="banner">
            <a href="1.html" id="hotspot1">1</a>
            <a href="2.html" id="hotspot2">2</a>
            <a href="3.html" id="hotspot3">3</a>
        </div>
    </div>
    
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for the welcome and thanks for answering this post on a Saturday; I wasn't expecting that.

    My goodness, that worked perfectly! I was tearing my hair out over this for 5 days!

    Thank you so much!!!

    Mike


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •