Make sure the header spans the full width of the body

I am trying to create a header that spans the entire width of the body, but I am encountering some issues with white space on both sides. I initially attempted to set the width to 100% on the header div, but this did not eliminate the white space. I then experimented with adjusting the position and increasing the width beyond 100%, only to end up with an unwanted horizontal scroll bar and residual white space on the right.

Here is the HTML code for the header:

<div id="header">
      <img id="logo" src="images/logo.png" alt="">
      <div id="nav">
            <a href="index.html">Home</a>
            <a href="about.html">About</a>
            <a href="gallery.html">Gallery</a>
            <a href="contact.html">Contact</a>
      </div>
 </div>

And here is the CSS styling for the header:

#header {
    position: relative;
    left: -9px;
    bottom: 8px;
    padding-top: 5px;
    padding-bottom: 5px; 
    background: url(images/dark_dotted.png);
    width: 105%;
    text-align: center;
    border-bottom: 4px orange solid;
}

#header #logo {
    padding-right: 700px;   
}

#nav {
    position: relative;
    bottom: 30px;   
    font-size: 28px;
    right: -300px;
    font-family: roboto;
}

Answer №1

To eliminate the white space on both sides of your div, start by resetting the CSS for the body tag:

body
{
margin:0;
padding:0;
}

By doing this, you will effectively remove any extra spacing around your div.

Answer №2

One issue arises from the <div id="nav"> where right: -300px; is set (likely to offset the text-align: center; on the header). Depending on your site's needs, a potential solution could be:

body {
    margin: 0;
    padding: 0;
}
#header {
    background: url("images/dark_dotted.png") repeat scroll 0 0 transparent;
    border-bottom: 4px solid orange;
    height: 150px;
    padding: 5px 0;
    width: 100%;
}
#header #logo {
    float: left;
    margin: 20px 0 0 100px;
}
#nav {
    float: right;
    font-family: roboto;
    font-size: 28px;
    margin: 90px 30px 0 0;
}

This approach utilizes floats for div positioning but requires explicit header height specification since floats are excluded during container size calculation.

Answer №3

Your navigation div, with the ID of nav, is causing an issue.

It currently has a width exceeding 1k pixels, extending far beyond the page boundaries.

To resolve this, consider applying the following styles to your nav element:

margin:0;
padding:0;
width:100%;
max-width:100%;

For more details:

The calculated CSS for the nav ID in my browser shows:

bottom: 30px;
display: block;
font-family: roboto;
font-size: 28px;
height: 37px;
position: relative;
right: -300px;
text-align: center;
width: 1326.140625px;

Clearly, the width value is excessively large; consider setting it to a fixed value of 100%.

Answer №4

html, body
{
margin:0;
padding:0;
}

By using the above code, everything will be adjusted within the viewport unless content extends it with absolute positioning or negative margins.

There seems to be excessive padding on items, particularly 700 on the logo. It is advisable to either reduce the padding or make it smaller.

The header width should always be set to 100%;

Instead of moving elements by pixels and using relative positioning, it's recommended to use static (default) position along with padding and margins for better control.

Home About Gallery Contact

CSS:

#header {
background: url(images/dark_dotted.png);
width: 100%;
text-align: center;
border-bottom: 4px orange solid;
}

#nav {
position: relative;
bottom: 30px;   
font-size: 28px;
float:right;
font-family: roboto; //this should be extended.. 'Robosto', serif (or appropriate type)
}

If you are not utilizing align="center" in your HTML, a more efficient way to horizontally center elements is by using margin:auto.

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

PHP: Parsing HTML Tables with Headers and Irregular Body Rows Using Simple HTML Dom Parser

In an HTML table, the data is structured as follows: Header 1 has Row 1, Header 2 has Row 2 and Row 3, while Header 3 has Row 4, Row 5, and Row 6. <table> <thead> <tr> <th>Header 1</th> </tr> </thead& ...

Why isn't the content of the initial tab in a <section> shown when clicking a link in the side-drawer?

View the JSFiddle here. Within this MCVC, a side drawer is implemented (opened by clicking the top left button) that contains three labeled links: Link One, Link Two, and Link Three. Each link has an href attribute value that starts with "#" concatenated ...

The Facebook messenger checkbox plugin does not appear to be displaying correctly

I have integrated the Facebook Messenger Checkbox Plugin into my AngularJS application by following the guide provided at this link. Initially, I was able to successfully display the messenger checkbox plugin on a page. However, upon navigating to another ...

Comparison of getComputedStyle() and cssText functionality between Internet Explorer and Firefox

Take a look at this example to see the issue in action. I'm attempting to access the cssText property of a <div> using window.getComputedStyle(element) (which provides a CSSStyleDeclaration object). While this works smoothly in Chrome, it' ...

Effortlessly glide to a specific div ID upon page load using the URL address

One way to implement smooth scrolling to an anchor on page load is by using jQuery. Here's an example code snippet: $(function(){ $('html, body').animate({ scrollTop: $( $('#anchor1').attr('href') ).offset(). ...

Ways to handle passing multiple parameters in AJAX requests using Laravel 5.2

What is the correct way to send multiple values using AJAX in Laravel? $('#submit_').on('click', function (e) { e.preventDefault(); var form_data = $('#create').serialize(); var form_taxonomy = 'category&a ...

Trouble with CSS: Struggling to apply margins to a line of images in React JS

I've encountered an issue where I can't seem to add margin to the row of images. The margin appears fine without any CSS when viewed on a wide screen, but once I scale it down to mobile view, the margin disappears completely. Even after attemptin ...

Utilizing the Bootstrap grid system for responsiveness is optimized for a first word that is precisely 15 characters

Trying to achieve responsiveness using basic Bootstrap. The layout becomes responsive only when the initial word in a sentence contains more than 15 characters, as illustrated below. https://i.stack.imgur.com/wyErA.png <!-- Headers --> <div clas ...

Is the functionality compatible with all browsers even though <div /> is not recognized as a proper HTML element?

Can the code $("<div />").appendTo($mySelector) be relied upon for safety and cross-browser compatibility, considering that <div /> is not a valid HTML element? I pose this question because it seems much simpler to use than $("<div><d ...

What is the best way to choose a particular radio button from a group of radio buttons using typescript?

Is there a way to automatically select a specific radio button when an item is chosen from a dropdown menu on the webpage using a TypeScript function that is triggered by the dropdown selection? ...

How can we apply position: fixed in print media queries using CSS?

In order to display a footer on every printed page, we have implemented the use of position: fixed. However, one issue that has arisen is that content ends up flowing underneath the footer. Is there a solution to prevent this from happening while still k ...

Is an audio player/playlist necessary for showcasing a mix engineer's skills in their

As a newcomer to the world of web development with some background knowledge from school, I work as a mix engineer and have created a portfolio website. Previously, I utilized Soundcloud and Spotify's API to showcase my mixes/songs, but the external J ...

What is causing the Jquery form submit event to not trigger?

I am attempting to use jQuery to submit a form. I need the form submit event to be triggered when jQuery submits the form. However, the submit event handler is not being called successfully after the form is submitted. Here is the code snippet: <html ...

Issue with highlighting when one string overlaps with another

I am facing a challenge with handling a string that contains Lorem Ipsum text. I have JSON data that specifies the start and end offsets of certain sections within the text that I need to highlight. The approach I am currently using involves sorting the JS ...

Regular Expression for jQuery to match both letters and numbers, including special characters

Is there a way to validate text input using a jquery regex that includes specific characters such as A-Z, a-z, 0-9, !, #, $, £ (preferably all currency characters), %, &, -, and _? If so, how can this be implemented? I have attempted to use the regex pa ...

Inject pure HTML code into a react div

I have extracted raw HTML content stored in the database and I am attempting to display it within a div element. const style = { border: '2px solid #eee', height: '270px', width: '100%', overflow: 'auto&ap ...

Angular 2: Dealing with Missing Image Loading

When viewing an HTML file containing the following code: <div> <img src="New-Google-Logo.png"/> </div> The image file New-Google-Logo.png is expected to load from the same folder as the HTML file. However, after running ng s ...

Creating custom style sheets in Smarty

The formatting of a template processed in Smarty is being lost Previously (viewed in notepad++): <!DOCTYPE html> <html lang='ru'> <head> Now (displayed in the browser): <!DOCTYPE html><html lang='ru'&g ...

Is there a JavaScript function available that can be used to execute a script once content has been loaded via AJAX?

There are multiple inquiries regarding executing a JavaScript function after content is loaded via Ajax. I am familiar with the common solution of running JS after Ajax load completion. If more than 40 pages are loading a page my-Page.php using Ajax, is t ...

What is the best way to change the maxWidthLg of Material UI's .MuiContainer?

I need to remove the max-width that is set in the theme. When I look at it in Chrome and uncheck the option, it functions as desired: @media (min-width: 1280px) .MuiContainer-maxWidthLg { max-width: 1280px; } How can I achieve this? I have attempted ...