gpt4 book ai didi

CSS 垂直对齐和内联 block 问题

转载 作者:行者123 更新时间:2023-11-28 08:40:26 24 4
gpt4 key购买 nike

我正在尝试使用此处描述的技术创建一个 3 列布局,其中所有 3 列具有相同的高度(高度事先未知,因此我无法指定高度的硬编码值):

http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks

我正在稍微改变方法,因为我想使用“display: inline-block”而不是“float: left”。我正在使用的代码如下。

我遇到的问题是内联 block 似乎无法正常工作,因为它将我的 3 个 div 一个放在另一个下面,而不是并排放置。谁能向我解释为什么这不起作用?

这是我的 CSS:

#col1 {
width:33.33333333%;
vertical-align: top;
margin-left: 66.66666667%;
display: inline-block;
}

#col2 {
width:33.33333333%;
vertical-align: top;
margin-left: 100%;
display: inline-block;
}

#col3 {
width:33.33333333%;
vertical-align: top;
margin-left: 133.33333333%;
display: inline-block;
}

#container3 {
width: 100%;
margin-left: 0%;
background-color: green;
overflow:hidden;
}

#container2 {
width: 100%;
margin-left: -33.33333333%;
background-color: yellow;
}

#container1 {
width: 100%;
margin-left: -33.33333333%;
background-color: red;
}

这是我的实际 HTML:

<!doctype html>
<html>
<head>
<title>My Sample Columns</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div id="container3">
<div id="container2">
<div id="container1">
<div id="col1">
one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one one
</div>
<div id="col2">
two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two two
</div>
<div id="col3">
three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three three
</div>
</div>
</div>
</div>
</div>
</body>
</html>

最佳答案

我认为您没有正确解释问题。这些列不是一个接一个,而是并排放置。 col2 中的文本从 col1 文本结束的地方开始。第 3 列也一样。

它不起作用的原因是 col2 和 col3 边距延伸到页面的左侧。

col2 边距不能穿过 col1 中的文本,因此它必须在文本下方才能到达左侧。与 col3 相同,但另外它也不能通过 col2 文本。如果您从 col2 中删除文本,col3 的顶部将开始在 col1 文本下方对齐。

在 FireFox 或 Chrome 中亲自查看。

  • 将光标放在 col2 中的文本上并右键单击
  • 选择检查元素。
  • 将光标移至开发者窗口并移至 HTML 行用于 Chrome 的“元素”选项卡或 FireFox 的“检查器”选项卡中的 col2 和 col3。
  • 您将需要扩展更高层的内容。

在浏览器窗口中,您会看到阴影框延伸到窗口的左侧。

关于CSS 垂直对齐和内联 block 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27829903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com