简体   繁体   English

需要在一侧有半宽边框,在另一侧有全宽边框

[英]Need to have half width border in one side and full width border in another side

Here is my html and css code:这是我的 html 和 css 代码:

 .image-box{ max-width: 300px; position: relative; }.image-box img{ max-width: 100%; width: 100%; object-fit: cover; border: 8px solid #000000; border-right: 0; border-bottom: 0; /* border-image: linear-gradient(to right, #000 68%, transparent 32%) 100% 1; */ }
 <div class="image-box"> <figure> <img src="https://via.placeholder.com/300" alt=""> </figure> </div>

While running this, I receive this.在运行它时,我收到了这个。

在此处输入图像描述

But, I want to have something like this:但是,我想要这样的东西:

在此处输入图像描述

If I uncomment this line如果我取消注释这一行

border-image: linear-gradient(to right, #000 68%, transparent 32%) 100% 1; 

在此处输入图像描述

Then it shows half width border on top, but this also make the left border disappear and show like this.然后它在顶部显示半宽边框,但这也会使左边框消失并像这样显示。

You can do something like this using pseudo classes to make the border at the top like how you want it to be achieved:您可以使用伪类做这样的事情,使顶部的边框像您希望的那样实现:

What it does is hide the half of the border at the top.它所做的是隐藏顶部的一半边框。

 .box { width: 150px; height: 150px; background-color: black; border-top: 5px solid red; border-left: 5px solid red; position: relative; }.box:after { content: ""; width: 50%; height: 5px; background-color: white; position: absolute; right: 0; top: -5px; }
 <div class="box"></div>

Disclaimer : Not an exhaustive list.免责声明:并非详尽清单。 Try to come up with yet another solution!尝试提出另一种解决方案!

With background-image: linear-gradient()带有background-image: linear-gradient()

To make a linear-gradient look like a border, we can add padding to the image and a hard color-stop to the gradient:为了使线性渐变看起来像边框,我们可以向图像添加填充并向渐变添加硬色标

 .image-box { --thickness: 8px; }.image-box img { padding-top: var(--thickness); padding-left: var(--thickness); background-image: linear-gradient(to right, black 60%, white 60%); } figure{margin:0;line-height:0}
 <div class="image-box"> <figure> <img src="https://via.placeholder.com/300"> </figure> </div>

A potential problem with this is that only one side of the border may be shorter.这样做的一个潜在问题是边界的只有一侧可能更短。

With Pseudo-elements使用伪元素

Pseudo-elements can be used for presentational styling. 伪元素可用于展示样式。 You can easily identify them in your CSS by their double-colons (eg ::before , ::marker ).您可以在 CSS 中通过双冒号轻松识别它们(例如::before::marker )。

Sidenote : While still supported for legacy reasons, they can also be written with a single-colon (eg :before ).旁注:虽然由于遗留原因仍受支持,但它们也可以用单冒号书写(例如:before )。 Do yourself a favour and use the double-colons.帮自己一个忙,使用双冒号。

We can use pseudo-elements to create the border.我们可以使用伪元素来创建边框。

I will use custom properties to make the code easier to change.我将使用自定义属性使代码更易于更改。
Personally, I enjoy the method Placing behind the most, because that way no bleeding through of the parent's background should happen in extreme (zoom) cases.就个人而言,我最喜欢放置在后面的方法,因为这样在极端(缩放)情况下不会发生父母背景的渗漏。

Composition作品

Composite the border with ::before for the left and ::after for the top side.左边用::before合成边框,顶边用::after合成边框。 Since these are separate elements, we can define their width and height individually:由于这些是独立的元素,我们可以单独定义它们的宽度和高度:

 .image-box::before, .image-box::after { content: ""; position: absolute; top: 0; left: 0; background-color: black; }.image-box::before { /* Left */ width: var(--thickness); height: 100%; }.image-box::after { /* Top */ width: 60%; height: var(--thickness); }.image-box { --thickness: 8px; position: relative; /* Leave space for border */ padding-top: var(--thickness); padding-left: var(--thickness); max-width: 300px; box-sizing: border-box; }.image-box img {max-width:100%} figure{margin:0;line-height:0}
 <div class="image-box"> <figure> <img src="https://via.placeholder.com/300"> </figure> </div>

Placing behind放在后面

Place the image on top of a pseudo-element to make it look like a (partially wide) border:将图像放在伪元素之上,使其看起来像(部分宽的)边框:

 .image-box::before, .image-box>figure { grid-area: 1/1 / 1/1; /* Place both on first grid-cell */ }.image-box::before { content: ""; width: 60%; height: 100%; background-color: black; }.image-box { --thickness: 8px; max-width: 300px; display: grid; }.image-box img { /* Leave space for border */ padding-top: var(--thickness); padding-left: var(--thickness); width: 100%; box-sizing: border-box; } figure{margin:0;line-height:0}
 <div class="image-box"> <figure> <img src="https://via.placeholder.com/300"> </figure> </div>

Using clip-path: polygon()使用clip-path: polygon()

Specify each point of the "top-left corner" shape in polygon() :polygon()中指定“左上角”形状的每个点:

 .image-box { position: relative; padding-top: 8px; padding-left: 8px; max-width:300px; }.image-box::before { --thickness: 8px; content: ""; position: absolute; top: 0; left: 0; width: 60%; height: 100%; clip-path: polygon( 0 0, 100% 0, 100% var(--thickness), var(--thickness) var(--thickness), var(--thickness) 100%, 0 100%); background-color: black; } figure {margin:0;line-height:0}
 <div class="image-box"> <figure> <img src="https://via.placeholder.com/300"> </figure> </div>

Why not use border ?为什么不使用border

Edit : Apparently border-image is a thing.编辑:显然border-image是一回事。 See an example .看一个例子 Bottom section is therefore outdated.因此底部部分已经过时。

Unfortunately, each side of a border can only be a single color.不幸的是,边框的每一边只能是一种颜色。 This means the black & white top border is not possible by only using the border property.这意味着仅使用border属性无法实现黑白顶部边框。

A solution to this would be to redraw over part of the border that we don't want:一个解决方案是重绘我们不想要的部分边界:

 #example { padding: 1rem; background-color: slateblue; }.image-box { --thickness: 8px; position: relative; border: 0 solid black; border-top-width: var(--thickness); border-left-width: var(--thickness); max-width: 300px; }.image-box::after { content: ""; position: absolute; top: calc(-1 * var(--thickness)); right: 0; /* 100% - <length> + <thickness left-side> */ width: calc(100% - 60% + var(--thickness)); height: var(--thickness); background-color: white; } body { display: flex; flex-direction: column; gap: 1rem; } figure{margin:0;line-height:0}
 <div class="image-box"> <figure> <img src="https://via.placeholder.com/300"> </figure> </div> <div id="example"> <div class="image-box"> <figure> <img src="https://via.placeholder.com/300"> </figure> </div> </div>

But as you can see, this way we will lose the background of the parent.但是正如你所看到的,这样我们将失去父级的背景。 Hard-coding some color only works if the parent's background is a solid color.仅当父母的背景是纯色时,硬编码某些颜色才有效。 If the parent's background is an image, this won't work.如果父母的背景是图像,这将不起作用。

Also, having to tell .image-box the color to draw over with is redundant information (because the background already exists) and would only cause more mental overhead for the developer.此外,必须告诉.image-box要绘制的颜色是多余的信息(因为背景已经存在)并且只会给开发人员带来更多的精神负担。

you are almost good with border-image.您几乎可以使用边框图像。 You need to correctly define the slice.您需要正确定义切片。

 .image-box { max-width: 300px; position: relative; }.image-box img { max-width: 100%; width: 100%; object-fit: cover; border: 8px solid #000000; border-right: 0; border-bottom: 0; border-image: linear-gradient(to right, #000 68%, transparent 32%) 1; }
 <div class="image-box"> <figure> <img src="https://via.placeholder.com/300" alt=""> </figure> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM