简体   繁体   English

帮助 CSS + 媒体查询 + 元素样式

[英]Help with CSS + Media Queries + element style

Can some CSS ninja please verify if my thought process is right or wrong.请一些 CSS 忍者验证我的思考过程是对还是错。

I have a project with media queries settings css depending on if device is landscape or portrait.我有一个带有媒体查询设置 css 的项目,具体取决于设备是横向还是纵向。 If my css sets width of an element to 500px and I modify that width to let say 600px, the element style is modified.如果我的 css 将元素的宽度设置为 500 像素,并且我修改该宽度为 600 像素,则元素样式将被修改。

When the device changes orientation - the element style stays at 600px regardless of what my css file is set.当设备改变方向时 - 无论我的 css 文件设置什么,元素样式都保持在 600 像素。 Because the element style is higher hierarchy than styling from a css file.因为元素样式的层次结构高于 css 文件中的样式。

What I need to do is set that element.style('width')='' to more or less erase it.我需要做的是设置 element.style('width')='' 或多或少地删除它。

Am I right?我对吗? is there a way to get the css file to always override the element style?有没有办法让 css 文件始终覆盖元素样式?

Thanks!谢谢!

If you use the.important declaration in a css file it will overwrite any inline styles.如果您在 css 文件中使用 .important 声明,它将覆盖任何内联 styles。 So in the example below the width of the div will be 400px.所以在下面的例子中,div 的宽度是 400px。

.example {
    width:400px !important;
}

<div class="example" style="width:600px"></div>

Not sure why you are allowing a user to modify a width though - seems like you'll always get into an undesirable situation.不知道为什么您允许用户修改宽度 - 似乎您总是会遇到不良情况。 But whatever, this should work:)但无论如何,这应该有效:)

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

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