CSS - display: none; 不起作用

31

我正在尝试开发一个移动样式表,并且在这个样式表中,我想要移除一个特定的 div。

在该 div 的 HTML 代码中,我放置了一个名为“tfl”的 id,如下所示:

<div id="tfl" style="display: block; width: 187px; height: 260px; font-family: Verdana, Arial, Helvetica, sans-serif !important; background: url(http://www.tfl.gov.uk/tfl/gettingaround/journeyplanner/banners/images/widget-panel.gif) #fff no-repeat; font-size: 11px; border: 1px solid #103994; border-radius: 4px; box-shadow: 2px 2px 3px 1px #ccc;">
            <div style="display: block; padding: 30px 0 15px 0;">
                <h2 style="color: rgb(36, 66, 102); text-align: center; display: block; font-size: 15px; font-family: arial; border: 0; margin-bottom: 1em; margin-top: 0; font-weight: bold !important; background: 0; padding: 0">Journey Planner</h2>
                <form action="http://journeyplanner.tfl.gov.uk/user/XSLT_TRIP_REQUEST2" id="jpForm" method="post" target="tfl" style="margin: 5px 0 0 0 !important; padding: 0 !important;">
                    <input type="hidden" name="language" value="en" />
                    <!-- in language = english -->
                    <input type="hidden" name="execInst" value="" /><input type="hidden" name="sessionID" value="0" />
                    <!-- to start a new session on JP the sessionID has to be 0 -->
                    <input type="hidden" name="ptOptionsActive" value="-1" />
                    <!-- all pt options are active -->
                    <input type="hidden" name="place_origin" value="London" />
                    <!-- London is a hidden parameter for the origin location -->
                    <input type="hidden" name="place_destination" value="London" /><div style="padding-right: 15px; padding-left: 15px">
                        <input type="text" name="name_origin" style="width: 155px !important; padding: 1px" value="From" /><select style="width: 155px !important; margin: 0 !important;" name="type_origin"><option value="stop">Station or stop</option>
                            <option value="locator">Postcode</option>
                            <option value="address">Address</option>
                            <option value="poi">Place of interest</option>
                        </select>
                    </div>
                    <div style="margin-top: 10px; margin-bottom: 4px; padding-right: 15px; padding-left: 15px; padding-bottom: 15px; background: url(http://www.tfl.gov.uk/tfl/gettingaround/journeyplanner/banners/images/panel-separator.gif) no-repeat bottom;">
                        <input type="text" name="name_destination" style="width: 100% !important; padding: 1px" value="232 Kingsbury Road (NW9)" /><select style="width: 155px !important; margin-top: 0 !important;" name="type_destination"><option value="stop">Station or stop</option>
                            <option value="locator">Postcode</option>
                            <option value="address" selected="selected">Address</option>
                            <option value="poi">Place of interest</option>
                        </select>
                    </div>
                    <div style="background: url(http://www.tfl.gov.uk/tfl/gettingaround/journeyplanner/banners/images/panel-separator.gif) no-repeat bottom; padding-bottom: 2px; padding-top: 2px; overflow: hidden; margin-bottom: 8px">
                        <div style="clear: both; background: url(http://www.tfl.gov.uk/tfl-global/images/options-icons.gif) no-repeat 9.5em 0; height: 30px; padding-right: 15px; padding-left: 15px"><a style="text-decoration: none; color: #113B92; font-size: 11px; white-space: nowrap; display: inline-block; padding: 4px 0 5px 0; width: 155px" target="tfl" href="http://journeyplanner.tfl.gov.uk/user/XSLT_TRIP_REQUEST2?language=en&amp;ptOptionsActive=1" onclick="javascript:document.getElementById('jpForm').ptOptionsActive.value='1';document.getElementById('jpForm').execInst.value='readOnly';document.getElementById('jpForm').submit(); return false">More options</a></div>
                    </div>
                    <div style="text-align: center;">
                        <input type="submit" title="Leave now" value="Leave now" style="border-style: none; background-color: #157DB9; display: inline-block; padding: 4px 11px; color: #fff; text-decoration: none; border-radius: 3px; border-radius: 3px; border-radius: 3px; box-shadow: 0 1px 3px rgba(0,0,0,0.25); box-shadow: 0 1px 3px rgba(0,0,0,0.25); box-shadow: 0 1px 3px rgba(0,0,0,0.25); text-shadow: 0 -1px 1px rgba(0,0,0,0.25); border-bottom: 1px solid rgba(0,0,0,0.25); position: relative; cursor: pointer; font: bold  13px/1 Arial,Helvetica,sans-serif; text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.4); line-height: 1;" />
                    </div>
                </form>
            </div>
        </div>

这段代码不是我的,而是TFL网站提供的嵌入代码。

我想隐藏这个div,以使移动用户更加友好,我做了以下操作:

#tfl {
display: none;
}

虽然我已经将其应用于相应的头部,但此代码对我没有效果,如下所示:

h3.tfl {
display: none;
}

问题出在哪里?这段代码在一个映射图像的另一个页面上运行正常。(我不能使用jQuery或JavaScript(考察CSS的大学作业)。)


2
这是在媒体查询中吗?您尝试过使用更具体的选择器或添加!important来显示:none吗?顺便说一下,h3.tfl正在寻找一个带有类tfl的h3。您可能意味着h3 .tfl。 - Kai Qing
你尝试过在你的类中添加visibility:hidden吗?我通常都会使用这个方法...也许改变颜色来确保你的样式被应用了。 - Tim
7个回答

47
这是因为内联样式 display:block 覆盖了您的 CSS 样式。您需要删除这个内联样式或使用以下代码:

这是因为内联样式display:block 覆盖了你的CSS。你需要删除这个内联样式或者使用:

#tfl {
  display: none !important;
}

这将覆盖内联样式。请注意,通常情况下不建议使用!important,除非这是最后的选择。


28

样式属性中删除display: block;

<div id="tfl" style="display: block; width: 187px; height: 260px;

内联样式具有比外部CSS文件更高的优先级。


11

尝试将此添加到您的CSS中

#tfl {
display: none !important;
}

9
另一个技巧是使用


.class {
position: absolute;
visibility:hidden;
display:none;
}

这不太可能影响您的布局(因为它将其移出了流程),并确保用户看不到它,如果稍后display:none可用,则会起作用。请记住,visibility:hidden可能无法从屏幕阅读器中删除它。


4
在所提供的HTML源代码中,元素#tfl具有内联样式"display:block"。内联样式始终会覆盖样式表样式...
然后,您有一些选项(正如您所说,您无法修改HTML代码或使用JavaScript):
  • force display:none with !important rule (not recommended)
  • put the div offscreen with theses rules :

    #tfl {
        position: absolute;
        left: -9999px;
    }
    

-1
最好的方法是在你的HTML代码中添加ID,然后在CSS中将其显示设置为none。 例如:
在HTML文件中 在CSS文件中
#check{ display:none; }

-2
请检查以下HTML代码,我已从样式中删除了"display:block"属性。
<div id="tfl" style="width: 187px; height: 260px; font-family: Verdana, Arial, Helvetica, sans-serif !important; background: url(http://www.tfl.gov.uk/tfl/gettingaround/journeyplanner/banners/images/widget-panel.gif) #fff no-repeat; font-size: 11px; border: 1px solid #103994; border-radius: 4px; box-shadow: 2px 2px 3px 1px #ccc;">
        <div style="display: block; padding: 30px 0 15px 0;">
            <h2 style="color: rgb(36, 66, 102); text-align: center; display: block; font-size: 15px; font-family: arial; border: 0; margin-bottom: 1em; margin-top: 0; font-weight: bold !important; background: 0; padding: 0">Journey Planner</h2>
            <form action="http://journeyplanner.tfl.gov.uk/user/XSLT_TRIP_REQUEST2" id="jpForm" method="post" target="tfl" style="margin: 5px 0 0 0 !important; padding: 0 !important;">
                <input type="hidden" name="language" value="en" />
                <!-- in language = english -->
                <input type="hidden" name="execInst" value="" /><input type="hidden" name="sessionID" value="0" />
                <!-- to start a new session on JP the sessionID has to be 0 -->
                <input type="hidden" name="ptOptionsActive" value="-1" />
                <!-- all pt options are active -->
                <input type="hidden" name="place_origin" value="London" />
                <!-- London is a hidden parameter for the origin location -->
                <input type="hidden" name="place_destination" value="London" /><div style="padding-right: 15px; padding-left: 15px">
                    <input type="text" name="name_origin" style="width: 155px !important; padding: 1px" value="From" /><select style="width: 155px !important; margin: 0 !important;" name="type_origin"><option value="stop">Station or stop</option>
                        <option value="locator">Postcode</option>
                        <option value="address">Address</option>
                        <option value="poi">Place of interest</option>
                    </select>
                </div>
                <div style="margin-top: 10px; margin-bottom: 4px; padding-right: 15px; padding-left: 15px; padding-bottom: 15px; background: url(http://www.tfl.gov.uk/tfl/gettingaround/journeyplanner/banners/images/panel-separator.gif) no-repeat bottom;">
                    <input type="text" name="name_destination" style="width: 100% !important; padding: 1px" value="232 Kingsbury Road (NW9)" /><select style="width: 155px !important; margin-top: 0 !important;" name="type_destination"><option value="stop">Station or stop</option>
                        <option value="locator">Postcode</option>
                        <option value="address" selected="selected">Address</option>
                        <option value="poi">Place of interest</option>
                    </select>
                </div>
                <div style="background: url(http://www.tfl.gov.uk/tfl/gettingaround/journeyplanner/banners/images/panel-separator.gif) no-repeat bottom; padding-bottom: 2px; padding-top: 2px; overflow: hidden; margin-bottom: 8px">
                    <div style="clear: both; background: url(http://www.tfl.gov.uk/tfl-global/images/options-icons.gif) no-repeat 9.5em 0; height: 30px; padding-right: 15px; padding-left: 15px"><a style="text-decoration: none; color: #113B92; font-size: 11px; white-space: nowrap; display: inline-block; padding: 4px 0 5px 0; width: 155px" target="tfl" href="http://journeyplanner.tfl.gov.uk/user/XSLT_TRIP_REQUEST2?language=en&amp;ptOptionsActive=1" onclick="javascript:document.getElementById('jpForm').ptOptionsActive.value='1';document.getElementById('jpForm').execInst.value='readOnly';document.getElementById('jpForm').submit(); return false">More options</a></div>
                </div>
                <div style="text-align: center;">
                    <input type="submit" title="Leave now" value="Leave now" style="border-style: none; background-color: #157DB9; display: inline-block; padding: 4px 11px; color: #fff; text-decoration: none; border-radius: 3px; border-radius: 3px; border-radius: 3px; box-shadow: 0 1px 3px rgba(0,0,0,0.25); box-shadow: 0 1px 3px rgba(0,0,0,0.25); box-shadow: 0 1px 3px rgba(0,0,0,0.25); text-shadow: 0 -1px 1px rgba(0,0,0,0.25); border-bottom: 1px solid rgba(0,0,0,0.25); position: relative; cursor: pointer; font: bold  13px/1 Arial,Helvetica,sans-serif; text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.4); line-height: 1;" />
                </div>
            </form>
        </div>
    </div

2
请记得让你的答案对未来遇到这个问题的人有用。考虑在几行代码中格式化或指出具体修复方法。 - Brian Phillips

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接