【问题标题】:How to cover piece of border with element over it?如何用元素覆盖一块边框?
【发布时间】:2016-08-11 07:10:41
【问题描述】:

In this example,div的边框顶部挂着一个数字。为了清楚起见,我想用数字覆盖边框的哪一部分,我也给了数字边框。当然,我们可以为数字提供背景颜色,但是身体的背景图像被隐藏了。如何隐藏数字透明的边框?

HTML:

<div class="reg-step first-step">
   <span class="step-number">1</span>
   <img src="" alt="Register">
   <h1>Register</h1>
   <div class="steps-separator"></div>
   <p>Lorem ipsum dolor sit amer, consetetur sed iam nonumy eirmod tempor invidunt ut labore et dolore magna aliquayam erat, sed diam vo</p>
</div>

CSS:

body{ 
  background: blue;
  padding-top: 30px;
  background-image: url('https://pbs.twimg.com/profile_images/993504415/css_400x400.png')
}
.reg-step {
    width: 240px;
    height: 325px;
    border: 3px solid white;
    color: white;
    position: relative;
    text-align: center;
    padding: 0px 7px;
}
.reg-step h1 {
    font-size: 14px;
    text-transform: uppercase;
}
.reg-step p {
    font-size: 11px;
}
.reg-step img {
    display: block;
    margin: 10px auto 20px;
}
.reg-step .step-number {
    font-size: 40px;
    margin: 0 auto;
    display: inline-block;
    position: relative;
    top: -23px;
    width: 50px;
    border: 1px solid black;
    background-color: blue;
}
.reg-step .steps-separator {
    width: 90%;
    margin: 20px auto;
    height: 1px;
    background-color: white;
    padding: 0px 7px;
}

想要的结果如下所示:

【问题讨论】:

    标签: html css


    【解决方案1】:

    你可以使用

    <fieldset>
    

    为此。

    检查小提琴 https://jsfiddle.net/sepyzund/

    html:

    <fieldset class="reg-step first-step">
        <legend class="step-number">1</legend>
        <img src="" alt="Register">
        <h1>Register</h1>
        <div class="steps-separator"></div>
        <p>Lorem ipsum dolor sit amer, consetetur sed iam nonumy eirmod tempor invidunt ut labore et dolore magna aliquayam erat, sed diam vo</p>
    </fieldset>
    

    css:

    body{ 
      background: blue;
      padding-top: 30px;
      background-image: url('https://pbs.twimg.com/profile_images/993504415/css_400x400.png')
    }
    .reg-step {
        width: 240px;
        height: 325px;
        border: 3px solid white;
        color: white;
        position: relative;
        text-align: center;
        padding: 0px 7px;
    }
    .reg-step h1 {
        font-size: 14px;
        text-transform: uppercase;
    }
    .reg-step p {
        font-size: 11px;
    }
    .reg-step img {
        display: block;
        margin: 10px auto 20px;
    }
    .reg-step .step-number {
        font-size: 40px;
        display: block;
        margin: 0 auto;
        width: 50px;
        border: 1px solid black;
    }
    .reg-step .steps-separator {
        width: 90%;
        margin: 20px auto;
        height: 1px;
            background-color: white;
        padding: 0px 7px;
    }
    

    【讨论】:

    • 是的,正是我想要的。顺便说一下火狐。在字段集中有一些故障居中图例。但&lt;legend align="center"&gt;legend&lt;/legend&gt; 解决了这个问题。
    【解决方案2】:

    如果你想要颜色透明,可以写成color: rgba(255, 255, 255, 0.5); 这里最后一个参数alpha就是你的透明度级别。您可以在 01 之间设置透明度。

    【讨论】:

    • 我应该将此规则应用于哪个元素?我认为背景或字体颜色的透明度会隐藏边框,因为它是透明的。
    • 如果你想隐藏边框,那么你可以写成border: 1px solid rgba(0, 0, 0, 0);,这样会隐藏你的边框。
    • 尝试使用fieldset 而不是顶部的div,对于该数字,您可以设置legend 而不是div
    【解决方案3】:

    我想到的第一个想法。使用像.reg-step:after 这样的伪元素作为底部边框,使用.reg-step 中的border-image 作为棘手的顶部(以及左右边框)。检查这个:

    body{ 
      background: blue;
      padding-top: 30px;
      background-image: url('https://pbs.twimg.com/profile_images/993504415/css_400x400.png')
    }
    .reg-step {
        width: 240px;
        height: 325px;
        color: white;
        position: relative;
        text-align: center;
        padding: 0px 7px;
      
        border: 3px solid;
        border-image:   linear-gradient(to right, white 40%, transparent 40%,  transparent 60%, white 60%) 1;
    }
    .reg-step:after {
      content: " ";
      position:absolute;
      top:0px; left:0px;
      width:100%;
      height:100%;
      border-bottom: 3px solid white;
    }
    .reg-step h1 {
        font-size: 14px;
        text-transform: uppercase;
    }
    .reg-step p {
        font-size: 11px;
    }
    .reg-step img {
        display: block;
        margin: 10px auto 20px;
    }
    .reg-step .step-number {
        font-size: 40px;
        display: block;
        margin: 0 auto;
        display: inline-block;
        position: relative;
        top: -23px;
        width: 50px;
        border: 1px solid black;
    }
    .reg-step .steps-separator {
        width: 90%;
        margin: 20px auto;
        height: 1px;
        background-color: white;
        padding: 0px 7px;
    }
    <div class="reg-step first-step">
      <span class="step-number">1</span>
      <img src="" alt="Register">
      <h1>Register</h1>
      <div class="steps-separator"></div>
      <p>Lorem ipsum dolor sit amer, consetetur sed iam nonumy eirmod tempor invidunt ut labore et dolore magna aliquayam erat, sed diam vo</p>
    </div>

    【讨论】:

    • 很有趣,我想我应该接受你的正确答案,因为&lt;fieldset&gt; 标签用于对表单中的相关元素进行分组。但后来稍微研究了一下,its valid HTML5 to use fieldset without form:]Honza 有点快..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多