【问题标题】:Background image size [duplicate]背景图像大小[重复]
【发布时间】:2014-09-27 05:31:35
【问题描述】:

我正在制作一个带有背景图片的网页,该网站在全页模式下看起来很好。但是当我开始重新调整浏览器的大小时,导航保持不变,但是背景图像变得太小并且有不需要的空白。 我该怎么做才能使背景图像在调整大小时符合浏览器的大小? 谢谢大家的帮助。 :)

<html>
<link rel="stylesheet" type="text/css" href="C:\Users\tom\Desktop\CSS\Mark & Howie Wedding/itinary.css">
<meta name="viewport" content="width=device-width">
    <head>
        <body>
            <div class="wrapper">
                <ul class="nav">
                    <li class="home"><a href="#">Home</a></li>
                    <li class="location"><a href="#">Location</a></li>
                    <li class="itinerary"><a href="#">Itinerary</a></li>
                    <li class="photos"><a href="#">Howie & Mark</a></li>
                </ul>
            </div>
        <div class="card">
            <div class="sign">
                <ul class="mark">
                    <li>Howard & Mark's wedding beings</li><br>
                    <li>We start dancing</li><br>
                    <li>Everybody has fun</li>
                </ul>
            </div>
        </div>
        </body>
    </head>
</html>

body {
background-image: url("http://i.istockimg.com/file_thumbview_approve/20453608/2/stock-photo-20453608-wedding-sign.jpg");
background-repeat: no-repeat;);
background-repeat: no-repeat;
background-size: cover;
}
.wrapper {
    top: 10px;
    height: 175px;
    width: 1100px;  
    background-color: black;
    opacity: 0.8;
    margin: auto;
    border-radius: 10px;
    z-index: 0;

}
.card {
    display: inline-block;
    width: 700px;
    height: 350px;
    margin: 40px;
    background-color: white;
    border-radius: 20px;
    text-align: center;
}
.sign {
    margin: 35px;
    width: 617px;
    height: 267px;
    border: solid black;
    border-radius: 5px;
    font-style: oblique;
    font-weight: 600;
    text-align: center;
}
.mark {
    font-size: 35px;
    list-style-type: none;
}
ul.nav {
    display: inline;
    color: white;
    text-decoration: none;
    text-align: center;
    padding: 8px 15px;
    border-radius: 5px;
    text-align: auto;
}
ul.nav li {
    float: left;
    margin-left: 50px;
}

ul.nav li.home {
    padding: 35px 15px;
    margin: 20px;
    top: 15px;
    font-weight: 100;
    height: 55px;
    width: 130px;
    font-size: 40px;
}
ul.nav li.home:hover {
        background-color: #138f00;
        border-radius: 8px;
}
ul.nav li.location {
    padding: 35px 15px;
    margin: 20px;
    font-weight: 100;
    top: 15px;
    height: 55px;
    width: 180px;
    border-radius: 5px;
    font-size: 40px;

}
ul.nav li.location:hover {
    background-color: #138f00;
    border-radius: 8px;
}
ul.nav li.itinerary {
    padding: 35px 15px;
    margin: 20px;
    font-weight: 100;
    height: 55px;
    width: 200px;
    border-radius: 5px;
    font-size: 40px;
}
ul.nav li.itinerary:hover {
    background-color: #138f00;
    border-radius: 8px;
}
ul.nav li.photos {
    padding: 35px 15px;
    margin: 20px;
    margin-left: 40px;
    font-weight: 120;
    height: 55px;
    width: 250px;
    border-radius: 5px;
    font-size: 40px;
}
ul.nav li.photos:hover {
    background-color: #138f00;
    border-radius: 8px;
}
ul.nav li a {
    color: #faf3bc;
    display: block;
    text-decoration: none;
}

【问题讨论】:

  • 我认为可能是导航大小的问题,当我使用 % 而不是 px 时,照片部分会跳出包装 div。 :s

标签: html css browser


【解决方案1】:

试试这个代码:

请记住,您的&lt;HEAD&gt; 标记位置错误,它必须在&lt;BODY&gt; 标记之前。

如果您更喜欢在不同的文件中使用 CSS,请使用 &lt;LINK&gt; 标签。

<html>
    <head>
<link rel="stylesheet" type="text/css" href="C:\Users\tom\Desktop\CSS\Mark & Howie Wedding/itinary.css">
<meta name="viewport" content="width=device-width">

    <style>
        img.bg {
        /* Set rules to fill background */
        min-height: 100%;
        min-width: 253px;

        /* Set up proportionate scaling */
        width: 100%;
        height: auto;

        /* Set up positioning */
        position: fixed;
        top: 0;
        left: 0;
        z-index: -1; // set it behind other stuffs
    }

.wrapper {
    top: 10px;
    height: 175px;
    width: 1100px;  
    background-color: black;
    opacity: 0.8;
    margin: auto;
    border-radius: 10px;
    z-index: 0;

}
.card {
    display: inline-block;
    width: 700px;
    height: 350px;
    margin: 40px;
    background-color: white;
    border-radius: 20px;
    text-align: center;
}
.sign {
    margin: 35px;
    width: 617px;
    height: 267px;
    border: solid black;
    border-radius: 5px;
    font-style: oblique;
    font-weight: 600;
    text-align: center;
}
.mark {
    font-size: 35px;
    list-style-type: none;
}
ul.nav {
    display: inline;
    color: white;
    text-decoration: none;
    text-align: center;
    padding: 8px 15px;
    border-radius: 5px;
    text-align: auto;
}
ul.nav li {
    float: left;
    margin-left: 50px;
}

ul.nav li.home {
    padding: 35px 15px;
    margin: 20px;
    top: 15px;
    font-weight: 100;
    height: 55px;
    width: 130px;
    font-size: 40px;
}
ul.nav li.home:hover {
        background-color: #138f00;
        border-radius: 8px;
}
ul.nav li.location {
    padding: 35px 15px;
    margin: 20px;
    font-weight: 100;
    top: 15px;
    height: 55px;
    width: 180px;
    border-radius: 5px;
    font-size: 40px;

}
ul.nav li.location:hover {
    background-color: #138f00;
    border-radius: 8px;
}
ul.nav li.itinerary {
    padding: 35px 15px;
    margin: 20px;
    font-weight: 100;
    height: 55px;
    width: 200px;
    border-radius: 5px;
    font-size: 40px;
}
ul.nav li.itinerary:hover {
    background-color: #138f00;
    border-radius: 8px;
}
ul.nav li.photos {
    padding: 35px 15px;
    margin: 20px;
    margin-left: 40px;
    font-weight: 120;
    height: 55px;
    width: 250px;
    border-radius: 5px;
    font-size: 40px;
}
ul.nav li.photos:hover {
    background-color: #138f00;
    border-radius: 8px;
}
ul.nav li a {
    color: #faf3bc;
    display: block;
    text-decoration: none;
}
    </style>
        </head>
        <body>
        <img src="http://i.istockimg.com/file_thumbview_approve/20453608/2/stock-photo-20453608-wedding-sign.jpg" class="bg" />
            <div class="wrapper">
                <ul class="nav">
                    <li class="home"><a href="#">Home</a></li>
                    <li class="location"><a href="#">Location</a></li>
                    <li class="itinerary"><a href="#">Itinerary</a></li>
                    <li class="photos"><a href="#">Howie & Mark</a></li>
                </ul>
            </div>
        <div class="card">
            <div class="sign">
                <ul class="mark">
                    <li>Howard & Mark's wedding beings</li><br>
                    <li>We start dancing</li><br>
                    <li>Everybody has fun</li>
                </ul>
            </div>
        </div>
        </body>

</html>

【讨论】:

  • 您的代码确实给了我一个想法。当我更改浏览器视口的大小时,您如何能够重新调整导航大小?
  • 老实说,我会为你推荐 bootstrap。它易于使用且简单。它也是完全响应的。只需尝试下载一个名为 Pingendo 的软件,它使用 Bootstrap3 和所见即所得,因此只需拖放元素即可。但是考虑到您已经完成了这个设计,并且为了帮助您不要从 0 开始重新设计。请查看这个简单易用的 developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag 链接如果您对此链接方向有任何疑问,请告诉我。
  • 另外,在完成此操作后,您可以在此工具上的多个设备上测试您的站点:browserstack.com 但请记住,您必须将页面上传到任何主机。如果您只使用 HTML,您可以在上传之前在此处测试您的代码:jmarshall.com/easy/html/testbed.html.
  • 我从未使用过引导程序。所以我很确定我会把它搞砸。 :( 媒体查询对它有帮助吗?
  • 我不确定。您可能会为自己做出这个决定。我创建了这个小提琴 (jsfiddle.net/wQ7w7) [这个小提琴展示了什么是媒体查询以及它是如何工作的] 来帮助你做出决定。在我看来,这不是你的麻烦的答案。但它可能有助于解决未来的麻烦,谁知道呢。现在我认为 mozilla 页面 (Viewport_meta_tag) 就是你要找的!
【解决方案2】:

不知道 Css 有时会如此不可预测,但这又如何呢?也许就像将图像放在 div 中并在图像上放置唯一类并将该类设置为:

#img.source-image {
width: 100%;
position: absolute;
top: 0;
left: 0;
}

OR 
.uniqueClass{
width: 100%;
position: absolute;
top: 0;
left: 0;

}

参考 - http://css-tricks.com/how-to-resizeable-background-image/

【讨论】:

    【解决方案3】:

    添加:

    html, body{
      height:100%;
    }
    

    您还应该删除重复的 css 元素:background-repeat: no-repeat;);

    这是一个工作示例:http://jsfiddle.net/LKrhJ/1/

    【讨论】:

    • 嗯,有趣的想法。但我只是想让背景图像重新调整大小。
    • 如果我理解正确,并且您不介意扭曲图像,请使用:background-size: 100% 100%;example
    • 我一直遇到图像重复的问题。但我下次必须记住这个想法。我试图弄清楚如何重新调整包装器 div 的大小,因为这可能会更好一些。任何关于如何做到这一点的线索,因为我很难过。 :(
    • 如果你真的想重新调整大小,你应该开始使用百分比。听起来您还想让它对移动(和平板电脑)友好。如果是这样,您确实必须先使用移动设备、平板电脑,然后是台式机来对网站进行编程。 Here 是一篇让您入门的好文章。
    猜你喜欢
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多