Hyperlinking an image using CSS

后端 未结 10 984
清酒与你
清酒与你 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 06:06

    You control design and styles with CSS, not the behavior of your content.

    You're going to have to use something like <a id="header" href="[your link]">Logo</a> and then have a CSS block such as:

    a#header {
      background-image: url(...);
      display: block;
      width: ..;
      height: ...;
    }
    

    You cannot nest a div inside <a> and still have 'valid' code. <a> is an inline element that cannot legally contain a block element. The only non-Javascript way to make a link is with the <a> element.

    You can nest your <a> tag inside <div> and then put your image inside :)

    If you don't want that, you're going to have to use JavaScript to make your <div> clickable:

    Document.getElementById("header").onclick = function() {
        window.location='...'; 
    }
    
    0 讨论(0)
  • 2020-12-11 06:08

    CSS is for presentation only, not content. A link is content and should be put into the HTML of the site using a standard <a href=""> tag. You can then style this link (or add an image to the link) using CSS.

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

    That's really not a CSS thing. You still need your A tag to make that work. (But use CSS to make sure the image border is either removed, or designed to your required spec.)

    <a href="index.html"><img src="foo" class="whatever" alt="foo alt" /></a>
    

    EDIT: Taking original intent (updated question) into account, a new code sample is below:

    <a href="index.html"><img id="header" alt="foo alt" /></a>
    

    You're still in an HTML world for links, as described by other answers on this question.

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

    sorry to spoil your fun ladies and gentlemen, it is possible.

    Write in your header: [link](http://"link here")
    
    then in your css:
    
    #header a[href="https://link here"] {
              display: inline-block;
              width: 75px;
              height: 75px;
              font-size: 0;
            }
            .side .md a[href="link here"] {
              background: url(%%picture here%%) no-repeat;
            }
    
    0 讨论(0)
提交回复
热议问题