0

Well, I want to understand why when I padded "header div" the content comes out of div, for example, if I apply 60% instead of applying 60% within div, 60% of the page applies, I believed that % was applied over the height/width of the parent Can anyone explain it to me? thank you :) enter image description here

*{
    margin: 0;
    text-decoration: none;
    list-style: none;
}
header{
    background-image: url("../img/stock-1.jpeg");
    background-position: 50% 25%;
    min-height: 650px;
    height: 60vh;
}
header nav{
    display: flex;
    justify-content: space-between;
    height: 60px;
}
header nav a img{
    width: 15rem;
}
header nav ul{
    display: flex;
    justify-content: center;
    align-items: center;
}
header nav ul li:nth-child(n + 2){
    margin-left: 20px;
}
header nav ul li a{
    color: black;
}
header div{
    background-color: aqua;
    display: flex;
    padding-top: 60%;
    flex-direction: column;
    align-items: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="assets/css/main.css">
</head>
<body>
    <header>
        <nav>
            <a href=""><img src="assets/img/logo_black.png" alt="Logo"></a>
            <ul>
                <li><a href="">Blog</a></li>
                <li><a href="">Vídeos</a></li>
                <li><a href="">Descubre</a></li>
            </ul>
        </nav>
        <div>
            <h1>Descubre nuevos lugares en los que aventurarte</h1>
            <input type="text" placeholder="Buscar lugares...">
        </div>
    </header>
    <main>
        <section>
            <h2>Últimos posts</h2>
            <article>
                <img src="assets/img/last-posts-1.jpeg" alt="">
                <h3>Subiendo el Himalaya</h3>
                <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Commodi earum neque libero officiis reprehenderit repudiandae placeat soluta deserunt harum qui. Fugit, in non repudiandae mollitia nemo tenetur explicabo a sequi?</p>
            </article>
        </section>
    </main>
</body>
</html>

*fill text because it tells me that my pubication contains too much code *fill text because it tells me that my pubication contains too much code

Carlos
  • 1
  • 2
  • 2
    I may just be speaking for myself, but I'm not entirely sure what you're asking. Perhaps re-wording the question or maybe add a screenshot with some arrows or something to help explain what you're referring to? – Chris W. Feb 14 '22 at 22:56
  • @ChrisW. I want the div inside the header to measure half of the vh or 650px as a minim and its content to have a top padding of 70%, but applying it makes the div much higher and invades the main, how do I apply 70% padding inside the div without changing its height? And why does it change? img example -> https://i.stack.imgur.com/w3AgZ.png – Carlos Feb 14 '22 at 23:48
  • To answer "How do I apply padding inside the div without changing its height?", you have to add the "box-sizing: border-box" property to the div, as padding is normally additive to the width and height. – bowlowl Feb 15 '22 at 00:40
  • @bowlowl I had already done it and it does not work :/ – Carlos Feb 15 '22 at 01:09
  • Ah, the reason for this in particular is that the padding percentage applies to width rather than height, regardless of where the padding is. https://stackoverflow.com/questions/4982480/how-to-set-the-margin-or-padding-as-percentage-of-height-of-parent-container – bowlowl Feb 15 '22 at 01:11
  • @bowlowl and how could I fix it if I had the div content at about 70% padding top and it was responsive? I am reading the link and I do not know – Carlos Feb 15 '22 at 01:28

1 Answers1

0

If you want to just move the contents of a div down by 30% of the container's height, then you can use position: relative and top: 30% on a wrapper around the contents. Like so:

.container {
  background-color: red;
  height: 200px;
}

.contents {
  background-color: yellow;
  position: relative;
  top: 30%;
}
<div class="container">
  <div class="contents">
    Blah blah blah
  </div>
</div>
bowlowl
  • 417
  • 2
  • 7