Flexbox 不适用于图像的背景。

huangapple go评论71阅读模式
英文:

Flexbox not working for image's background

问题

这是您要翻译的内容:

"这是我在这里的第一篇帖子。我需要在CSS中水平和垂直居中我的背景图像,但无论我如何尝试,图像都顽固地保持在原位。我已经阅读了大约30篇与此问题相关的帖子,但似乎没有什么对我有用。感谢您的任何建议。

.angryjalapeno {
    width: 500px;
    height: 300px;
    background-image: url(https://placehold.co/200x200);
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
}

HTML版本如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Background as color and image</title>
    <link rel="stylesheet" href="css/style.css">

    <div class="angryjalapeno">


    </div>
</head>
<body>
</body>
</html>

希望这对您有帮助。"

英文:

It's my first post here. I need to center my background image horizontally and vertically in CSS,however whatever way I try the image stubbornly remains on its place. I've read like 30 posts on the issue, but nothing seems to work for me. Thank you for any suggestions

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.angryjalapeno {
    width: 500px;
    height: 300px;
    background-image: url(https://placehold.co/200x200);
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
}

<!-- language: lang-html -->

&lt;div class=&quot;angryjalapeno&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

HTML version here

&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
    &lt;title&gt;Background as color and image&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot;&gt;

        &lt;div class=&quot;angryjalapeno&quot;&gt;


        &lt;/div&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

删除background-size: cover会将图像占位符放在div的中心。

.angryjalapeno {
    width: 500px;
    height: 300px;
    background-image: url(https://placehold.co/200x100);
    background-repeat: no-repeat;
    background-position: center;
    /* background-size: cover; */
    display: flex;
    justify-content: center;
    align-items: center;
}
<div class="angryjalapeno">
</div>
英文:

Removing background-size: cover puts the image placeholder in the center of the div.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.angryjalapeno {
    width: 500px;
    height: 300px;
    background-image: url(https://placehold.co/200x100);
    background-repeat: no-repeat;
    background-position: center;
    /* background-size: cover; */
    display: flex;
    justify-content: center;
    align-items: center;
}

<!-- language: lang-html -->

&lt;div class=&quot;angryjalapeno&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

如果您的图片大小超过300x500,则您的代码为真。但如果小于此尺寸,则需要移除 background-size: cover;

英文:

If your picture is bigger than 300x500, then your code true. But if it's smaller than this size you need to remove background-size: cover;

答案3

得分: -2

我建议您将您的 background-position 属性更改为类似以下内容的方式。

background-position: center center;
英文:

I recommend that you change your background-position property to something like this.

background-position: center center;

huangapple
  • 本文由 发表于 2023年5月26日 16:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定