Max-Height Firefox / Opera / IE

时间:2013-07-11 13:26:56

标签: css

我正在尝试为图像设置最大高度。它适用于Safari和Chrome,但不适用于Firefox / Opera / IE。现在我读到html和身高应该是100%,当我使用jsfiddle时它确实有效。但是,它在我的页面中不起作用(memo-designs.com/portfolio.php)。

以下是该页面的来源:

<!DOCTYPE html>
    <html>
    <head>
        <title>memodesigns</title>
        <link rel='stylesheet' href='style/stylesheet.css'>
        <script type = 'text/javascript'>
            function displayImage(image, link) {
                document.getElementById('img').src = image;
                document.getElementById('mylink').href = link;
            }

            function displayNextImage() {
                if (x < images.length-1){
                    x++;
                } else {
                    x = 0;
                }
                displayImage(images[x], links[x]);           
            }

            function displayPreviousImage() {
                if (x > 0){
                    x--;
                } else {
                    x = images.length-1;
                }
                displayImage(images[x]);           
            }

            function startTimer() {
                setInterval(displayNextImage, -1);
            }

          var images = [], links = [], x = 0;images[0] = "http://memo-designs.com/items/doublek-01.png"
links[0] = "http://memo-designs.com/items/doublek-01.png"
images[1] = "http://memo-designs.com/items/memodesigns.png"
links[1] = "http://memo-designs.com/items/memodesigns.png"

      </script>
    </head>

    <body style = 'background-color: #000000'><div id = 'menucontainer'>
            <div id = 'menu'>
                <p>
                    <ul>
                        <li><a class = 'menu' href = '/'>HOME</a></li>
                        <li><a class = 'menu' href = 'about.php'>ABOUT</a></li>
                        <li><a class = 'menu' href = 'portfolio.php'>PORTFOLIO</a></li>
                        <li><a class = 'menu' href = 'rates.php'>RATES</a></li>
                        <li><a class = 'menu' href = 'contact.php'>CONTACT</a></li>
                    </ul>
                </p>
            </div>
        </div>
                <div id = 'contentcontainer' style = 'padding-top: 0%; max-height: 100%; overflow: hidden; background-color: #000000'>
                <p>
                    <img id= 'img' src = 'http://memo-designs.com/items/doublek-01.png' style = 'max-height: 100%; max-width: 100%; display: block; margin-left: auto; margin-right: auto;'>
                    <img class = 'arrow' onclick = 'displayPreviousImage()' id= 'img' src = 'style/graphics/larrow.png' style = 'position: absolute; left: 0; top: 40%;'>
                    <img class = 'arrow' onclick = 'displayNextImage()' id= 'img' src = 'style/graphics/rarrow.png' style = 'position: absolute; right: 0; top: 40%;'>          </p>
            </div>
    </body>

    </html>

css样式表(此处仅显示部分内容):

*{
    margin:  0;
    padding:  0;
}

html{
    margin: 0;
    min-width: 100%;
    height: 100%;
    min-height: 100%;
}

body{
    margin: 0px;
    background-color: #f3f4f4;
    min-width: 100%;
    height: 100%;
    min-height: 100%;
}

对于我做错的任何帮助表示感谢:)

1 个答案:

答案 0 :(得分:1)

首先,我建议您开始使用CSS-Reset之类的Normalize.css。它使浏览器更加一致地呈现所有元素并符合现代标准。

您的HTML表示法也可能导致浏览器不一致。将<div id = 'menu'>之类的内容转换为<div id="menu">。这使得它更具可读性恕我直言。

内联样式属性使页面难以维护,并且可能会覆盖您不想要的内容。它们还需要应用于每个元素,因此也增加了下载时间。使用classes / id是可行的方法。伪元素也不能与内联样式一起使用。我建议只在开发过程中使用它们进行快速更改。我使用Chrome / Firefox中的元素检查器快速更改内容,并立即查看更改的外观,然后复制/粘贴编辑内容。

因此,请务必将所有css放入样式表中。它也被视为maintainability的最佳做法,以及更好的网页下载速度(minify生产文件)。

你以前肯定听说过jQuery。尝试使用它。 jQuery使像图像滑块这样的东西变得轻而易举(一旦理解了语法,但它的学习曲线很低)。此外,还有很多ready-to-use plugins for jQuery

另一个“好习惯”是将您的javascripts放在文档的最末端</body>标记之前。详细了解herehere

好的,有足够的提示。让我们弄清楚:

HTML部分:

<!DOCTYPE html>
<html>
<head>
    <title>memodesigns</title>
    <link rel="stylesheet" href="/assets/css/normalize.css">
    <link rel="stylesheet" href="/assets/css/style.css">
</head>
<body>

    <div id="menuContainer">
        <div id="menu">
            <p>
                <ul>
                    <!-- Instead of writing in CAPITALS use the text-transform:uppercase; css property -->
                    <li><a class="menu" href="/">Home</a></li>
                    <li><a class="menu" href="about.php">About</a></li>
                    <li><a class="menu" href="portfolio.php">Portfolio</a></li>
                    <li><a class="menu" href="rates.php">Rates</a></li>
                    <li><a class="menu" href="contact.php">Contact</a></li>
                </ul>
            </p>
        </div>
    </div>

    <div id="contentContainer">
        <p>
            <!-- NOTE: Use IDs only once, else use classes to share css styles -->
            <img id="img" src="http://memo-designs.com/items/doublek-01.png">
            <img class="arrow left" src="style/graphics/larrow.png" onclick="displayPreviousImage()">
            <img class="arrow right" src="style/graphics/rarrow.png" onclick="displayNextImage()">
        </p>
    </div>


    <!-- Put the JavaScript at the end of the document just before the closing body tag -->
    <script>
        var images = [], links = [], x = 0,
            baseUrl   = "http://memo-designs.com/items/";
            images[0] = baseUrl + "doublek-01.png";
            links[0]  = baseUrl + "doublek-01.png";
            images[1] = baseUrl + "memodesigns.png";
            links[1]  = baseUrl + "memodesigns.png";

        function displayImage(img, link)
        {
            document.getElementById('img').src = img;
            document.getElementById('mylink').href = link;
        }

        function displayNextImage()
        {
            if (x < images.length-1) x++; 
                else x = 0;

            displayImage(images[x], links[x]);           
        }

        function displayPreviousImage()
        {
            if (x > 0) x--; 
                else x = images.length-1;

            displayImage(images[x]);           
        }

        function startTimer()
        {
            setInterval(displayNextImage, -1);
        }
    </script>

</body>
</html>

...和CSS:

/* Assuming you'll use a CSS-Reset */
body {
    background-color: #f3f4f4;
    font:
    ...

    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}

#menuContainer { ... }
#menu { ... }
#menu ul { ... }

/* Making the menu labels all UPPERCASE */
#menu ul > li {
    text-transform: uppercase;
}

#contentContainer {
    background-color: #000;
    padding-top: 0;
    overflow: hidden;

    /* IMPORTANT: Set a fixed pixel height here to make the images use up the given space */
    height: 200px; /* change 200 to your needs */
}
#img {
    display: block;
    width: 100%;
    height: 100%;
    margin-left: auto;
    margin-right: auto;

}
#contentContainer .arrow {
    position: absolute;
    top: 40%;
}
#contentContainer .arrow.left {
    left: 0;
}
#contentContainer .arrow.right {
    right: 0;
}

好的,试试建议和代码示例。告诉我们是否以及有什么帮助。

祝你好运,编码愉快!