左、中、右对齐同一行底部的 div

发布于 2024-10-17 20:42:04 字数 199 浏览 5 评论 0原文

我想在同一行显示三个 div。这三个都有不同的宽度和高度,并且它们不是直文本。我想左对齐一个(一直到左边),右对齐另一个(一直到右边),并将第三个居中(在包含 div 的中间,在这种情况下是整个页面) )。

此外,我希望这三个 div 与包含的 div 的底部垂直对齐。我的解决方案将它们垂直对齐到包含 div 的顶部。

处理这个问题的最佳方法是什么?

I have three divs that I would like to display on the same line. Each of the three has different widths and heights, and they are not straight text. I'd like to left-align one (all the way to the left), right-align another (all the way to the right), and center the third (in the middle of the containing div, the whole page in this case).

In addition, I'd like the three divs to be vertically aligned to the bottom of the containing div. The solution I have vertically aligns them to the top of the containing div.

What is the best way to handle this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

泪意 2024-10-24 20:42:04

通过将容器 div 设置为 position:relative 并将子 div 设置为 position:absolute,您可以在容器的范围内对 div 进行绝对定位。

这很容易,因为您可以使用 bottom:0px 将所有内容垂直对齐到容器的底部,然后使用左/右样式沿水平轴定位。

我设置了一个工作 jsFiddle: http://jsfiddle.net/Damien_at_SF/KM7sQ/5/ 代码如下:

HTML:

<div id="container">
    <div id="left">left</div>
    <div id="center">center</div>
    <div id="right">right</div>    
</div>

CSS:

#container {
    position:relative;
    height:400px;
    width:100%;
    border:thick solid black;
}
#container div {
    background:grey;
    width:200px;
}
#left {
    position:absolute;
    left:0px;
    bottom:0px;
}
#center {
    position:absolute;
    left:50%;
    margin-left:-100px;
    bottom:0px;
}
#right {
    position:absolute;
    right:0px;
    bottom:0px;
}

注意:对于“center”div,margin-left = 1/2 div 的宽度:)

希望有帮助:)

By setting your container div to position:relative and the child divs to position:absolute you can absolute position the divs within the confines of the container.

This makes it easy, as you can use bottom:0px to align all vertically to the bottom of the container, and then use left/right styling to position along the horizontal axis.

I set up a working jsFiddle: http://jsfiddle.net/Damien_at_SF/KM7sQ/5/ and the code follows:

HTML:

<div id="container">
    <div id="left">left</div>
    <div id="center">center</div>
    <div id="right">right</div>    
</div>

CSS:

#container {
    position:relative;
    height:400px;
    width:100%;
    border:thick solid black;
}
#container div {
    background:grey;
    width:200px;
}
#left {
    position:absolute;
    left:0px;
    bottom:0px;
}
#center {
    position:absolute;
    left:50%;
    margin-left:-100px;
    bottom:0px;
}
#right {
    position:absolute;
    right:0px;
    bottom:0px;
}

Note: For the "center" div, the margin-left = 1/2 the width of the div :)

Hope that helps :)

芸娘子的小脾气 2024-10-24 20:42:04

我的技术类似于@Damien-at-SF:

我试图严格证明您要求的所有要求。

现场演示

HTML:

<div id="container">

    <div id="left"></div>
    <div id="mid"></div>
    <div id="right"></div>

</div>

CSS:

#container {
    position: relative;
    height: 400px;
    width: 80%;
    min-width: 400px;
    margin: 0 auto;

    background: #ccc
}
#left, #right, #mid {
    position: absolute;
    bottom: 0;
}
#left {
    left: 0;
    width: 80px;
    height: 200px;

    background: red
}
#right {
    right: 0;
    width: 120px;
    height: 170px;

    background: blue
}

#mid {
    left:50%;

    margin-left: -80px;
    width: 160px;
    height: 300px;

    background: #f39
}

My technique is similar to @Damien-at-SF:

I tried to rigorously demonstrate all the requirements you asked for.

Live Demo

HTML:

<div id="container">

    <div id="left"></div>
    <div id="mid"></div>
    <div id="right"></div>

</div>

CSS:

#container {
    position: relative;
    height: 400px;
    width: 80%;
    min-width: 400px;
    margin: 0 auto;

    background: #ccc
}
#left, #right, #mid {
    position: absolute;
    bottom: 0;
}
#left {
    left: 0;
    width: 80px;
    height: 200px;

    background: red
}
#right {
    right: 0;
    width: 120px;
    height: 170px;

    background: blue
}

#mid {
    left:50%;

    margin-left: -80px;
    width: 160px;
    height: 300px;

    background: #f39
}
夜夜流光相皎洁 2024-10-24 20:42:04

为了使你的中心 div 有弹性,你可以这样做:

<div style="display:table; width:500px;">
  <div style="display:table-row;">
    <div style="display:table-cell; width:50px;"></div>
    <div style="display:table-cell;"></div>
    <div style="display:table-cell; width:50px;"></div>
  </div>
</div>

To make your center div elastic, you could do something like:

<div style="display:table; width:500px;">
  <div style="display:table-row;">
    <div style="display:table-cell; width:50px;"></div>
    <div style="display:table-cell;"></div>
    <div style="display:table-cell; width:50px;"></div>
  </div>
</div>
泪眸﹌ 2024-10-24 20:42:04

对第一个答案的进一步增强:

在“center”div CSS中,您需要添加:

text-align:center;

在“right”div CSS中,您需要添加:

text-align:right;

...以完美实现左/中/右对齐。

A further enhancement to the first answer:

In the "center" div CSS, you need to add:

text-align:center;

In the "right" div CSS, you need to add:

text-align:right;

... to perfectly achieve left/center/right aligning.

盗琴音 2024-10-24 20:42:04

在包含的 div 上设置 position:relative,在 3 个 div 上设置 position:relative,并将 3 个 div 的 bottom 属性设置为 <代码>0:

bottom: 0

Set position: relative on the containing div, set position: relative on your 3 divs, and set the bottom attribute of the 3 divs to 0:

bottom: 0
卷耳 2024-10-24 20:42:04

您可以使用 align-items: flex-end< /code>使 Flexbox 项目在底部垂直对齐。

.container {
  display: flex;
  height: 300px;
  min-width: 400px;
  background-color: #61a0f8;
  justify-content: space-between;
  align-items: flex-end;
}

.item {
  width: 100px;
  height: 120px;
  background-color: #f08bc3;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 2rem;
}

.flex-2 {
  width: 140px;
  height: 240px;
}
<div class="container">
  <div class="item">1</div>
  <div class="item flex-2">2</div>
  <div class="item">3</div>
</div>

You can use align-items: flex-end to make the flexbox items aligned vertically at the bottom.

.container {
  display: flex;
  height: 300px;
  min-width: 400px;
  background-color: #61a0f8;
  justify-content: space-between;
  align-items: flex-end;
}

.item {
  width: 100px;
  height: 120px;
  background-color: #f08bc3;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 2rem;
}

.flex-2 {
  width: 140px;
  height: 240px;
}
<div class="container">
  <div class="item">1</div>
  <div class="item flex-2">2</div>
  <div class="item">3</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文