Replacing H1 text with a logo image: best method for SEO and accessibility?

前端 未结 15 2155
渐次进展
渐次进展 2020-11-28 00:16

It seems like there are a few different techniques out there, so I was hoping to get a \"definitive\" answer on this...

On a website, it\'s common practice to create

相关标签:
15条回答
  • 2020-11-28 00:59

    I think you'd be interested in the H1 debate. It's a debate about whether to use the h1 element for the page's title or for the logo.

    Personally I'd go with your first suggestion, something along these lines:

    <div id="header">
        <a href="http://example.com/"><img src="images/logo.png" id="site-logo" alt="MyCorp" /></a>
    </div>
    
    <!-- or alternatively (with css in a stylesheet ofc-->
    <div id="header">
        <div id="logo" style="background: url('logo.png'); display: block; 
            float: left; width: 100px; height: 50px;">
            <a href="#" style="display: block; height: 50px; width: 100px;">
                <span style="visibility: hidden;">Homepage</span>
            </a>
        </div>
        <!-- with css in a stylesheet: -->
        <div id="logo"><a href="#"><span>Homepage</span></a></div>
    </div>
    
    
    <div id="body">
        <h1>About Us</h1>
        <p>MyCorp has been dealing in narcotics for over nine-thousand years...</p>
    </div>
    

    Of course this depends on whether your design uses page titles but this is my stance on this issue.

    0 讨论(0)
  • 2020-11-28 01:03

    If accessibility reasons is important then use the first variant (when customer want to see image without styles)

    <div id="logo">
        <a href="">
            <img src="logo.png" alt="Stack Overflow" />
        </a>
    </div>
    

    No need to conform imaginary SEO requirements, because the HTML code above has correct structure and only you should decide does this suitable for you visitors.

    Also you can use the variant with less HTML code

    <h1 id="logo">
      <a href=""><span>Stack Overflow</span></a>
    </h1>
    
    /* position code, it may be absolute position or normal - depends on other parts of your site */
    #logo {
      ...
    }
    
    #logo a {
       display:block;
       width: actual_image_width;
       height: actual_image_height;
       background: url(image.png) no-repeat left top;
    }
    
    /* for accessibility reasons - without styles variant*/
    #logo a span {display: none}
    

    Please note that I have removed all other CSS styles and hacks because they didn't correspond to the task. They may be usefull in particular cases only.

    0 讨论(0)
  • 2020-11-28 01:03

    After reading through the above solutions, I used a CSS Grid solution.

    1. Create a div containing the h1 text and the image.
    2. Position the two items in the same cell and make them both relatively positioned.
    3. Use the old zeldman.com technique to hide the text using text-indent, white-space, and overflow properties.
    <div class="title-stack">
        <h1 class="title-stack__title">StackOverflow</h1>
        <a href="#" class="title-stack__logo">
            <img src="/images/stack-overflow.png" alt="Stack Overflow">
        </a>
    </div>
    
    .title-stack {
        display: grid;
    }
    .title-stack__title, .title-stack__logo {
        grid-area: 1/1/1/1;
        position: relative;
    }
    .title-stack__title {
        z-index: 0;
        text-indent: 100%;
        white-space: nowrap;
        overflow: hidden;
    }
    
    0 讨论(0)
提交回复
热议问题