【问题标题】:How to properly center the background image within the current bootstrap template如何在当前引导模板中正确居中背景图像
【发布时间】:2021-02-20 21:14:06
【问题描述】:

我目前正在尝试让整个背景图像保留在用户的窗口内,无论他们使用哪种设备。我已经能够添加背景图像,但未能成功尝试实现图像的居中。当前结果为。 (我还是 webdev 的新手,所以暂时不用担心导航栏在那里)

我已经尝试过使用一种解决方案,我之前已经看到其他一些项目使用过但无济于事:

html { 
  background: url(/images/login.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

由于该解决方案带走了整个背景图像,我摆脱了它,现在我所要做的就是将背景设置为主要内容 div 内的图片。

<style>
    .main-content {
        background-image: url(/images/login.jpg);
    }
</style>

无论设备如何,我如何正确地修复背景大小以适应屏幕?

以防万一有人想查看 Area/Identity/Pages/Account/Login.cshtml: https://github.com/zhadjah9559/HelpDeskTicket/tree/4.MyTicketsView

【问题讨论】:

  • 您尝试过background-position: center center 我知道背景图像可能需要花点时间才能弄好,直到您把它弄得恰到好处。此外,如果您将 HTML/CSS 发布到 Codepen,我将进一步深入研究它。我只是不想关闭你的仓库,因为它在 C# 中,我没有经验。

标签: html css asp.net-core bootstrap-4 frontend


【解决方案1】:

您可以使用下面的 css 样式使其适合浏览器窗口。

.main-content {
    background-image: url(/images/login.jpg);
    background-size: cover;
}

结果:

【讨论】:

  • 完美运行!
【解决方案2】:

在css里面,可以尝试将图片的宽高调整为100%。然后添加 margin: auto 将图像定位到页面的中心。

<style>
    .main-content {
        background-image: url(/images/login.jpg);
        width: 100%;
        height: 100%;
        margin: auto;
        display: block;
    }
</style>

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 2011-04-20
    相关资源
    最近更新 更多