AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79596868
Accepted
s427
s427
Asked: 2025-04-29 00:02:30 +0800 CST2025-04-29 00:02:30 +0800 CST 2025-04-29 00:02:30 +0800 CST

在 min() 函数中使用 max-content

  • 772

使用网格为 Web 应用程序构建布局,我有一个侧边栏,我希望它足够宽以容纳其内容(所以,max-content),但我不希望它变得太大,比如说不超过视口的 20%(所以,20vw)。

我以为我会使用一些简单的东西:

.page {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-columns: min(max-content, 20vw) 1fr;
  grid-template-areas: "side main";
}

但是 Firefox 提示我grid-template-columns值不正确,因此被忽略。如果我直接使用:

  grid-template-columns: max-content 1fr;

然后它就可以工作了,但是我没有想要避免侧边栏过大的安全措施。

CSS 函数中是否不允许使用诸如min-content和 之类的关键字?max-content

还有什么其他方法可以实现我的目标?

css
  • 3 3 个回答
  • 60 Views

3 个回答

  • Voted
  1. Best Answer
    imhvost
    2025-04-29T05:00:11+08:002025-04-29T05:00:11+08:00

    看起来您正在寻找fit-content()函数:

    // just a test button
    
    button.onclick = function() {
      const sidebar = this.closest('.page').querySelector('.sidebar');
      if (this.classList.contains('active')) {
        sidebar.textContent = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, exercitationem?';
      } else {
        sidebar.textContent = 'Lorem ipsum';
      }
      this.classList.toggle('active');
    }
    * {
      margin: 0;
    }
    
    .page {
      display: grid;
      height: 100vh;
      grid-template-columns: fit-content(20%) 1fr;
    }
    
    .sidebar {
      background: yellow;
    }
    
    .main {
      background: blue;
    }
    <div class="page">
      <aside class="sidebar">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, exercitationem?</aside>
      <main class="main">
        <button id="button">just a test button</button>
      </main>
    </div>

    • 2
  2. zer00ne
    2025-04-29T06:28:26+08:002025-04-29T06:28:26+08:00

    不要使用固有最小宽度,尝试使用minmax()CSS 函数为“side”设置绝对最小宽度。下面的演示可以设置grid-template-columns左右列(两个<select>)的值和一个选项(复选框),从而转换为简单的弹性布局。如果“side”的内容较少,那么还max-content可以;但如果内容较多,除非你通过minmax()“side”和“main”限制,否则我不建议这样做。如果你设置了最大值,20vw它会随着视口的扩大和缩小,所以它是固有的,所以它的行为不会像你预期的那样。

    注意:您可能会遇到一个非常恼人的 bug,它会影响<select>Stack Snippet 内部。所以,如果<select>s 不起作用,请转到此处CodePen(我建议你去那里,这样更容易调整窗口大小)。

    另外,在模式下查看演示Full page并调整窗口大小。

    // For demo purpsoses
    const root = document.documentElement;
    const main = document.querySelector("main");
    const left = document.querySelector("aside");
    const right = document.querySelector("article");
    
    const ui = document.forms.ui;
    const io = ui.elements;
    
    io.view.value = `${io.left.value} ${io.right.value};`;
    
    ui.onchange = (e) => {
      const chg = e.target;
      if (chg.name === "lr") {
        root.style.setProperty("--l", io.left.value);
        root.style.setProperty("--r", io.right.value);
        io.view.value = `${io.left.value} ${io.right.value};`;
      } else if (chg.id === "flex") {
        io.left.toggleAttribute("disabled");
        io.right.toggleAttribute("disabled");
        main.classList.toggle("flex");
        io.view.value = chg.checked ? "flex;" : `${io.left.value} ${io.right.value};`;
      }
    };
    /* For demo purposes */
    :root {
      --l: 150px;
      --r: minmax(450px, 80vw);
      font: 2ch/1.5 "Segoe UI";
    }
    
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
    }
    
    /* See the blue text for the value of grid-template-columns */
    main {
      display: grid;
      grid-template-columns: var(--l) var(--r);
      grid-template-areas: "aside article";
      column-gap: 0.5rem;
      max-width: 100vw;
      margin: 0 auto;
      padding: 1rem 1.5rem;
    }
    
    /* The rest is for demo purposes */
    
    .rel {
      position: relative;
    }
    
    aside {
      color: red;
    }
    
    form {
      position: absolute;
      bottom: -20vmin;
      left: 1rem;
      z-index: 1;
      background: #ddd;
    }
    
    fieldset {
      padding-bottom: 0.5rem;
    }
    
    select {
      font: inherit;
    }
    
    output {
      display: inline-block;
      margin: 0.5rem 0;
      color: blue;
    }
    
    #view {
      display: inline-block;
    }
    
    #view::before {
      content: "grid-template-columns: ";
      color: black;
    }
    
    .flex {
      display: flex;
    }
    
    .flex aside {
      flex: 1 1 auto;
    }
    
    .flex article {
      flex: 0 1 auto;
    }
    
    .flex #view {
      display: inline-block;
    }
    
    .flex #view::before {
      content: "Main display: ";
      color: black;
    }
    
    #auxl,
    #auxr {
      display: none;
    }
    
    #auxl::before {
      content: "Left flex: ";
      color: black;
    }
    
    #auxr::before {
      content: "Right flex: ";
      color: black;
    }
    
    .flex #auxl,
    .flex #auxr {
      display: inline-block;
    }
    <main class="rel">
     <aside>
      <p>No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb.</p>
     </aside>
     <article>
      <p>The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy My brothers. And you will know My name is the Lord when I lay My vengeance upon thee.</p>
     </article>
     <!-- The rest is for demo purposes -->
     <form id="ui">
      <fieldset>
       <output id="view"></output>&nbsp;
       <output id="auxl">1 1 auto</output>&nbsp;
       <output id="auxr">0 1 auto</output><br>
       <label>Left&nbsp;
        <select id="left" name="lr">
         <option value="minmax(150px, 20vw)">minmax(150px, 20vw)</option>
         <option value="minmax(20vw, max-content)">minmax(20vw, max-content)</option>
         <option value="minmax(min(150px, 20vw), max-content)">minmax(min(150px, 20vw), max-content)</option>
         <option value="max-content">max-content</option>
         <option value="20vw">20vw</option>
        </select>
       </label>
       <label>&nbsp;Right&nbsp;
        <select id="right" name="lr">
         <option value="minmax(450px, 80vw)">minmax(450px, 80vw)</option>
         <option value="minmax(80vw, max-content)">minmax(80vw, max-content)</option>
         <option value="max-content">max-content</option>
         <option value="1fr">1fr</option>
         <option value="80vw">80vw</option>
        </select>
       </label>
       <label>&nbsp;flex&nbsp;
        <input id="flex" type="checkbox">
       </label>
      </fieldset>
     </form>
    </main>

    • 0
  3. Frox
    2025-04-29T03:36:28+08:002025-04-29T03:36:28+08:00

    CSS 函数中是否不允许使用诸如 min-content 和 max-content 之类的关键字?

    它们在 min() 中是不允许的。请阅读此处的文档。

    要指定尺寸范围,您需要使用minmax()。这确实可以接受max-content,但是最大内容在网格中并不像您假设的那样起作用。

    尽可能宽以容纳其内容

    相反,它提供了内容可以容纳的最大尺寸。在这种情况下,它永远不会缩小。

    您可能正在寻找minmax(min-content, 20vw)以下内容:

    .parent {
      width: 100vw;
      height: 100vh;
      display: grid;
      grid-template-columns: minmax(min-content, 20vw) 1fr;
    }
    
    
    .sidebar {
      background-color: lightblue;
    }
    
    
    .content {
      background-color: lightgreen;
    }
    <div class="parent">
      <div class="sidebar">
        SIDEBAR CONTENT GOES BRRRRRRRRR
      </div>
      <div class="content">
        MAIN CONTENT
      </div>
    </div>

    此外,如果您希望强调 20vw 的限制,即使您的内容太大,您也可以使用minmax(auto, 20vw)

    .parent {
      width: 100vw;
      height: 100vh;
      display: grid;
      grid-template-columns: minmax(auto, 20vw) 1fr;
    }
    
    
    .sidebar {
      background-color: lightblue;
    }
    
    
    .content {
      background-color: lightgreen;
    }
    <div class="parent">
      <div class="sidebar">
        SIDEBAR CONTENT GOES BRRRRRRRRR
      </div>
      <div class="content">
        MAIN CONTENT
      </div>
    </div>

    您需要真正减小宽度才能看到差异。

    PS:请阅读文档中有关 min-content 和 auto 关键字的描述以获得澄清。

    • -1

相关问题

  • Prettier/VS Code 在 CSS 中的 !important 之前添加一个空格,导致它在 WordPress 中崩溃

  • 如何将动态参数从react tsx文件发送到css

  • CSS 网格:包裹并适合内容

  • 是否可以在元素之间对齐渐变?

  • 带盒阴影的圆角边缘 - Tailwind CSS

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve