在CSS中自动调整与图像内联的标题

时间:2017-06-12 17:32:14

标签: html css

我希望我的标题h1能够使用与图像内联的浏览器大小自动调整大小,以便在小屏幕中查看时不会占用大部分屏幕尺寸并为图像留出一些空间。另外请注意标题h3,即它还应占用所需的空间。

代码:

您可以在jsfiddle here

上找到代码
<body>

  <div id="main_wrapper">
    <header>
      <div id="header_title">
        <h1>Lorem Ipsum dolor sit amet facilisis ut </h1>
        <h3>"Vivamus sed libero nec mauris pulvinar facilisis ut non sem."</h3>
      </div>
      <aside id="header_photo">
        <img src="http://3.bp.blogspot.com/-hSDYcX9cd5w/UZnDC0Z27PI/AAAAAAAAAGg/WmWDp_cZfw4/s1600/lorem-ipsum-dolor-sit-amet-5.png" alt="Placeholder" class="floatingimage" title="Placeholder" />
      </aside>
    </header>
  </div>
</body>


body {
  background-size: 120%;
  font-family: 'MyFont', Arial, sans-serif;
  color: #181818;
}

#main_wrapper {
  width: 80%;
  margin: auto;
}

header {
  width: 100%;
  height: auto;
  margin: 0;
  padding: 0 0 40%;
}

#header_title {
  text-align: center;
  max-width: 78%;
}

#header_photo {
  /*display: none;*/
  margin: 0;
  padding: 0;
  max-width: 20%;
  height: auto;
  float: right;
}

.floatingimage {
  position: relative;
  display: none;
}

.wh100 {
  width: 100px;
  height: 100px;
}

@media all and (max-width:1024px) {
  #main_wrapper {
    width: auto;
    margin: none;
  }
  #header_title,
  #header_photo img {
    margin: 0;
    padding: 0;
    display: inline!important;
    vertical-align: middle!important;
  }
  #header_title {
    max-width: 78%;
    position: relative;
    float: left;
  }
  #header_photo {
    margin-top: 4%;
    max-width: 20%;
    position: relative;
    float: right;
  }
  #header_photo img {
    position: relative;
    max-width: 100%;
    height: auto;
  }
}

注意:我使用的是Firefox 53.0.3(32位)

1 个答案:

答案 0 :(得分:1)

如果您希望字体大小与浏览器的宽度一致,请使用vw

font-size单位

&#13;
&#13;
body {
  background-size: 120%;
  font-family: 'MyFont', Arial, sans-serif;
  color: #181818;
}

#main_wrapper {
  width: 80%;
  margin: auto;
}

header {
  width: 100%;
  height: auto;
  margin: 0;
  padding: 0 0 40%;
  font-size: 3vw;
}

#header_title {
  text-align: center;
  max-width: 78%;
}

#header_photo {
  /*display: none;*/
  margin: 0;
  padding: 0;
  max-width: 20%;
  height: auto;
  float: right;
}

.floatingimage {
  position: relative;
  display: none;
}

.wh100 {
  width: 100px;
  height: 100px;
}

@media all and (max-width:1024px) {
  #main_wrapper {
    width: auto;
    margin: none;
  }
  #header_title,
  #header_photo img {
    margin: 0;
    padding: 0;
    display: inline!important;
    vertical-align: middle!important;
  }
  #header_title {
    max-width: 78%;
    position: relative;
    float: left;
  }
  #header_photo {
    margin-top: 4%;
    max-width: 20%;
    position: relative;
    float: right;
  }
  #header_photo img {
    position: relative;
    max-width: 100%;
    height: auto;
  }
}
&#13;
<body>

  <div id="main_wrapper">
    <header>
      <div id="header_title">
        <h1>Lorem Ipsum dolor sit amet facilisis ut </h1>
        <h3>"Vivamus sed libero nec mauris pulvinar facilisis ut non sem."</h3>
      </div>
      <aside id="header_photo">
        <img src="http://3.bp.blogspot.com/-hSDYcX9cd5w/UZnDC0Z27PI/AAAAAAAAAGg/WmWDp_cZfw4/s1600/lorem-ipsum-dolor-sit-amet-5.png" alt="Placeholder" class="floatingimage" title="Placeholder" />
      </aside>
    </header>
  </div>
</body>
&#13;
&#13;
&#13;