Hyperlinking an image using CSS

后端 未结 10 983
清酒与你
清酒与你 2020-12-11 05:39

I know this is probably the dumbest question ever, however I am a total beginner when it comes to CSS; how do you hyperlink an image on a webpage using an image which is sou

相关标签:
10条回答
  • 2020-12-11 05:51

    You have to use an anchor element, wrapped in a container. On your homepage, your title would normally be an h1, but then on content pages it would probably change to a div. You should also always have text in the anchor element for people without CSS support and/or screen readers. The easiest way to hide that is through CSS. Here are both examples:

    <h1 id="title"><a title="Home" href="index.html>My Title</a></h1>
    <div id="title"><a title="Home" href="index.html>My Title</a></div>
    

    and the CSS:

    #title {
    position:relative; /*Makes this a containing element*/
    }
    #title a {
    background: transparent url(../images/logo.png) no-repeat scroll 0 0;
    display:block;
    text-indent:-9999px; /*Hides the anchor text*/
    height:50px; /*Set height and width to the exact size of your image*/
    width:200px;
    }
    

    Depending on the rest of your stylesheet you may need to adjus it for the h1 to make it look the same as the div, check out CSS Resets for possible solutions to this.

    0 讨论(0)
  • 2020-12-11 05:52

    HTML is the only way to create links - it defines the structure and content of a web site.

    CSS stands for Cascading Style Sheets - it only affects how things look.

    Although normally an <a/>; tag is the only way to create a link, you can make a <div/> clickable with JavaScript. I'd use jQuery:

    $("div#header").click(function() {window.location=XXXXXX;});
    
    0 讨论(0)
  • 2020-12-11 05:54

    To link a css-sourced background-image:

    #header { 
    display:block;
    
    margin: 0px auto;
    padding: 0px 15px 0px 15px;
    border: none;
    background: url(images/title.png) no-repeat bottom;
    width: 1000px;
    height: 100px;    
    }    
    
    <a id="header" href="blah.html" class="linkedImage">
    

    The key thing here is to turn the anchor tag into a block element, so height and width work. Otherwise it's an inline element and will ignore height.

    0 讨论(0)
  • 2020-12-11 05:54
    <a href="linkto_title_page.html" class="titleLink"></a>
    

    then in your css

    .titleLink {
      background-image: url(imageUrl);
    }
    
    0 讨论(0)
  • 2020-12-11 05:54

    You still create links in HTML with 'a' (anchor) tags just like normal. CSS does not have anything that can specify if something is a link to somewhere or not.

    Edit The comments of mine and others still apply. To clarify, you can use JavaScript to make a div act as a link:

    <div id="header" onclick="window.location='http://google.com';">My Header</div>
    

    That isn't really great for usability however as people without JavaScript enabled will be unable to click that and have it act as a link.

    Also, you may want to add a cursor: pointer; line to your CSS to give the header div the correct mouse cursor for a link.

    0 讨论(0)
  • 2020-12-11 06:05

    Try this - use an H1 as the seat of your graphic instead. Saved my butt time and time again:

    <h1 class="technique-six">
        CSS-Tricks
    </h1>
    
    h1.technique-six {
        width: 350px;
        padding: 75px 0 0 0;
        height: 0;
        background: url("images/header-image.jpg") no-repeat;
        overflow: hidden;
    }
    

    Accessible, and also solid across browsers IE6 and > . You could also link the H1.

    0 讨论(0)
提交回复
热议问题