第一个问题在这里。希望我把它贴对了。
按照DOM操作YouTube过程,这里可以制作Javascript秒表。
出于某种原因,我似乎无法集中我的起点零。
根本找不到真相。
任何帮助都很感激。
//My Mark Up//
<body>
<div class="container">
<div id="timer">00:00:00</div>
<div class="buttons">
<button id="startStopBtn">
<i class="fa-solid fa-play" id="play"></i>
</button>
<button id="resetBtn">
<i class="fa-solid fa-arrow-rotate-left" id="reset"></i>
</button>
</div>
</div>//他们的标记://
<div class="container">
<div id="timer">
00:00:00
</div>
<div class="buttons">
<button id="startStopBtn">
<i class="fa-solid fa-play" id="play"></i>
</button>
<button id="resetBtn">
<i class="fa-solid fa-arrow-rotate-left" id="reset"></i>
</button>
</div>//我的CSS://
body {
height: 100vh;
background: url(https://d1s9j44aio5gjs.cloudfront.net/2021/10/Underwater- empty-pool.jpg)
no-repeat center top / cover;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 60%;
height: 250px;
background-color: #fff;
border-radius: 30px;
box-shadow: 0 0 3px;
}
#timer {
width: 100%;
font-size: 72px;
text-align: center;
margin: 0px auto;
padding: 35px;
}
.buttons {
text-align: center;
}
.button {
margin: 0 10px;
border: none;
}
button i {
font-size: 2rem;
padding: 10px;
color: #fff;
width: 50px;
}
#play {
background-color: green;
}
#pause {
background-color: orange;
}
#reset {
background-color: red;
}//他们的CSS//
body {
height: 100vh;
background: url(img/project-4.jpg) no-repeat center top / cover;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 60%;
height: 250px;
background-color: #fff;
border-radius: 30px;
box-shadow: 0 0 3px;
}
#timer {
width: 100%;
font-size: 72px;
text-align: center;
margin: 0px auto;
padding: 35px;
}
.buttons {
text-align: center;
}
button {
margin: 0 10px;
border: none;
}
button i {
font-size: 2rem;
padding: 10px;
color: #fff;
width: 50px;
}
#play {
background-color: green;
}
#pause {
background-color: orange;
}
#reset {
background-color: red;
} 发布于 2022-10-10 18:09:34
问题是,您给计时器的宽度为100%,边垫为35 of。
假设您没有更改框大小的默认设置,默认情况下它的值为content,因此总的宽度将为100% (父级.container的宽度),加上2x35px。
如果在timer元素上放置背景颜色,您可以看到这一点。
您可以删除边填充的文本-对齐:中心将确保您得到尽可能多的填充(即空白处两侧的文本)。
然而,您的导师的代码中有填充,所以我怀疑他们有一个毯子设置的盒子大小。您经常在样式表中看到这一点:
* {
box-sizing: border-box;
}这些设置通常还包括页边距: 0;以摆脱浏览器设置的默认边距。
值得一问。
这里是“您的代码”与框大小集,顺便说一句,从样式表中的按钮设置前面删除了点。
<head>
<style>
* {
box-sizing: border-box;
}
body {
height: 100vh;
background: url(https://d1s9j44aio5gjs.cloudfront.net/2021/10/Underwater-empty-pool.jpg) no-repeat center top / cover;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 60%;
height: 250px;
background-color: #fff;
border-radius: 30px;
box-shadow: 0 0 3px;
}
#timer {
width: 100%;
font-size: 72px;
text-align: center;
margin: 0px auto;
padding: 35px;
background: cyan;
}
.buttons {
text-align: center;
background: pink;
}
button {
margin: 0 10px;
border: none;
}
button i {
font-size: 2rem;
padding: 10px;
color: #fff;
width: 50px;
}
#play {
background-color: green;
}
#pause {
background-color: orange;
}
#reset {
background-color: red;
}
</style>
<body>
<div class="container">
<div id="timer">00:00:00</div>
<div class="buttons">
<button id="startStopBtn">
<i class="fa-solid fa-play" id="play"></i>
</button>
<button id="resetBtn">
<i class="fa-solid fa-arrow-rotate-left" id="reset"></i>
</button>
</div>
</div>
</body>
注意:有了这么大的字体大小,可能会有时间仍在容器外流动的情况下--试着把视口的宽度往下压。如果您支持非常窄的设备,并引入一些响应性大小的大小-例如,使字体大小依赖于大众单元,您可能会考虑这一点。
还要注意的是,背景中的图像没有显示,因为url中有虚假的空格,所以我删除了它们。
发布于 2022-10-10 14:53:51
您只需将width从#timer中删除,或者给它一个max-width: 100%;而不是width: 100%;。
看下面的片段。
body {
height: 100vh;
background: url(https://d1s9j44aio5gjs.cloudfront.net/2021/10/Underwater-empty-pool.jpg)
no-repeat center top / cover;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 60%;
height: 250px;
background-color: #fff;
border-radius: 30px;
box-shadow: 0 0 3px;
}
#timer {
max-width: 100%;
font-size: 72px;
text-align: center;
margin: 0px auto;
padding: 35px;
}
.buttons {
text-align: center;
}
.button {
margin: 0 10px;
border: none;
}<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="./style.css" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"
integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
</head>
<body>
<div class="container">
<div id="timer">00:00:00</div>
<div class="buttons">
<button id="startStopBtn">
<i class="fa-solid fa-play" id="play"></i>
</button>
<button id="resetBtn">
<i class="fa-solid fa-arrow-rotate-left" id="reset"></i>
</button>
</div>
</div>
</body>
</html>
发布于 2022-10-10 14:57:20
我假设在浏览器的默认样式表与导师的样式表之间可能有一些不同。
我解决这个问题的方法是将box-sizing of #timer改为border-box。您可以找到关于该属性这里的更多细节。您可以看到,在使用content-box时,它的行为就像您的示例一样,因为它不将填充作为元素宽度的一部分,因此当您将元素宽度设置为100%时,它会溢出。
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 60%;
height: 250px;
background-color: #fff;
border-radius: 30px;
box-shadow: 0 0 3px;
}
#timer {
width: 100%;
font-size: 72px;
text-align: center;
margin: 0px auto;
padding: 35px;
box-sizing: border-box; /* This is what you need if you use padding otherwise the padding won't get considered */
}<body>
<div class="container">
<div id="timer">00:00:00</div>
</div>
</body>
https://stackoverflow.com/questions/74016658
复制相似问题