简体   繁体   English

在初始布局中命名DIV的最佳实践

[英]Best practice for naming DIVs in initial layout

Newbie question here. 这里有新手问题。 I'm in the beginning stages of laying out a site in HTML/CSS based on a sketch I did. 我正处于基于我所做的草图在HTML / CSS中布置网站的开始阶段。 So I'm planning my DIVs on the home page and I'm wondering how to name them and which elements should have their own containers. 所以我在主页上计划我的DIV,我想知道如何命名它们以及哪些元素应该有自己的容器。 So far, I have the following : 到目前为止,我有以下内容:

  • container (contains everything on the page) 容器(包含页面上的所有内容)
  • header (will contain the logo and links, which are sort of connected visually) 标题(将包含徽标和链接,它们在视觉上是一种连接)
  • footer 页脚
  • content (this will only contain a widget that plays audio tracks) 内容(这将只包含播放音轨的小部件)

Now, I also want, on the right side of my page, in a sidebar of sorts, three other elements: 现在,我还想在页面的右侧,在一个侧栏中,还有其他三个元素:

  • a box containing some headlines/descriptions of an RSS feed from my blog 一个包含来自我博客的RSS源的标题/描述的框
  • a box containing a few dates of upcoming events 包含即将发生的事件的几个日期的框
  • a box containing some links to my other sites 一个包含指向其他网站的链接的框

I'm looking for opinions as to what is the best (and I guess most "semantic") way of handling the DIVs for those "sidebar" elements. 我正在寻找关于那些“侧边栏”元素处理DIV的最佳方式(我猜最“语义”)的意见。 Since my plan is to have those three boxes aligned on top of each other, should I just create one main "sidebar" DIV and then create separate DIVs for each of the elements ("feed-box", "events-box", and "links-box)? Or should there be no "sidebar" DIV (doesn't that imply style/position anyway?), and instead just use the three specific DIVs? 由于我的计划是将这三个框对齐,我应该创建一个主“侧边栏”DIV,然后为每个元素创建单独的DIV(“feed-box”,“events-box”和“链接盒子”?或者应该没有“侧边栏”DIV(这不是暗示风格/位置吗?),而只是使用三个特定的DIV?

Also, should I give that box that contains the audio widget an id of "content" (even though I know exactly what is going into it) or should it be something like "audio-widget"? 另外,我应该给那个包含音频小部件的框一个“内容”的ID(即使我确切知道它会发生什么),还是应该像“audio-widget”? (Who knows, maybe a year from now, it will hold some other kind of content instead.) (谁知道,也许一年之后,它会保留其他类型的内容。)

What do you think is the best practice for this kind of naming? 您认为这种命名的最佳实践是什么? I realize that there is no one correct answer and that it might be pedantic to get too hung up on these names. 我意识到没有一个正确的答案,而且过于依赖这些名字可能是迂腐的。 But the "best" way of handling this is not obvious to me and I would like to be clear from the beginning as to why I'm adopting a given naming convention 但处理这个问题的“最佳”方式对我来说并不明显,我想从一开始就清楚我为什么采用给定的命名约定

Thanks. 谢谢。

The first question you need to ask is this: is your layout fixed or variable width? 您需要问的第一个问题是:您的布局是固定的还是可变宽度?

If it's fixed width, the usual practice is to wrap everything in an overall div (usually called "container" or "wrapper"). 如果它是固定宽度,通常的做法是将所有内容包装在一个整体div中(通常称为“容器”或“包装器”)。 You have this already. 你已经有了。

How you lay out the rest of your divs then affects the order in which they appear in your document (eg using floats tends to "invert" the natural order). 然后,如何布置其余的div会影响它们在文档中出现的顺序(例如,使用浮动倾向于“反转”自然顺序)。

I would probably have something like this: 我可能会有这样的事情:

<div id="container">
  <div id="header"></div>
  <div id="content"></div>
  <div id="sidebar">
    <div id="headlines"></div>
    <div id="events"></div>
    <div id="links"></div>
  </div>
  <div id="footer"></div>
</div>

with rudimentary CSS: 基本的CSS:

html, body, div { margin: 0; padding: 0; border: 0 none; }
#container { width: 960px; margin: 0 auto; }
#content { width: 760px; float: left; }
#sidebar { width: 200px; float: right; }
#footer { clear: both; }

The only correct answer is to use as few containers as possible to get the job done. 唯一正确的答案是使用尽可能少的容器来完成工作。 Then supplying an ID value just remember each ID value must be unique. 然后提供ID值只记得每个ID值必须是唯一的。 DO NOT add extra div elements just to have extra naming conventions. 不要添加额外的div元素只是为了有额外的命名约定。 A div is a semantic element, whose meaning is a division of a document, that is often abused. div是一个语义元素,其含义是文档的一个分区,经常被滥用。

Here is how I'd recommend. 这是我推荐的方式。 I prefer to keep the id="container" reserved and available for a content (as opposed to header or footer) section <div> id. 我更喜欢保留id="container"保留并可用于内容(而不是页眉或页脚)部分<div> id。 That way content section id elements can use id="content" and I don't have to worry about collisions. 这样内容部分id元素可以使用id="content" ,我不必担心冲突。

<div class="container">
  <div class="header">
    <div class="headerLeft"></div>
    <div class="headerMiddle"></div>
    <div class="headerRight"></div>
    <div style="clear:both"></div>
  </div>
  <div class="content">
    <div class="contentLeft"></div>
    <div class="contentMiddle">
      <div id="style" onclick="javascript:document.getElementById(this.id).setAttribute('visibility','hidden');">
        Style
      </div>
      <div id="content" onclick="javascript:document.getElementById(this.id).setAttribute('visibility','hidden');">
        Content
      </div>
      <div id="originality" onclick="javascript:document.getElementById(this.id).setAttribute('visibility','hidden');">
        Originality
      </div> 
    </div>
    <div class="contentRight">
      <div class="feedBox"></div>
      <div class="eventsBox"></div>
      <div class="linksBox"></div>
      <div class="feedBox"></div>
      <div class="eventsBox"></div>
      <div class="linksBox"></div>
    </div>
    <div style="clear:both"></div>
  </div>
  <div class="footer">
    <div class="footerLeft"></div>
    <div class="footerMiddle"></div>
    <div class="fooderRight"></div>
    <div style="clear:both"></div> 
  </div>
</div>

1) The above example may re-render the entire content section of the web page hidden instead of only that div if the container also had id="content" as intended if the id attribute is used instead of class . 1)如果使用id属性而不是class ,如果容器也按预期具有id="content" ,则上述示例可以重新呈现网页的整个内容部分而不仅仅是该div。

2) If you might want to have more that one feed-box or other element with multiple occurrences, classes are the only way to go. 2)如果您可能希望有更多的那个feed-box或其他多次出现的元素,那么类是唯一的方法。 Only one id can be used per page, but many classes can occur. 每页只能使用一个id ,但可能会出现许多classes

Some basic CSS to style it: 一些基本的CSS来设计它:

.container { width: 960px; margin: 0 auto; }
.header{ background-color:#000000; color:#FFFFFF; }
.headerLeft{ width:0px; float:left; }
.headerMiddle{ width: 960px; float:left; }
.headerRight{ width: 0px; float:right; }
.content { background-color:#000000; color:#FFFFFF;}
.contentLeft { width: 200px; float: left; color:#FFFFFF;}
.contentMiddle{ width: 680px; float: left; min-height:550px; background-color:#FFFFFF; color:#000000;}
.contentRight{ width: 180px; float: right; color:#FFFFFF;}
.footer { background-color:#000000; color:#FFFFFF; }
.footerLeft { width:0px; float:left; }
.footerMiddle { width: 960px; float:left; }
.footerRight { width: 0px; float:right; }
.feedBox {}
.eventsBox {}
.linksBox {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM