gpt4 book ai didi

css - 使用负边距时如何导致相邻单元格溢出

转载 作者:可可西里 更新时间:2023-11-01 14:48:01 24 4
gpt4 key购买 nike

我想在使用 CSS 表格布局时使用负边距导致相邻表格单元格溢出。

更新 3 - 这是我想要的,但没有明确指定绿框宽度:

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<div style="display: table; margin-left: 300px;">
<div style="display: table-row;">

<!--Hack: width: 250px;-->
<div style="display: table-cell; background-color: green; height: 30px; overflow-x: visible; width: 250px;"></div>


<div style="display: table-cell; background-color: gray; height: 30px;">

<div style="position: relative; width: 100%; background-color: azure;">
<div style="position: relative; width: 500px; margin-left: -250px; background-color: yellow; height: 5px;"></div>
</div>
</div>
</div>
</div>
</body>
</html>

更新 2

@Per Salbark - 情况相当简单。黄色框由用户设置样式,而微笑和灰色框是“框架”部分。黄色框的中心必须与第一个单元格的右边缘对齐。绿色和灰色框的宽度必须相同且等于黄色框宽度/2。 (请注意,我在绿色单元格中放置了一个 ? 标记只是为了确保它可见,否则它应该是空白的)。

这里的关键点是,当人们设计他们的黄色盒子时,他们不需要知道绿色盒子也需要设计,因为负边距是行不通的。

更新 1 -

问:为什么我需要它?

A:我试图让我的布局尽可能动态。绿色单元格包含一些未知宽度的东西,黄色框的宽度也是可变的。我必须完整地展示他们的内容。我原以为主 div(表格)会溢出并使用第一个单元格自行扩展,但这并没有发生。

http://jsbin.com/koqefomevulu/1/edit?html,output

<div style="display: table; margin-left: 300px;">
<div style="display: table-row;">
<div style="display: table-cell; background-color: green; overflow-x: visible;
height: 30px">
?
</div>
<div style="display: table-cell; background-color: gray; height: 30px;">
<div style="position: relative; width: 100%; background-color: azure;">
<div style="position: relative; width: 500px;
margin-left: -250px; background-color: yellow; height: 5px;">
</div>
</div>
</div>
</div>
</div>

基本上我希望绿色单元格扩展 250px 以完全适合黄色框 - 想知道是否有可能实现它?

最佳答案

编辑:看这个:http://jsbin.com/xibeyucelowu/1/ -- 不需要为此设置固定边距或宽度 -- 我认为比我发布的其他 fiddle 更好..


怎么样:http://jsbin.com/koqefomevulu/29/

它的工作原理类似于将绿色宽度设置为 250px

之所以有效,是因为黄色的宽度恰好是它的负边距的两倍......也许一些 JS 在加载黄色的动态内容后设置负边距?您只需要以这种方式定位一个元素。我认为您无法仅使用 CSS 找到解决方案。

有必要使用表格样式吗?

关于css - 使用负边距时如何导致相邻单元格溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25894164/

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