[英]How to make CSS long column text widen grid instead of overflowing?
我目前正在重構我的 Firefox 插件之一,我注意到其中一個翻譯出現了一個問題:我有一個選項卡開關,它是一個 CSS 網格,有 2 列,兩列的寬度都是 50%。 使用較短的選項卡標題,一切都按預期工作,但較長的標題會溢出。
相反,我想要更長的標題來擴大它們各自的欄目。 tab-button
s 應具有相同的寬度, tab-button-container
應具有fit-content
寬度。 雖然可以為tab-button-container
寬度設置一個適當的絕對值,但我不想不必要地浪費任何屏幕空間。
你能幫助我嗎? 謝謝!
html { overflow: hidden; padding: 0px; margin: 0px; border: none; width: max-content; height: fit-content; font-size: 14px; } body { overflow: hidden; font-family: sans-serif; margin: 0px; height: fit-content; width: max-content; border: none; } main { padding-left: 20px; padding-right: 20px; width: max-content; }.tab-button-container { grid-template-columns: repeat(2, calc(50% - 2px)); padding: 4px; column-gap: 4px; width: calc(100% - 4px); min-width: max-content; display: grid; position: relative; left: 50%; transform: translate(-50%,0); background-color: rgb(226, 226, 226); border-radius: 5px; cursor: default; margin-top: 20px; margin-bottom: 20px; }.tab-button { white-space: nowrap; display: inline-block; padding-left: 15px; padding-right: 15px; padding-top: 8px; padding-bottom: 8px; border-radius: 5px; cursor: pointer; text-align: center; }.tab-button--active { background-color: #0a84ff; color: white; }.tab-button--inactive { background-color: transparent; color: #676767; transition: 0.3s; }.tab-button--inactive:hover { background-color: rgb(200, 200, 200); transition: 0.3s; }
<html> <body> <main> <div class="tab-button-container"> <span class="tab-button tab-button-encrypt tab-button--active"> <i class="fas fa-lock"></i> <a class="tab-button-label">Salataan</a> </span> <span class="tab-button tab-button-decrypt tab-button--inactive"> <a class="tab-button-label">Salaus puretaan loremipsum</a> </span> </div> </main> </body> </html>
溢出問題是由於寬度有限引起的: .tab-button-container { grid-template-columns: repeat(2, calc(50% - 2px)); }
.tab-button-container { grid-template-columns: repeat(2, calc(50% - 2px)); }
。 您需要更改列寬以適合內容:
html { overflow: hidden; padding: 0px; margin: 0px; border: none; width: max-content; height: fit-content; font-size: 14px; } body { overflow: hidden; font-family: sans-serif; margin: 0px; height: fit-content; width: max-content; border: none; } main { padding-left: 20px; padding-right: 20px; width: max-content; }.tab-button-container { grid-template-columns: repeat(2, minmax(min-content, calc(50% - 2px))); padding: 4px; column-gap: 4px; width: calc(100% - 4px); min-width: max-content; display: grid; position: relative; left: 50%; transform: translate(-50%,0); background-color: rgb(226, 226, 226); border-radius: 5px; cursor: default; margin-top: 20px; margin-bottom: 20px; }.tab-button { white-space: nowrap; display: inline-block; padding-left: 15px; padding-right: 15px; padding-top: 8px; padding-bottom: 8px; border-radius: 5px; cursor: pointer; text-align: center; }.tab-button--active { background-color: #0a84ff; color: white; }.tab-button--inactive { background-color: transparent; color: #676767; transition: 0.3s; }.tab-button--inactive:hover { background-color: rgb(200, 200, 200); transition: 0.3s; }
<html> <body> <main> <div class="tab-button-container"> <span class="tab-button tab-button-encrypt tab-button--active"> <i class="fas fa-lock"></i> <a class="tab-button-label">Salataan</a> </span> <span class="tab-button tab-button-decrypt tab-button--inactive"> <a class="tab-button-label">Salaus puretaan loremipsum</a> </span> </div> </main> </body> </html>
這就是解決方案。
html { overflow: hidden; padding: 0px; margin: 0px; border: none; width: max-content; height: fit-content; font-size: 14px; } body { overflow: hidden; font-family: sans-serif; margin: 0px; height: fit-content; width: max-content; border: none; } main { padding-left: 20px; padding-right: 20px; width: max-content; }.tab-button-container { grid-template-columns: 1fr 1fr; padding: 4px; column-gap: 4px; width: calc(100% - 4px); min-width: max-content; display: grid; position: relative; left: 50%; transform: translate(-50%,0); background-color: rgb(226, 226, 226); border-radius: 5px; cursor: default; margin-top: 20px; margin-bottom: 20px; }.tab-button { white-space: nowrap; display: inline-block; padding-left: 15px; padding-right: 15px; padding-top: 8px; padding-bottom: 8px; border-radius: 5px; cursor: pointer; text-align: center; }.tab-button--active { background-color: #0a84ff; color: white; }.tab-button--inactive { background-color: transparent; color: #676767; transition: 0.3s; }.tab-button--inactive:hover { background-color: rgb(200, 200, 200); transition: 0.3s; }
<html> <body> <main> <div class="tab-button-container"> <span class="tab-button tab-button-encrypt tab-button--active"> <i class="fas fa-lock"></i> <a class="tab-button-label">Salataan</a> </span> <span class="tab-button tab-button-decrypt tab-button--inactive"> <a class="tab-button-label">Salaus puretaan loremipsum</a> </span> </div> </main> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.