CSS:是否有一个单位相当于vh + vw?

时间:2016-10-22 23:54:09

标签: javascript html css resize viewport-units

我想知道是否有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>

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

使用calc( n * ( 1vh + 1vw ) ),其中n是您选择的系数。

答案 1 :(得分:1)

不,没有这样的单位,但vmin可能是您问题的解决方案。

修改

如果你想垂直和水平居中,你可以使用这样的东西:

&#13;
&#13;
.wrapper {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}
.wrapper__text {
  font-size: 30vmin;
}
&#13;
<div class="wrapper">
  <div class="wrapper__text">My text</div>
</div>
&#13;
&#13;
&#13;

感谢:

http://howtocenterincss.com/

答案 2 :(得分:1)

calc( n * ( 1vh + 1vw ) ) proposed by Dai的演示。

&#13;
&#13;
* 
  { 
  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
}
&#13;
<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>
&#13;
&#13;
&#13;

  

我希望字体大小符合其所在的div(在这种情况下)

我所做的是你希望文本保持与容器相同的比例。我将通过使用SVG来解决问题:

&#13;
&#13;
* 
  { 
  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;
}
&#13;
<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>
&#13;
&#13;
&#13;

缺点是你失去了h1的语义。