【问题标题】:How can I create a gap in a CSS left border around an image?如何在图像周围的 CSS 左边框中创建间隙?
【发布时间】:2017-04-19 17:35:34
【问题描述】:

试图弄清楚如何通过 CSS 或 JavaScript/jQuery 在图像周围的 CSS 左边框中创建间隙。

我找到了几个答案,如何在顶部或底部边框上使用间隙来执行此操作,但无法弄清楚如何将其应用于左边框。

Here (https://ibb.co/cGS0vk) 是我努力实现的目标。

到目前为止,这是我的 HTML:

<div class="frame">
  <img class="quote" src="quote.jpg">
   <h2>Heading<h2>
   <p>Lorem ipsum<p>
</div>

这是 CSS:

.frame {
    Border-top: 10px sloid grey;
    Border-right: 10px sloid grey;
    Border-bottom: 10px sloid grey;
}
.quotes {
    position: relative;
    right: 100px;
}

【问题讨论】:

  • 图片指向错误的链接。请修复它。
  • 图片链接无效
  • 你需要分享你目前尝试过的代码
  • 抱歉现在更新了
  • 截图帮不了你,分享你的代码和尝试

标签: javascript jquery html css image


【解决方案1】:

您可以使用:before:after 等伪元素来实现。

.box {
  position: relative;
  overflow: hidden;
  width: 100px;
  height: 50px;
  border: solid #000;
  border-width: 5px 5px 5px 0;
}

.box:before,
.box:after {
  content: '';
  position: absolute;
  background: #000;
  height: 30%;
  width: 5px;
  top: 0;
  left: 0;
}

.box:after {
  top: auto;
  bottom: 0;
}
&lt;div class="box"&gt;&lt;/div&gt;

【讨论】:

  • 谢谢@DanPhilip 正是我想要的。
  • @martinschwartz 如果它有效,您能否接受答案。
  • 抱歉,我是 SO 新手,需要 15 名声望才能投票给答案...所以我现在无法执行此操作。不过,我可能会在我到 15 岁后再回来查看并投票支持您的答案。
  • 回答已接受!
【解决方案2】:

使用最少的要求我能想出的最佳主意:

使用::before/::after pseudo elements 将背景颜色匹配元素放置在现有边框的顶部。这基本上将您的边界“切片”了一部分。

HTML:

<div class="wrap">
  <div class="box"></div>
</div>

CSS:

.wrap { position: relative; width: 300px; height: 300px; background-color: #fff; }
.box { position: absolute; margin: auto; top: 0; bottom: 0; left: 0; right: 0; width: 200px; height: 200px; border: 10px solid #8af; }
.box::before { content: ''; position: absolute; width: 10px; height: 100px; margin: auto; left: -10px; top: 0; bottom: 0; background-color: #fff; }

JSFiddle:https://jsfiddle.net/gam9s0mz/

编辑 如前所述,此方法不适用于背景图像。但仅限于纯色背景色匹配。

【讨论】:

  • 它不会使边框透明,如果您需要添加背景图像而不是纯色。 @martinschwartz
  • @DanPhilip 没错,我以为我可以将颜色改为“透明”
  • 最好使用更少的标记和更多的伪元素,因为标记在放大时会给浏览器带来更多负载。
  • @martinschwartz 如果将颜色更改为透明,则下方的边框将可见。
  • 你是对的,除了边框样式,比如圆角边框。两种方法都有利有弊,因此请选择对您的实施有效的方法。然而,我看不出这会如何扩大规模,因为它使用的元素更少。 wrap div 只是为了在我的示例中显示对比。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-10
  • 2018-07-21
  • 2012-06-05
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-08-31
相关资源
最近更新 更多