gpt4 book ai didi

固定的 CSS 位置不会出现在移动设备上

转载 作者:技术小花猫 更新时间:2023-10-29 10:55:26 24 4
gpt4 key购买 nike

我有一个类似这个 css 的元素:

element.style {
background-image: url(image.jpg);
}
.panel-image {
background-position: center center;
background-repeat: no-repeat;
-webkit-background-size: cover;
background-size: cover;
position: relative;
}

我现在正试图让它成为一个固定位置,所以当我尝试执行以下操作时:

.panel-image {
background-position: center center;
background-repeat: no-repeat;
-webkit-background-size: cover;
background-size: cover;
position: fixed;
width: 100%;
height: 200px;
}

该元素不再出现在页面上。

有一个元素环绕着这个元素:

.twentyseventeen-panel {
overflow: hidden;
position: relative;
}

这是 html:

<article id="panel1" class="twentyseventeen-panel  post-47 page type-page status-publish has-post-thumbnail hentry">


<div class="panel-image" style="background-image: url(https://2017.wordpress.net/wp-content/uploads/2016/10/pexels-photo-30724-2000x1200.jpg);">
<div class="panel-image-prop" style="padding-top: 60%"></div>
</div><!-- .panel-image -->


<div class="panel-content">
<div class="wrap">
<header class="entry-header">
<h2 class="entry-title">Our Drinks</h2>

</header><!-- .entry-header -->

<div class="entry-content">
<h2>Coffee and Espresso</h2>
<p><strong>Drip Coffee</strong>&nbsp;$2.50</p>
<p><strong>Espresso Shots</strong> $3.00</p>
<p><strong>Americano</strong>&nbsp;$4.50</p>
<p><strong>Cappuccino</strong>&nbsp;$4.50</p>
<p><strong>Macchiato</strong>&nbsp;$4.50</p>
<p><strong>Cortado</strong>&nbsp;$4.50</p>
<p><strong>Flat White</strong>&nbsp;$5.00</p>
<p><strong>Latte</strong>&nbsp;$4.50<br>
<em>hot or iced</em></p>
<p><strong>Mocha</strong>&nbsp;$4.50<br>
<em>hot or iced</em></p>
<p><strong>Cold Brew</strong> $4.00<br>
<em>regular or nitro</em></p>
<h2>Other Drinks</h2>
<p><strong>Chai</strong><strong>&nbsp; </strong>$4.50</p>
<p><strong>Iced Tea&nbsp;&nbsp;</strong>$3.50<br>
<em>green, black, or white</em></p>
<p><strong>Hot Tea</strong><strong>&nbsp;&nbsp;</strong>$3.00<br>
<em>english breakfast, ceylon, green, white, chamomile, rooibos, peach</em></p>
<p><strong>Flavored Sodas</strong><strong>&nbsp; </strong>$4.00<br>
<em>raspberry, blueberry, peach, mango, vanilla, mint</em></p>
</div><!-- .entry-content -->


</div><!-- .wrap -->
</div><!-- .panel-content -->

</article>

我的问题是,如何让带有背景图片的元素固定。

这是一个示例站点:

http://www.grogenex.com/

最佳答案

这是另一种方法,它不使用“background-attachment”或“position: fixed”。

.panel-image {
background-position: center center;
background-repeat: no-repeat;
-webkit-background-size: cover;
background-size: cover;
position: relative;
z-index: 0;
width: 100%;
height: 100%;
}
.twentyseventeen-panel {
overflow: hidden;
position: relative;
height: 400px;
}
.panel-content {
position: absolute;
top: 0 !important;
left: 0 !important;
overflow: auto !important;
height: 100%;
width: 100%;
z-index: 1;
}
.panel-content > .wrap {
position: relative;
background: rgba(255,255,255,0.8);
top: 200px;
}
<article id="panel1" class="twentyseventeen-panel  post-47 page type-page status-publish has-post-thumbnail hentry">


<div class="panel-image" style="background-image: url(https://2017.wordpress.net/wp-content/uploads/2016/10/pexels-photo-30724-2000x1200.jpg);">
<div class="panel-image-prop" style="padding-top: 60%"></div>
</div><!-- .panel-image -->


<div class="panel-content">
<div class="wrap">
<header class="entry-header">
<h2 class="entry-title">Our Drinks</h2>

</header><!-- .entry-header -->

<div class="entry-content">
<h2>Coffee and Espresso</h2>
<p><strong>Drip Coffee</strong>&nbsp;$2.50</p>
<p><strong>Espresso Shots</strong> $3.00</p>
<p><strong>Americano</strong>&nbsp;$4.50</p>
<p><strong>Cappuccino</strong>&nbsp;$4.50</p>
<p><strong>Macchiato</strong>&nbsp;$4.50</p>
<p><strong>Cortado</strong>&nbsp;$4.50</p>
<p><strong>Flat White</strong>&nbsp;$5.00</p>
<p><strong>Latte</strong>&nbsp;$4.50<br>
<em>hot or iced</em></p>
<p><strong>Mocha</strong>&nbsp;$4.50<br>
<em>hot or iced</em></p>
<p><strong>Cold Brew</strong> $4.00<br>
<em>regular or nitro</em></p>
<h2>Other Drinks</h2>
<p><strong>Chai</strong><strong>&nbsp; </strong>$4.50</p>
<p><strong>Iced Tea&nbsp;&nbsp;</strong>$3.50<br>
<em>green, black, or white</em></p>
<p><strong>Hot Tea</strong><strong>&nbsp;&nbsp;</strong>$3.00<br>
<em>english breakfast, ceylon, green, white, chamomile, rooibos, peach</em></p>
<p><strong>Flavored Sodas</strong><strong>&nbsp; </strong>$4.00<br>
<em>raspberry, blueberry, peach, mango, vanilla, mint</em></p>
</div><!-- .entry-content -->


</div><!-- .wrap -->
</div><!-- .panel-content -->

</article>

关于固定的 CSS 位置不会出现在移动设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45910650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com