亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Flexbox繼承問題

Flexbox繼承問題

慕碼人8056858 2023-10-20 16:49:52
我正在建立一個簡單的網站。我有充滿“模塊”的彈性盒子,其中包含文本內容。當這些模塊之一懸停在上面時(使用本文未包含的 JavaScript 代碼,因為它工作正常),我希望出現一個“陰影”,它使整個模塊變暗并顯示一個按鈕。我在將陰影設置為正確的尺寸時遇到了很多麻煩:每個模塊寬度和高度的 100%。由于某種原因,高度和寬度不會從模塊繼承到陰影。HTML:<div class="support">    <div class="module">        <div class="shade">            <a href="#"><button type="button" class="source_btn">View Source</button></a>        </div>        <div class="content">            <h1>Homocide rates have skyrocketed in the United States.</h1>            <p>Jeffery Miron, an economist estimates that the homicide rate in America is as much as seventy-five percent higher $        </div>    </div>    <div class="module">        <div class="shade">            <a href=""><button type="button" class="source_btn">View Source</button></a>        </div>        <div class="content">            <h1>Drug markets are forced to solve their disputes through violence.</h1>            <p>Because the War on Drugs has forced drug markets into the shadows, there is now way they can settle disputes throu$        </div>     </div>    <div class="module">        <div class="shade">            <a href=""><button type="button" class="source_btn">View Source</button></a>        </div>        <div class="content">            <h1>The violence is not only occurring in the United States.</h1>            <p>For some perspective, there have been almost one hundred thousand deaths in Mexico in the past decade caused not b$        </div>    </div></div>CSS: section .support {     display: flex;     flex-direction: row;     background: var(--bg);     height: 60vh; } .module {     flex: 1;     text-align: center;     height: inherit; } .module .content h1 {      margin-top: 5rem;     margin-bottom: 5rem;     font-size: 2.5rem; }如果有 3 個模塊,我就能得到確切的預期效果。我相信這意味著寬度和高度是從其他地方繼承的,我不知道為什么。我需要能夠在 .shade 樣式中說width: 100%and以使陰影占據整個模塊,因為每個支持類會有不同數量的模塊。height: 100%我很困惑為什么寬度和高度沒有像我期望的那樣被繼承。既然 .shade 是 .module 的子級,為什么寬度和高度不繼承自 .module div 呢?
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

當您設置absolute為 的值時position,該元素的父元素應具有relative的值position。否則,子元素無法測量其父元素的位置和大小。


以下 css 應該適合你:


 .module {

     flex: 1;

     text-align: center;

     height: inherit;

     position: relative;

 }

 

 .module .shade {

     position: absolute;

     width: 100%;

     height: 100%;

     top: 0;

     left: 0;

     background: rgba(0,0,0,.6);

 }


查看完整回答
反對 回復 2023-10-20
  • 1 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號