What is the distance between the different segments in HTML code?

I have the following HTML structure:

<body cz-shortcut-listen="true">
    <div id="panels">
        <section id="sect0" name="lvl0">
            <div id="divLvel0" class="level zero">
                <h2>top<nav><ul><li><a href="#sect1">Languages</a></li><li><a href="#sect2">Proficiency</a></li><li><a href="#sect3">Milestones</a></li><li><a href="#sect4">Details</a></li></ul></nav></h2>
            </div>
        </section>
        <section id="sect1" name="lvl1">
            <div id="divLvel1" class="level one">
                <div id="panel_lvl1">
                    <h2>Languages</h2>
                </div>
            </div>
        </section>
        <section id="sect2" name="lvl2">
        </section>
        <section id="sect3" name="lvl3">
            <div id="divLvel3" class="level three">
                <div id="panel_lvl3">
                    <h2>Milestones</h2>
                    <div id="chart3">
                    </div>
                </div>
            </div>
        </section>
        <section id="sect4" name="lvl4">
            <div id="divLvel4" class="level four">
                <div id="panel_lvl4">
                    <h2>Details</h2>
                    <div id="chart4">
                    </div>
                </div>
            </div>
        </section>
    </div>
</body>

https://jsfiddle.net/thadeuszlay/Lkdo5xv3/2/

Each section is directly adjacent to the other sections without any additional elements in between, but there is a visible gap (shown with green background color) between them.

Despite setting padding and margins of the body to zero and replacing sections with DIVs, the gap persists.

How can I eliminate this gap and make each section touch others seamlessly without any spaces in between?

Answer №1

Start your style by resetting the padding and margin for all elements

* {
margin:0;
padding:0;
}

Once you've done that, feel free to customize the padding and margin for each individual element as needed

Answer №2

This is the source of the green background you are seeing, which is applied to the body element. Visualize your layout as having 'windows' that reveal what's behind them.

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

I recommend always incorporating a CSS Normalizer to align and establish sensible defaults for your elements.

In this particular case, you should include the following:

h2 { margin: 0; padding: 0}
body { margin: 0; }

Also, remember to eliminate padding from sections. Below is an example snippet with those adjustments:

body {
  padding: 0;
  margin: 0;
  background: green;
}
ul li {
  list-style-type: none;
  display: inline-block;
  margin-right: 10px;
}
.zero,
.one,
.two,
.three,
.four {
  padding: 40px 0;
  font-size: 20px;
}
.outer-box {
  background-color: blue;
  padding-top: 100px;
}
.zero {
  background: red;
}
.one {
  background: LightSeaGreen;
}
.two {
  background: LightGreen;
}
.three {
  background: HotPink;
}
.four {
  background: LightSteelBlue;
}
h2 {
  margin: 0;
  padding: 0;
}
<body cz-shortcut-listen="true">
  <div id="panels">
    <section id="sect0" name="lvl0">
      <div id="divLvel0" class="level zero">
        <h2>top<nav><ul><li><a href="#sect1">Languages</a></li><li><a href="#sect2">Proficiency</a></li><li><a href="#sect3">Milestones</a></li><li><a href="#sect4">Details</a></li></ul></nav></h2>
      </div>
    </section>
    <section id="sect1" class="outer-box" name="lvl1">
      <div id="divLvel1" class="level one">
        <div id="panel_lvl1">
          <h2>Languages</h2>
        </div>
      </div>
    </section>
    <section id="sect3" class="outer-box" name="lvl3">
      <div id="divLvel3" class="level three">
        <div id="panel_lvl3">
          <h2>Milestones</h2>
          <div id="chart3">
          </div>
        </div>
      </div>
    </section>
    <section id="sect4" class="outer-box" name="lvl4">
      <div id="divLvel4" class="level four">
        <div id="panel_lvl4">
          <h2>Details</h2>
          <div id="chart4">
          </div>
        </div>
      </div>
    </section>
  </div>
</body>

Answer №3

My earlier comment was inaccurate as your body had a margin, causing the space around the elements.

If you are using a browser with a similar rendering engine, default styles may not display correctly. It appears that was the issue here. Ensure to set the margin-bottom on all elements to 0 or reset margins and padding on all elements and add them as needed.

* {
  margin: 0;
  padding: 0;
}
body {
  background: green;
}
.zero,
.one,
.two,
.three,
.four {
  min-height: 100vh;
  max-height: 1000px;
  font-size: 20px;
}
section {
  padding-top: 100px;
}
.zero,
.sel:nth-child(1) {
  background: red;
}
.one,
.sel:nth-child(2) {
  background: LightSeaGreen;
}
.two,
.sel:nth-child(3) {
  background: LightGreen;
}
.three,
.sel:nth-child(4) {
  background: HotPink;
}
.four,
.sel:nth-child(5) {
  background: LightSteelBlue;
}
#sect0,
#sect1,
#sect2,
#sect3,
#sect4 {
  /* The image used */
  background: blue;
}
<body cz-shortcut-listen="true">
  <div id="panels">
    <section id="sect0" name="lvl0">
      <div id="divLvel0" class="level zero">
        <h2>top<nav><ul><li><a href="#sect1">Languages</a></li><li><a href="#sect2">Proficiency</a></li><li><a href="#sect3">Milestones</a></li><li><a href="#sect4">Details</a></li></ul></nav></h2>
      </div>
    </section>
    <section id="sect1" name="lvl1">
      <div id="divLvel1" class="level one">
        <div id="panel_lvl1">
          <h2>Languages</h2>
        </div>
      </div>
    </section>
    <section id="sect2" name="lvl2">
    </section>
    <section id="sect3" name="lvl3">
      <div id="divLvel3" class="level three">
        <div id="panel_lvl3">
          <h2>Milestones</h2>
          <div id="chart3">
          </div>
        </div>
      </div>
    </section>
    <section id="sect4" name="lvl4">
      <div id="divLvel4" class="level four">
        <div id="panel_lvl4">
          <h2>Details</h2>
          <div id="chart4">
          </div>
        </div>
      </div>
    </section>
  </div>
</body>

Answer №4

Ensure padding and margin are set to 0. Also, update your HTML code as follows:

<section></section><!--
--><section></section>

Alternatively, you can do it in a single line like this:

<section></section><section></section>

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

Completely triggering a forced refresh on a single page application, disregarding cache, service workers, and all other

After experimenting with service workers on my Vue-built website, I made a critical error that has left Safari on my iPhone displaying only a blank page. To troubleshoot the issue, I connected my phone to my Mac and utilized Safari's inspector tool t ...

Update the event binding for a button with jQuery

After binding an event to an icon click, I need to change the id of a button on the page. A new event should be bound to this new id while unbinding the existing event from the old id. Any suggestions on how to achieve this? Upon inspection in Firebug, th ...

Interactive div containing elements that cannot be clicked

http://codepen.io/anon/pen/zxoYBN To demonstrate my issue, I created a small example where there is a link button within a div that toggles another div when clicked. However, I want the link button to not trigger the toggle event and be excluded from the ...

Scrolling to a specific anchor in a React webpage when the URL is opened in

Imagine having a component "Post" that contains multiple components "Comment". How can we make the application automatically scroll down to a specific comment when entering a URL like this: /post/:postId/#commentId The route for postId /post/:postId is a ...

Utilizing Font Awesome icons within a JSON structure

I'm running into a bit of trouble trying to display font awesome icons. The text is written in json using a rakefile, and I'm attempting to insert a font awesome icon within the text. However, I'm facing difficulties because the text is in j ...

Tips for pausing keyframes animation on its final animation frame

Is there a way to halt my animation at the 100% keyframe? What I'm attempting to accomplish is animating these boxes so that when you click on the top one, it moves to the bottom and vice versa. Any suggestions or ideas on how to achieve this? <h ...

enclosing the pre tag within a table cell

Despite using the latest Twitter Bootstrap, I am facing some issues with pre tags inside table cells. When a line is too long, it should create a horizontal scroll, but unfortunately, that does not happen. Check out this example without using the pre tag. ...

Tips for changing an input value when clicked using JQuery

Struggling to create an interactive mind mapping tool using JQuery? One challenge I'm facing is editing the content of a div once it's been set. I've implemented a function that successfully adds text to a div within the (mind-container). H ...

"Div does not perfectly match the height of its parent div when using the 'inherit' value

I have 3 divs inside a parent div, intended to be arranged like so: ______________________________________ | | | HEADER | |______________________________________| ____ ________________ ...

Ways to dynamically update CSS properties (such as changing the color scheme throughout the entire application)

I have a question... If you're interested in conditional styling, the best approach is to utilize either ng-class or ng-style. However... For instance, let's say I'm an admin and I would like to customize the color of my application using ...

SharePoint 2013 on a mobile device does not support scrolling within iframes

Recently, I set up a SharePoint 2013 website with a few iframes. However, when I access the site on my mobile devices (iPad and Android), I am unable to scroll through the entire page by touching within the iframe. I attempted to solve this issue by inclu ...

How can I make the item's image field optional using HTML and CSS?

I am currently working on a project for an e-marketplace. In my database, I have set the ImageField as optional. However, I am facing difficulty in making it optional on the HTML page in the code snippet below: <div class="mt-6 px-6 py-12 bg-gray ...

Custom 404 Page by Google

Could anyone provide information on the property #goog-fixurl for the custom 404 widget? I've searched but can't seem to find any documentation on it. I'm familiar with all the other property styles #goog-fixurl Any assistance would be grea ...

Prevent click event listener from activating if the click results in a new tab or window being opened

Occasionally, when using my web app, I need to prevent click event listeners from activating if the click is meant to open a new window or tab. For instance, in my single page application, there are links to other content. While they function well, there i ...

Developing a Highchart Graph using JavaScript

I am trying to develop a high chart graph. Check out my code on FIDDLE LINK. I have two values, a and b, for the x-axis and y-axis. The issue arises when the difference between a and b is minimal, such as when both are equal (-9). In such cases, the grap ...

Enabling HTML elements in a postback operation

In my user interface, there is a text box where users can input text containing HTML markup. Whenever the page triggers a postback action, it results in an error (500) as the parser interprets the HTML code in the textbox as a hacking attempt. I vaguely ...

Add a line break tag (<br>) specifically for devices with a width less than 768 pixels

I have a lengthy subtitle that includes the following: Self-discovery | Personal development | Health prevention This is how it appears in HTML: <h2>Self-discovery <span class="divider"> | </span> Personal development <span class="d ...

"Struggling with vertical alignment in my scenario, any tips on how to achieve

I'm trying to vertically align my texts while ensuring that the green background div covers the entire height of the red color div. Right now, the green div only covers 90% of the red div and I'm not sure why. Can someone please explain what&apos ...

Behavior of Bootstrap Modal When Closing

I'm encountering an issue with a button labeled Sign Up that is supposed to load content in the form of a Modal from another file. Initially, when I click the button for the first time, everything works smoothly without any errors or warnings. However ...

What could be causing my HTML button to malfunction when attempting to navigate to a different section of the webpage?

Just starting out and developing my website. My hosting provider is IPage, but I'm running into an issue. When I click on a button to switch to another section of the site, it's not working as expected. Here's the code snippet: <button on ...