No specified margin at the top of the website's header section

My task was to design the header section of a webpage to resemble this desired webpage look. I started by creating a "header" tag as a container and added a navbar within it. To display the items, I used an unordered list with CSS adjustments for a horizontal layout.

However, there is an unwanted margin at the top of the page (background color changed for visibility). Upon inspection using Chrome Dev tools, I discovered that the issue stemmed from the margin of the <ul tag, which I fixed by setting the margin to zero.

I am puzzled about why the margin appeared outside its parent <nav tag. Shouldn't the width of the <ul (content + margin) be contained within its container? Why does the margin width extend beyond its parent?

body {
    margin: 0px;
}

/* Body Header */

#body-header {
    height: 65vh;
    opacity: 0.8;
    background-image: url(https://ninjasfiles.s3.amazonaws.com/asset_0000000000000020_1549743985_macbook_mouse.jpg);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
}

/* Horizontal Lists */

.horizontal-list {
    list-style: none;
    padding-left: 0px;
}

.horizontal-list li {
    display: inline-block;
    margin: 0px 8px 8px 0px;
}


.horizontal-list li a {
    color: white;
    text-decoration: none;
    transition: color 0.5s, border-bottom 4s;
}

.horizontal-list li a:hover {
    color: lightgrey;
    border-bottom: 1px solid white;
}

.text-center {
    text-align: center;
}
<!DOCTYPE html>
<html>
<head>
    <title> My Resume </title>
    <link rel="stylesheet" href="experiment.css">
</head>
<body>

<header id="body-header">
    <nav>

        <ul class="horizontal-list text-center">
            <li>
                <a href="#" > Home </a>
            </li>
            <li>
                <a href="#about" > About </a>
            </li>
            <li>
                <a href="#skills" > Skills </a>
            </li>
            <li>
                <a href="#experience" > Experience </a>
            </li>
            <li>
                <a href="#education" > Education </a>
            </li>
            <li>
                <a href="#portfolio" > Portfolio </a>
            </li>
            <li>
                <a href="#contact" > Contact </a>
            </li>
        </ul>

    </nav>
<header>
</body>
</html>

Answer №1

By default, the ul element has a margin at the top that can be eliminated using:

.horizontal-list { margin-top: 0; }

The default margin of the <ul> is only present at the top and bottom

.horizontal-list {
    margin-top: 0;
}



/* --- --- Original CSS --- --- */
body {
    margin: 0px;
}

/* Body Header */

#body-header {
    height: 65vh;
    opacity: 0.8;
    background-image: url(https://ninjasfiles.s3.amazonaws.com/asset_0000000000000020_1549743985_macbook_mouse.jpg);
    background-size: cover;
    background-position: center;
    background-attachment: fixed;
}

/* Horizontal Lists */

.horizontal-list {
    list-style: none;
    padding-left: 0px;
}

.horizontal-list li {
    display: inline-block;
    margin: 0px 8px 8px 0px;
}


.horizontal-list li a {
    color: white;
    text-decoration: none;
    transition: color 0.5s, border-bottom 4s;
}

.horizontal-list li a:hover {
    color: lightgrey;
    border-bottom: 1px solid white;
}

.text-center {
    text-align: center;
}
<!DOCTYPE html>
<html>
<head>
    <title> My Resume </title>
    <link rel="stylesheet" href="experiment.css">
</head>
<body>

<header id="body-header">
    <nav>

        <ul class="horizontal-list text-center">
            <li>
                <a href="#" > Home </a>
            </li>
            <li>
                <a href="#about" > About </a>
            </li>
            <li>
                <a href="#skills" > Skills </a>
            </li>
            <li>
                <a href="#experience" > Experience </a>
            </li>
            <li>
                <a href="#education" > Education </a>
            </li>
            <li>
                <a href="#portfolio" > Portfolio </a>
            </li>
            <li>
                <a href="#contact" > Contact </a>
            </li>
        </ul>

    </nav>
<header>
</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

Step-by-step guide to dynamically updating array indexes using jQuery and HTML input fields

const list = []; function updateIndex(index) { list.splice(index, 1); console.log(list); var html = ""; html += "<ul id = 'del'>"; for (var i = 0; i < list.length; i++) { html += "<li>" + list[i] + " <button oncl ...

Design the title attribute for the style area tag

Currently, I have set up an image with various clickable areas on it, and I want to display a description when the user hovers over these areas. My current solution involves adding the descriptions as titles, but I feel like there might be a better way to ...

Sharing Variables with $(document).ready

I need help understanding why this code is not functioning and how I can fix it. Despite my efforts to use namespaces and IIFEs, I am still unable to make it work. $(document).ready(function() { alert (hi); }); $(document).ready(function() { var hi = ...

"Can you guide me on positioning an image logo before text by utilizing the content attribute within the style tag

I am new to coding and experimenting with HTML. I want to display an image before the text "hello html" but for some reason, it is not working. Here is the code I have tried: <head> <style> .img::before { content: url("img/smartphone.png"); ...

Switch to using addresses instead of latitude and longitude coordinates when utilizing the Google Maps API

I am looking to utilize Google Map Markers to indicate the locations where our services are offered. The code I have created using latitude and longitude is functioning properly, however, for my project I need to display city names instead of lat/long ...

How to align several DIVs within a container with an unspecified size

I have several blue rectangles with known dimensions (180 x 180 px) within a container of unknown size. <html> <head> <style> @import url('http://getbootstrap.com/dist/css/bootstrap.css&a ...

Tips for concealing the loader once all pages have been loaded

Within the context of my website development project at , I am utilizing a script for infinite scrolling to consolidate multiple pages into one seamless scrolling experience. I am seeking a solution to hide the loader (the spinning icon) when no additiona ...

Hide all the div elements on the web page and only display one when a button is clicked

I have successfully set up a few buttons that can show and hide divs on click. However, I am wondering if it is possible to hide all other divs when one is showing, and also have "divone" show up on load. Buttons: <button class="btn btn-outline-primar ...

Tips for creating a custom Angular Material modal window that is fully responsive to its parent modal window

Hey there! I've put together a custom modal window in Angular Material by combining the custom modal window example with the Angular Material slider demo. Everything is working well except for the fact that the sliders inside the modal window are not ...

Need assistance posting on Twitter? Utilize PHP for a solution!

I'm attempting to share a user-generated and dynamic URL on Twitter, but I'm having trouble with the encoding... <a href="http://twitter.com/?status=[urlencode('I'd like to borrow this item @neighborrow')].">TWEET THIS REQUE ...

CSS causing navigation bar drop down content to be hidden when hovering

I'm currently working on a drop-down menu and everything seems to be functioning properly. However, when I hide my drop-down block, it doesn't reappear on hover. Can someone help me identify the issue here? HTML-: <div id="nav_wrapper"> ...

The Angulajrjs popup timepicker is not located within the input row

Currently, I am working on creating a popup timepicker within a modal. My main goal is to have the button inside the input field rather than after it. Does anyone know how I can achieve this? Thank you. If you need more context, you can check out my plun ...

Converting the OpenCV GetPerspectiveTransform Matrix to a CSS Matrix: A Step-by-Step Guide

In my Python Open-CV project, I am using a matrix obtained from the library: M = cv2.getPerspectiveTransform(source_points, points) However, this matrix is quite different from the CSS Transform Matrix. Even though they have similar shapes, it seems that ...

JQuery's attempt to disable the 'hover' feature fails to function as intended

I have a styled table where rows change background colors based on odd and even indexes, with a hover effect included. However, I want to disable the hover effect when a row is selected. Here's the current code snippet: .odd{ background: #f7f7f7 ...

I'm having trouble getting jQuery to work properly with Bootstrap buttons

In simple terms, my objective is to have two buttons on a page where "1" is displayed when the first button is pressed and "2" is displayed when the second button is pressed. This functionality works fine with radio inputs, but when I incorporate button la ...

How can you center the body of a webpage while using the zoom in and zoom out features in HTML5?

What is the best way to maintain body alignment while zooming in and out on a web page? <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> body { font-family: 'Merriweather Sans&apos ...

What are the steps to implement a horizontal scroll feature on a website when the scroll width is set to 0?

My goal is to enable users to scroll in the web view similar to how they can drag to scroll on mobile devices Check out my sandbox here for reference I have created a sample sandbox with the following code: <!DOCTYPE html> <html lang="en&qu ...

Having trouble with margins not working after adding the clear property in CSS?

I recently applied clear:left to a div in my code, but when I tried adjusting the top margin, nothing seemed to change. Take a look at the snippet below: <!DOCTYPE html> <html> <head> <title>Float</title> <meta cha ...

Modify CSS using Javascript by targeting the class of ".modalDialog > div {}"

I am currently working on a project that involves creating a Modal Layer using CSS. I have successfully implemented the modal layer, but now I need to figure out how to change the width from the default 400px to something else when I open it with Javascrip ...

Manipulate the text() function within jQuery as the div is being created

The code $('#box').text("hello") will display "hello" as text. But how can I use it when creating a div in a loop? for(i=0; i<3; i++) { description += "<div id='box'>" + text[i] + "</div>"; } Is there a way to s ...