覆盖并重置 CSS 样式:自动或无不起作用

发布于 2024-10-19 08:27:18 字数 624 浏览 1 评论 0原文

我想覆盖为所有表定义的以下 CSS 样式:

table {
        font-size: 12px;
        width: 100%;
        min-width: 400px;
        display:inline-table;
    }

我有一个特定的表,其类名为 'other'
最后表格装饰应该如下所示:

table.other {
    font-size: 12px;
}

所以我需要删除 3 个属性:widthmin-widthdisplay

我尝试使用 重置noneauto,但没有帮助,我的意思是这些情况:

table.other {
    width: auto;
    min-width: auto;
    display:auto;
}
table.other {
    width: none;
    min-width: none;
    display:none;
}

I would like to override following CSS styling defined for all tables:

table {
        font-size: 12px;
        width: 100%;
        min-width: 400px;
        display:inline-table;
    }

I have specific table with class called 'other'.
Finally table decoration should looks like:

table.other {
    font-size: 12px;
}

so i need remove 3 properties: width,min-width and display

I tried to reset with none or auto, but didn't help, I mean these cases:

table.other {
    width: auto;
    min-width: auto;
    display:auto;
}
table.other {
    width: none;
    min-width: none;
    display:none;
}

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

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

发布评论

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

评论(8

指尖上的星空 2024-10-26 08:27:18

我相信第一组属性不起作用的原因是因为 display,因此应忽略该属性。在这种情况下,inline-table 仍然有效,并且由于 width 不适用于 inline 元素,因此该属性集将不起作用任何事物。

第二组属性将简单地隐藏表格,因为这就是 display: none 的用途。

尝试将其重置为table

table.other {
    width: auto;
    min-width: 0;
    display: table;
}

编辑: min-width 默认为 0,而不是 auto

I believe the reason why the first set of properties will not work is because there is no auto value for display, so that property should be ignored. In that case, inline-table will still take effect, and as width do not apply to inline elements, that set of properties will not do anything.

The second set of properties will simply hide the table, as that's what display: none is for.

Try resetting it to table instead:

table.other {
    width: auto;
    min-width: 0;
    display: table;
}

Edit: min-width defaults to 0, not auto

看春风乍起 2024-10-26 08:27:18

“none”不会做你认为它会做的事情。为了“清除”CSS 属性,您必须将其设置回 CSS 标准定义的默认值。因此,您应该在您最喜欢的参考中查找默认值。

table.other {
    width: auto;
    min-width: 0;
    display:table;
}

"none" does not do what you assume it does. In order to "clear" a CSS property, you must set it back to its default, which is defined by the CSS standard. Thus you should look up the defaults in your favorite reference.

table.other {
    width: auto;
    min-width: 0;
    display:table;
}
雪落纷纷 2024-10-26 08:27:18
Set min-width: inherit /* Reset the min-width */

试试这个。它会起作用的。

Set min-width: inherit /* Reset the min-width */

Try this. It will work.

尬尬 2024-10-26 08:27:18

表的默认 display 属性为 display:table;。唯一有用的值是inline-table。所有其他 display 值对于表格元素均无效。

没有 auto 选项可以将其重置为默认值,但如果您使用的是 Javascript,则可以将其设置为空字符串,这样就可以解决问题。

width:auto; 有效,但不是默认值。表格的默认宽度为 100%,而 width:auto; 将使元素仅占用所需的宽度。

不允许使用 min-width:auto;。如果设置 min-width,它必须有一个值,但将其设置为零可能与将其重置为默认值一样好。

The default display property for a table is display:table;. The only other useful value is inline-table. All other display values are invalid for table elements.

There isn't an auto option to reset it to default, although if you're working in Javascript, you can set it to an empty string, which will do the trick.

width:auto; is valid, but isn't the default. The default width for a table is 100%, whereas width:auto; will make the element only take up as much width as it needs to.

min-width:auto; isn't allowed. If you set min-width, it must have a value, but setting it to zero is probably as good as resetting it to default.

沐歌 2024-10-26 08:27:18

好吧, display: none; 根本不会显示表格,请尝试 display: inline-block; 并将宽度和最小宽度声明保留为“auto”。

Well, display: none; will not display the table at all, try display: inline-block; with the width and min-width declarations remaining 'auto'.

诗笺 2024-10-26 08:27:18

我发现恢复最小宽度设置的最佳方法是:

min-width: 0;
min-width: unset;

未设置在规范中,但某些浏览器(IE 10)不尊重它,因此 0 在大多数中是一个很好的后备案例。
最小宽度:0;

The best way that I've found to revert a min-width setting is:

min-width: 0;
min-width: unset;

unset is in the spec, but some browsers (IE 10) do not respect it, so 0 is a good fallback in most cases.
min-width: 0;

最笨的告白 2024-10-26 08:27:18

我最终使用 Javascript 来完善一切。

我的JS小提琴: https://jsfiddle.net/QEpJH/612/

HTML:

<div class="container">
    <img src="http://placekitten.com/240/300">
</div>

<h3 style="clear: both;">Full Size Image - For Reference</h3>
<img src="http://placekitten.com/240/300">

CSS:

.container {
    background-color:#000;
    width:100px;
    height:200px;

    display:flex;
    justify-content:center;
    align-items:center;
    overflow:hidden;

}

JS:

$(".container").each(function(){
    var divH = $(this).height()
    var divW = $(this).width()
    var imgH = $(this).children("img").height();
    var imgW = $(this).children("img").width();

    if ( (imgW/imgH) < (divW/divH)) { 
        $(this).addClass("1");
        var newW = $(this).width();
        var newH = (newW/imgW) * imgH;
        $(this).children("img").width(newW); 
        $(this).children("img").height(newH); 
    } else {
        $(this).addClass("2");
        var newH = $(this).height();
        var newW = (newH/imgH) * imgW;
        $(this).children("img").width(newW); 
        $(this).children("img").height(newH); 
    }
})

I ended up using Javascript to perfect everything.

My JS fiddle: https://jsfiddle.net/QEpJH/612/

HTML:

<div class="container">
    <img src="http://placekitten.com/240/300">
</div>

<h3 style="clear: both;">Full Size Image - For Reference</h3>
<img src="http://placekitten.com/240/300">

CSS:

.container {
    background-color:#000;
    width:100px;
    height:200px;

    display:flex;
    justify-content:center;
    align-items:center;
    overflow:hidden;

}

JS:

$(".container").each(function(){
    var divH = $(this).height()
    var divW = $(this).width()
    var imgH = $(this).children("img").height();
    var imgW = $(this).children("img").width();

    if ( (imgW/imgH) < (divW/divH)) { 
        $(this).addClass("1");
        var newW = $(this).width();
        var newH = (newW/imgW) * imgH;
        $(this).children("img").width(newW); 
        $(this).children("img").height(newH); 
    } else {
        $(this).addClass("2");
        var newH = $(this).height();
        var newW = (newH/imgH) * imgW;
        $(this).children("img").width(newW); 
        $(this).children("img").height(newH); 
    }
})
要走就滚别墨迹 2024-10-26 08:27:18

我尝试过:

width:0%

为我工作

I have tried:

width:0%

Worked for me

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