CSS:将高度设置为父级的 100%

发布于 2024-10-06 07:15:15 字数 1532 浏览 3 评论 0原文

我在 div 内的锚点内有一些行图像拇指,每行一个 div,如下所示:

<div class="row"><a><img></a><a><img></a><a><img></a></div>
<div class="row"><a><img></a><a><img></a><a><img></a></div>

图像具有不同的比例,有些是纵向,有些是横向。拇指的最大尺寸为 150 像素(宽或高),并且它们的宽度或高度始终为 150 像素。

我希望所有图像与其父 .row div 的底部对齐。 因此,如果连续有任何肖像,则任何风景都会在底部对齐,并在其上方留出一些空间。 为了让图像在底部对齐,我将它们绝对定位,并相对于它们的锚点 为此,我使用以下 CSS:

<style>
div.row {
    float: left;
    clear: both;
}

.row a {
    position: relative;
    float: left;
    width: 175px;
}

.row a img{
    position: absolute;
    bottom: 0px;
}
</style>

上面的工作正常并且符合预期,但它有一个我正在尝试修复的缺陷:它需要将相对定位的锚标记的高度设置为特定大小。

我不希望这样,因为如果一行仅包含横向拇指,我不希望它不必要地高。

我希望每个 .row div 都是它包含的最高图像的高度。

如果一行中只有风景,则该行的高度将是最高风景的高度。

我想我可以通过将元素的高度设置为 100% 来解决这个问题:

<style>
.row a {
    position: relative;
    float: left;
    width: 150px;
    height: 100%;   
}
</style>

这将是其父级 .row div 的 100%。但由于某种原因,只有当我指定 .row div 的高度时,这才有效,然后我遇到同样的问题(无论实际内容如何,​​所有行都等于高度:

<style>
div.row {
    float: left;
    clear: both;
    height: 150px;
}

.row a {
    position: relative;
    float: left;
    width: 150px;
    height: 100%;   
}
</style>

但是,由于 .row div 随其包含的图像动态增长,它实际上有我想要用我的高度来引用的高度:100%,但由于某种原因它不起作用,

为什么? 在转向这里之前,我花了很多时间研究这个问题,希望有人能给出黄金答案。

I have some some rows of image thumbs inside anchors inside a div, one div for each row, like this:

<div class="row"><a><img></a><a><img></a><a><img></a></div>
<div class="row"><a><img></a><a><img></a><a><img></a></div>

The images have different proportions, some portrait, some landscape orientation. The max dimensions for the thumbs are 150px (wide or tall), and they are always either 150px wide or high.

I want all images to align with the bottom of their parent .row div.
So if there is any portraits in a row, any landscapes will align at the bottom and have some space above them.
To have the images align at bottom, I position them absolute, and relative to their anchor
To do this, I use the following CSS:

<style>
div.row {
    float: left;
    clear: both;
}

.row a {
    position: relative;
    float: left;
    width: 175px;
}

.row a img{
    position: absolute;
    bottom: 0px;
}
</style>

The above works fine and as expected, but it's got a flaw that I'm trying to fix: It requires the height of the relatively positioned anchor tags to be set to a specific size.

I don't want that, because if a row contains only landscape thumbs, I do not want it to be unnecessarily high.

I want each .row div to be the height of the highest image it contains.

If there are only landscapes in a row, the height of that row will be just that of the highest landscape.

I though I would be able to fix this by setting the height if the elements to 100%:

<style>
.row a {
    position: relative;
    float: left;
    width: 150px;
    height: 100%;   
}
</style>

That would then be 100% of its parent, the .row div. But for some reason, this only works if I specify the height of the .row div, and then I have the same problem (all rows equal hight regardless of actual content:

<style>
div.row {
    float: left;
    clear: both;
    height: 150px;
}

.row a {
    position: relative;
    float: left;
    width: 150px;
    height: 100%;   
}
</style>

However, since the .row div grows dynamically with its contained images, it actually has the height I'm trying to refer to with my height : 100% for the a tag. But for some reason it does not work.

Why? What am I not understanding here, and how to do this?
I spend many hours on this before turning here, hopefully someone has the golden answer.

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

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

发布评论

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

评论(1

失去的东西太少 2024-10-13 07:15:15

使用inline-block而不是浮动可能更容易。像这样的东西:

http://jsfiddle.net/cvQAZ/1/

或者您需要链接吗实际上有相同的高度吗?然后尝试“表格”布局(IE6不支持):

http://jsfiddle.net/DpvgR/1/

It's possibly easier to use inline-block instead of floating. Something like this:

http://jsfiddle.net/cvQAZ/1/

Or do you need the links actually have the same height? Then try a "table" layout (not supported by IE6):

http://jsfiddle.net/DpvgR/1/

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