【发布时间】:2017-12-13 01:01:53
【问题描述】:
我知道 CSS 中的 background-size:cover 属性使背景图像尽可能大,以覆盖整个屏幕。但是,因为这会在大屏幕上扭曲图像,所以我希望能够使背景图像覆盖它所在的 div 容器的宽度和高度,而不是整个屏幕的宽度。我不知道该怎么做。即使将我的图像放在 HTML 中的 div 中,background-size:cover 属性仍会将图像扩展到整个屏幕。
我的 CSS 是:
.main {
height: 30vh;
text-align: center;
background: url("https://i.imgur.com/6FYizew.jpg");
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover
}
我的 HTML 是:
<div class = "container" style = "padding-top: 5vh">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class = "active hidden-xs"></li>
</ol>
<div class="carousel-inner">
<div class="item main active">
<div class = "hidden-xs" style = "width: 80%; max-width: 1200px; margin: auto; position: relative; top: 15%; padding-top: 30px; padding-bottom: 40px; background-color: rgba(255, 255, 255, 0.8);">
<h1 class = "hidden-xs" style = "font-size: 5em; font-weight: normal; color: #0b3c5d; font-weight: bold">TITLE</h1>
<hr class = "intro-divider hidden-xs" style = "width: 550px; background-color: #232f3f">
<h4 class = "hidden-xs" style = "margin-top: 15px; font-size: 1.5em; color: #232f3f"><em>SUBTITLE</em></h4>
</div>
</div>
请告诉我如何将封面设置为仅覆盖一个容器而不是整个屏幕。谢谢!
【问题讨论】:
-
你可以在这里摆弄吗
-
在此处更新您的 html 代码。实际上,如果我们将该背景图像放在 css 类中,然后如果我们从部门调用它,它将仅显示在该部门中。我希望我能帮助你
-
@SumithChalil 我在上面添加了我的 HTML。你打电话给部门到底是什么意思?