左侧文字

发布于 2025-01-12 10:36:17 字数 2488 浏览 4 评论 0原文

我在这里尝试做的是将文本“Typerore”与左上角对齐,但文本位于左侧中心。

我尝试在 CSS 中使用“left:”标签,但这不起作用,我也尝试使用“float:left”,但这也不起作用。

我想知道我需要使用什么标签来将 div“标题”对齐到左上角。

输入图片此处描述

* {
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

.title {
  left: 3rem;
}
<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">
<div id="title">
  <h1>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>
  <script src="script.js"></script>
</div>

What I am trying to do here is to align the text "Typerore" to the top left corner, but the text is positioned in the left center.

I tried using the "left:" tag in CSS, but that did not work, I also tried using "float:left" but that did not work either.

I was wondering what tag do I need to use to align the div "title" to the top left.

enter image description here

* {
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

.title {
  left: 3rem;
}
<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">
<div id="title">
  <h1>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>
  <script src="script.js"></script>
</div>

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

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

发布评论

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

评论(3

夜访吸血鬼 2025-01-19 10:36:18

您可以添加边距

也可以使用div而不是使整个主体弯曲

最后您在CSS中使用.title而不是#title

* {
  box-sizing: border-box;
}

#main {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

#title {
  left: 3rem;
  margin-bottom: 8rem;
  margin-right: 1rem;
}
<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">
<div id="main">
  <div id="title">
    <h1>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>
    <script src="script.js"></script>
  </div>
</div>

You can add a margin

Also do use divs instead of making the whole body flex

Lastly you had .title instead of #title in the CSS

* {
  box-sizing: border-box;
}

#main {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

#title {
  left: 3rem;
  margin-bottom: 8rem;
  margin-right: 1rem;
}
<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">
<div id="main">
  <div id="title">
    <h1>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>
    <script src="script.js"></script>
  </div>
</div>

帅气称霸 2025-01-19 10:36:18

#title 的高度设置为 container 的渲染高度。然后,要在最顶角获取文本,您必须删除 h1 上的默认边距。

* {
  box-sizing: border-box;
}

.main {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body {
  margin: 0;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

#title {
  left: 3rem;
  height: 180px;
}
<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">
<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>
    <script src="script.js"></script>
  </div>
</div>

Set height of #title to rendered height of container. Then to get the text at the very top corner you will have to remove the default margin on h1.

* {
  box-sizing: border-box;
}

.main {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body {
  margin: 0;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

#title {
  left: 3rem;
  height: 180px;
}
<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">
<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>
    <script src="script.js"></script>
  </div>
</div>

小瓶盖 2025-01-19 10:36:17

:D

我为你做了一个快速解决方案(也许不是最好的,但效果很好),
我将“id=title”更改为“class=title”,因为您在 css 文件中使用了点(.title)。

* {
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

.title {
  margin-bottom: 150px;
  margin-right: 5px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <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">
  <title>Typerore</title>
</head>

<body>
  <div class="title">
    <h1>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>
    <script src="script.js"></script>
  </div>
</body>

</html>

:D

I Made a Quick solve for you (maybe no the best, but works fine),
i changed "id=title" to "class=title" because you use the dot (.title) in css file.

* {
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  background-color: coral;
}

body,
.quote-input {
  font-family: 'Fredoka', sans-serif;
}

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

.timer {
  position: absolute;
  top: 2rem;
  font-size: 3rem;
  color: grey;
  font-weight: bold;
}

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

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

.quote-input:focus {
  border-color: black;
}

.correct {
  color: green;
}

.incorrect {
  color: red;
  text-decoration: underline;
}


/* Background Styles Only */

* {
  font-family: 'Fredoka', sans-serif;
}

.side-links {
  position: absolute;
  top: 15px;
  right: 15px;
}

.side-link {
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  margin-bottom: 10px;
  color: white;
  width: 180px;
  padding: 10px 0;
  border-radius: 10px;
}

.side-link-text {
  margin-left: 10px;
  font-size: 18px;
}

.side-link-icon {
  color: white;
  font-size: 30px;
}

.title {
  margin-bottom: 150px;
  margin-right: 5px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <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">
  <title>Typerore</title>
</head>

<body>
  <div class="title">
    <h1>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>
    <script src="script.js"></script>
  </div>
</body>

</html>

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