CSS: 有没有等价于vh + vw的单位?
CSS: Is there a Unit that the Equivalent to vh + vw?
我想知道是否有 measures 1% of the width and height of a page
的字体大小单位?我认为这会让我更容易,并帮助我做我想做的事情,而不必使用 JavaScript
(尽管任何答案都是可以接受的,只要它有效)。如果没有这样的单位,我该如何操作字体来做到这一点?我唯一能想到的是 %
但是,我不知道它们是否有效。
我的问题来了,当我尝试以两种方式调整页面大小时。它适用于预期目的,但是当您将页面调整得非常小时,它看起来不会很好。我是一个完美主义者,当它不起作用时我会很烦恼。这是我的代码(准确地说是我的代码片段。如果你想要完整的代码,我会把它编辑到这里):
更新(0.1):
我正在尝试使用 javascript 做出更好的答案。我还没有完成代码。这是:
function myFunction() {
var h11a = document.getElementById("h1_1_a");
var w = window.innerWidth;
var h = window.innerHeight;
window.addEventListener("resize", function() {
var w2 = window.innerWidth;
var h2 = window.innerHeight;
if (w > w2) {
h11a.style.fontSize = "calc(6*(0vh + 1.5vw))";
} else if (h > h2) {
h11a.style.fontSize = "calc(6*(3vh + 0vw))";
}
});
}
* {
box-sizing: border-box;
}
body {
margin: 0%;
}
.div_1 {
position: relative;
width: 100%;
Height: 20%;
background-color: #000000;
border-top: .4vh solid #ff7400;
border-left: .4vh solid #ff7400;
border-right: .4vh solid #ff7400;
border-bottom: .2vh solid #ff7400;
}
#div_1_a {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
#h1_1_a {
line-height: 100%;
height: 60%;
color: #ff7400;
font-size:calc(6*(1vh + 1vw));
white-space: nowrap
}
<html lang="en">
<head>
<link rel="stylesheet" href="halloweenEvent1.css" type="text/css" />
<meta charset="UTF-8" />
<meta name="description" content="A Halloween Event Website" />
<meta name="keywords" content="Brad,Website,Personal,Information,Halloween" />
<meta name="author" content="Bradley William Elko" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halloween Event (Home)</title>
</head>
<body onload="myFunction()">
<div class="div_1">
<div id="div_1_a">
<h1 id="h1_1_a">Halloween Website</h1>
</div>
</div>
</body>
</html>
非常感谢任何帮助!
使用 calc( n * ( 1vh + 1vw ) )
,其中 n
是您选择的系数。
不,没有这样的单位,但也许 vmin
可以解决您的问题。
编辑:
如果你想垂直和水平居中,你可以使用这样的东西:
.wrapper {
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper__text {
font-size: 30vmin;
}
<div class="wrapper">
<div class="wrapper__text">My text</div>
</div>
感谢:
calc( n * ( 1vh + 1vw ) )
的演示。
*
{
box-sizing: border-box;
}
body
{
margin:0%;
}
.div_1
{
position:relative;
width:100%;
Height:20%;
background-color:#000000;
border-top:.4vh solid #ff7400;
border-left:.4vh solid #ff7400;
border-right:.4vh solid #ff7400;
border-bottom:.2vh solid #ff7400;
}
#div_1_a
{
height:100%;
display: flex;
align-items: center;
justify-content: center;
}
#h1_1_a
{
line-height:100%;
height:60%;
color:#ff7400;
font-size: calc( 6 * ( 1vh + 1vw ) );
white-space: nowrap
}
<html lang="en">
<head>
<link rel="stylesheet" href="halloweenEvent1.css" type="text/css"/>
<meta charset="UTF-8"/>
<meta name="description" content="A Halloween Event Website"/>
<meta name="keywords" content="Brad,Website,Personal,Information,Halloween"/>
<meta name="author" content="Bradley William Elko"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halloween Event (Home)</title>
</head>
<body>
<div class="div_1">
<div id="div_1_a">
<h1 id="h1_1_a">Halloween Website</h1>
</div>
</div>
</body>
</html>
I want the font size to be in accordance to the div it's held in (in this case )
我的理解是您希望文本与容器保持相同的比例。我将使用 SVG 来避免这个问题:
*
{
box-sizing: border-box;
}
body
{
margin:0%;
}
.div_1
{
position:relative;
width:100%;
Height:20%;
background-color:#000000;
border-top:.4vh solid #ff7400;
border-left:.4vh solid #ff7400;
border-right:.4vh solid #ff7400;
border-bottom:.2vh solid #ff7400;
}
<html lang="en">
<head>
<link rel="stylesheet" href="halloweenEvent1.css" type="text/css"/>
<meta charset="UTF-8"/>
<meta name="description" content="A Halloween Event Website"/>
<meta name="keywords" content="Brad,Website,Personal,Information,Halloween"/>
<meta name="author" content="Bradley William Elko"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halloween Event (Home)</title>
</head>
<body>
<div class="div_1">
<svg class="svg" viewbox="0 0 100 20">
<text fill="#ff7400" x="50" y="10" font-size="50%" text-anchor="middle" dominant-baseline="central">Halloween Website</text>
</svg>
</div>
</body>
</html>
缺点是失去了h1
的语义。
我想知道是否有 measures 1% of the width and height of a page
的字体大小单位?我认为这会让我更容易,并帮助我做我想做的事情,而不必使用 JavaScript
(尽管任何答案都是可以接受的,只要它有效)。如果没有这样的单位,我该如何操作字体来做到这一点?我唯一能想到的是 %
但是,我不知道它们是否有效。
我的问题来了,当我尝试以两种方式调整页面大小时。它适用于预期目的,但是当您将页面调整得非常小时,它看起来不会很好。我是一个完美主义者,当它不起作用时我会很烦恼。这是我的代码(准确地说是我的代码片段。如果你想要完整的代码,我会把它编辑到这里):
更新(0.1): 我正在尝试使用 javascript 做出更好的答案。我还没有完成代码。这是:
function myFunction() {
var h11a = document.getElementById("h1_1_a");
var w = window.innerWidth;
var h = window.innerHeight;
window.addEventListener("resize", function() {
var w2 = window.innerWidth;
var h2 = window.innerHeight;
if (w > w2) {
h11a.style.fontSize = "calc(6*(0vh + 1.5vw))";
} else if (h > h2) {
h11a.style.fontSize = "calc(6*(3vh + 0vw))";
}
});
}
* {
box-sizing: border-box;
}
body {
margin: 0%;
}
.div_1 {
position: relative;
width: 100%;
Height: 20%;
background-color: #000000;
border-top: .4vh solid #ff7400;
border-left: .4vh solid #ff7400;
border-right: .4vh solid #ff7400;
border-bottom: .2vh solid #ff7400;
}
#div_1_a {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
#h1_1_a {
line-height: 100%;
height: 60%;
color: #ff7400;
font-size:calc(6*(1vh + 1vw));
white-space: nowrap
}
<html lang="en">
<head>
<link rel="stylesheet" href="halloweenEvent1.css" type="text/css" />
<meta charset="UTF-8" />
<meta name="description" content="A Halloween Event Website" />
<meta name="keywords" content="Brad,Website,Personal,Information,Halloween" />
<meta name="author" content="Bradley William Elko" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halloween Event (Home)</title>
</head>
<body onload="myFunction()">
<div class="div_1">
<div id="div_1_a">
<h1 id="h1_1_a">Halloween Website</h1>
</div>
</div>
</body>
</html>
非常感谢任何帮助!
使用 calc( n * ( 1vh + 1vw ) )
,其中 n
是您选择的系数。
不,没有这样的单位,但也许 vmin
可以解决您的问题。
编辑:
如果你想垂直和水平居中,你可以使用这样的东西:
.wrapper {
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper__text {
font-size: 30vmin;
}
<div class="wrapper">
<div class="wrapper__text">My text</div>
</div>
感谢:
calc( n * ( 1vh + 1vw ) )
*
{
box-sizing: border-box;
}
body
{
margin:0%;
}
.div_1
{
position:relative;
width:100%;
Height:20%;
background-color:#000000;
border-top:.4vh solid #ff7400;
border-left:.4vh solid #ff7400;
border-right:.4vh solid #ff7400;
border-bottom:.2vh solid #ff7400;
}
#div_1_a
{
height:100%;
display: flex;
align-items: center;
justify-content: center;
}
#h1_1_a
{
line-height:100%;
height:60%;
color:#ff7400;
font-size: calc( 6 * ( 1vh + 1vw ) );
white-space: nowrap
}
<html lang="en">
<head>
<link rel="stylesheet" href="halloweenEvent1.css" type="text/css"/>
<meta charset="UTF-8"/>
<meta name="description" content="A Halloween Event Website"/>
<meta name="keywords" content="Brad,Website,Personal,Information,Halloween"/>
<meta name="author" content="Bradley William Elko"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halloween Event (Home)</title>
</head>
<body>
<div class="div_1">
<div id="div_1_a">
<h1 id="h1_1_a">Halloween Website</h1>
</div>
</div>
</body>
</html>
I want the font size to be in accordance to the div it's held in (in this case )
我的理解是您希望文本与容器保持相同的比例。我将使用 SVG 来避免这个问题:
*
{
box-sizing: border-box;
}
body
{
margin:0%;
}
.div_1
{
position:relative;
width:100%;
Height:20%;
background-color:#000000;
border-top:.4vh solid #ff7400;
border-left:.4vh solid #ff7400;
border-right:.4vh solid #ff7400;
border-bottom:.2vh solid #ff7400;
}
<html lang="en">
<head>
<link rel="stylesheet" href="halloweenEvent1.css" type="text/css"/>
<meta charset="UTF-8"/>
<meta name="description" content="A Halloween Event Website"/>
<meta name="keywords" content="Brad,Website,Personal,Information,Halloween"/>
<meta name="author" content="Bradley William Elko"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halloween Event (Home)</title>
</head>
<body>
<div class="div_1">
<svg class="svg" viewbox="0 0 100 20">
<text fill="#ff7400" x="50" y="10" font-size="50%" text-anchor="middle" dominant-baseline="central">Halloween Website</text>
</svg>
</div>
</body>
</html>
缺点是失去了h1
的语义。