把一个div放在锚内是否正确? [英] Is putting a div inside an anchor ever correct?

查看:112
本文介绍了把一个div放在锚内是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < a href = http://www.mydomain.com >< DIV> 
我们在这里有一个问题。
您会看到,锚元素是内联元素,
,div元素是块级元素。
< / div>< / a>

但是,如果将外部锚点设置为 display:block 在样式表中?它仍然是错的吗?关于块级和内联元素的HTML 4.01规范a>似乎认为如此:


样式表为
指定了任意
元素的渲染方式,包括元素
是否呈现为块或内联。在
中有些情况下,例如列表元素的内联样式
,这可能是
合适的,但一般来说,
作者不鼓励从
覆盖传统
这样解释
中的HTML元素。


有没有人有关于这个问题的进一步提示?根据你所服务的HTML版本:

$ b


  • HTML 5 指出< a> 元素可以包裹整个段落,列表,表格等等,甚至整个部分,只要内部没有交互式内容(例如按钮或其他链接)。


  • HTML 4.01 指定< a> 元素只能包含内联元素< div> 块元素,因此它可能不会出现在< a> 中。

    当然,您可以自由设计内联元素,使其出现为块,或确实对块进行样式化,以便内联呈现。在HTML中使用 inline block 这两个术语是指元素与文档语义结构的关系,而CSS中的相同术语更多地与元素的视觉样式相关。如果您以块状方式显示内联元素,那很好。



    然而,例如,当CSS不存在时,您应该确保文档的结构仍然有意义,例如当通过屏幕阅读器等辅助技术进行访问时 - 或者实际上当由强大的Googlebot进行检查时。

  • I've heard that putting a block element inside a inline element is a HTML sin:

    <a href="http://www.mydomain.com"><div>
    What we have here is a problem. 
    You see, an anchor element is an inline element,
    and the div element is a block level element.
    </div></a>
    

    But what about if you style the outer anchor as display:block in the stylesheet? Is it still wrong? The HTML 4.01 spec on block-level and inline elements seems to think so:

    Style sheets provide the means to specify the rendering of arbitrary elements, including whether an element is rendered as block or inline. In some cases, such as an inline style for list elements, this may be appropriate, but generally speaking, authors are discouraged from overriding the conventional interpretation of HTML elements in this way.

    Does anyone have any further tips about this issue?

    解决方案

    Depending on the version of HTML you're catering to:

    • HTML 5 states that the <a> element "may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links)".

    • HTML 4.01 specifies that <a> elements may only contain inline elements. A <div> is a block element, so it may not appear inside an <a>.

      Of course you are at liberty to style an inline element such that it appears to be a block, or indeed style a block so that it is rendered inline. The use of the terms inline and block in HTML refers to the relationship of the elements to the semantic structure of the document, whereas the same terms in CSS are related more to the visual styling of the elements. If you make inline elements display in a blocky manner, that's fine.

      However you should ensure that the structure of the document still makes sense when CSS is not present, for example when accessed via an assistive technology such as a screen reader - or indeed when examined by the mighty Googlebot.

    这篇关于把一个div放在锚内是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆