html - CSS背景水平居中

标签 html css center

我有一个图像作为背景,具有以下 CSS 设置

#background {
  background-image: url('../img/bgimage.jpg');
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  opacity: 0.8;
  filter:alpha(opacity=80);
  top:0;
}

我的目标是使背景居中(添加一点不透明度),但它的开头仍位于页面顶部。现在当用户的显示器太大时,它看起来像这样

enter image description here

可以看到图像不是从顶部开始的。 (灰色应该是稍后的导航栏,但在这个问题中并不有趣)。蓝色是背景的一部分,白色显然只是一个通配符,直到背景开始保持中心可用。如何将图像水平居中以使其仍从页面顶部开始,或者如何调整图像大小以适合用户显示器大小?

最佳答案

您可以像解释的那样使用background-size: cover; here .

关于html - CSS背景水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30863060/

相关文章:

html - 中心 Christmax 日历

html - 如何正确对齐社交页脚和页脚菜单?

javascript - 不安全的 <form> 调用。在 index.html 页面上找到

javascript - 如何使用复选框来制作过滤器?

html - Bootstrap 4 的图库缩略图大小

html - background-image: url() 当所有文件都在桌面上但不包含其文件夹中的文件时有效

javascript - 多行 jquery 工具提示

css - 如何隐藏 div onload 并需要在单击按钮时显示?

css - GWT - CSS 中常量的问题

android - 在 ViewPager android 的中心对齐 subview