【问题标题】:Omitting a border when there are 2 consecutive paragraphs当有 2 个连续段落时省略边框
【发布时间】:2018-12-10 00:11:03
【问题描述】:

我有一个包含边框的段落样式:

p.caution {
    border-top: 1.5pt double #FF0000;
    border-bottom: 1.5pt double #FF0000;
}

当我的文档包含 2 个连续的“注意”段落时,我想省略这些段落之间的边界。 我想省略两个边框:第一段的border-bottom和第二段的border-top。

所以这是我想要的结果:

似乎没有允许我查看下一段的 CSS 选择器。
border-collapse: collapse; 也没有所需的结果。

这可能吗? (我正在使用 Antennahouse 渲染器处理 CSS Paged Media,但这似乎不是 Paged Media 特有的问题)

HTML sn-p:

<div>
  <p class="other">some text</p>
  <p class="caution">some text</p>
  <p class="caution">more text</p>
  <p class="other">some text</p>
</div>

【问题讨论】:

    标签: css antenna-house


    【解决方案1】:

    新答案

    你可以试试这样的:

    p.caution {
      border-top: 1.5pt double #FF0000;
      border-bottom: 1.5pt double #FF0000;
      margin:2px;
      padding:10px;
    }
    p.caution + p.caution {
      border-top-color:#fff;
      margin-top:-3pt;
      position:relative;
    }
    <div>
      <p class="other">some text</p>
      <p class="caution">some text</p>
      <p class="caution">more text</p>
      <p class="other">some text</p>
    </div>

    旧答案

    如果所有p 都在同一个容器中,您可以尝试如下操作:

    p.caution {
      border-top: 1.5pt double #FF0000;
      margin:0;
      padding:20px;
    }
    p.caution:last-child {
        border-bottom: 1.5pt double #FF0000
    }
    <div>
      <p class="caution">some text</p>
      <p class="caution">some text</p>
      <p class="caution">more text</p>
      <p class="caution">some text</p>
    </div>

    只有一个p 也可以正常工作:

    p.caution {
      border-top: 1.5pt double #FF0000;
      margin:0;
      padding:20px;
    }
    p.caution:last-child {
        border-bottom: 1.5pt double #FF0000
    }
    <div>
      <p class="caution">some text</p>
    </div>

    更新

    要省略p 之间的所有边界,您可以试试这个:

    p.caution {
      margin:0;
      padding:20px;
    }
    p.caution:first-child {
        border-top: 1.5pt double #FF0000
    }
    p.caution:last-child {
        border-bottom: 1.5pt double #FF0000
    }
    <div>
      <p class="caution">some text</p>
      <p class="caution">some text</p>
      <p class="caution">more text</p>
      <p class="caution">some text</p>
    </div>

    【讨论】:

    • border-top 不应该只用于p.caution:first-child吗?
    • @IiroP 可以,但不应该 ;) .. 有两种不同的设置,两者都很好,我也会添加这个
    • 我的意思是,目前段落之间仍有边界,这不是 OP 想要的。
    • @IiroP 好吧,我明白 OP 想要省略一个边框......也许是错的,让我们看看他们会说什么
    • 同一个容器中其他样式的段落比较多,所以好像不能使用first-child和last-child。
    【解决方案2】:

    以下可能是一个解决方案:

    p {
      margin: 0; 
    }
    
    .caution {
      border-top: 4px double red;
    }
    
    .caution + .caution {
      border-top: none;
    }
    
    .caution:last-child {
      border-bottom: 4px double red;
    }
    
    .caution + p:not(.caution) {
      border-top: 4px double red;
    }
    <div>
      <p class="other">some text</p>
      <p class="caution">some text</p>
      <p class="caution">more text</p>
      <p class="other">some text</p>
    </div>

    这段代码在做什么:

    • border-top 添加到.caution
    • 如果.caution 是另一个具有.caution 类的元素的相邻兄弟元素,请移除其上边框。
    • 如果.caution:last-child,添加border-bottom
    • 如果.caution 有一个没有.cautionclass 的相邻兄弟(这也意味着它不是最后一个孩子,因此前面的情况不适用)将border-top 添加到相邻兄弟。

    这将与一个、两个或多个连续的p.caution 一起按预期工作。

    【讨论】:

    • 有趣的方法,但是通过将最后一个边框附加到.caution + p:not(.caution),很难在所有情况下都获得正确的间距。
    • 是的,确实如此。您仍然可以通过自定义填充替换默认边距来解决这个问题。
    • 例如:p {padding: 8px 0;} p:first-child {padding-top: 0;} p:last-child {padding-bottom: 0;}
    【解决方案3】:

    如果您承诺不会将::before 用于任何可以遵循警告的内容:

    .caution {
      border-top: 1.5pt double red;
    }
    .caution:last-child {
      border-bottom: 1.5pt double red;
    }
    
    .caution + .caution {
      border-top: none;
    }
    .caution + *:not(.caution) {
      margin-top: -1.12em;
    }
    .caution + *:not(.caution)::before {
      border-top: 1.5pt double red;
      display:block;
      content: "";
      margin-bottom: 1.12em;
    }
    

    1.12em 来自 AH Formatter 附带的html.css。如果您使用不同的值,那么您也需要在此处使用它。

    【讨论】:

    • 有趣,但不幸的是我需要在某些段落样式上使用 :before。
    【解决方案4】:

    我最终使用 XSLT 来修改我的输入 HTML。我添加了一个属性来指定我希望边框出现的位置:

    <xsl:template match="p[@class='caution']">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:attribute name="border-after">
                    <xsl:choose>
                        <xsl:when test="following-sibling::*[1]/@class='caution'">no</xsl:when>
                        <xsl:otherwise>yes</xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
                <xsl:attribute name="border-before">
                    <xsl:choose>
                        <xsl:when test="preceding-sibling::*[1]/@class='caution'">no</xsl:when>
                        <xsl:otherwise>yes</xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:template>
    

    在我的 CSS 中:

    p.caution[border-after="yes"] {
        border-bottom: 3pt double #FF0000;
    } 
    p.caution[border-after="no"] {
        margin-bottom: 3pt;
    } 
    p.caution[border-before="yes"] {
        border-top: 3pt double #FF0000;
    }
    p.caution[border-before="no"] {
        margin-top: 3pt;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 2011-08-10
      • 2019-05-15
      • 2014-10-31
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多