简体   繁体   English

CSS特殊性与!important内联

[英]CSS specificity beat inline with !important

Here is my site: http://stage.samkeddy.com/ 这是我的网站: http : //stage.samkeddy.com/

It's responsive using a couple media queries and a mobile menu button powered by javascript. 使用几个媒体查询和一个由javascript驱动的移动菜单按钮,它可以做出响应。

Here is the javascript for the menu button: 这是菜单按钮的javascript:

function toggleMenu () {
    if (menuIsVisible == false) {
        collapsibleMenu.style.height = 'auto';
        content.style.paddingTop = '290px';
        menuIsVisible = true;
    }
    else {
        collapsibleMenu.style.height = '0';
        content.style.paddingTop = '80px';
        menuIsVisible = false;
    }
}

so you can see that I need to adjust the padding at the top of the content div, in order to offset the menu 因此您可以看到我需要调整content div顶部的填充,以抵消菜单

But if resize to the mobile size, open the menu, and then resize back to the desktop size, the padding isn't fixed by the media query, because there's still an inline style from the javascript. 但是,如果将大小调整为移动大小,请打开菜单,然后再调整为桌面大小,则媒体查询不会固定填充,因为javascript中仍存在内联样式。 I tried making the padding on the desktop version !important, but it the padding still doesn't change when resized, even though according to this !important beats inline. 我试图使桌面版本的填充!重要的,但它的填充仍然调整时不改变,即使按照这个 !重要的节拍在线。

You can test for yourself by opening the size (how it should look), resizing to a mobile width(the nav will disappear, and you will see the menu button), clicking the menu button (leave the menu open), then resize the site back to a desktop width. 您可以通过以下步骤进行测试:打开尺寸(外观),调整为移动宽度(导航栏将消失,您将看到菜单按钮),单击菜单按钮(将菜单保持打开状态),然后调整尺寸网站回到桌面宽度。 You will see the padding is still there. 您将看到填充仍然存在。 If you inspect it, you can see the original padding is crossed out in favor of the inline style. 如果您对其进行检查,则可以看到原始的填充已被删除,有利于内联样式。

I know this would be possible by monitoring the width with javascript and setting the padding then, but I really don't want to do that, and don't think I should have to. 我知道可以通过使用javascript监视宽度并设置填充来实现,但是我真的不想这样做,也不认为我应该这样做。

EDIT: solved 编辑:解决

First, I should have been adding classes, rather than adding CSS with my javascript. 首先,我应该添加类,而不是在JavaScript中添加CSS。

Then I assumed that putting !important outside of a media query would make it only show up on desktop, but it took over all media queries. 然后,我假设将!important放在媒体查询之外将使其仅显示在桌面上,但它接管了所有媒体查询。 So placing just this in a query made it work. 因此,将其仅放在查询中就可以了。 Note that if I was using 2 separate menus (mobile/desktop), I wouldn't need this, but since it was fixed and the #content needed padding, it had to be done. 请注意,如果我使用2个单独的菜单(移动/桌面),则不需要此菜单,但由于它是固定的,并且#content需要填充,因此必须完成此操作。 But using this technique you can also use only a single menu, but doing the height for the menu this way. 但是使用这种技术,您也可以只使用一个菜单,但是可以通过这种方式来调整菜单的高度。 I've demonstrated the technique in a codepen: http://codepen.io/anon/pen/JFvay 我已经在Codepen中演示了该技术: http ://codepen.io/anon/pen/JFvay

Adding this code to your stylesheet should solve the problem, I just tried this on your website using the Developer Tools and it's working: 将此代码添加到样式表应该可以解决问题,我刚刚使用开发人员工具在您的网站上尝试了此方法,并且可以正常工作:

@media only screen and (min-width: 643.2px) {
    #content {
        padding-top: 80px !important;
    }
}

Although I'd strongly recommend you to create a separate navigation menu for mobile devices and resort to using @media-queries to display it. 尽管我强烈建议您为移动设备创建一个单独的导航菜单,然后使用@ media-queries进行显示。

Your problem at heart is that you're mixing CSS and in-line styles. 内在的问题是您要混合使用CSS和内联样式。 As a general rule, avoid placing specific CSS properties directly on elements, whether in HTML, by using element.style.<property> = , or via jQuery's .css() feature. 通常,请避免通过使用element.style.<property> =或通过jQuery的.css()功能,将特定的CSS属性直接放置在元素上(无论是在HTML中.css() Instead, you should define the properties you want as CSS rules, using classes: 相反,您应该使用类将所需的属性定义为CSS规则:

#collapsible-menu                       { height: auto;  }
#content                                { padding-top: 290px; }
#someelt.menu-visible #collapsible-menu { height: 0; }
#someelt.menu-visible #content          { padding-top: 80px; }

where someelt is some higher-level ancestor element. 其中someelt是一些高级祖先元素。 Then, your JS becomes simply 然后,您的JS变得简单

function toggleMenu() {
    document.getElementById('someelt').classList.toggle('menu-visible');
}

If you are targeting browsers which do not support classList (see CanIUse ), jQuery provides its own version of class toggling. 如果您要针对不支持classList浏览器(请参见CanIUse ),则jQuery提供其自己的版本的类切换。

CSS is not an imperative language, but if you want, you can think of the #someelt.menu-visible part of the last two rules above as a kind of if statement: " If menus are visible, then shrink collapsible-menu to zero height", etc. In this metaphor of CSS as a kind of programming language (which it is), the presence of the menu-visible class of #someelt could be thought of as a kind of boolean "variable", I suppose. CSS不是强制性语言,但是如果您愿意,可以将上述最后两个规则的#someelt.menu-visible部分视为一种if语句:“ 如果菜单可见, 则将 collapsible-menu缩小为零我想,在CSS作为一种编程语言的隐喻中(它是),可以将#someelt menu-visible类的#someelt视为一种布尔“变量”。 Most likely, you will no longer need a corresponding variable in your JS. 很可能,您将不再需要在JS中使用相应的变量。

Anyway, the advantage of this is that people looking at your code can see all your CSS-related logic just by looking at the CSS file, instead of having to look at both CSS and JS, and you can change CSS-related things in just one place. 无论如何,这样做的好处是,查看代码的人可以仅通过查看CSS文件来查看与CSS相关的所有逻辑,而不必同时查看CSS和JS,并且您可以仅更改CSS相关的内容一个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM