如何用CSS阻止元素溢出?

我有一个 SVG 图标(Font Awesome),它不断溢出并超出我的导航栏。我尝试过overflow: hidden;,但没有改变任何东西。请帮助我理解并解决问题。

更新#1:

我注意到,如果我不设置position(默认为static),那么它的行为就像正常的一样。但我需要设置position为,relative因为我正在使用该right属性来对齐项目。我还注意到,将位置从静态更改为任何位置都会产生问题。

HTML 结构-

<div class="collapsible-card">

  <div class="collapsible-card-title">

    <img src="https://yt3.ggpht.com/a/AATXAJzlZzr16izsGHBCHIkO3H7n-UiHyZPCJFEPiQ=s88-c-k-c0xffffffff-no-rj-mo" class="unselectable" alt="Youtuber's Logo">

    <h5>PewDiePie</h5>

    <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="chevron-down" class="svg-inline--fa fa-chevron-down fa-w-14 drop-icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">

      <path fill="currentColor" d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z">

      </path>

    </svg>

 </div>

 <div class="collapsible-card-item-container" style="display: block; max-height: 1140px;">

  <div class="collapsible-card-items">

    <a href="https://www.youtube.com/watch?v=8HHZiNdrZGA" target="_blank"> 

      <img src="https://i.ytimg.com/vi/8HHZiNdrZGA/mqdefault.jpg" alt="Video's Thumbnail">

      <div class="collapsible-card-item-info-container">

         <h3>I Made The WORST Minecraft MISTAKE There Is. .. - Part 40</h3>

         <span>2020-03-13</span>

       </div>

     </a>

   </div>

   <div class="collapsible-card-items">

     <a href="https://www.youtube.com/watch?v=1B1f9PGLbIs" target="_blank"> 

       <img src="https://i.ytimg.com/vi/1B1f9PGLbIs/mqdefault.jpg" alt="Video's Thumbnail">

       <div class="collapsible-card-item-info-container">

         <h3>I'm Back in Minecraft! - Part 39</h3>

         <span>2020-03-06</span>

       </div>

     </a>

   </div>

慕尼黑的夜晚无繁华
浏览 82回答 2
2回答

红糖糍粑

问题是你没有设置svg图像height width,我设置了它,20px你可以根据你的要求更改它第二次使用z-index:1111您的图标header/nav,使您的svg图标位于标题后面.collapsible-card {&nbsp; &nbsp; &nbsp; background-color: #353638;&nbsp; &nbsp; &nbsp; margin: 0 5px 20px 5px;&nbsp; &nbsp; &nbsp; border-radius: 20px;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; transition: display 1.5s ease-in-out;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .collapsible-card-title {&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; flex-direction: row;&nbsp; &nbsp; &nbsp; align-items: center;&nbsp; &nbsp; &nbsp; font-size: 2.75em;&nbsp; &nbsp; &nbsp; cursor: pointer;&nbsp; &nbsp; &nbsp; /* background-color: rgb(107, 218, 74); */&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .collapsible-card-title svg {&nbsp; &nbsp; &nbsp; overflow: hidden !important;&nbsp; &nbsp; &nbsp; color: #ff304f;&nbsp; &nbsp; &nbsp; margin-left: auto;&nbsp; &nbsp; &nbsp; margin-right: 25px;&nbsp; &nbsp; &nbsp; width: 20px;&nbsp; &nbsp; height: 20px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .collapsible-card-title img {&nbsp; &nbsp; &nbsp; width: 88px;&nbsp; &nbsp; &nbsp; height: 88px;&nbsp; &nbsp; &nbsp; border-top-left-radius: 20px;&nbsp; &nbsp; &nbsp; border-bottom-left-radius: 20px;&nbsp; &nbsp; &nbsp; margin-right: 20px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .collapsible-card-items a {&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; flex-direction: row;&nbsp; &nbsp; &nbsp; align-items: center;&nbsp; &nbsp; &nbsp; font-size: 1.5em;&nbsp; &nbsp; &nbsp; background-color: #2d2f31;&nbsp; &nbsp; &nbsp; margin-bottom: 10px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .collapsible-card-item-info-container {&nbsp; &nbsp; &nbsp; margin-left: 20px;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; .collapsible-card-item-container {&nbsp; &nbsp; &nbsp; display: none;&nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; &nbsp; max-height: 0;&nbsp; &nbsp; &nbsp; background-color: #262729;&nbsp; &nbsp; &nbsp; transition: max-height 0.35s ease-in-out;&nbsp; &nbsp; }<div class="collapsible-card"><div class="collapsible-card-title">&nbsp; <img src="https://yt3.ggpht.com/a/AATXAJzlZzr16izsGHBCHIkO3H7n-UiHyZPCJFEPiQ=s88-c-k-c0xffffffff-no-rj-mo" class="unselectable" alt="Youtuber's Logo">&nbsp; <h5>PewDiePie</h5>&nbsp; <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="chevron-down" class="svg-inline--fa fa-chevron-down fa-w-14 drop-icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"> <path fill="currentColor" d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z"> </path>&nbsp; </svg></div><div class="collapsible-card-item-container" style="display: block; max-height: 1140px;">&nbsp; <div class="collapsible-card-items"> <a href="https://www.youtube.com/watch?v=8HHZiNdrZGA" target="_blank">&nbsp; &nbsp; <img src="https://i.ytimg.com/vi/8HHZiNdrZGA/mqdefault.jpg" alt="Video's Thumbnail"> &nbsp; <div class="collapsible-card-item-info-container"> <h3>I Made The WORST Minecraft MISTAKE There Is. .. - Part 40</h3> <span>2020-03-13</span> &nbsp; &nbsp;</div> </a>&nbsp; &nbsp;</div>&nbsp; &nbsp;<div class="collapsible-card-items"> <a href="https://www.youtube.com/watch?v=1B1f9PGLbIs" target="_blank">&nbsp; &nbsp; &nbsp;<img src="https://i.ytimg.com/vi/1B1f9PGLbIs/mqdefault.jpg" alt="Video's Thumbnail"> &nbsp; &nbsp;<div class="collapsible-card-item-info-container"> <h3>I'm Back in Minecraft! - Part 39</h3> <span>2020-03-06</span> &nbsp; &nbsp;</div> </a>&nbsp; &nbsp;</div>&nbsp; &nbsp;<div class="collapsible-card-items"> <a href="https://www.youtube.com/watch?v=WS54qL3GeaI" target="_blank">&nbsp; &nbsp; &nbsp;<img src="https://i.ytimg.com/vi/WS54qL3GeaI/mqdefault.jpg" alt="Video's Thumbnail"> &nbsp; &nbsp;<div class="collapsible-card-item-info-container"> <h3>Subnautica Part 1 (OMG GAME)</h3> <span>2020-03-15</span> </div> &nbsp; </a> </div> <div class="collapsible-card-items"> &nbsp; <a href="https://www.youtube.com/watch?v=fq_rzA3RAmY" target="_blank"> <img src="https://i.ytimg.com/vi/fq_rzA3RAmY/mqdefault.jpg" alt="Video's Thumbnail"> <div class="collapsible-card-item-info-container"> &nbsp; <h3>Worst TIMING I've EVER HAD in a videogame.......</h3> &nbsp; <span>2020-03-20</span> </div> &nbsp; </a> </div> <div class="collapsible-card-items"> &nbsp; &nbsp;<a href="https://www.youtube.com/watch?v=Lq8QxKnN_5I" target="_blank"> <img src="https://i.ytimg.com/vi/Lq8QxKnN_5I/mqdefault.jpg" alt="Video's Thumbnail"> <div class="collapsible-card-item-info-container"> &nbsp; &nbsp;<h3>LOST my HAIR, When Hearing New Meme Music! [MEME REVIEW] 👏 👏#76</h3> &nbsp; &nbsp;<span>2020-03-03</span> </div> &nbsp; &nbsp;</a> </div> <div class="collapsible-card-items"> &nbsp; <a href="https://www.youtube.com/watch?v=K2i-fPWWy4A" target="_blank"> <img src="https://i.ytimg.com/vi/K2i-fPWWy4A/mqdefault.jpg" alt="Video's Thumbnail"> <div class="collapsible-card-item-info-container"> &nbsp; <h3>Video flagged for: False Information&nbsp; [MEME REVIEW] 👏 👏#73</h3> &nbsp; <span>2019-12-20</span> </div> &nbsp; </a> </div>&nbsp; </div></div>

青春有我

如果我正确理解你的要求,那么你希望 svg 位于右侧吗?您使用的尺寸svg大于您需要的尺寸,因此您需要提供 和height,width为了便于理解,我添加了一个 border white。您可以继续删除该边框并设置您自己的边框,height然后width大家就都设置好了。在蓝色标题上使用z-index:1(或更大的整数,我假设没有z-index用于标题的元素),以便图标将保留在.header.collapsible-card {&nbsp; background-color: #353638;&nbsp; margin: 0 5px 20px 5px;&nbsp; border-radius: 20px;&nbsp; transition: display 1.5s ease-in-out;}.collapsible-card-title {&nbsp; display: flex;&nbsp; flex-direction: row;&nbsp; align-items: center;&nbsp; font-size: 2.75em;&nbsp; cursor: pointer;&nbsp; /* background-color: rgb(107, 218, 74); */}.collapsible-card-title svg {&nbsp; height: 50px; /* use your desired height as per your requirement */&nbsp; color: #ff304f;&nbsp; margin-left: auto;&nbsp; position: relative;&nbsp; right: 25px;&nbsp; border:1px solid white;}.collapsible-card-title img {&nbsp; width: 88px;&nbsp; height: 88px;&nbsp; border-top-left-radius: 20px;&nbsp; border-bottom-left-radius: 20px;&nbsp; margin-right: 20px;}.collapsible-card-items a {&nbsp; display: flex;&nbsp; flex-direction: row;&nbsp; align-items: center;&nbsp; font-size: 1.5em;&nbsp; background-color: #2d2f31;&nbsp; margin-bottom: 10px;}.collapsible-card-item-info-container {&nbsp; margin-left: 20px;}.collapsible-card-item-container {&nbsp; display: none;&nbsp; overflow: hidden;&nbsp; max-height: 0;&nbsp; background-color: #262729;&nbsp; transition: max-height 0.35s ease-in-out;}<div class="collapsible-card">&nbsp; <div class="collapsible-card-title">&nbsp; &nbsp; <img src="https://yt3.ggpht.com/a/AATXAJzlZzr16izsGHBCHIkO3H7n-UiHyZPCJFEPiQ=s88-c-k-c0xffffffff-no-rj-mo" class="unselectable" alt="Youtuber's Logo">&nbsp; &nbsp; <h5>PewDiePie</h5>&nbsp; &nbsp; <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="chevron-down" class="svg-inline--fa fa-chevron-down fa-w-14 drop-icon" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512">&nbsp; &nbsp; &nbsp; <path fill="currentColor" d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z">&nbsp; &nbsp; &nbsp; </path>&nbsp; &nbsp; </svg>&nbsp; </div>&nbsp; <div class="collapsible-card-item-container" style="display: block; max-height: 1140px;">&nbsp; &nbsp; <div class="collapsible-card-items">&nbsp; &nbsp; &nbsp; <a href="https://www.youtube.com/watch?v=8HHZiNdrZGA" target="_blank">&nbsp; &nbsp; &nbsp; &nbsp; <img src="https://i.ytimg.com/vi/8HHZiNdrZGA/mqdefault.jpg" alt="Video's Thumbnail">&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapsible-card-item-info-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>I Made The WORST Minecraft MISTAKE There Is. .. - Part 40</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>2020-03-13</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="collapsible-card-items">&nbsp; &nbsp; &nbsp; <a href="https://www.youtube.com/watch?v=1B1f9PGLbIs" target="_blank">&nbsp; &nbsp; &nbsp; &nbsp; <img src="https://i.ytimg.com/vi/1B1f9PGLbIs/mqdefault.jpg" alt="Video's Thumbnail">&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapsible-card-item-info-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>I'm Back in Minecraft! - Part 39</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>2020-03-06</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="collapsible-card-items">&nbsp; &nbsp; &nbsp; <a href="https://www.youtube.com/watch?v=WS54qL3GeaI" target="_blank">&nbsp; &nbsp; &nbsp; &nbsp; <img src="https://i.ytimg.com/vi/WS54qL3GeaI/mqdefault.jpg" alt="Video's Thumbnail">&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapsible-card-item-info-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>Subnautica Part 1 (OMG GAME)</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>2020-03-15</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="collapsible-card-items">&nbsp; &nbsp; &nbsp; <a href="https://www.youtube.com/watch?v=fq_rzA3RAmY" target="_blank">&nbsp; &nbsp; &nbsp; &nbsp; <img src="https://i.ytimg.com/vi/fq_rzA3RAmY/mqdefault.jpg" alt="Video's Thumbnail">&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapsible-card-item-info-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>Worst TIMING I've EVER HAD in a videogame.......</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>2020-03-20</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="collapsible-card-items">&nbsp; &nbsp; &nbsp; <a href="https://www.youtube.com/watch?v=Lq8QxKnN_5I" target="_blank">&nbsp; &nbsp; &nbsp; &nbsp; <img src="https://i.ytimg.com/vi/Lq8QxKnN_5I/mqdefault.jpg" alt="Video's Thumbnail">&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapsible-card-item-info-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>LOST my HAIR, When Hearing New Meme Music! [MEME REVIEW] 👏 👏#76</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>2020-03-03</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="collapsible-card-items">&nbsp; &nbsp; &nbsp; <a href="https://www.youtube.com/watch?v=K2i-fPWWy4A" target="_blank">&nbsp; &nbsp; &nbsp; &nbsp; <img src="https://i.ytimg.com/vi/K2i-fPWWy4A/mqdefault.jpg" alt="Video's Thumbnail">&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapsible-card-item-info-container">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3>Video flagged for: False Information [MEME REVIEW] 👏 👏#73</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>2019-12-20</span>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div>&nbsp; </div></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5