Creating white space around the entire page using CSS is a simple yet effective way to

I'm relatively inexperienced with CSS and HTML and I'm attempting to create a website layout with some white space surrounding it, similar to a border. I have come across many solutions on removing white space, but none on how to add it.

Currently, when I adjust the width, the right side of the element shrinks and fails to expand with the page.

If I use the margin property, only the left side of the element goes inside the page.

Any assistance on this matter would be greatly appreciated.

Below is my code:

HTML -

*, *::before, *::after {box-sizing: border-box;}

* {
  margin: 0;
  padding: 0;
}

#heading {
  width: min(90%, 70.5rem);
  z-index: 1;
  width:90%;
}

.nav-list {
  position:fixed;
  width: 100%;
  display: flex;
  padding: 10px 10px;
  margin: 0 auto;
  justify-content: flex-end;
  align-items: center;
  box-shadow: 0px 0px 10px gainsboro;
  background-color:blanchedalmond;
}

.nav-item{
  list-style: none;
  padding: 10px;
  margin-right: 50px;
  font-family: Arial, Helvetica, sans-serif;
}

.nav-item a {
  text-decoration: none;
  color: black;
  overflow:hidden;
}

.nav-item:first-child{
  margin-right: auto;
}

#imgcontainer {
  overflow:hidden;
}

#bannerimg {
  box-sizing: border-box;
  width: 100%;
  max-width: 100%;
  max-height: 10%;
}

.intro{
  display:grid;
  grid-template-columns: 70% 30%;
  grid-column-gap: 10px;
  grid-row-gap: 10px;
}

.intro > div{
  background-color: grey;
  font-family: Arial, Helvetica, sans-serif;
  padding: 1em;
}

.intro > div:nth-child(odd){
  background: #ddd
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./style.css">
</head>

<body>
    <header>
        <div id="heading">
            <navbar>
                <ul class="nav-list">
                    <li class="nav-item">
                        <h1>Rad Cafe</h1>
                    </li>
                    <li class="nav-item">
                        <a href="./index.html">Home</a>
                    </li>
                    <li class="nav-item">
                        <a href="./about.html">About</a>
                    </li>
                    <li class="nav-item">
                        <a href="./store.html">Store</a>
                    </li>
                </ul>
            </navbar>
        </div>
    </header>
    <div id="imgcontainer" class="container">
        <img id="bannerimg" src="./images/pexels-maria-orlova-4906513.jpg" alt="cafe">
    </div>
    <section class="intro">
        <div id="intro-grid-1" class="grid">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus deserunt obcaecati velit facilis a.
             Facere consequatur eaque beatae repellendus dicta! Maiores nulla recusandae fugit,
              nam illum soluta eius enim tenetur autem quia cupiditate ipsam tempora maxime tempore quos et veniam voluptatibus illo.
               Ratione cum sit, quia, beatae,
                minima sequi dolorem numquam ullam quidem incidunt quasi doloribus soluta vitae fugit amet quam suscipit repellendus delectus natus laudantium! Dolores, 
                quisquam maxime maiores reprehenderit optio at, voluptates aut provident est perferendis laborum rem!
        </div>
            <div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nihil magni odit, id corporis dicta a,
                 inventore impedit quae neque obcaecati nulla itaque saepe quod dolorum. Ducimus nam iusto in nemo.
             </div>
             <div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nihil magni odit, id corporis dicta a,
                inventore impedit quae neque obcaecati nulla itaque saepe quod dolorum. Ducimus nam iusto in nemo.
            </div>
            <div>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nihil magni odit, id corporis dicta a,
                inventore impedit quae neque obcaecati nulla itaque saepe quod dolorum. Ducimus nam iusto in nemo.
            </div>
        
    </section>  
    
</body>
</html>

Answer №1

Why not try adjusting the margins for the entire body?

body {
  margin-top: 10px;
  margin-bottom: 10px;
  margin-right: 20px;
  margin-left: 10px;
}

By doing this, most parts of the page will have space around them. However, I found that changing the margin-right to 20px instead of 10px worked better, possibly due to the online tool I'm using to view the page.

You can also modify the nav-list to alter the size and alignment. Try setting it to 99% instead of 100% to achieve the desired look. Feel free to make further adjustments to achieve your desired appearance.

.nav-list {
    width: 99%;  
}

Answer №2

Update your stylesheet and test it out

 *, *::before, *::after {box-sizing: border-box;}

* {
    margin: 0;
    padding: 0;
}

#heading {
    width: min(90%, 70.5rem);
    z-index: 1;
    width:90%;

    
}
body{margin: 10px 10px 20px 10px;   }

.nav-list {
    position:fixed;
        width: 98.2%;
    display: flex;
    padding: 10px 10px;
    margin: 0 auto;
    justify-content: flex-end;
    align-items: center;
    box-shadow: 0px 0px 10px gainsboro;
    background-color:blanchedalmond;
  
}

.nav-item{
   
    list-style: none;
    padding: 10px;
    margin-right: 50px;
    font-family: Arial, Helvetica, sans-serif;
    

}

.nav-item a {
  
    text-decoration: none;
    color: black;
    overflow:hidden;
}

.nav-item:first-child{
    margin-right: auto;
}


#imgcontainer {

    overflow:hidden;

}


#bannerimg {

    box-sizing: border-box;
    
    width: 100%;
    max-width: 100%;
    max-height: 10%;
    
}

.intro{
    display:grid;
    grid-template-columns: 70% 29%;
    grid-column-gap: 10px;
    grid-row-gap: 10px;
}

.intro > div{
    background-color: grey;
    font-family: Arial, Helvetica, sans-serif;
    padding: 1em;
}

.intro > div:nth-child(odd){
    background: #ddd
}

Answer №3

To incorporate additional spaces within your website layout, you can apply the following CSS code:

body{
   margin: 30px;
}

Answer №4

To begin with, if you're looking to incorporate some extra white space across the entire webpage, including the navigation bar, simply insert the following code: body { margin: 20px; }

Alternatively, if you only want to add white space to specific elements excluding the navbar, you can use: section { padding: 20px; }

Furthermore, it seems like your banner image is being obscured by the navbar. This issue will also need to be addressed.

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

Include the attribute exclusively for the initial division within a group of corresponding elements

Looking to change the appearance of this HTML code without being able to edit it directly? <form method="POST" id="go123pago"> <div align="center" style="width:220px"> <div style="float:left;padding:10px;width:190px"> ...

Place the blockquote in the middle, but maintain the text's alignment to

I recently designed a webpage using Bootstrap and CSS. My main goal is to have my two blockquotes centered on the page while keeping the text left-aligned (which is the default behavior for the selected classes in Bootstrap). To see the full code, take a ...

Send the result of a successful AJAX request to a partial view

I have added these lines of code, where the success function returns data in the form of [object Object], with attributes like Name, Category, and Description. $.ajax({ url: rootUrl + 'Admin/EditRSS', type: "GET", data: { ...

Guide to creating an autoplay feature for a CSS slider

I have created a basic HTML/CSS slider with just 2 slides and I'm looking to set it to automatically switch slides every 7 seconds. Since I'm not well-versed in jQuery or JavaScript, I'm hoping for a CSS-based solution... Could you assist m ...

Adjusting the image to fit the container based on its shorter side, regardless of the image's orientation

I have a square container with a predetermined size. I want to place an image inside the container so that its smaller side fits perfectly with the parent container, while the larger side extends beyond the edges of the container without distorting the ima ...

`Flexbox: Achieving Alignment of Items``

I am looking to align items in a specific way. My goal is to have 4 items in one row, and then the remaining 3 items in the next row, all centered. Despite setting the width at 25%, I am encountering some issues with alignment. Please check out the code ...

Is there a way to position the table to the right of the registration text boxes?

https://i.sstatic.net/Fh9Bk.jpg Is it possible to position the table using <div> elements? ...

Using JQuery functions from other JavaScript files is Simple

When it comes to my CSS, I have taken the approach of creating smaller files for specific functions and then using minification to merge and compress them into one file for easier download. I want to apply the same strategy to my JS Files by logically sep ...

Optimizing Your HTML5 Code: Best Practices

Lately, I've come across some articles on HTML5 best practices and they all seem to emphasize the following guideline: "Prefer using id attribute over name attribute" But is this really the best approach? How can I effectively manage forms in PHP wi ...

CSS styles may not be consistently displayed or may vanish after being initially implemented

The colors and background remain unchanged. In previous projects, everything ended up falling apart Refreshing the page with F5 or CTRL + F5 does not make a difference. When using Open Live Server in VS Code, it initially shows the changes being applied b ...

Tips on setting a background image to a div element

I'm facing an issue with a div setup like this: https://i.sstatic.net/4nuyO.png My goal is to remove a specific section of the circle shape displayed below: https://i.sstatic.net/09IWX.png The desired outcome is to achieve a final shape similar to ...

Dividing the content: A two-column CSS layout

I am currently redesigning a layout that is using tables for a two-column design but I have encountered some issues. <div id="frame"> <div id="leftcol"> <div id="1">blah</div> </div> <div id="leftcol"> <div ...

Disabling all images within a <td> element by selecting them

Here is a sample of my HTML code: <table class="disabled"> <tr> <td> <input blah blah> </td> <td> <img id="reallyLongASP.NetID" etcetc/> </td> </tr> < ...

Activate a monitoring pixel with a click of your mouse

I have a tracking pixel that is typically placed in the body of a "installation success" page: <img src="http://domain.com/adclick.php" width="1" height="1" border="0" /> However, I want to trigger this pixel when someone clicks on a download butto ...

Using Javascript to generate a button that randomly chooses links within the webpage

Looking for help with JavaScript to select a link when a button is clicked on the page? Check out my code and let me know what I'm doing wrong. For the full code, visit: here HTML <!doctype html> <html lang="it" xmlns="http:/ ...

When attempting to overlay CSS text onto an image, the text appears to be hiding behind the

Essentially, what I was attempting to do was overlay some text on top of an image on a website I'm creating. After researching online, I learned that I could achieve this by using position absolute and relative, which worked to some extent. However, n ...

Turn off HTML display for Internet Explorer users

I am looking to implement code that will block certain pages for Internet Explorer users, making it accessible only for Google Chrome and Firefox users. Do you have any suggestions on how I can achieve this or if there are existing solutions available? I& ...

What is the best way to limit an input field to only allow up to two decimal places and a maximum of 10 total

Is there a way to limit an input field to only accept two decimal places and have a maximum of 10 digits? I have implemented a function that is triggered by the onkeypress event and checks the length of the value entered into the input field. I have manag ...

Challenges with jQuery: Appending strings to a dynamically selected element

Hello everyone, I have a question regarding adding the string 'url(" ")' around my Any assistance you can provide would be greatly appreciated. Thank you! $(function() { $('#list li a').hover( function(){ $("#meow").css( " ...

The content at “http://localhost:3000/script.js” could not be accessed because of a MIME type mismatch with the X-Content-Type-Options set to nosniff

I encountered an issue while attempting to transfer all the JavaScript to a separate js file and then adding that js file to the html per usual. The console displayed the following error message: The resource from “http://localhost:3000/public/main.js” ...