【问题标题】:style guide for multiline html多行 html 的样式指南
【发布时间】:2015-09-15 05:17:06
【问题描述】:

我有一些冗长的 HTML,超过了我的项目的 80 个字符的限制。我们有一个样式指南,它限制了每行的字符数,这很好,因为现在行很长,如果不向右滚动就看不到所有内容。

<div id="email-signup-container">
  <div id="mc_embed_signup">
    <div class="interested">
      <div class="container">

        <div class="left col-lg-6 col-md-6 col-sm-12 col-xs-12">
          <h3>Help New York residents keep the heat on this winter.</h3>
          <a href="http://www.nycharities.org/donate/charitydonate.asp?ID=4081" class="donate-btn btn-interest">DONATE</a>
        </div>

        <div class="right col-lg-6 col-md-6 col-sm-12 col-xs-12">
          <h3>Without heat? Visit our resources page.</h3>
          <a class="btn-interest" href="resources">RESOURCES</a>
        </div>

      </div>
    </div>
  </div>
</div>

很遗憾,我找不到任何涵盖多行 HTML 的样式指南。我参与了一个项目,我们按属性对事物进行了换行,但这是有争议的:

          <a
             href="http://www.nycharities.org/donate/charitydonate.asp?ID=4081"
             class="donate-btn btn-interest">DONATE</a>

有些人希望像这样在新行上结束carot:

          <a
             href="http://www.nycharities.org/donate/charitydonate.asp?ID=4081"
             class="donate-btn btn-interest"
          >DONATE</a>

其他人希望结束标签与开始标签处于同一级别:

          <a
             href="http://www.nycharities.org/donate/charitydonate.asp?ID=4081"
             class="donate-btn btn-interest"
          >
            DONATE
          </a>

我有点讨厌他们所有人。任何人都可以指出任何已发布的包含此内容的样式指南,以便我们可以采用一个并继续前进吗?

【问题讨论】:

    标签: html coding-style multiline


    【解决方案1】:

    不是一个明确的答案,但我想补充一点:属性缩进两次,内容只缩进一次。

    一个例子:

    <a
            href="http://www.nycharities.org/donate/charitydonate.asp?ID=4081"
            class="donate-btn btn-interest">
        DONATE
    </a>
    

    对于a 标签,这种看起来很奇怪,但对于较长的标签名称,如 Angular 中常见的,它看起来不错:

    <my-component
            attribute1="val"
            attribute2="val2"
            attribute3="etc">
        <span>Hello :)</span>
    </my-component>
    

    【讨论】:

    • 你只是举了一个例子,没有解释你在做什么。也没有推理
    • @aross 你是对的。我添加了一个关于我的例子的小解释。这样够清楚吗?
    • @TheOddler 在属性上缩进两次有什么好处?
    • @AJHsu 只是为了轻松查看属性的结束位置和内容的开始位置。如果您只在内容和属性处于同一级别时才缩进,这使得很难看到一个结束另一个开始。
    【解决方案2】:

    我也一直在想这个。我唯一能找到的是the guide from GoCardless,上面写着:

    <!-- Try using line-breaks for multiple attributes, keeping the first attribute
    on the tag's opening line -->
    
    Good:
    
    <div custom-attribute 
    class="something" 
    role="something-else"></div> 
    <!-- The closing tag    ^^^    can stay on the same line for empty elements-->
    
    <div custom-attribute 
    class="something" 
    role="something-else">
    ··Foo <!-- Otherwise nest plz -->
    </div> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2013-04-15
      • 2011-08-22
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多