#html #css #web #svg #visual-web-developer
Вопрос:
У меня есть svg и два раздела, и я пытаюсь сделать так, чтобы 2 раздела и svg занимали весь экран при входе на сайт, но с помощью svg он становится больше, потому что css не учитывает svg. Есть ли способ, которым я могу заставить css учитывать пространство, занимаемое svg
Я попытался установить максимальную высоту для контейнера-заголовка
Я также пытался настроить vh для 2 дивов, но на меньшем или большем экране они становятся разных размеров, и это не работает
* {
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>
Ответ №1:
Я переместил ваше 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>
Ответ №2:
Флекс, вероятно, работал бы здесь. Попробуйте установить контейнер в положение flex.
.header-container {
display: flex;
}
Затем удалите height
свойство у каждого ребенка и вместо этого разрешите ему расти с помощью flex: 1
.
.header-top {
flex: 1;
}
.header-bottom {
flex: 1;
}
Элемент SVG можно оставить как есть. Он займет все пространство, которое обычно занимает, а другие элементы будут изменены в соответствии с размером внутри контейнера.
Возможно, вам придется включить height: 100vh
.header-container
, если max-height
это не сработает.
Комментарии:
1. Я попробовал это, и это занимает до 100 вч, но тогда мои цвета .верхний заголовок и .нижний заголовок не отображаются
2. О да, вам нужно немного переставить элементы.
Ответ №3:
Вы также можете посмотреть на сетку и построить свой макет на сетке 3×3 не более чем из 2 дочерних элементов (если вы не включаете SVG в фоновый режим).
пример
* {
margin: 0%;
padding: 0%;
}
.header-container {
min-height: 100vh;
display: grid;
grid-template-rows: 1fr auto 1fr;
grid-template-columns: repeat(3, 1fr);
background-image: linear-gradient(to bottom, #3c8dad 50%, #f5a962 50%)
/* note : you can also use grid-area if that helps set children within the grid via grid-area */
}
svg {
grid-column: 1 / -1;
grid-row: 2;
}
.header-content {
grid-column: 2;
grid-row: 1 / span 3;
margin: auto;
}
.name {
font-family: "Titillium Web", sans-serif;
font-size: 40px;
/* calc() or clamp() can be used to set that value */
color: white;
line-height: clamp(1ch, 25vh, 150px);
}
.job {
font-family: "Noto Serif", serif;
font-size: 66px;
/* calc() or clamp() can be used to set that value */
font-weight: 800;
color: white;
margin-top: 3%;
margin-left: 4%;
min-width: 15ch;
/* whatever suits you */
line-height: clamp(1.6ch, 20vh, 100px);
}
.job:first-line {
/* it avoids the first word to be split */
word-spacing: 5em;
/* calc() or clamp() can be used to set that value */
}
<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=Noto Serifamp;family=Titillium Webamp;display=swap" rel="stylesheet">
<div class="header-container">
<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-content">
<h1 class="name">Andrew Saenger</h1>
<p class="job">Front-End Web Developer</p>
</div>
</div>