css - 背景图像位置,中心中心不起作用

标签 css background-image

我正在尝试创建一个具有居中背景图像并使用位置中心中心的页面。但是图像并没有垂直和水平居中,而是垂直延伸到页面顶部之外。我哪里/为什么会出错?

<!DOCTYPE html>
<html>
<style>
body
{
background-image:url('tumbleweed.jpg');
background-repeat:no-repeat;
background-position:center center;
background-color:#EAEAEA;
}
</style>
</html>

最佳答案

你会看到下图这样的画面吗?

enter image description here

body{
  background-image:url(../IMG/BG/pizzaBackground.jpg);
  background-size: cover;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

我认为这是因为您没有声明“高度”。

添加'height:100vh'样式。

body{
  height: 100vh;    /* ADD STYLE ! */
  background-image:url(../IMG/BG/pizzaBackground.jpg);
  background-size: cover;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

设置为100vh的原因是因为目标是body标签。

如果是不同的标签,则使用不同的值。

那就是

enter image description here

并添加 'margin:0px' 样式来移除滚动条。

body{
  margin: 0px;     /* ADD STYLE ! */
  height: 100vh;
  background-image:url(../IMG/BG/pizzaBackground.jpg);
  background-size: cover;
  background-position: 50% 50%;
  background-repeat: no-repeat;
}

然后就可以看到了

enter image description here


谢谢!

关于css - 背景图像位置,中心中心不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26760647/

相关文章:

javascript - Highcharts - 使用选定的饼图切片获得 3d 效果

javascript - 在 codeigniter 中,我通过 form_open 提交了一个表单,所有按钮都由我的 form_open ('Home/page' 链接)

javascript - OnClick JS If 语句

html - 更改背景图像不透明度

php - 如果表单中的值为空,如何显示警告消息

javascript - 如何关闭响应式菜单,单击导航外部

html - 如何在 Angular 中的整个页面上添加透明背景图像?

css - 应该使用哪种技术来更改 tumblr 主题背景?

html - 如何在背景图像上创建挖空文本,以便文本可以在图像上动画

html - 背景图片,但带有不透明 CSS