html - 使用 Flexbox 对齐 3 个元素

标签 html css flexbox

我有 3 个 div 元素,我想将它们放入 2 列中。其中一个我想垂直填充第一列,其他的则填充 2 中的下一列 单独的行。 (运行代码片段以查看各部分)

.news-feed {
  display: flex;
  flex-flow: row wrap;
}
.image {
  flex-direction: column;
  height: 20px;
}
.title_date {
  flex-direction: column;
}
.job-description {
  flex-direction: column;
}
<section class="news-feed">
    <section class="image">
        <img src= 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlUiyrmhTXFppqk4aYzqTOU9nimCQsYibukwAV8rstsDkAVQT-mA'/>
    </section>
    <section class="title_date">
        <p class="date">27 Dec 1997</p>
        <h3 class="title">Urgent Job offer</h3>
    </section>
    <section class="job_description">
        <p class="job_info">This is a job for speacial people like you. Long text bla bla bla. The main issue is that I cannot put the 'title_date' and 'job_description' section in a single div because I want to put the 'title_date' at top of 'image' section when the device is mobile. Any workaround idea is highly appreciated. Thanks</p>
        <p class="tags">Jobs, HighPay, Carrer</p>
    </section>
</section>

主要问题是我无法将“title_date”和“job_description”部分放在单个 div 中,因为我想在设备移动时将“title_date”放在“image”部分的顶部。任何解决方法的想法都受到高度赞赏。谢谢

最佳答案

如果您在 css 文件中仅使用 grid 而不是 flex,那么这很容易实现。那么你就不需要任何 JavaScript 了。只需像这样更改 CSS 代码:

    .news-feed {
        display: grid;
        grid-template-columns: 1fr 2fr;
        grid-template-rows: 50% 50%;
        grid-template-areas: 
            "image title_date"
            "image job_description"
    }
    .image {
          grid-area: image;
    }
    .title_date {
          grid-area: title_date;
    }
    .job-description {
          grid-area: job_description;
    }
    @media screen and (max-width: 425px) {
        .news-feed {
            grid-template-columns: 100%;
            grid-template-rows: 1fr 1fr 1fr;
            grid-template-areas: 
                "title_date"
                "image"
                "job_description"
        }
    } 

这会将桌面模式中的图像放置在左侧,而在移动版本中将图像放置在中间。您当然可以根据需要更改列和行的高度和宽度。

关于html - 使用 Flexbox 对齐 3 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51592960/

相关文章:

javascript - 在jquery插件中添加事件

html - 将文本中心与表头对齐并向 sibling 对齐(html - 表)?

css - 为什么我的 anchor 链接没有到达正确的位置?

java - 为什么 request.getParameter() 方法返回 null?

html - 在 Chrome 中,<select> 元素在禁用时没有背景色

javascript - 将鼠标悬停在文本/图像上时如何触发动画文本?

jquery - WP 主题中的背景图像 slider - 父元素不透明度隐藏内容

css - 将 flexbox 中的图像缩放到可用宽度或高度

html - Flex div 不显示溢出滚动条

html - 对齐 self : flex-end not working in flexbox