英文:
How to center hero image on mobile phone
问题
我正在构建一个简单的“即将推出”页面,试图弄清楚如何将我的主图像定位在中心。目前在桌面上看起来很好,但在移动设备上,文本很好地居中显示,但主图像不是。您可以在这里自行查看:https://quintillion.ai/
我希望图像始终位于中心位置,如果需要,它将在图像上方和下方显示黑色背景。
以下是我的HTML和CSS代码:
body {
background-image: url("https://picsum.photos/1000/1000");
background-repeat: no-repeat;
background-color: black;
background-position: center;
background-position-y: 0px;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
min-height: 100%;
margin: 0;
padding: 0;
}
.overlay {
box-shadow: inset 0 0px 0 1000px rgba(0, 0, 0, 0.6);
padding: 0;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center; /* Added */
align-items: center; /* Added */
}
.text {
font-family: Arial, sans-serif;
background-color: rgba(0, 0, 0, 0.0);
color: #ffffff;
align-items: center;
justify-content: center;
text-align: center;
margin-left: 60px;
}
h1 {
font-size: 48px;
margin-bottom: 20px;
}
h2 {
font-size: 36px;
margin-bottom: 20px;
}
p {
font-size: 18px;
margin-bottom: 20px;
}
<div class="overlay">
<div class="text">
<h1>Quintillion AI</h1>
<h2>Coming soon</h2>
<p>
We are currently in stealth mode, building AI Assistants for the future of work.
<br>Please check back soon for updates.
</p>
</div>
</div>
这是您的HTML和CSS代码。
英文:
I'm building a simple "Coming soon" page and trying to figure out how to position my hero image in the center. Currently it looks fine on desktop but on mobile the text nicely centered but the hero image is not. You can check it out for yourself here: https://quintillion.ai/
I would like to have it such that the image is always positioned in the center and if need be it would just show a black background above and below the image.
Here is my html and css code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
background-image: url("https://picsum.photos/1000/1000");
background-repeat: no-repeat;
background-color: black;
background-position: center;
background-position-y: 0px;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
min-height:100%;
margin: 0;
padding: 0;
}
.overlay {
box-shadow: inset 0 0px 0 1000px rgba(0, 0, 0, 0.6);
padding: 0;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center; /* Added */
align-items: center; /* Added */
}
.text {
font-family: Arial, sans-serif;
background-color: rgba(0, 0, 0, 0.0);
color: #ffffff;
align-items: center;
justify-content: center;
text-align: center;
margin-left: 60px;
}
h1 {
font-size: 48px;
margin-bottom: 20px;
}
h2 {
font-size: 36px;
margin-bottom: 20px;
}
p {
font-size: 18px;
margin-bottom: 20px;
}
<!-- language: lang-html -->
<div class="overlay">
<div class="text">
<h1>Quintillion AI</h1>
<h2>Coming soon</h2>
<p>
We are currently in stealth mode, building AI Assistants for the future of work.
<br>Please check back soon for updates.
</p>
</div>
</div>
<!-- end snippet -->
答案1
得分: 1
使用 background-size: cover;
可以让背景图覆盖整个空间。以下是您的代码的简写:
body {
background: #000 url("https://picsum.photos/1000/1000") no-repeat center / cover;
/* 移除其他 background-* 属性 ... */
/* 其他样式 ... */
}
英文:
To make a background-image cover the entire space use background-size: cover;
. Here's a shorthand for your code:
body {
background: #000 url("https://picsum.photos/1000/1000") no-repeat center / cover;
/* remove the other background-* properties ... */
/* other styles ... */
}
答案2
得分: 0
解决方案
你没有在 .text
类中添加 display: flex
和 flex-direction: column
。
.text {
display: flex; /* 在这里 */
flex-direction: column; /* 在这里 */
justify-content: center;
align-items: center;
}
并且在 <p>
标签中添加 CSS。
p {
text-align: center; /* 在这里 */
}
示例
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-css -->
body {
background-image: url("https://picsum.photos/1000/1000");
background-repeat: no-repeat;
background-color: black;
background-position: center;
background-position-y: 0px;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
min-height:100%;
margin: 0;
padding: 0;
}
.overlay {
box-shadow: inset 0 0px 0 1000px rgba(0, 0, 0, 0.6);
padding: 0;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center; /* 添加 */
align-items: center; /* 添加 */
}
.text {
font-family: Arial, sans-serif;
background-color: rgba(0, 0, 0, 0.0);
color: #ffffff;
display: flex; /* 在这里 */
flex-direction: column; /* 在这里 */
align-items: center;
justify-content: center;
text-align: center;
margin-left: 60px;
}
h1 {
font-size: 48px;
margin-bottom: 20px;
}
h2 {
font-size: 36px;
margin-bottom: 20px;
}
p {
text-align: center; /* 在这里 */
font-size: 18px;
margin-bottom: 20px;
}
<!-- 语言: lang-html -->
<div class="overlay">
<div class="text">
<h1>Quintillion AI</h1>
<h2>Coming soon</h2>
<p>
We are currently in stealth mode, building AI Assistants for the future of work.
<br>Please check back soon for updates.
</p>
</div>
</div>
<!-- 结束代码片段 -->
英文:
Solution
You didn't add display: flex
and flex-direction: column
to .text
class.
.text {
display: flex; /* here */
flex-direction: column; /* here */
justify-content: center;
align-items: center;
}
and add CSS in <p>
tag
p {
text-align: center; /* here */
}
Example
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
background-image: url("https://picsum.photos/1000/1000");
background-repeat: no-repeat;
background-color: black;
background-position: center;
background-position-y: 0px;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
min-height:100%;
margin: 0;
padding: 0;
}
.overlay {
box-shadow: inset 0 0px 0 1000px rgba(0, 0, 0, 0.6);
padding: 0;
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
justify-content: center; /* Added */
align-items: center; /* Added */
}
.text {
font-family: Arial, sans-serif;
background-color: rgba(0, 0, 0, 0.0);
color: #ffffff;
display: flex; /* here */
flex-direction: column; /* here */
align-items: center;
justify-content: center;
text-align: center;
margin-left: 60px;
}
h1 {
font-size: 48px;
margin-bottom: 20px;
}
h2 {
font-size: 36px;
margin-bottom: 20px;
}
p {
text-align: center; /* here */
font-size: 18px;
margin-bottom: 20px;
}
<!-- language: lang-html -->
<div class="overlay">
<div class="text">
<h1>Quintillion AI</h1>
<h2>Coming soon</h2>
<p>
We are currently in stealth mode, building AI Assistants for the future of work.
<br>Please check back soon for updates.
</p>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论