0

所以我想对显示标题、作者和帖子本身的内容部分进行编码,我正在使用的代码是我希望标题在一行顶部,然后作者在另一行,内容在另一行:

<div id="main">
    <div class="container">
        <div class="title">Title</div>
        <div class="author">Author</div>
        <div class="content">
            Content here
        </div>
    </div>
</div>

或者跨度会是这样一个更好的选择吗?

<div id="main">
    <div class="container">
        <span class="title">Title</span>
        <span class="author">Author</span>
        <span class="content">
            Content here
        </span>
    </div>
</div>

还是有完全不同的更好的方法来做到这一点?

谢谢你。

4

5 回答 5

1

div对内容进行分组span文本级语义。在给出的两个示例中,您最好使用后者:

<div id="main">
    <div class="container">
        <span class="title">Title</span>
        <span class="author">Author</span>
        <span class="content">
            Content here
        </span>
    </div>
</div>

但是,根据其中包含的内容,.content您可能想要使用div它。

为了正确符合您应该cite用于标题的规范。同样,如果您.content是引用或摘录,您应该使用qblockquote

<div id="main">
    <div class="container">
        <cite class="title">Title</cite>
        <span class="author">Author</span>
        <q class="content">
            Content here
        </q>
    </div>
</div>
于 2013-03-17T21:26:33.830 回答
1

也许您应该尝试使用 HTML 语义标记而不是divs?

<section id="main">
    <article>
        <hgroup>
            <h1>Title</h1>
            <h2>author</h2>
        </hgroup>
        <section>
            Content
        </section>
    </article>
</section>
于 2013-03-17T21:27:44.513 回答
1

没关系。所有支持一个优于另一个的答案都很好,但几乎是武断的意见。

最后,两者DIVSPAN只是通用容器。它们本身并没有任何意义。

因此,如果您必须在这两者之间进行选择,那没关系。但是,由于您的一个要求:

是我希望标题在一行顶部的位置,然后是另一行的作者和另一行的内容

...我建议DIV除了 div 之外没有其他原因,默认情况下,它是块级元素,这意味着它会自动为您格式化为单独的行。

综上所述,所有建议您考虑使用更多语义容器的人都是正确的。这会更好地对待ol吗?或者甚至只是p标签?

于 2013-03-17T21:46:00.863 回答
0

<div>标签应该用于您的页面划分为逻辑块。这里 a<span>会更合适。

但是,当您显示文本时,出现以下问题:

<div id="main">
    <div class="container">
        <h2 class="title">Title</h2>
        <a class="author">Author</a>
        <p class="content">
            Content here
        </p>
    </div>
</div>

毕竟,它们是专门为文本设计的。

于 2013-03-17T21:27:19.770 回答
0

如果问题只是一个选择divspan那么答案是......

span用于单个元素和div块。

第二个变体比第一个更好。

有关详细信息,请参阅分组元素:DIV 和 SPAN 元素

这是那里的报价:

这些元素将内容定义为内联 (SPAN) 或块级 (DIV)

于 2013-03-17T21:25:45.013 回答