将容器居中对齐

发布于 2025-01-12 09:38:57 字数 1586 浏览 3 评论 0原文

我试图将 .container 对齐到页面的中心,现在它只在左侧。 我尝试过“float:left”和“left:”,但这些都没有任何好处。 我正在寻找的是正确的对齐标签以使 .container 在中心对齐。 谢谢, 薄荷 输入图片此处描述

.container {
  background-color: lightgrey;
  padding: 1rem;
  border-radius: .5rem;
  width: 700px;
  max-width: 90%;
}

.quote-display {
  margin-bottom: 1rem;
  margin-left: calc(1rem + 2px);
  margin-right: calc(1rem + 2px);
}

.quote-input {
  background-color: transparent;
  border: 2px solid red;
  width: 100%;
  height: 8rem;
  margin: auto;
  resize: none;
  padding: .5rem 1rem;
  font-size: 1rem;
  border-radius: .5rem;
}

.quote-input:focus {
  border-color: black;
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fredoka&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">


<div class="main">
  <div id="title">
    <h1 style="margin: 0;">Typerore</h1>
  </div>
  <div class="timer" id="timer"></div>
  <div class="container">
    <div class="quote-display" id="quoteDisplay"></div>
    <textarea id="quoteInput" class="quote-input" autofocus></textarea>
  </div>
</div>

I am trying to align .container to the center of the page, right now it is only going on the left side.
I have tried "float:left" as well as "left:" Neither of these did me any good.
What I am looking for is the correct alignment tag to get .container aligned in the center.
Thanks,
Mint
enter image description here

.container {
  background-color: lightgrey;
  padding: 1rem;
  border-radius: .5rem;
  width: 700px;
  max-width: 90%;
}

.quote-display {
  margin-bottom: 1rem;
  margin-left: calc(1rem + 2px);
  margin-right: calc(1rem + 2px);
}

.quote-input {
  background-color: transparent;
  border: 2px solid red;
  width: 100%;
  height: 8rem;
  margin: auto;
  resize: none;
  padding: .5rem 1rem;
  font-size: 1rem;
  border-radius: .5rem;
}

.quote-input:focus {
  border-color: black;
}
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fredoka&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">


<div class="main">
  <div id="title">
    <h1 style="margin: 0;">Typerore</h1>
  </div>
  <div class="timer" id="timer"></div>
  <div class="container">
    <div class="quote-display" id="quoteDisplay"></div>
    <textarea id="quoteInput" class="quote-input" autofocus></textarea>
  </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2025-01-19 09:38:57

如果我是你,我会研究 Flexbox。解决您的问题的方法是在容器周围添加一个包装器并对其进行以下样式设置:

.wrapper { display: flex;
        justify-content:center;
}

也许是这样的: https://jsfiddle.net/s35x607o/1/

要获得更多真正精彩的阅读内容,请查看 https://css-tricks.com/snippets/css/a-guide -到弹性盒/

If I were you I would look into Flexbox. A solution to your problem would be to add a wrapper around your container and do this styling on it:

.wrapper { display: flex;
        justify-content:center;
}

Maybe something like this: https://jsfiddle.net/s35x607o/1/

For more really nice reading, check out https://css-tricks.com/snippets/css/a-guide-to-flexbox/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文