简体   繁体   English

CSS显示没有对div及其内容不起作用

[英]Css display none not working on div and its contents

Having a little trouble with css display:none. CSS显示有点麻烦:无。 The problem is I want to hide a div that contains a repeater on large screens but the div and its contents are still there when testing in the browser. 问题是我想在大屏幕上隐藏一个包含转发器的div,但是在浏览器中测试时div及其内容仍然存在。 The div I would like to hide has the id imgList 我要隐藏的div的ID为imgList

I am unsure where the problem lies. 我不确定问题出在哪里。 Any help would be greatly received. 任何帮助将不胜感激。

html html

 <div class="row">
            <div id="slideShowContainer" class="col-md-12">

        <div id="slideShow" class="slideshow ">
            <div id="slideShowWindow">
                <asp:Repeater ID="rptSlides" runat="server" ClientIDMode="Static">
                    <ItemTemplate>
                        <div class="slide">
                            <img runat="server" src='<%#DataBinder.Eval(Container.DataItem,"Value") %>' width="650" height="450" />
                        </div>
                    </ItemTemplate>
                </asp:Repeater>

            </div>
        </div>

            </div>
            <div id="imgList" class="col-xs-12">


        <asp:Repeater id="rptShowPics" runat="server">
            <ItemTemplate>
                <div class="col-sm-12">
                    <img src='<%#DataBinder.Eval(Container.DataItem,"Value") %>'/>
                </div></div>
            </ItemTemplate>
                        </asp:Repeater>

            </div>

my css: 我的CSS:

img {
    width: 100%;
    height: auto;
}
@media screen and (min-width: 800px) {

#slideShowContainer {
    display: block;
}

#imgList {
    display:none;
}

#slideShow #slideShowWindow {
    width: 650px;
    height: 450px;
    margin: 0;
    padding: 0;
    position: relative;
    overflow: hidden;
    margin-left: auto;
    margin-right: auto;
}

    #slideShow #slideShowWindow .slide {
        margin: 0;
        padding: 0;
        width: 650px;
        height: 450px;
        float: left;
        position: relative;
    }


}

@media screen and (max-width:800px) {
#imgList {
    display:block;
}
#slideShowContainer {
    display: none;
}
}

As I said I am unsure where the problem lies. 正如我所说,我不确定问题出在哪里。 the slideshow div display:none works fine and I cant see what is so different to cause such a problem 幻灯片div显示:无效果,我看不出有什么不同而导致这种问题

EDIT: 编辑:

the rendered html: 呈现的html:

 <div class="col-sm-12">
        <span id="ContentPlaceHolder1_lblError"></span>
        <div class="row">
            <div id="slideShowContainer" class="col-md-12">

        <div id="slideShow" class="slideshow ">
            <div id="slideShowWindow">

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_10708540.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_1308456.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_15254098.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_1886302.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_2008768.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_2008770.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_2275868.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_2854978.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_77129413.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_77347582.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_80296861.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_80313958.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_8922649.jpg" width="650" height="450" />
                        </div>

                        <div class="slide">
                            <img src="Images/portfolioImages/shutterstock_9754504.jpg" width="650" height="450" />
                        </div>


            </div>
        </div>

            </div>
            <div id="imgList" class="col-xs-12">



                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_10708540.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_1308456.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_15254098.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_1886302.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_2008768.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_2008770.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_2275868.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_2854978.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_77129413.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_77347582.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_80296861.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_80313958.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_8922649.jpg'/>
                </div></div>

                <div class="col-sm-12">
                    <img src='Images/portfolioImages/shutterstock_9754504.jpg'/>
                </div></div>


            </div>

Another Edit: 另一个编辑:

Incase it is something to do with my code behind or javascript they are as follows. 如果这与我的代码或javascript有关,则如下所示。

JavaScript: JavaScript:

$(document).ready(function () {

    var currentPosition = 0;
    var slideWidth = 650;
    var slides = $('.slide');
    var numberOfSlides = slides.length;
    var slideShowInterval;
    var speed = 2000;



    slideShowInterval = setInterval(changePosition, speed);

    slides.wrapAll('<div id="slidesHolder"></div>');

    slides.css({ 'float': 'left' });

    $('#slidesHolder').css('width', slideWidth * numberOfSlides);


    function changePosition() {
        if (currentPosition == numberOfSlides - 1) {
            currentPosition = 0;
        } else {
            currentPosition++;
        }
        moveSlide();
    }


    function moveSlide() {
        $('#slidesHolder').animate({ 'marginLeft': slideWidth * (-currentPosition) });
    }

});

My code behind (c#) 我的代码背后(C#)

protected void Page_Load(object sender, EventArgs e)
{
    string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/portfolioImages/"));
    List<ListItem> files = new List<ListItem>();
    foreach (string filePath in filePaths)
    {
        string fileName = Path.GetFileName(filePath);
        files.Add(new ListItem(fileName, "Images/portfolioImages/" + fileName));
    }

    rptSlides.DataSource = files;
    rptSlides.DataBind();
    rptShowPics.DataSource = files;
    rptShowPics.DataBind();
}

Try having your display: block; 尝试display: block; outside of a media query and then overriding it with your min-width query, like this: 在媒体查询之外,然后用min-width查询覆盖它,如下所示:

#imgList {
  display: block;
}

@media screen and (min-width: 800px) {

  #imgList {
    display: none;
  }
}

This will hide it above a width of 800px . 这会将其隐藏在800px的宽度以上。

I copied your code into a simple html doc and ran it, it worked perfectly in Chrome. 我将您的代码复制到一个简单的html文档中并运行,它在Chrome浏览器中运行完美。

I inspected the element, and indeed, when the screen is big, it is hidden, but when small, it shows up. 我检查了元素,实际上,当屏幕很大时,它是隐藏的,但是当屏幕很小时,它就会显示。 this means that somewhere else in your code there is a style that is overriding your media query. 这意味着在代码的其他地方,有一种样式会覆盖您的媒体查询。 most likely, somewhere in your code (possibly your bootstraps) it is being displayed. 最有可能在代码的某个地方(可能是引导程序)显示它。

to find out where this is doing it, go into chrome or firefox (i use chrome so it might be a tad different in firefox) and right click on the elements that should be hidden and choose inspect element. 要找出执行此操作的位置,请进入chrome或firefox(我使用chrome,因此在firefox中可能有点不同),然后右键单击应隐藏的元素,然后选择inspect元素。 move up until you are located on the div with id="imgList" and you should be able to see every single CSS style affecting it. 向上移动,直到您通过id =“ imgList”位于div上,您应该能够看到影响它的所有CSS样式。 crossed out ones are overridden by others, usually ones higher on the list. 划掉的部分会被其他部分覆盖,通常是列表中较高的部分。 i bet you will find a display:block that is not crossed out. 我敢打赌,您会发现display:block没有被划掉。

@media screen and (min-width: 800px) { This code will run when the screen is at least 800px wide. @media screen and (min-width: 800px) {当屏幕宽度至少为800像素时,将运行此代码。

@media screen and (max-width:800px) { This code will run when the screen is smaller than 800px wide. @media screen and (max-width:800px) {当屏幕小于800px宽时,将运行此代码。

What screen width are you trying to show hide it at? 您要显示隐藏在什么屏幕宽度上?

I would also you not mix min and max queries in your CSS unless necessary. 除非必要,否则我也不会在CSS中混合使用minmax查询。 I personally like to start small and only use min queries. 我个人喜欢从小处开始,仅使用min查询。 So my default styles run on mobile, then I do a min for phablets/tablets and another min for small desktops and a final min for wide screens. 所以,我的默认样式上移动运行,然后我做了min的平板手机/平板电脑和其他min的小型台式机和最终min的宽屏幕。 That way as I go down my CSS document I know the styles at the bottom override the styles at the top as the screens get larger. 这样,当我浏览CSS文档时,我知道随着屏幕变大,底部的样式将覆盖顶部的样式。 I never have to use max queries at all. 我根本不需要使用max查询。 Using both can get very confusing so to what is using what and when, in my opinion. 在我看来,同时使用这两种方法会造成非常混乱。

Have you tried using display:none !important; 您是否尝试过使用display:none !important; ? I've found with my CSS media queries that I had to use !important when hiding or displaying content at different widths. 我发现我的CSS媒体查询在隐藏或显示不同宽度的内容时必须使用!important

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

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