【问题标题】:Hide part of a border in CSS?在 CSS 中隐藏部分边框?
【发布时间】:2015-03-29 01:27:01
【问题描述】:

我有一些简单的 CSS 类用于带有标题的面板。当背景是固定颜色时效果很好,因为我必须使用该颜色来隐藏标题文本后面的边框。但是,我现在想要一个背景图像而不是纯色。

有没有办法让标题元素覆盖父元素的边框并允许背景图像显示出来?我可以轻松地将颜色设置为transparent,但我没有found 将覆盖除纯色或图像以外的边框。

我有一种预感,它可以用 JS 来完成,并将图像重新用作标题的背景,并根据页面上的位置设置一些偏移量,但这似乎过于复杂,并且对我不起作用,因为我'想说明几个分层的背景图像形成一个模式,或者固定的附件,必须不断地重新计算。

有没有办法做到这一点?

.panel {
    border: 2px solid #000000;
    padding: 1em;
    margin: 1em;
    display: block;
}

.panel-header {
    position: relative;
    top: -1.5em;
    background-color: #ffffff;
    padding-left: 0.4em;
    padding-right: 0.4em;
    font-size: 1.2rem;
    font-weight: bold;
}

.panel-content {
    margin-top: -0.5em; 
}
<div class="panel">
  <span class="panel-header">
    Title Goes Here
    </span>
  <p class="panel-content">Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
  </div>

<hr/>

<div style="background-image: url(http://placekitten.com/510/200); padding: 2em;">
  <div class="panel">
    <span class="panel-header">
      Title Goes Here
      </span>
    <p class="panel-content">Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
    </div>
</div>

【问题讨论】:

  • 不确定我是否明白你想要什么。像这样jsfiddle.net/ht27wg5b
  • 我为您创建了一个可重复使用的版本,它可以非常简单地在任何图像上实现。

标签: html css background transparency


【解决方案1】:

我认为这是一个更有用的版本,它只需要一个额外的 div 并且可以转移到几乎任何图像。另外,这是一个很大的优势,不需要额外的图像部分。可重复使用!

.panel-wrapper {
    overflow: hidden;
}

.panel {
    border: 2px solid #000000;
    border-top: 0;
    padding: 1em;
    margin: 1em 0 0;
}

.panel-header {
    position: relative;
    top: -1.5em;
    padding-left: 0.4em;
    padding-right: 0.4em;
    font-size: 1.2rem;
    font-weight: bold;
}
.panel-header:before,
.panel-header:after {
    content: "";
    position: absolute;
    height: 2px;
    background: #000;
    top: 50%;
}
.panel-header:before {
    width: 100px;
    left: -100px;
}
.panel-header:after {
    width: 1000px;
    right: -1000px;
}
.panel-content {
    margin-top: -0.5em; 
}
<div>
    <div class="panel-wrapper">
  <div class="panel">
    <span class="panel-header">
      Title Goes Here
      </span>
    <p class="panel-content">Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
    </div>
    </div>
</div>

<div style="background-image: url(http://placekitten.com/510/200); padding: 2em;">
    <div class="panel-wrapper">
  <div class="panel">
    <span class="panel-header">
      Title Goes Here
      </span>
    <p class="panel-content">Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
    </div>
    </div>
</div>

【讨论】:

  • 我喜欢跳过顶部边框并用 :before 和 :after 伪装它的想法,我可能会在不同的项目中使用这种方法。不过,在这个项目中,我有时需要处理圆角,所以我不得不接受已接受的答案。
【解决方案2】:

这是您需要的样本:

首先,您需要一个带有背景的容器 div(我从互联网上抓取了一个用于此示例)。

在这之后,你需要从 HTML 中使用和 sn-ps。

你的 CSS

div {
    padding: 30px;
    background: url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSkAuRoZnkLBZlEYtgL5GJ5w_2Tufpxh5PqkOf-Negy7eL-JGC5Mk-DW-I) no-repeat  center center;
    background-size: cover;
}

fieldset {
    border: 2px solid black;
    padding: 10px;
}

您的 HTML

<div>
 <fieldset>
  <legend>Personalia:</legend>
  <p>some text here</p>
 </fieldset>
</div>

在这里你可以看到Fiddle

【讨论】:

  • 我不知道在表单之外使用字段集是否有效。这对我有用。
【解决方案3】:

这是一个常见问题。

我的解决办法是:

  1. .panel-header的背景设置为与背景图片相同。
  2. 设置z-index: 0使其位于顶层。
  3. 调整background-position 属性。

调整background-position时,也可以使用jQuery计算偏移量,使其准确。

希望对您有所帮助,请参阅 sn-p。


有效的代码只是添加:

.panel-header {
    z-index: 0;
    background-image: url(http://placekitten.com/510/200);
    background-position: -66px -38px;
}

片段:

.panel {
    border: 2px solid #000000;
    padding: 1em;
    margin: 1em;
    display: block;
}

.panel-header {
    position: relative;
    top: -1.5em;
    // here!
    z-index: 0;
    background-image: url(http://placekitten.com/510/200);
    background-position: -66px -38px;
    padding-left: 0.4em;
    padding-right: 0.4em;
    font-size: 1.2rem;
    font-weight: bold;
}

.panel-content {
    margin-top: -0.5em; 
}
<div class="panel">
  <span class="panel-header">
    Title Goes Here
    </span>
  <p class="panel-content">Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
  </div>

<hr/>

<div style="background-image: url(http://placekitten.com/510/200); padding: 2em;">
  <div class="panel">
    <span class="panel-header">
      Title Goes Here
      </span>
    <p class="panel-content">Glorious baklava ex librus hup hey ad infinitum. Non sequitur condominium facile et geranium incognito.</p>
    </div>
</div>

【讨论】:

  • 您必须创建这些小图像并将它们添加到正确的位置。我认为使用伪元素会更好
  • @Aaron 是的,实际上是个好建议,但要点是一样的。
  • 我知道可以这样,但是这些面板可以动态生成和放置,背景可以使用background-attachment:fixed;这意味着每次滚动事件都会更新位置,这不值得为我的需要付出努力。
  • @NathanRabe 所以你可以用javascript控制background-position。这个答案只是显示了一个解决方案,一个想法,也许不是最好的,但一个整洁的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 2014-05-15
  • 2023-03-26
  • 2021-05-07
  • 1970-01-01
相关资源
最近更新 更多