Wrap link around

后端 未结 8 953
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 06:54

Is it possible to wrap an tag around

8条回答
  • 2020-11-28 07:19

    You would just want to style the "a" tag as display: block;

    Eclipse is appropriately telling you that your HTML is not to spec (as a div tag is not allowed in an anchor tag).

    But, since you seem to want to be visually making the anchor look like a big-ol-box, then simply style it as such :)

    0 讨论(0)
  • 2020-11-28 07:22

    That structure would be valid in HTML5 since in HTML5 anchors can wrap almost any element except for other anchors and form controls. Most browsers nowadays have support for this and will parse the code in the question as valid HTML. The answer below was written in 2011, and may be useful if you're supporting legacy browsers (*cough* Internet Explorer *cough*).


    Older browsers without HTML5 parsers (like, say, Firefox 3.6) will still get confused over that, and possibly mess up the DOM structure.

    Three options for HTML4 - use all inline elements:

    <a href=etc etc>
        <span class="layout">
            <span class="title">
            Video Type
                <span class="description">Video description</span>
            </span>
        </span>
    </a>
    

    Then style with display: block


    Use JavaScript and :hover:

    <div class="layout">
        <div class="title">
        Video Type
            <div class="description">Video description</div>
        </div>
    </div>
    

    And (assuming jQuery)

    $('.layout').click(function(){
        // Do something
    }):
    

    And

    .layout:hover {
        // Hover effect
    }
    

    Or lastly use absolute positioning to place an a anchor with CSS to cover the whole of .layout

    <div class="layout">
        <div class="title">
        Video Type
            <div class="description">Video description</div>
        </div>
        <a class="more_link" href="somewhere">More information</a>
    </div>
    

    And CSS:

    .layout {
        position: relative;
    }
    
    .layout .more_link {
        position: absolute;
        display: block;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        text-indent: -9999px;
        z-index: 1000;
    }
    

    This won't work with older versions of IE, of course.

    0 讨论(0)
  • 2020-11-28 07:24

    HTML provides two general elements, where div is a natural block element, and span is a natural inline element. All other elements are similarly assigned to be a natural block or inline.

    Now, while both can be made by css display to be any of inline, inline-block or block, they are still treated for enclosure purposes as their natural selves, hence the warning messages. Leopards and spots sort of thing.

    However, css is only meant to be for making what an element looks like (presentation), but not actually be like (functionality), so it doesn't change an element's basic nature, though that gets very fuzzy in practice. A span made block becomes a bully that kicks everything else off the line, which is very un-inline sort of behaviour.

    So, to mitigate against possible conflicts between their natural and css-induced behaviours, it is better to allow:

    • div or any natural block tag to only ever be block or inline-block.
    • span or any natural inline tag to only ever be inline or inline-block.

    This will also mitigate against tending to build page structures that will likely end up churning out error and warning messages.

    Basically, NEVER embed a natural block tag inside a natural inline tag, at any depth.

    Why there is a really a distinction is perhaps due to a simplistic idea of what HTML was going to be used for when it was first dreamed up.

    Certainly, framework makers got around a lot of these what-to-embed-where problems by just using myriads of divs everywhere, and 'divitis' was born, and still alive and well in every framework. Just have to press F12 in a browser on almost any commercial web page and drill down through a dozen divs. This very page has 15 unbroken levels of divs.

    It meant that they really only had to manage one tag per purpose and manage it as if it was an isolated environment. So what was meant to be an occasionally-used functional grouping tag became the web's Lego block. And none of them are going to risk breaking their frameworks by converting to HTML5 semantic tags in a hurry.

    0 讨论(0)
  • 2020-11-28 07:30

    Another simple solution - just add an onclick event handler to the div thusly:

    <div class="layout" onclick="location.href='somewhere'">
        <div class="title">
        Video Type
            <div class="description">Video description</div>
        </div>
    </div>
    

    This works great for me but there is one small gotcha. I'm not sure how search engine friendly this is. I fear that google's web crawlers might not find this link so I also tend to include a traditional A HREF link somewhere in the block like this:

    <div class="layout" onclick="location.href='destination_url'">
        <div class="title">
        Video Type
            <div class="description">Video description</div>
        </div>
        <a href="destination_url">This is a link</a>
    </div>
    
    0 讨论(0)
  • 2020-11-28 07:31

    One easy way to make the div a link/clickable is by using html javascript onclick attribute:

    <div class="clickable-div" onclick="location.href='#';"><div> ... </div></div> 

    0 讨论(0)
  • 2020-11-28 07:33

    While the <a> tag is not allowed to contain <div> element, it is allowed to contain other inline elements such as <span>.

    When I encountered the problem i swapped the div tag with a <span>. Since the span tag is an inline element, you need to apply a display:block to the css of your <span> element, in order to make it behave like the <div> block element. This should be valid xhtml and does not require any javascript.

    Here's an example:

    <a href="#">
        <span style="display:block">
            Some content. Maybe some other span elements, or images.
        </span>
    </a>
    
    0 讨论(0)
提交回复
热议问题