What could be causing the overflow of the div element within my header?

I recently embarked on a personal project to create my own portfolio website using HTML, CSS, and JavaScript. As I was working on styling the header section with bright colors for visualization purposes, I encountered an issue where the green box was overflowing, causing unintended spacing between "My Portfolio" and "Front End Developer." I'm seeking assistance in resolving this problem. Below is the code snippet and a reference image:

body{
    margin:0;
    padding:0;
    box-sizing: border-box;
}

.hero-page-container{
    height:100vh;
    width:100vw;
    padding:0px 40px;
    box-sizing: border-box;
}

header{
    display: flex;
    align-items: center;
    height:10%;
    justify-content: flex-end;
    background-color: aqua;
}

.header-title{
    margin-right: auto;
    align-items: center;
    background-color: green;
}

.header-title h1{
    background-color: yellow;
}

.header-title p{
    background-color: orange;
}


ul{
    display:flex;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

header ul li{
    display: inline-block;
    margin-left:40px;
}

header ul li a{
    text-decoration: none;
}

.header-contact-btn{
    text-decoration: none;
    margin-left:40px;
}

.hero-content{
    width:100%;
    background-color: red;
    text-align: center;
    height:50vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <section class="hero-page-container">
        <header>
            <div class="header-title">
                <h1>My Portfolio</h1>
                <p>Front End Developer</p>
            </div>
            <nav>
                <ul>
                    <li><a class="active" href="#home">Home</a></li>
                    <li><a href="#contact">Contact</a></li>
                    <li><a href="#about"/>About</a></li>
                </ul>
            </nav>
            <a href="#" class="header-contact-btn">Contact</a>
        </header>
        <div class="hero-content">
            <div>
                <h1>MY NAME</h1>
                <button>Hire Me</button>
                <button>Let's Talk</button>
            </div>
        </div>
    </section>
</body>
</html>

https://i.sstatic.net/cU1Jz.png

Answer №1

Make adjustments to the layout by removing the 10% height from the header and eliminating margins on the h1 and <p>.

.header-title h1, .header-title p {
  margin: 0;
}

body{
    margin:0;
    padding:0;
    box-sizing: border-box;
}

.hero-page-container{
    height:100vh;
    width:100vw;
    padding:0px 40px;
    box-sizing: border-box;
}

header{
    display: flex;
    align-items: center;
    justify-content: flex-end;
    background-color: aqua;
}

.header-title{
    margin-right: auto;
    align-items: center;
    background-color: green;
}

.header-title h1{
    background-color: yellow;
    margin: 0;
}

.header-title p{
    background-color: orange;
    margin: 0;
}


ul{
    display:flex;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

header ul li{
    display: inline-block;
    margin-left:40px;
}

header ul li a{
    text-decoration: none;
}

.header-contact-btn{
    text-decoration: none;
    margin-left:40px;
}

.hero-content{
    width:100%;
    background-color: red;
    text-align: center;
    height:50vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <section class="hero-page-container">
        <header>
            <div class="header-title">
                <h1>My Portfolio</h1>
                <p>Front End Developer</p>
            </div>
            <nav>
                <ul>
                    <li><a class="active" href="#home">Home</a></li>
                    <li><a href="#contact">Contact</a></li>
                    <li><a href="#about">About</a></li>
                </ul>
            </nav>
            <a href="#" class="header-contact-btn">Contact</a>
        </header>
        <div class="hero-content">
            <div>
                <h1>MY NAME</h1>
                <button>Hire Me</button>
                <button>Let's Talk</button>
            </div>
        </div>
    </section>
</body>
</html>

Answer №2

It has been pointed out by others that the h1 and p elements in your header have default top and bottom margins, causing the overall height to surpass the height:10%; specified for the header.

To address this issue, you can modify the height:10%; property to min-height:10%; in the header rule. By doing so, you will increase the minimum height of your header while preventing it from becoming smaller than 10%.

If you prefer to restrict the header from exceeding 10% height, you would need to reset the margins for the internal elements and reconsider whether they truly fit within that space (which is likely not the case).

body{
    margin:0;
    padding:0;
    box-sizing: border-box;
}

.hero-page-container{
    height:100vh;
    width:100vw;
    padding:0px 40px;
    box-sizing: border-box;
}

header{
    display: flex;
    align-items: center;
    min-height:10%;
    justify-content: flex-end;
    background-color: aqua;
}

.header-title{
    margin-right: auto;
    align-items: center;
    background-color: green;
}

.header-title h1{
    background-color: yellow;
}

.header-title p{
    background-color: orange;
}


ul{
    display:flex;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

header ul li{
    display: inline-block;
    margin-left:40px;
}

header ul li a{
    text-decoration: none;
}

.header-contact-btn{
    text-decoration: none;
    margin-left:40px;
}

.hero-content{
    width:100%;
    background-color: red;
    text-align: center;
    height:50vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <section class="hero-page-container">
        <header>
            <div class="header-title">
                <h1>My Portfolio</h1>
                <p>Front End Developer</p>
            </div>
            <nav>
                <ul>
                    <li><a class="active" href="#home">Home</a></li>
                    <li><a href="#contact">Contact</a></li>
                    <li><a href="#about">About</a></li>
                </ul>
            </nav>
            <a href="#" class="header-contact-btn">Contact</a>
        </header>
        <div class="hero-content">
            <div>
                <h1>MY NAME</h1>
                <button>Hire Me</button>
                <button>Let's Talk</button>
            </div>
        </div>
    </section>
</body>
</html>

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

Column Flow in CSS Grid: Maximizing Auto-Fit - How to Optimize Row Layouts?

Having some trouble understanding how auto-fill and fit work in grid layouts. I am familiar with basic CSS, but new to working with grid layouts. I have a set of checkbox items with labels that I want to display in columns depending on screen size, with c ...

What is the best way to align a form in the center of a page both horizontally and vertically

Currently, this is the HTML code I am working with: <body> <form id="form_login"> <p> <input type="text" id="username" placeholder="username" /> </p> ...

Reorganize divisions using Bootstrap

I am exploring different ways to manage responsiveness through the reordering of divs. While I am aiming for a solution that is highly flexible, any suggestion would be appreciated. Desktop View: https://i.stack.imgur.com/boSOa.png Mobile View: https:// ...

Eliminate repeated entries in a drop-down menu and display them with commas in between

I am working with a list that contains various language combinations: German to English German to Spanish German to Chinese German to French English to Spanish English to French English to Greek English to Portuguese Does anyone have suggestions on how ...

Executing PHP code on button click in HTMLThe process of running a PHP script when

I am currently working on a project that involves detecting facial expressions using Python. However, I need to pass an image to this code through PHP. The PHP code provided below saves the image in a directory. How can I trigger this code using an HTML ...

Ensure the cursor is continually grabbing while moving items within a list using the @angular/cdk/drag-drop functionality

I have an example on stackblitz where I am using @angular/cdk/drag-drop in my project. I am attempting to change the cursor to cursor:grabb and cursor:grabbing when the cursor is over an element and when I drag a picked element. Here is the CSS line I am ...

Utilize the :not() and :hover selectors in Less when styling with CSS

Some of my elements have a specific class called .option, while others also have an additional class named .selected. I want to add some style definition for the :hover state on the .option elements, but only for those without the .selected class. I' ...

What is the best way to adjust column sizes, setting one with a minimum width while allowing the other to expand to accommodate its content?

In the process of creating a user interface, I have included clickable cards that reveal a detailed panel when clicked. The detail panel is set to a minimum width of 900px and should adjust to the remaining space between the cards and itself. The column ...

Saving changes made in HTML is not supported when a checkbox is selected and the page is navigated in a React application using Bootstrap

In my array, there are multiple "question" elements with a similar structure like this: <><div className="row"><div className="col-12 col-md-9 col-lg-10 col-xl-10 col-xxl-10"><span>Question 1</span></div ...

Elements being impacted by the CSS Transition are being pushed

I am facing a CSS issue that I just can't seem to resolve. If you visit and check out the top search bar. On hovering over it, you'll notice it resizes to fit the content width. This is the CSS code responsible for this: .header .search-wrap ...

Avoid automatic background resizing to match the full width of the header text

I've been trying to use CSS to style my <h1> element in a specific way, but it seems the default browser behavior is getting in the way: Despite my efforts, the browser continues to display it like this: Anyone have any suggestions on how I ca ...

Is there a way for me to position my menu on the right side of my website?

I am currently working on a gallery website and facing an issue with the menu placement. I want to position the menu items on the right side of the images, but they keep appearing at the bottom instead. Despite adjusting the width in classes like galleryme ...

powerful enhancer separates the heading into a layout

I have an HTML div element that just isn't behaving right when I resize the screen. The title pretty much sums it up: <div class='serveMessage'><strong>Fun fact:</strong>&nbsp;over 1,259,468 American students fail ev ...

What is the best way to split two sets of radio buttons with the same name into distinct blocks in an HTML form?

For my project, I am working with two sets of radio buttons where the values are stored in a Database. Depending on another result in the HTML form, I need to display one set of radio buttons or the other. The issue arises when using the same name for all ...

The issue of the highlighted row not disappearing persists even after clicking on the adjacent table row

When selecting the gear icon for the menu option in a table row, I would like the background to turn yellow. I attempted the following code to highlight the table row: var view = Core.view.Menu.create({ model: model, menuContext: { ibmm: ibmm }, ...

Creating a scrolling sidebar in Bootstrap 4

Today I learned about CSS Bootstrap 4 and created my template with a sidebar. However, when there are many menus, I want the sidebar to show a scrollbar. I am looking for help on how to make a scrollbar appear on my sidebar template. Below are my HTML and ...

Using Python to interact with website dropdown menus without relying on Selenium

Is there a Python library available that can assist me in opening a web page, selecting options from drop-down lists, and performing clicks without relying on Selenium? ...

Dealing with a section that won't stay in place but the rest of the webpage is

I recently came across the angular-split library while trying to address a specific need. It partially solved my problem, but I still have some remaining challenges. In my setup, I have divided my content into 2 sections using angular-split. The goal is f ...

jQuery functions failing to target dynamically generated elements

I've encountered an issue while attempting to implement my jQuery functions on dynamically created content using the .on API from jQuery. The main objective of the code is to display a specific set of options only when a user hovers over the ".feed_po ...

What are the best practices for avoiding text wrapping around a DIV?

I have scoured this site extensively, searching for a solution to my problem. What I thought was a simple issue has left me frustrated after hours of trying to figure it out on my own. So, I've finally admitted defeat and turned to seek help... Here& ...