Why does the content spill out of the div when I add padding?

Can someone help me understand why when I add padding to the "header div," the content overflows the div? For instance, if I set it to 60%, instead of limiting to within the div, it takes up 60% of the entire page. I thought that % would be applied based on the parent's height/width. Any explanations would be appreciated! Thank you :) check out this image for reference

*{
    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="">Videos</a></li>
                <li><a href="">Discover</a></li>
            </ul>
        </nav>
    <div>
        <h1>Explore new places and adventures</h1>
        <input type="text" placeholder="Search for places...">
    </div>
    </header>
    <main>
        <section>
            <h2>Latest posts</h2>
            <article>
                <img src="assets/img/last-posts-1.jpeg" alt="">
                <h3>Climbing the Himalayas</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>

*filler text included due to excessive code*filler text included due to excessive code

Answer №1

To adjust the position of a div to be 30% lower than its container, you can utilize the properties position: relative and top: 30% within a wrapper element surrounding the content. See example below:

.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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

DNN's utilization of the HTML5 Object (deprecated Codebase and Codetype)

Forgive me if this is a silly question, but I have a client who doesn't want their Dot Net Nuke skin created in a .ascx format. I've run into a problem. In Dot Net Nuke, you could simply use: <object id = "Name" codebase = "Name" codetype = ...

How can I extract text from a website without retaining number, dot, and alphabet bullets in the list?

I am currently using Python 2.7.8 for a project involving a website with text displayed in an ordered list format using ol tags. I need to extract the specific text items listed below: Coffee Tea Milk This is an example of the HTML code used on my websit ...

Addressing see-through box styling in CSS

When working on a website using HTML and CSS, I encountered an issue with a transparent box. All elements within this box are also transparent, but I want them to be solid without any opacity. How can I resolve this? .parent { display: inline-block; ...

jQuery Accordian malfunctioning for all elements except the first one in the list

Trying to implement an accordion feature that can be utilized across the entire website. The current logic seems to be partially functional... When I click on any of the accordions, it should open by adding a 'show' class, but this only works for ...

Adjust the colors of two elements by clicking a button's onclick event

As stated in the title, I have a button in the books component. When this button is clicked, the color of a div within the books component and another div in the navbar component should change. - Below is the code for the books component : export class Bo ...

What is the purpose of setting the image border to inline block?

When I added a span element as the circular border to my image, which is placed inside a span tag, I observed that the span tag was being pushed down. By giving the span a display:inline-block property, the issue was resolved. But why does this happen? ...

Setting the maximum width for images across various Bootstrap responsive sizes: a step-by-step guide

I'm struggling with optimizing images of varying sizes in bootstrap to ensure they are responsive without losing quality. Current Image: https://i.sstatic.net/akoNy.jpg Desired Image: https://i.sstatic.net/37VFX.jpg HTML: <div class="row"> ...

Stack two divs together

It may seem silly, but I just can't get it to work. I'm attempting to enclose two divs with different classes in another div, but my current code is automatically closing the divs. There are multiple sets of divs with classes .panel-heading and ...

The second tab on Bootstrap5's tab content feature seems to generate an endless amount of white

I am working on a project where I need to display statistics for both the current month and year. To organize this data, I created a card with tabs for each - one tab for the month and another for the year. By default, the month tab is loaded first. Howev ...

Modify the button's text with jQuery after it has been clicked

I've been struggling to update the text of a button after it's clicked, and I'm encountering an issue where it works in one part of my code but not in another. Could someone please guide me on what might be missing in my code? The first bl ...

After transitioning my Image Modal from an ID to a Class, it appears to not be functioning properly

I recently implemented an image modal on my website using the ID attribute, but it didn't seem to work as expected. After changing the id to a class, now the image modal is not functioning at all and I'm struggling to figure out what went wrong. ...

The index.html file is unable to read the main.css file specifically on Chrome (desktop) browsers

Currently, my website is being hosted by 000webhostapp.com. It seems to be functioning correctly in all browsers such as Mozilla, Opera, and on mobile devices, but there seems to be an issue specifically with desktop Chrome. After checking with the W3C val ...

What's the best way to create a dropdown similar to Github's collaborators menu, but with input text that differs from the dropdown items' text?

In my HTML5 input field, I have a bootstrap dropdown menu that populates the input field with the same text as the selected dropdown item. How can I modify this so that selecting an item updates the input field with different text than what's in the d ...

Achieve a seamless alignment of text/icons at the bottom of a row in BootStrap 4

Looking at this screenshot showing red and blue lines from a Firefox add-on called Pesticide: https://i.sstatic.net/h3Dmy.png Here is the code from the screenshot: <div class="row my-auto"> <p class="h6"> < ...

The functionality of ngClass fails to toggle the value appropriately when utilizing an object within a component

When implementing the code below to toggle the class, everything runs smoothly <p [ngClass]="idPresent ? 'alert alert-success' : 'alert alert-danger'"> Working </p> After changing the value of IdPresent in the c ...

Overflow causes flexbox to become unreactive

Implementing the overflow style on a flexbox element appears to negatively impact its responsiveness. Here is the corresponding code snippet: .projects-wrapper{ display: flex; flex-wrap: wrap; justify-content: center; text-align: center; width ...

Seamless transition of lightbox fading in and out

Looking to create a smooth fade in and out effect for my lightbox using CSS and a bit of JavaScript. I've managed to achieve the fading in part, but now I'm wondering how to make it fade out as well. Here is the CSS code: @-webkit-keyframes Fad ...

Guide on incorporating a rating and review feature into your Windows 8 store app with JavaScript and HTML

Looking for assistance on integrating a rate and review feature into a Windows 8 store app using javascript and html. Specifically, I would like to add this option to both the charm settings and the app bar. When a user clicks on it, they should be directe ...

I am looking to incorporate automatic scrolling functionality into my RSS Feed

I'm in the process of developing an RSS feed for my website. My expertise in JS/jQuery is limited, so any assistance would be greatly appreciated. After utilizing Google's Feed API and creating my own RSS Reader Widget, I realized that it lacked ...

The text is not rendering properly, with some characters being replaced by different ones

RESOLUTION: To fix the issue, eliminate font-family, font-size, and color properties from the parent div. UPDATE: The problem only occurs when I press CTRL + F5. UPDATE 2: After investigating, I found that the culprit is .site-footer with a position:abso ...