2张图片并排显示,响应式时裁剪路径出现问题。

huangapple go评论67阅读模式
英文:

2 images side by side, clip-path messes up when responsive

问题

以下是翻译好的内容:

我已经卡在这里一段时间了,但我无法正确地弄清楚如何做到这一点。

我想要的结果是将两张图片并排放置,并在它们之间留有间隙。

我的问题是当浏览器调整大小时,它不会保持间隙,图片会“合并”,或者在扩展浏览器时间隙会变大。

**代码:**

<!-- 开始片段: js 隐藏: false 控制台: true 通用: false -->

<!-- 语言: lang-css -->

.hs-media {
  --hs-media-overlap: 7.5rem;
  --hs-media-col-size: calc(var(--hs-media-overlap) / 2);
  list-style: none;
  display: grid;
  gap: 1rem;
  grid-template-columns: [one] auto [two-begins] minmax(0, var(--hs-media-col-size)) minmax(0, var(--hs-media-col-size)) [one-ends] auto [two];
}

[class*="hs-media__col-"] {
  grid-row: 1;
  position: relative;
  display: flex;
  min-block-size: 100%;
}

.hs-media__col-1 {
  grid-column: one/one-ends;
  clip-path: polygon(0% 0%, 100% 0%, 85% 100%, 0% 100%);
}

.hs-media__col-1::before {
  content: '';
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
  block-size: 100%;
  inline-size: 100%;
  background: rgba(51, 153, 68, 0.79);
  mix-blend-mode: multiply;
}

.hs-media__line {
  grid-column: one/line;
  grid-row: 1;
  block-size: 100%;
  background-color: white;
  clip-path: polygon(0% 0%, 100% 0%, 85% 100%, 0% 100%);
}

.hs-media__col-2 {
  grid-column: two-begins/two;
  clip-path: polygon(15% 0, 100% 0%, 100% 100%, 0% 100%);
}

.hs-media__image {
  inline-size: 100%;
  object-fit: cover;
}

<!-- 语言: lang-html -->

<ul class="hs-media">
  <li class="hs-media__col-1">
    <img src="https://source.unsplash.com/Ul-Cp7f_CGQ/1440x546" class="hs-media__image" />
  </li>

  <li class="hs-media__col-2">
    <img src="https://source.unsplash.com/t8QeaJkxp4M/1800x546" class="hs-media__image" />
  </li>
</ul>

<!-- 结束片段 -->

  [1]: https://i.stack.imgur.com/Tmf1M.jpg
英文:

I been stuck on this for a while but I cant figure out how to do this correctly.

My wanted result is to have two images side by side and a gap between them.

My problem is that when the browser resize it wont keep the gap and the images "merge" or extending the browser the gap gets bigger.

2张图片并排显示,响应式时裁剪路径出现问题。

code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.hs-media {
  --hs-media-overlap: 7.5rem;
  --hs-media-col-size: calc(var(--hs-media-overlap) / 2);
  list-style: none;
  display: grid;
  gap: 1rem;
  grid-template-columns: [one] auto [two-begins] minmax(0, var(--hs-media-col-size)) minmax(0, var(--hs-media-col-size)) [one-ends] auto [two];
}

[class*=&quot;hs-media__col-&quot;] {
  grid-row: 1;
  position: relative;
  display: flex;
  min-block-size: 100%;
}

.hs-media__col-1 {
  grid-column: one/one-ends;
  clip-path: polygon(0% 0%, 100% 0%, 85% 100%, 0% 100%);
}

.hs-media__col-1::before {
  content: &#39;&#39;;
  position: absolute;
  inset-block-start: 0;
  inset-inline-start: 0;
  block-size: 100%;
  inline-size: 100%;
  background: rgba(51, 153, 68, 0.79);
  mix-blend-mode: multiply;
}

.hs-media__line {
  grid-column: one/line;
  grid-row: 1;
  block-size: 100%;
  background-color: white;
  clip-path: polygon(0% 0%, 100% 0%, 85% 100%, 0% 100%);
}

.hs-media__col-2 {
  grid-column: two-begins/two;
  clip-path: polygon(15% 0, 100% 0%, 100% 100%, 0% 100%);
}

.hs-media__image {
  inline-size: 100%;
  object-fit: cover;
}

<!-- language: lang-html -->

&lt;ul class=&quot;hs-media&quot;&gt;
  &lt;li class=&quot;hs-media__col-1&quot;&gt;
    &lt;img src=&quot;https://source.unsplash.com/Ul-Cp7f_CGQ/1440x546&quot; class=&quot;hs-media__image&quot; /&gt;
  &lt;/li&gt;

  &lt;li class=&quot;hs-media__col-2&quot;&gt;
    &lt;img src=&quot;https://source.unsplash.com/t8QeaJkxp4M/1800x546&quot; class=&quot;hs-media__image&quot; /&gt;
  &lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

答案1

得分: 0

对我来说,看起来你把它弄得太复杂了,这对你来说可以吗?

斜杠将始终占据总宽度的1%。

英文:

To me it looks like you are complicating it too much, is this ok for you ?

diagonal slash will always take 1% of total width

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  position: relative;
  width: 100%;
  display: flex;
  flex-flow: row nowrap;
  justify-content: center;
  overflow: hidden;
}

.container img {
  width: 55%;
  object-fit: cover;
}

.container img:nth-child(1) {
  clip-path: polygon(0% 0%, 100% 0%, 85% 100%, 0% 100%);
  transform: translateX(7%)
}

.container img:nth-child(2) {
  clip-path: polygon(15% 0, 100% 0%, 100% 100%, 0% 100%);
  transform: translateX(-7%)
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;img src=&quot;https://source.unsplash.com/Ul-Cp7f_CGQ/1440x546&quot; class=&quot;hs-media__image&quot; /&gt;
  &lt;img src=&quot;https://source.unsplash.com/t8QeaJkxp4M/1800x546&quot; class=&quot;hs-media__image&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

这是您提供的代码部分的翻译:

.gallery {
  --s: 70px; /* 控制倾斜部分 */
  
  display: flex;
  height: 280px; /* 图片的高度 */
  gap: 8px; /* 控制间距 */
}
.gallery > img {
  flex: 1;
  min-width: 0;
  object-fit: cover;
  cursor: pointer;
  transition: .5s;
}
.gallery > img:hover {
  flex: 1.8; 
}
.gallery > img:first-child {
  margin-right: calc(-.5*var(--s));
  clip-path: polygon(0 0,100% 0,calc(100% - var(--s)) 100%,0 100%);
}
.gallery > img:last-child {
  margin-left: calc(-.5*var(--s));
  clip-path: polygon(var(--s) 0,100% 0,100% 100%,0 100%);
}

body {
  background: #ECD078;
}
<div class="gallery">
  <img src="https://picsum.photos/id/433/600/400" alt="A Bear">
  <img src="https://picsum.photos/id/593/600/400" alt="A Tiger">
</div>

请注意,以上内容只是代码的翻译部分。

英文:

I have a detailed article about such layouts where you can also have hover effect: https://css-tricks.com/css-grid-and-custom-shapes-part-2/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.gallery {
  --s: 70px; /* control the slanted part */
  
  display: flex;
  height: 280px; /* the height of the images*/
  gap: 8px; /* control the gap */
}
.gallery &gt; img {
  flex: 1;
  min-width: 0;
  object-fit: cover;
  cursor: pointer;
  transition: .5s;
}
.gallery &gt; img:hover {
  flex: 1.8; 
}
.gallery &gt; img:first-child {
  margin-right: calc(-.5*var(--s));
  clip-path: polygon(0 0,100% 0,calc(100% - var(--s)) 100%,0 100%);
}
.gallery &gt; img:last-child {
  margin-left: calc(-.5*var(--s));
  clip-path: polygon(var(--s) 0,100% 0,100% 100%,0 100%);
}


body {
  background: #ECD078;
}

<!-- language: lang-html -->

&lt;div class=&quot;gallery&quot;&gt;
  &lt;img src=&quot;https://picsum.photos/id/433/600/400&quot; alt=&quot;A Bear&quot;&gt;
  &lt;img src=&quot;https://picsum.photos/id/593/600/400&quot; alt=&quot;A Tiger&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月24日 20:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定