You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让含列表与响应式图片的Flexbox两列宽度相等?

问题:Flexbox实现两列等宽且图片保持响应式

我需要在CSS Flexbox布局中实现两列宽度相等的效果:第一列包含列表文本,第二列放置一张需保持响应式的大图片。

常见教程针对3-4列且各列文本量不同的场景,会通过为Flex容器的直接子元素添加如下样式解决等宽问题:

.row-container > * {
  flex-basis: 100%;
}

但在我的场景中,图片所在列的宽度远大于文本列,请问如何实现两列宽度相等,同时保证图片的响应式特性?


解决思路

要让Flex两列严格等宽,需要同时控制flex属性和图片的缩放规则:

  1. 给Flex容器的子元素设置flex: 1 1 50%,强制每列默认占容器50%宽度,同时允许缩放适应容器
  2. 给图片容器设置width: 100%,避免图片撑爆容器
  3. 给图片添加响应式样式,让它在容器内自适应缩放并保持比例

修改后的代码

CSS部分

*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --background-col: #1B1B1B;
  --font-col: #F5F5F5;
  --transition-col: #EE578A;
}

* {
  margin: 0;
  padding: 0;
}

html {
  margin: 1px;
}

body {
  background-color: var(--background-col);
  color: var(--font-col);
  max-width: 1300px;
  margin: 1px auto;
  line-height: 1.3em;
  font-family: sans-serif;
  font-size: 16px;
  font-weight: normal;
  outline: 1px solid pink;
}

.header-ulist>li,
a,
button {
  color: var(--font-col);
  font-family: sans-serif;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  text-transform: uppercase;
}

.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-direction: row;
  gap: 2rem;
  padding: 1.5rem 2rem;
  outline: 1px solid pink;
}

.h1 {
  font-size: 24px;
  font-weight: 600;
  line-height: 1.2em;
  text-align: center;
  text-transform: uppercase;
  outline: 1px solid pink;
}

.header-ulist {
  list-style: none;
  outline: 1px solid pink;
}

.header-ulist>li {
  display: inline-block;
  padding: 1rem 1rem;
}

.header-nav-anchor>a {
  transition: all 0.4s ease 0s;
}

.header-nav-anchor>a:hover {
  color: var(--transition-col);
}

.mroesler>button {
  padding: 1rem 2rem;
  background-color: var(--transition-col);
  color: var(--background-col);
  border: none;
  border-radius: 1.5em;
  cursor: pointer;
  outline: 1px solid pink;
  transition: background-color 400ms ease 0ms, color 400ms ease 400ms;
}

.mroesler>button:hover {
  background-color: var(--background-col);
  color: var(--font-col);
}

.row-container {
  display: flex;
  gap: 1rem; /* 可选:添加列间距 */
}

/* 核心修改:强制两列等宽 */
.row-container > * {
  flex: 1 1 50%; /* flex-grow:1, flex-shrink:1, flex-basis:50% */
  width: 0; /* 修复部分浏览器中内容撑开宽度的问题 */
}

/* 图片容器和响应式图片样式 */
.img-container {
  width: 100%;
}

.img-fluid40 {
  width: 100%;
  height: auto;
  max-width: 100%; /* 确保图片不会超出容器宽度 */
}

HTML部分(保持原结构)

<body>
  <header class="main-header">
    <h1 class="h1">Joanna<br>Kowalska<br>Resume</h1>
    <nav class="header-nav">
      <ul class="header-ulist">
        <li class="header-nav-anchor"><a href="index.html">main pink</a></li>
        <li class="header-nav-anchor"><a href="index2.html">main gray1</a></li>
        <li class="header-nav-anchor"><a href="index3.html">main gray2</a></li>
        <li class="header-nav-anchor"><a href="myprojects.html">myprojects</a></li>
        <li class="header-nav-anchor"><a href="contact.html" target="_blank">contact</a></li>
      </ul>
    </nav>
    <a class="mroesler" href="https://mroesler.com/" target="_blank"><button>mroesler</button></a>
  </header>

  <div class="row-container">
    <section class="column-container">
      <h2 class="h2">Joanna Kowalska<br>Senior Accountant in<br> Captive Finance Organization<br>VFS Usługi Finansowe Sp&nbsp;z&nbsp;o.o.</h2>
      <p class="initial-paragraph">Joanna Kowalska Senior Accountant in Captive Finance Organization</p>
      <ul>
        <li>first responsibility description</li>
        <li>second responsibility description</li>
        <li>third responsibility description</li>
        <li>fourth responsibility description</li>
        <li>fifth responsibility description</li>
        <li>sixth responsibility description</li>
        <li>seventh responsibility description</li>
        <li>eighth responsibility description</li>
        <li>january 2022 - present moment</li>
      </ul>
    </section>

    <div class="img-container">
      <img class="img-fluid40" src="https://via.placeholder.com/800x300" alt="joanna_kowalska">
    </div>
  </div>

  <h2 class="previous">My Previous Positions<br>and Professional Experience</h2>
</body>

关键说明

  • flex: 1 1 50%:让每列默认分配50%容器宽度,同时支持缩放,保证两列始终等宽
  • width: 0:解决部分浏览器中,图片等内容会突破flex-basis设置的问题,强制按比例分配宽度
  • 图片的width:100%; height:auto:确保图片自适应容器宽度,保持原始比例,实现响应式效果

内容的提问来源于stack exchange,提问作者michal roesler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 09:45:53