Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full screen width div area

Tags:

html

css

What's the best approach to creating a div that will take the full width of the screen, no matter what resolution? I'm trying to add a 'top' bar and bottom 'footer' area with divs that have a black background and styled border that I'd create with a small image and repeat. For some reason my attempts are leading to small spaces on the top and sides of the div?

My markup is similar to:

<div id="top">

Top bar stuff

</div>

<div id="pagewrap">

All the page content

</div>

CSS

#top {

width: 100%;
margin: 0;
padding: 0;
background-color:#000
like image 738
fred randall Avatar asked Nov 26 '11 18:11

fred randall


People also ask

How do I make a Div full screen?

When you say "full-screen", do you mean like full-screen for the computer, or for taking up the entire space in the browser? You can't force the user into full-screen F11; however, you can make your div full screen by using the following CSS div {width: 100%; height: 100%;} This will of course assume your div is child of the <body> tag.

How to make a Div take the full width horizontally?

I do not have to add one for horizontal as div is a block-level element that will take the full width horizontally by default. You can also use position absolute as well as setting all the viewport sides (top, right, bottom, left) to 0px will make the div takes the full screen.

What is the width of the <div> element in HTML?

This <div> element has a width of 500px, and margin set to auto. Note: The problem with the <div> above occurs when the browser window is smaller than the width of the element.

How to create a full-width Div inside a body?

Here's how I would create a full-width, but also full-height div inside a body in a cross-browser (but modern browser only) way: document.getElementById ("pagewrap").innerHTML = "All the page content<br>".repeat (100);


2 Answers

Usually this is the body tag having some paddings and/or margins. Try adding:

body {
  padding: 0;
  margin: 0;
}

If this works, you may want to consider using a normalization stylesheet that fixes this type of issue as well as many other related types of issues.

Extended answer...

The above answers the core issue folks landing here seem to have. But to expand a bit, try to directly answer the original question, and also providing some staple code that I use for things nowaydays:

Here's how I would create a full-width, but also full-height div inside a body in a cross-browser (but modern browser only) way:

document.getElementById("pagewrap").innerHTML = "All the page content<br>".repeat(100);
* {
  box-sizing: border-box; /* not completely needed, yet useful */
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  display: flex; /* or css grid for more intricate layouts */
  flex-direction: column;
}

#top {
  background-color: LightCoral;
  height: 150px;
  border-bottom: 3px solid Crimson;
}

#pagewrap {
  background-color: LightGreen;
  flex-grow: 1; /* make it stretch to the bottom even if little content */
  overflow-y: scroll; /* optional */
}
<div id="top">Top bar stuff</div>
<div id="pagewrap">All the page content</div>
like image 195
Jeroen Avatar answered Sep 22 '22 09:09

Jeroen


Just use top:0; and left: 0; and you can also eliminate padding: 0. Don't use top: 0; for other div except top, use left: 0; for other div for eliminate the left space.

#top {
   width: 100%;
   margin: 0;
   padding: 0;
   background-color:#000
   top: 0;
   left: 0;
}
like image 28
Ariful Islam Avatar answered Sep 20 '22 09:09

Ariful Islam