首页 > 解决方案 > 有没有办法让css考虑svg占用多少空间

问题描述

我有一个 svg 和两个 div,我正在努力做到这一点,所以当你进入网站时,2 个 div 和 svg 占据了整个屏幕,但是使用 svg 会使它变得更大,因为 css 不占 svg 是有一种方法可以让 css 占 svg 占用的空间

我试图设置标题容器的最大高度

我也尝试过调整 2 个 div 的 vh,但在更小或更大的屏幕上,它们会变成不同的大小,并且不起作用

* {
  margin: 0%;
  padding: 0%;
}
body {
  height: 100000px;
}
.header-container {
  width: 100%;
  background-color: #3C8DAD;
  max-height: 100vh;

}

.header-top {
  width: 100%;
  height: 50vh;
}
svg {
  vertical-align: top;
}
.header-bottom {
  width: 100%;
  height: 50vh;
  background-color: #F5A962;
}
.header-content {
  align-items: center;
  width: 45%;
  height: 300px;
  border: 2px red solid;
  position: absolute;
  left: 25%;
  top: 50%; 
}
.name {
  font-family: 'Titillium Web', sans-serif;
  font-size: 40px;
  color: white;
}
.job {
  font-family: 'Noto Serif', serif;
  font-size: 66px;
  font-weight: 800;
  color: white;
  margin-top: 3%;
  margin-left: 4%;
}
<div class="header-container">
  <div class="header-top">
      <div class="header-content">
          <div class="name">Andrew Saenger</div>
          <div class="job">Front-End Web Developer</div>
      </div>
  </div>
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320">
    <path fill="#F5A962" fill-opacity="1" d="M0,160L48,160C96,160,192,160,288,138.7C384,117,480,75,576,85.3C672,96,768,160,864,160C960,160,1056,96,1152,80C1248,64,1344,96,1392,112L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path>
  </svg>
  <div class="header-bottom"></div>
</div>


    

标签: htmlcsswebsvgvisual-web-developer

解决方案


我将您的svg图像移动到header-top课堂内,并对 css 进行了一些更改。

* {
  margin: 0%;
  padding: 0%;
}

body {
  height: 100vh; /* changed */
}

.header-container {
  width: 100%;
  display: flex; /* added */
  flex-direction: column; /* added */
  background-color: #3c8dad;
  /* max-height: 100vh; can remove */
}

.header-top {
  width: 100%;
  height: 50vh;
  position: relative; /* added */
}
svg {
  width: 100%; /* added */
  height: max-content; /* added */
  position: absolute; /* added */
  bottom: -30px; /* added */
  /* vertical-align: top; can remove */
}
.header-bottom {
  width: 100%;
  height: 50vh;
  background-color: #f5a962;
}
.header-content {
  /* align-items: center; can remove */
  /* width: 45%; can remove */
  height: max-content; /* changed */
  border: 2px red solid;
  padding: 1rem;
  position: absolute;
  left: 50%; /* changed */
  top: 100%; /* changed */
  transform: translate(-50%, -50%);
  z-index: 5;
}
.name {
  font-family: 'Titillium Web', sans-serif;
  font-size: clamp(1.5em, 5vw, 3em); /* changed */
  color: white;
}
.job {
  font-family: 'Noto Serif', serif;
  font-size: clamp(2em, 5vw, 4em); /* changed */
  font-weight: 800;
  color: white;
}
<div class="header-container">
  <div class="header-top">
    <div class="header-content">
      <div class="name">Andrew Saenger</div>
      <div class="job">Front-End Web Developer</div>
    </div>
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320">
          <path
            fill="#F5A962"
            fill-opacity="1"
            d="M0,160L48,160C96,160,192,160,288,138.7C384,117,480,75,576,85.3C672,96,768,160,864,160C960,160,1056,96,1152,80C1248,64,1344,96,1392,112L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"
          ></path>
        </svg>
  </div>
  <div class="header-bottom"></div>


推荐阅读