在移动电话上如何居中英雄图像

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

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(&quot;https://picsum.photos/1000/1000&quot;);
  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 -->

&lt;div class=&quot;overlay&quot;&gt;
  &lt;div class=&quot;text&quot;&gt;
    &lt;h1&gt;Quintillion AI&lt;/h1&gt;
    &lt;h2&gt;Coming soon&lt;/h2&gt;
    &lt;p&gt;
      We are currently in stealth mode, building AI Assistants for the future of work.
      &lt;br&gt;Please check back soon for updates.
    &lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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(&quot;https://picsum.photos/1000/1000&quot;) no-repeat center / cover;
  /* remove the other background-* properties ... */
  /* other styles ... */
}

答案2

得分: 0

解决方案

你没有在 .text 类中添加 display: flexflex-direction: column

.text {
  display: flex; /* 在这里 */
  flex-direction: column; /* 在这里 */
  justify-content: center;
  align-items: center;
}

并且在 &lt;p&gt; 标签中添加 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 &lt;p&gt; tag

p {
  text-align: center; /* here */
}

Example

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

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

body {
  background-image: url(&quot;https://picsum.photos/1000/1000&quot;);
  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 -->

&lt;div class=&quot;overlay&quot;&gt;
  &lt;div class=&quot;text&quot;&gt;
    &lt;h1&gt;Quintillion AI&lt;/h1&gt;
    &lt;h2&gt;Coming soon&lt;/h2&gt;
    &lt;p&gt;
      We are currently in stealth mode, building AI Assistants for the future of work.
      &lt;br&gt;Please check back soon for updates.
    &lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 16:28:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603062.html
匿名

发表评论

匿名网友

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

确定