Both of my divs are set with zero margins, yet there still appears to be a gap separating them

Currently, I am delving into the realm of CSS and web design in an effort to grasp the fundamentals of organizing a basic website.

Ideally, my goal is to eliminate any space between the main navigation and the main content. However, despite setting both div's margins to 0, there persists a visible gap between them that seems unaffected (I initially assumed it was merely a collapsed margin issue).

body {
  background-color: black;
  margin: 0 0 0 0;
}

#main-nav {
  height: 37px;
  width: 100%;
  overflow: hidden;
  background-color: blue;
  margin: 0 0 0 0;
}

#main-content {
  height: 100px;
  width: 100%;
  background-color: red;
  margin: 0 0 0 0;
}

p {
  color: lime;
}
<div id="main-nav">
  <p>This is just a demonstration of my problem.</p>
</div>
<div id="main-content">
  <p>This is just a demonstration of my problem.</p>
</div>

Answer №1

There's some extra space due to the p element tag. By default, browsers add margin and padding to all elements, but you can eliminate this by using an HTML reset.

If you're interested, you can take a look at - HTML5 Reset

Below is your adjusted code without the reset applied:

body {
    background-color: black;
    margin: 0 0 0 0;
}
#main-nav {
    height: 37px;
    width: 100%;
    overflow: hidden;
    background-color: blue;
    margin: 0 0 0 0; 
}
#main-content {
    height: 100px;
    width: 100%;
    background-color: red;
    margin: 0 0 0 0; 
}
p {
    color: lime;
    margin:0
}
<div id="main-nav">
    <p>This is just a demonstration of my problem.</p>
</div>
<div id="main-content">
    <p>This is just a demonstration of my problem.</p>
</div>

Answer №2

Check out this CSS snippet

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

body {
   background-color: black;
}

#main-nav {
  height: 37px;
  width: 100%;
  overflow: hidden;
  background-color: blue;
  margin: 0;
}

 #main-content {
   height: 100px;
   width: 100%;
   background-color: red;
 }

 p {
  color: lime;
 }

Answer №3

Here is the CSS code to include:

.header-nav a,
.main-section {
    padding:0;
}

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

Ways to position buttons at the bottom of a webpage

I am looking to position all buttons at the bottom of the page: https://i.sstatic.net/ikXW5.png This is the code snippet: https://codesandbox.io/s/angular-7-bootstrap-4-3pv0p?from-embed <div class="container-fluid py-2"> <div class="row"> ...

Tips for saving and editing a file with JavaScript

I need a website visitor to input text into a text area on my site. When they submit the form, I want the entered text to be saved in a .txt file located in the same directory as the current web page. I am unsure of how this can be accomplished, and if i ...

What are some solutions for resolving a background image that fails to load?

HTML: `<div class="food-imagesM imagecontainer"> <!--Page info decoration etc.--> </div>` CSS: `.food-imagesM.imagecontainer{ background-image: url("/Images/Caribbean-food-Menu.jpg"); background-repeat: no-repeat; backgroun ...

Ways to include additional bars in your Animated jQuery, CSS, and HTML Bar Graph

I found this awesome website that explains how to create an animated bar graph using HTML, CSS, and JQuery. I want to implement it in my web application to display monthly statistics for each of the 7 divisions in my company. However, I'm having troub ...

Is the "Wait for Document Loaded" function available for Chrome in Blue Prism?

Currently using BP 6.5 with the new Browser Automation mode, it appears that some features found in the HTML mode are missing. The absence of "Wait for Parent Document Loaded" is particularly notable - only "Check Exists" is available. Is there something ...

What is the process for crafting dynamic HTML pages?

My challenge involves developing web pages with a variety of checkbox options that allow users to select at least one item before submitting the form. Once the user selects multiple items and submits the form, I need to display the next page with data bas ...

When clicked, the menu disappears successfully with the "show" functionality, but unfortunately, the "hide" feature is not working as intended

Within my menu, I have a section for portfolios that transforms into a dropdown on desktop hover and mobile click. The issue arises on mobile devices where the first click displays the menu correctly, but subsequent clicks do not trigger any change. I att ...

Activate Input by Clicking on Label in Internet Explorer version 8

I am trying to implement a widget in JQuery colorbox that allows users to load files. My approach involves using an input tag with type='file' and styling it with visibility:hidden. Additionally, I have created two labels that are styled like but ...

Transferring HTML documents using the Express framework

I'm currently developing a small website that includes a subscription list for sending emails using Node and Express. One issue I've encountered is with providing users an unsubscribe link. app.get('/unsubscribe', ... ...); My goal i ...

Obtain the texture from a user upload and apply it to a geometry in three.js

As a beginner in three.js and JavaScript, I am excited about the possibilities and have been creating small Three.js scenes to improve my skills. My ultimate goal is to build a 3D website, but I am currently stuck on how to allow users to upload an image f ...

Trouble with Bootstrap CSS buttons: color consistency problem persists

I am facing an issue with my Bootstrap button, which currently looks like this: https://i.sstatic.net/MWHYP.png The HTML code for the button in question is as follows: <button type="button" class="btn-primary.custom-btn" data-bs-to ...

Overlay text on top of image with fading transparency when hovering over it

Is there a way to make the transparent layer that appears when hovering over an image on a card only cover the image itself and not extend onto the footer of the card? Currently, the image on the card covers most of it, and I want the hover overlay to beh ...

Adjust the borderBottomColor of Material-UI TextField upon completion of input

When working with MUI to create a form, I noticed that the default TextField bottom border is grey, turns blue when focused, and then back to grey when focus is lost. My goal is to prevent it from losing the blue color after filling in the field: https:// ...

Detecting the scroll events of elements with the overflow:hidden property

Looking to synchronize scrolling between two different panels or divs? In one element, there's an overflow: auto while the other has overflow: hidden (trying to mimic a grid with frozen columns). I've managed to sync the scroll when it occurs w ...

What causes Google fonts to fail on Heroku but succeed when used locally?

I am currently using express and heroku to host a prototype of a landing page. While the webpage functions properly when running locally, I encounter font loading issues when accessing it on heroku most of the time... The CSS files are located under /pub ...

Creating a navigation bar that smoothly slides into view from the top

In my Angular app (version 2+), the current code I have is: .header { background: rgba(white, 0); &.fixed-top { background: rgba(white, 1); border-bottom: solid whitesmoke 1px; position: fixed; top: 0; right: 0; left: 0; ...

Tips and tricks for sending variables to an iFrame using jQuery functions

My goal is to dynamically change the source, width, and height of an iframe based on the link that is clicked. Currently, I am able to fire the iframe with hardcoded values using the following code: This is what works: $('.myLink').click(functi ...

What is the best way to arrange map items using justify-content-around for optimal display?

I'm currently iterating through products using the map function, but I'm having trouble getting the justify-content-around property to apply. I am working on a React project with Bootstrap 4 integrated. Below is the code snippet: {props.product ...

Using JavaScript drag and drop feature to remove the dragged element after a successful drop operation

Trying to figure out how to remove a dragged element from the DOM after a successful drop using the native JavaScript drag and drop API. After attempting to listen for the drop event, it seems that it only fires on the element being dropped onto and does ...

What is quicker: loading SVG images or requesting sprite images?

Is there a recommended approach for creating a basic shape and icon that is used in various sections of the website with different colors? Should I opt for SVG or sprites? However, I wonder if there is a universally accepted solution for this issue. ...