首页 > 解决方案 > 尝试使用@media 查询来调整手机、平板电脑桌面的图像大小

问题描述

我很难让我的网站上的背景图像响应。我已将媒体查询添加到我的页面,但它们不起作用,我不确定还能做什么。下面,我列出了三个图像的 html 和 css。有什么建议请指教,谢谢!

body {
    background: url(/img/sect-bkgrd.jpg) center/cover fixed no-repeat;
    font-family: 'Roboto', sans-serif;
    color: #000;
}

#header {
    min-height: calc(100vh - 94px);
    background: linear-gradient(to right, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.3)), url(/img/sushi-main.jpg);
    background-position: center center;
    background-size: cover;
    background-attachment: fixed;
    background-repeat: no-repeat;

    background-color: #000;
}

#menu {
    background: linear-gradient(rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.2)), url(/img/menu-bkgrd.jpg) center/cover fixed no-repeat;
}
<header id="header">
    <div class="container">
        <div class="row height-90 align-items-center justify-content-center">
            <div class="col">
                <div class="banner text-center">
                    <h1 class="display-1 text-capitalize underline w-50 mx-auto">
                        <strong class="primary-color">Title</strong>
                        <img src="/img/sushi-logo2.png" class="img-fluid" alt="" />
                    </h1>
                </div>
            </div>
        </div>
    </div>

    <section id="menu" class="py-5 my-5">
        <div class="container">
            <div class="row">
                <div class="col-md-6">
                    <!--        title-->
                    <div class="row">
                        <div class="col">
                            <h1 class="primary-color text-uppercase">sushi</h1>
                        </div>
                    </div>
                    <!--         title-->
                    <!--          single item-->
                    <div class="single-item d-flex justify-content-between my-3 p-3">
                        <div class="single-item-text">
                            <h2 class="text-uppercase text-black">sushi</h2>
                            <h4 class="text-white">Lorem ipsum dolor sit amet.</h4>
                        </div>
                        <div class="single-item-price align-self-end">
                            <h1 class="text-uppercase text-black">$10</h1>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</header>

标签: htmlcssbootstrap-4media-queries

解决方案


您可以使用 max-width 或 min-width 属性为多个设备调整背景图像的大小。例如,对于小型设备,媒体查询将是

@media screen & (max-width:768px){
#img{
max-width: 100%;
}

}

推荐阅读