简体   繁体   English

麻烦居中背景图片

[英]Trouble centering background image

I'm having problems centering the background image within a certain div. 我在某个div内将背景图像居中时遇到问题。 I'm using the following code for CSS: 我将以下代码用于CSS:

.background3 {
  max-width: 90%;
  height: 400px;
  margin: 0 auto;
  background: linear-gradient( rgba(86, 71, 71, 0.56), rgba(86, 71, 71, 0.56)), url(img/kitpage.jpg) no-repeat 50% 50%;
}

I've tinkered and tried, but to no avail. 我已经尝试过,但无济于事。 I'm pretty sure it's an easy answer, and thank you all in advance for your help and advice. 我很确定这是一个简单的答案,在此先感谢大家的帮助和建议。

A screenshot of the problem is attached below 问题的屏幕截图如下

在此处输入图片说明

在CSS中使用以下内容:

display: block;

You have to use background position property eg: 您必须使用背景位置属性,例如:

.background3 {
  max-width: 90%;
  height: 400px;
  margin: 0 auto;
  background-position:20% 20%; // Margin background from top and left
}

You can adjust margin as you need but it's not a good way. 您可以根据需要调整边距,但这不是一个好方法。 If you want more proper way then try this: 如果您想要更正确的方法,请尝试以下操作:

.background3 {
  max-width: 90%;
  height: 400px;
  margin: 0 auto;
  background-size:auto 100%;
  background-position:center center;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM