css 如何从低阶元素中删除高阶元素的填充?

z0qdvdin  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(80)

这是非常hacky,但我被难住了。我正在使用一个内部工具来创建一个网页。因此,由于这些工具的性质,我只能访问一些生成的HTML和CSS。
所以,强加在我身上的是HTML

<div class="example">
    <div class="whatICanAccess">
    </div>
</div>

我的CSS:

.example {
padding : 1.6em;
}

我唯一可以编辑我的CSS的地方是在“WhaticanAccess”HTML标记内,使用style=“foo”。
我是否可以以任何方式从外部元素(“example”)中删除填充?
我是否可以删除外部元素的填充,只影响“WhatICanAccess”的直接父元素?
Example是一个贯穿整个代码的类,我只想在一个特定的地方删除它--但是正如我所说的,我不能添加更具体的标识符/标记--我只能在这一个地方进行编辑。

更新

现在我有了这个HTML:

<div class="example">
   <div class="moreSpecific" style=" padding: -1.8em;>
</div>
</div>

但是.example的1. 6 em仍然是覆盖的,我在这里做错了什么?

8yoxcaq7

8yoxcaq71#

给予第1个地方一个单独的类,并给它负边距来平衡填充。
或者,使用position: absolute和width和height值,使用这两个值都不用担心不能访问父元素。
尝试做一个JSFiddle,但显然他们暂时删除了“保存”选项...

<div class="example">
    <div class="whatICanAccess">
        <div class="noPadding">This div has negative margins and absolute positioning</div>
        <div>This div inherits its padding from 'example'</div>
    </div>
</div>

.example {
    padding : 1.6em;
    height: 400px;
    width 100%;
    background-color: #ccc;
}
.whatICanAccess {
    height: 300px;
    background-color: #fff;
}
.noPadding {
    position: absolute;
    margin: -1.6em 0 0 0;
    background-color: #eee;
}

如果您必须使用内联CSS...

<div style="margin: -1.6em 0 0 0; position: absolute;">This div has negative margins and absolute positioning</div>
14ifxucb

14ifxucb2#

如果知道example的id,就可以使用document.getElementById('example').style.padding='0px';
给你的html一个负的边距或者填充也可以。

相关问题