英文:
Google font not working on Mobile browser
问题
在使用Google Web Fonts中的Space Mono(粗体700斜体)时,它在桌面Chrome上工作正常,但在移动Chrome(或Safari)上不起作用。尝试清除设备缓存也没有效果。似乎它只是回退到标准的等宽字体(New Courier)。
HTML部分看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>Countdown to Ibiza 2023...</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
<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=Space+Mono:ital,wght@1,700&display=swap" rel="stylesheet">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<style> @import url('https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@1,700&display=swap'); </style>
</head>
<body>
<div class="container">
<p class="para">Countdown to Ibiza...</p>
<div class="box">
<p id="days">Loading...</p>
<p id="hms">Loading...</p>
</div>
</div>
</body>
</html>
CSS部分看起来像这样:
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@1,700&display=swap');
body {
padding: 0;
margin: 0;
background: rgb(238,174,202);
background: radial-gradient(circle, rgba(238,174,202,1) 0%, rgba(148,187,233,1) 100%);
font-family: 'Space Mono', monospace;
}
.container {
display: flex;
flex-direction: column;
height: 80vh;
align-items: center;
justify-content: center;
}
.para {
margin: 0px 0px 10px 0px;
font-size: 36px;
text-shadow: 2px 2px rgba(0, 0, 255, .2);
}
.box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border-radius: 40px;
padding: 0px 60px 0px 60px;
background-color: white;
margin: 0;
box-shadow: 6px 6px 2px 1px rgba(0, 0, 255, .2);
}
#days {
font-size: 80px;
margin-top: 30px;
margin-bottom: 30px;
}
#hms {
margin-top: 0px;
font-family: 'Courier New', Courier, monospace;
font-weight: bold;
}
@media only screen
and (device-width: 390px)
and (device-height: 844px)
and (-webkit-device-pixel-ratio: 3) {
.container {
height: 60vh;
}
.para {
font-size: 56px;
font-family: 'Space Mono', monospace;
}
#days {
font-size: 160px;
font-family: 'Space Mono', monospace;
}
#hms {
font-size: 40px;
font-family: 'Courier New', Courier, monospace;
}
}
@media only screen
and (device-width: 428px)
and (device-height: 926px)
and (-webkit-device-pixel-ratio: 3) {
.container {
height: 60vh;
}
.para {
font-size: 56px;
font-family: 'Space Mono', monospace;
}
#days {
font-size: 160px;
font-family: 'Space Mono', monospace;
}
#hms {
font-size: 40px;
font-family: 'Courier New', Courier, monospace;
}
}
你在这里做错了什么?这个问题可能出现在移动浏览器不支持字体的情况下,或者可能是字体加载速度较慢导致页面首次加载时使用了默认字体。你可以尝试以下几个步骤来解决问题:
-
确保网络连接正常,以便字体可以从Google Web Fonts正确加载。你可以在浏览器的开发者工具中查看网络请求,以确认字体是否成功加载。
-
考虑使用Web字体加载器,例如Font Face Observer(https://fontfaceobserver.com/),来确保在字体加载完成之前不显示文本。
-
如果字体加载速度较慢,可以考虑将字体文件下载到你的服务器并从本地加载,以提高加载速度。
-
在移动浏览器上,确保没有任何CSS规则覆盖了字体设置,可以在开发者工具中检查元素的计算样式来确认。
-
如果可能的话,尝试使用通用字体族作为后备,以便在字体加载失败时使用默认字体,而不是New Courier。例如,你可以使用
font-family: 'Space Mono', monospace, sans-serif;
,这将在字体加载失败时回退到等宽字体。
通过以上步骤,你应该能够解决在移动浏览器上字体显示不正确的问题。希望这可以帮助你解决困扰你的问题。
英文:
Using Space Mono (Bold 700 Italic) from Google web fonts: https://fonts.google.com/specimen/Space+Mono?query=space+mono
It works fine on desktop Chrome but not Mobile Chrome (or Safari for that matter). I have tried clearing the device cache but no work. It looks like it is just defaulting back to standard monospace font (New Courier).
html looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Countdown to Ibiza 2023...</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
<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=Space+Mono:ital,wght@1,700&display=swap" rel="stylesheet">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
<style> @import url('https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@1,700&display=swap'); </style>
</head>
<body>
<div class="container">
<p class="para">Countdown to Ibiza...</p>
<div class="box">
<p id="days">Loading...</p>
<p id="hms">Loading...</p>
</div>
</div>
</body>
</html>
and CSS looks like this:
@import url('https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@1,700&display=swap');
body {
padding: 0;
margin: 0;
background: rgb(238,174,202);
background: radial-gradient(circle, rgba(238,174,202,1) 0%, rgba(148,187,233,1) 100%);
font-family: 'Space Mono', monospace;
}
.container {
display: flex;
flex-direction: column;
height: 80vh;
align-items: center;
justify-content: center;
/* border: 1px solid black; */
}
.para {
margin: 0px 0px 10px 0px;
font-size: 36px;
text-shadow: 2px 2px rgba(0, 0, 255, .2);
}
.box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
/* border: 1px dotted black; */
border-radius: 40px;
padding: 0px 60px 0px 60px;
background-color: white;
margin: 0;
box-shadow: 6px 6px 2px 1px rgba(0, 0, 255, .2);
}
#days {
font-size: 80px;
margin-top: 30px;
margin-bottom: 30px;
/* border: 1px solid black; */
}
#hms {
margin-top: 0px;
font-family: 'Courier New', Courier, monospace;
font-weight: bold;
/* font-weight: 500; */
}
@media only screen
and (device-width: 390px)
and (device-height: 844px)
and (-webkit-device-pixel-ratio: 3) {
.container {
height: 60vh;
}
.para {
font-size: 56px;
font-family: 'Space Mono', monospace;
}
#days {
font-size: 160px;
font-family: 'Space Mono', monospace;
}
#hms {
font-size: 40px;
font-family: 'Courier New', Courier, monospace;
}
}
/* 2778x1284 pixels at 458ppi */
@media only screen
and (device-width: 428px)
and (device-height: 926px)
and (-webkit-device-pixel-ratio: 3) {
.container {
height: 60vh;
}
.para {
font-size: 56px;
font-family: 'Space Mono', monospace;
}
#days {
font-size: 160px;
font-family: 'Space Mono', monospace;
}
#hms {
font-size: 40px;
font-family: 'Courier New', Courier, monospace;
}
}
What am I doing wrong here? Pulling my hair out over this one!
答案1
得分: 2
目前,您只加载了 font-style: italic
和 font-weight: 700
。
检查谷歌字体 @font-face
输出:
/* 拉丁字符集 */
@font-face {
font-family: 'Space Mono';
font-style: italic;
font-weight: 700;
font-display: swap;
src: url(https://fonts.gstatic.com/s/spacemono/v12/i7dSIFZifjKcF5UAWdDRYERE_FeqHCSR.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
但是,在您的类中没有指定任何字体重量或样式。
所以只需像这样添加这些属性:
body,
.para,
#days {
font-family: 'Space Mono', monospace;
font-weight: 700;
font-style: italic;
}
如果您希望将 'Space Mono' 映射到常规字体重量和样式 - 只需添加一个覆盖的 @font-face
规则,像这样:
@font-face {
font-family: 'Space Mono';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/spacemono/v12/i7dSIFZifjKcF5UAWdDRYERE_FeqHCSR.woff2) format('woff2');
}
您的 HTML 和 CSS 代码看起来都很好,没有需要翻译的部分。
英文:
Currently, you're loading only font-style:italic
and font-weight:700
.
Check the google fonts @font-face
output:
/* latin */
@font-face {
font-family: 'Space Mono';
font-style: italic;
font-weight: 700;
font-display: swap;
src: url(https://fonts.gstatic.com/s/spacemono/v12/i7dSIFZifjKcF5UAWdDRYERE_FeqHCSR.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
But you haven't specified any font-weights or styles in your classes.
So just add these properties like so:
body,
.para,
#days {
font-family: 'Space Mono', monospace;
font-weight:700;
font-style:italic;
}
If you want the 'Space Mono' to be mapped to regular font-weight and style - just add an overriding @font-face
rule like so:
@font-face {
font-family: 'Space Mono';
font-style: regular;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/spacemono/v12/i7dSIFZifjKcF5UAWdDRYERE_FeqHCSR.woff2) format('woff2');
}
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-css -->
@font-face {
font-family: 'Space Mono';
font-style: regular;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/spacemono/v12/i7dSIFZifjKcF5UAWdDRYERE_FeqHCSR.woff2) format('woff2');
}
body {
padding: 0;
margin: 0;
background: rgb(238, 174, 202);
background: radial-gradient(
circle,
rgba(238, 174, 202, 1) 0%,
rgba(148, 187, 233, 1) 100%
);
font-family: "Space Mono", monospace;
}
.container {
display: flex;
flex-direction: column;
height: 80vh;
align-items: center;
justify-content: center;
/* border: 1px solid black; */
}
.para {
margin: 0px 0px 10px 0px;
font-size: 36px;
text-shadow: 2px 2px rgba(0, 0, 255, 0.2);
}
.box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
/* border: 1px dotted black; */
border-radius: 40px;
padding: 0px 60px 0px 60px;
background-color: white;
margin: 0;
box-shadow: 6px 6px 2px 1px rgba(0, 0, 255, 0.2);
}
#days {
font-size: 80px;
margin-top: 30px;
margin-bottom: 30px;
/* border: 1px solid black; */
}
#hms {
margin-top: 0px;
font-family: "Courier New", Courier, monospace;
font-weight: bold;
/* font-weight: 500; */
}
@media only screen
and (device-width: 390px)
and (device-height: 844px)
and (-webkit-device-pixel-ratio: 3) {
.container {
height: 60vh;
}
.para {
font-size: 56px;
font-family: 'Space Mono', monospace;
}
#days {
font-size: 160px;
font-family: 'Space Mono', monospace;
}
#hms {
font-size: 40px;
font-family: 'Courier New', Courier, monospace;
}
}
/* 2778x1284 pixels at 458ppi */
@media only screen
and (device-width: 428px)
and (device-height: 926px)
and (-webkit-device-pixel-ratio: 3) {
.container {
height: 60vh;
}
.para {
font-size: 56px;
font-family: 'Space Mono', monospace;
}
#days {
font-size: 160px;
font-family: 'Space Mono', monospace;
}
#hms {
font-size: 40px;
font-family: 'Courier New', Courier, monospace;
}
}
<!-- language: lang-html -->
<html>
<head>
<title>Countdown to Ibiza 2023...</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js"></script>
<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=Space+Mono:ital,wght@1,700&display=swap" rel="stylesheet">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="theme-color" content="#ffffff">
</head>
<body>
<div class="container">
<p class="para">Countdown to Ibiza...</p>
<div class="box">
<p id="days">Loading...</p>
<p id="hms">Loading...</p>
</div>
</div>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论