CSS `width:max-content`在Firefox中无法正常工作

jxct1oxe  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(258)

我有一个图像容器,它应该根据图像的宽度调整自己的大小,但在Firefox中它不能正确调整自己的大小,它在Chrome中工作得很好,但在Firefox中图像周围有一些空间,我将图像的高度设置为100%,使其占据容器的所有高度。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  width: 899px;
  height: 500px;
  padding: 1rem;
  gap: 0.5rem;
  display: flex;
  flex-direction: column;
  background-color: #303030;
}

.card {
  height: 100%;
  width: 100%;
  background-color: #202020;
  border-radius: 10px;
  overflow: hidden;
}

.section-heading {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-transform: uppercase;
}

.section-heading>h2 {
  font-weight: 500;
  letter-spacing: 1px;
  font-size: 1rem;
}

.section-heading a {
  text-decoration: none;
  transition: 0.1s all linear;
}

.section-heading>a {
  font-size: 0.8rem;
  letter-spacing: 1px;
  font-weight: 500;
}

.section-heading a:hover {
  color: var(--theming);
}

.trending.card {
  display: flex;
  position: relative;
  padding: 1rem;
  gap: 0.5rem;
}

.cover-container {
  overflow: hidden;
  display: flex;
  justify-content: center;
  background-size: cover;
  background-position: center;
  transition: 0.1s all linear;
  background-color: red;
}

.cover-container:hover {
  filter: brightness(0.7);
}

.cover {
  width: 100%;
  background-color: var(--same-background);
  opacity: 0;
  transition: 0.2s all linear;
}

.title a {
  text-decoration: none;
  transition: 0.1s all linear;
}

.title a:hover {
  color: var(--theming);
}

.genres {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  overflow-x: auto;
  scrollbar-width: none;
  flex-shrink: 0;
}

.genre {
  padding: 0.1rem 0.5rem;
  text-decoration: none;
  border-radius: 5px;
  background-color: #404040;
  transition: 0.1s all linear;
  color: var(--secondary-color);
}

.genre:hover {
  background-color: var(--primary-background);
}

.genre:active {
  background-color: var(--lighter-background);
}

.trending.banner {
  height: 30%;
  width: 100%;
  background-color: var(--same-background);
  position: absolute;
  align-self: flex-start;
  z-index: 0;
  background-size: cover;
  background-position: center;
  filter: brightness(0.5);
  overflow: hidden;
  top: 0;
  left: 0;
  transition: 0.2s all linear;
}

.trending.cover-container {
  height: 100%;
  width: max-content;
  max-width: 40%;
  z-index: 3;
  border-radius: 10px;
  flex-shrink: 0;
  border-radius: 10px;
}

.trending.manga.cover {
  height: 100%;
  width: auto;
}

.trending.info {
  width: 100%;
  height: 71%;
  align-self: flex-end;
  padding: 0.5rem;
  gap: 0.1rem;
  display: flex;
  flex-direction: column;
}

.trending.title {
  font-weight: 500;
  font-size: 1.5rem;
  flex-shrink: 0;
}

.trending.description {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 6;
  flex-shrink: 0;
  color: var(--secondary-color);
}

.trending.actions {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.trending.action {
  height: 2rem;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-decoration: none;
  background-color: var(--same-background);
  border-radius: 5px;
  border: none;
  transition: 0.1s all linear;
  cursor: pointer;
  color: var(--secondary-color);
}

.nav {
  width: 2.5rem !important;
}

.nav-icon {
  height: 100%;
}

.nav-icon path {
  stroke: var(--secondary-color);
  stroke-width: 1.5;
}

.trending.action:hover {
  background-color: var(--primary-background);
}

.trending.action:active {
  background-color: var(--lighter-background);
}
<div class="trending container">
  <div class="trending section-heading">
    <h2><a href="#">Trending Now</a></h2>
    <a href="#">View All</a>
  </div>
  <div class="trending card" id="1">
    <div class="trending banner">
      <img src="https://s4.anilist.co/file/anilistcdn/media/manga/banner/117195-VVYq1EEOwR0K.jpg" alt="" class="trending cover" loading="lazy" style="opacity: 1;">
    </div>
    <a href="/manga/117195" class="trending cover-container">
      <img src="https://s4.anilist.co/file/anilistcdn/media/manga/cover/large/bx117195-r3kf8eF0xkDJ.png" alt="" class="trending manga cover" loading="lazy" style="opacity: 1;">
    </a>
    <div class="trending info">
      <h3 class="trending title"><a href="/manga/117195">[Oshi no Ko]</a></h3>
      <div class="genres">

        <a href="/search/manga/Drama" class="trending genre">Drama</a>

        <a href="/search/manga/Mystery" class="trending genre">Mystery</a>

        <a href="/search/manga/Psychological" class="trending genre">Psychological</a>

        <a href="/search/manga/Supernatural" class="trending genre">Supernatural</a>

      </div>
      <p class="trending description">Gorou is a gynecologist and idol fan who’s in shock after his favorite star, Ai, announces an impromptu hiatus. Little does Gorou realize that he’s about to forge a bond with her that defies all common sense! Lies are an idol’s weapon! (Source:
        Yen Press)</p>
      <div class="trending actions">
        <a href="/manga/117195/read" class="trending action read">Read this Manga</a>
        <button class="trending action nav previous">
           <svg class="nav-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" transform="matrix(-1, 0, 0, 1, 0, 0)">
             <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
             <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
             <g id="SVGRepo_iconCarrier">
               <g id="Arrow / Arrow_Right_SM">
                 <path id="Vector" d="M7 12H17M17 12L13 8M17 12L13 16" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
               </g>
             </g>
           </svg>
         </button>
        <button class="trending action nav next">
           <svg class="nav-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
             <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
             <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
             <g id="SVGRepo_iconCarrier">
               <g id="Arrow / Arrow_Right_SM">
                 <path id="Vector" d="M7 12H17M17 12L13 8M17 12L13 16" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
               </g>
             </g>
           </svg>
         </button>
      </div>
    </div>
  </div>
</div>

这是代码,.trending.container应该占用内部图像的宽度,但它在图像的左侧和右侧有一些额外的空间,这个额外的空间只在Firefox中可见,而.trending.container只和chrome中的图像一样宽,并且正如我所期望的那样工作。请帮助,谢谢。JSFiddle

5ssjco0h

5ssjco0h1#

解决方案总结

我发现,在周围的锚标记(.trending.cover-container)上应用display: contents,然后将重要的视觉样式向下移动到图像本身(.trending.manga.cover)似乎可以解决Chrome和Firefox中的问题。

说明

由于CSS中百分比宽度/高度的工作方式,图像周围的额外间距非常难以删除。基本上,当指定百分比高度时:

  • 百分比是相对于生成的框的包含块的高度计算的。
  • “包含块”由父块级元素 * 或 * 创建新格式上下文的最近元素(例如Flex或Grid容器)标识。
  • 如果没有显式指定包含块的高度,则该值计算为auto

Source on MDN
现在,我不是浏览器实现方面的Maven,但我认为差异来自Chrome和Firefox对“显式”一词的解释。在摆弄CSS时,我发现如果我在层次结构中应用足够多的硬编码像素尺寸,那么额外的间距在Chrome和Firefox上都会消失。但是,当然,所有这些硬编码都会导致无法维护的样式表。
作为替代方案,我发现应用上面描述的display: contents似乎可以以一种相当不干涉的方式解决这个问题。请参阅下面的代码片段。您可能可以删除一些因此更改而变得不必要的样式,但我只做了必要的修改。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  width: 899px;
  height: 500px;
  padding: 1rem;
  gap: 0.5rem;
  display: flex;
  flex-direction: column;
  background-color: #303030;
}

.card {
  height: 100%;
  width: 100%;
  background-color: #202020;
  border-radius: 10px;
  overflow: hidden;
}

.section-heading {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-transform: uppercase;
}

.section-heading>h2 {
  font-weight: 500;
  letter-spacing: 1px;
  font-size: 1rem;
}

.section-heading a {
  text-decoration: none;
  transition: 0.1s all linear;
}

.section-heading>a {
  font-size: 0.8rem;
  letter-spacing: 1px;
  font-weight: 500;
}

.section-heading a:hover {
  color: var(--theming);
}

.trending.card {
  display: flex;
  position: relative;
  padding: 1rem;
  gap: 0.5rem;
}

.cover-container {
  overflow: hidden;
  display: flex;
  justify-content: center;
  background-size: cover;
  background-position: center;
  transition: 0.1s all linear;
  background-color: red;
}

.cover-container .cover:hover {
  filter: brightness(0.7);
}

.cover {
  width: 100%;
  background-color: var(--same-background);
  opacity: 0;
  transition: 0.2s all linear;
}

.title a {
  text-decoration: none;
  transition: 0.1s all linear;
}

.title a:hover {
  color: var(--theming);
}

.genres {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  overflow-x: auto;
  scrollbar-width: none;
  flex-shrink: 0;
}

.genre {
  padding: 0.1rem 0.5rem;
  text-decoration: none;
  border-radius: 5px;
  background-color: #404040;
  transition: 0.1s all linear;
  color: var(--secondary-color);
}

.genre:hover {
  background-color: var(--primary-background);
}

.genre:active {
  background-color: var(--lighter-background);
}

.trending.banner {
  height: 30%;
  width: 100%;
  background-color: var(--same-background);
  position: absolute;
  align-self: flex-start;
  z-index: 0;
  background-size: cover;
  background-position: center;
  filter: brightness(0.5);
  overflow: hidden;
  top: 0;
  left: 0;
  transition: 0.2s all linear;
}

.trending.cover-container {
  display: contents;
  height: 100%;
  width: max-content;
  max-width: 40%;
  flex-shrink: 0;
}

.trending.manga.cover {
  border-radius: 10px;
  z-index: 3;
  height: 100%;
  width: auto;
}

.trending.info {
  width: 100%;
  height: 71%;
  align-self: flex-end;
  padding: 0.5rem;
  gap: 0.1rem;
  display: flex;
  flex-direction: column;
}

.trending.title {
  font-weight: 500;
  font-size: 1.5rem;
  flex-shrink: 0;
}

.trending.description {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-line-clamp: 6;
  flex-shrink: 0;
  color: var(--secondary-color);
}

.trending.actions {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.trending.action {
  height: 2rem;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  text-decoration: none;
  background-color: var(--same-background);
  border-radius: 5px;
  border: none;
  transition: 0.1s all linear;
  cursor: pointer;
  color: var(--secondary-color);
}

.nav {
  width: 2.5rem !important;
}

.nav-icon {
  height: 100%;
}

.nav-icon path {
  stroke: var(--secondary-color);
  stroke-width: 1.5;
}

.trending.action:hover {
  background-color: var(--primary-background);
}

.trending.action:active {
  background-color: var(--lighter-background);
}
<div class="trending container">
  <div class="trending section-heading">
    <h2><a href="#">Trending Now</a></h2>
    <a href="#">View All</a>
  </div>
  <div class="trending card" id="1">
    <div class="trending banner">
      <img src="//dummyimage.com/1500x500" alt="" class="trending cover" loading="lazy" style="opacity: 1;">
    </div>
    <a href="/manga/117195" class="trending cover-container">
      <img src="//dummyimage.com/300x450" alt="" class="trending manga cover" loading="lazy" style="opacity: 1;">
    </a>
    <div class="trending info">
      <h3 class="trending title"><a href="/manga/117195">[Oshi no Ko]</a></h3>
      <div class="genres">

        <a href="/search/manga/Drama" class="trending genre">Drama</a>

        <a href="/search/manga/Mystery" class="trending genre">Mystery</a>

        <a href="/search/manga/Psychological" class="trending genre">Psychological</a>

        <a href="/search/manga/Supernatural" class="trending genre">Supernatural</a>

      </div>
      <p class="trending description">Gorou is a gynecologist and idol fan who’s in shock after his favorite star, Ai, announces an impromptu hiatus. Little does Gorou realize that he’s about to forge a bond with her that defies all common sense! Lies are an idol’s weapon! (Source:
        Yen Press)</p>
      <div class="trending actions">
        <a href="/manga/117195/read" class="trending action read">Read this Manga</a>
        <button class="trending action nav previous">
           <svg class="nav-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" transform="matrix(-1, 0, 0, 1, 0, 0)">
             <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
             <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
             <g id="SVGRepo_iconCarrier">
               <g id="Arrow / Arrow_Right_SM">
                 <path id="Vector" d="M7 12H17M17 12L13 8M17 12L13 16" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
               </g>
             </g>
           </svg>
         </button>
        <button class="trending action nav next">
           <svg class="nav-icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
             <g id="SVGRepo_bgCarrier" stroke-width="0"></g>
             <g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
             <g id="SVGRepo_iconCarrier">
               <g id="Arrow / Arrow_Right_SM">
                 <path id="Vector" d="M7 12H17M17 12L13 8M17 12L13 16" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path>
               </g>
             </g>
           </svg>
         </button>
      </div>
    </div>
  </div>
</div>

相关问题