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 / 问题 / 79198517
Accepted
gregsdennis
gregsdennis
Asked: 2024-11-18 09:56:51 +0800 CST2024-11-18 09:56:51 +0800 CST 2024-11-18 09:56:51 +0800 CST

垂直居中,但在内容扩展时保持上边距

  • 772

我有一个包含两列的弹性框。左侧内容通常比右侧内容高,我想将右侧内容渲染为居中。

但是,左侧的内容可以扩展,我想让右侧的内容在扩展时保持在原处,而不是保持居中。

到目前为止,我有第一部分,但是当左侧部分扩展时,右侧部分会下降以保持居中。

.host {
  width: 500px;
  padding: 5px;
  display: flex;
  flex-direction: row;
  align-items: center;
  background: yellow;
}

.left-panel {
  width: 50%;
  margin: 5px;
  flex-direction: column;
  background: pink;
}

.summary {
  margin: 5px;
  background: green;
}

.expand-list {
  margin: 5px;
  background: skyblue
}

.right-panel {
  width: 50%;
  margin: 5px;
  display: flex;
  flex-direction: row;
  background: blue;
}

.image {
  margin: 5px;
  background: red;
}

button {
  margin: auto 5px;
  width: 100px;
}
<div class="host">
  <div class="left-panel">
<div class="summary">
  Here is a long paragraph of text that I need to display.
  It takes up most of the container, but there's an expander
  below that will make this text take up less because the
  expander will take up more when expanded.
</div>
<details class="expand-list">
  <summary>click me</summary>
  <ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
    <li>Five</li>
    <li>Six</li>
    <li>Seven</li>
  </ul>
</details>
  </div>
<div class="right-panel">
<div class="image">
  <p>
    Don't let this text fool you;
  </p>
  <p>
   it's actually an image.
  </p>
</div>
<button>
  Do it
</button>
  </div>
</div>

我无法明确设置右侧内容的上边距,因为左侧内容没有设置高度。

如何让左侧内容扩展时右侧内容保持不变?最好使用纯 CSS 解决方案。

css
  • 1 1 个回答
  • 52 Views

1 个回答

  • Voted
  1. Best Answer
    Alohci
    2024-11-18T16:56:20+08:002024-11-18T16:56:20+08:00

    为了实现这一点,我必须将 ul 扩展面板移到详细信息元素之后而不是其中,以便它可以参与包含网格。

    这里的想法是,右侧面板位于网格的前两行中央,而左侧面板在详细信息面板关闭时高 2 行,在详细信息面板打开时高 3 行。

    .host {
      width: 490px;
      padding: 10px;
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: auto auto;
      background: yellow;
      column-gap: 10px;
      &:has([open]) {
        grid-template-rows: auto auto auto;
      }
    }
    
    .left-panel {
      padding: 5px;
      flex-direction: column;
      background: pink;
      display: grid;
      grid-row-end: span 2;
      grid-template-rows: subgrid;
      &:has([open]) {
        grid-row-end: span 3;
      }
    }
    
    .summary {
      background: green;
    }
    
    .expand-list {
      margin-top: 5px;
      background: skyblue;
    }
    .expand-list[open] {
      padding-bottom: 5px;
    }
    .expand-list + ul {
      display: none;
    }
    .expand-list[open] + ul {
      margin: 0;
      padding-top: calc(1em - 5px);
      display: block;
      background: skyblue;
    }
    
    .right-panel {
      grid-row-end: span 2;
      align-self: center;
      display: flex;
      flex-direction: row;
      background: blue;
      padding: 5px;
    }
    
    .image {
      background: red;
    }
    
    button {
      margin: auto 5px;
      width: 100px;
    }
        <div class="host">
          <div class="left-panel">
            <div class="summary">
              Here is a long paragraph of text that I need to display.
              It takes up most of the container, but there's an expander
              below that will make this text take up less because the
              expander will take up more when expanded.
            </div>
            <details class="expand-list">
              <summary>click me</summary>
            </details>
            <ul>
              <li>One</li>
              <li>Two</li>
              <li>Three</li>
              <li>Four</li>
              <li>Five</li>
              <li>Six</li>
              <li>Seven</li>
            </ul>
          </div>
          <div class="right-panel">
            <div class="image">
              <p>
                Don't let this text fool you;
              </p>
              <p>
                it's actually an image.
              </p>
            </div>
            <button>
              Do it
            </button>
          </div>
        </div>

    • 4

相关问题

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

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

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

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

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

Sidebar

Stats

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

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

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

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

    • 1 个回答
  • Marko Smith

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

    • 6 个回答
  • Marko Smith

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

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 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 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +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
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +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