Can't get rid of white margin on div's

228 views Asked by At

So, I have tried everything I can think of to get rid of the white margins around everything on my page.

I have put: margin: 0; on everything I can think of and it still does not get rid of it. Any help would be great!

I apologize for the giant wall of code.

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
/* global */

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

html {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  font-family: sans-serif;
}

body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  font-family: "Tahoma", sans-serif;
  font-size: 16px;
  color: #454545;
  background-color: #fff;
}

div {
  margin: 0;
}


/* end global */


/* custom */

.container {
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto;
}

.footer {
  padding: 5px 10px;
  background-color: #428cd9;
}


/* end custom */


/* custom responsive */

.row::after {
  content: "";
  clear: both;
  display: block;
}

[class*="col-"] {
  float: left;
  padding: 15px;
}

.col-1 {
  width: 8.33%;
}

.col-2 {
  width: 16.66%;
}

.col-3 {
  width: 25%;
}

.col-4 {
  width: 33.33%;
}

.col-5 {
  width: 41.66%;
}

.col-6 {
  width: 50%;
}

.col-7 {
  width: 58.33%;
}

.col-8 {
  width: 66.66%;
}

.col-9 {
  width: 75%;
}

.col-10 {
  width: 83.33%;
}

.col-11 {
  width: 91.66%;
}

.col-12 {
  width: 100%;
}


/*end custom responsive */


/* navbar */

ul.topnav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #428cd9;
}

ul.topnav li {
  float: left;
}

ul.topnav li a {
  height: 55px;
  display: inline-block;
  color: #454545;
  padding: 0px 16px;
  line-height: 3.0;
  text-decoration: none;
  text-align: center;
  font-size: 17px;
  transition: 0.3s;
}

ul.topnav li a:hover {
  background-color: #2162a6;
  color: #757575;
}

ul.topnav li.icon {
  display: none;
}


/* end navbar */


/* responsive navbar */

@media screen and (max-width:680px) {
  ul.topnav li:not(: first-child) {
    display: none;
  }
  ul.topnav li.icon {
    float: right;
    display: inline-block;
  }
}

@media screen and (max-width:680px) {
  ul.topnav.responsive {
    position: relative;
  }
  ul.topnav.responsive li.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  ul.topnav.responsive li {
    float: none;
    display: inline;
  }
  ul.topnav.responsive li a {
    display: block;
    text-align: left;
  }
}


/* end responsive navbar */

@media screen and (max-width:680px) {
  .nomobile {
    display: none;
  }
  .yesmobile {
    width: 100%;
  }
}

.header-img {
  min-height: 300px;
  background-image: url(http://thirdshiftdesigns.net/images/cabheader2.png);
  background-repeat: no-repeat;
  background-size: auto 100%;
  background-position: center top;
  /*padding: 40px; (If don't want to set min-height or some image content is there) */
}

.end-header {
  width: 100%;
  height: 15px;
  background-color: #428cd9;
}
<body>

  <div class="col-12">

    <ul class="topnav" id="myTopnav">
      <li><a href="#">Logo</a></li>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact</a></li>
      <li class="icon">
        <a href="javascript:void(0);" style="font-size:15px;" onclick="myFunction()">&#9776;</a></li>
    </ul>

    <div class="row">
      <div class="col-12">
        <div class="header-img">
        </div>
      </div>
    </div>

    <div class="row">
      <div class="col-12">
        <div class="end-header">
        </div>
      </div>
    </div>

  </div>
  <!-- end header -->
  <!-- footer -->
  <div class="col-12">
  </div>

</body>

3

There are 3 answers

1
Arun Kumar Mohan On BEST ANSWER

If you inspect the element, you can find that the unwanted space is because of the following style which applies padding to all elements with the class value containing col-.

[class*="col-"] {
  float: left;
  padding: 15px;
}

Override the style and you can get rid of the unwanted space. Note that this will set the padding to 0 for all the classes whose value contains col-.

[class*="col-"] {
  padding: 0;
}

Or you can only override the padding of .col-12 which will apply padding of 0 to .col-12 while the other classes containing col- will still get a padding of 15px.

.col-12 {
  padding: 0;
}
0
Jon Uleis On

You're wrapping many elements in a .col-12 class. All .col- elements are set in your CSS to contain 15px of padding around the edges. Here's a screenshot from inspecting the page in Chrome Developer Tools where you can see the element highlighted:

enter image description here

2
tamak On

You need to set padding to 0 on the col-12 element.

change the following in your CSS

.col-12 {
    width: 100%;
}

to

.col-12 {
    width: 100%;
    padding: 0px;
}

fiddle example here