Skip to content Skip to sidebar Skip to footer

How To Extend Section Width: 100% Using Css

I have a section inside width: 1180px; i want to extend this green color div I want to make width: 100% I have tried using vw but not getting but some extra space is coming. can an

Solution 1:

You need to reset the margin using media query. Initially you have a negative margin but after 1180px it will be a positive one creating the unwanted space. You also don't need to set width using vw unit. Keeping the default width is enough:

.wrapper {
  width: 100%;
  position: relative;
  background: #ccc;
}

.inner {
  width: 1180px;
  margin: 0 auto;
  background: pink;
}

.box1 {
  height: 50px;
  background: red;
}

.box2 {
  height: 50px;
  background: green;
  margin-left: calc(-100vw/2 + 100%/2);
  margin-right: calc(-100vw/2 + 100%/2);
}
@media all and (max-width:1180px) {
  .box2 { 
    margin:0;
  }
}
<divclass="wrapper"><divclass="inner"><divclass="box1"></div><divclass="box2"></div></div></div>

Solution 2:

You could use negative margin - the only problem with this approach is that if the page gets a vertical scroll, this will add a horizontal scroll as 100vw doesn't take into account the 20px caused by the vertical scroll:

body {
  margin: 0;
}

.wrapper {
  width: 100%;
  position: relative;
  background: #ccc;
}

.inner {
  width: 1180px;
  margin: 0 auto;
  background: pink;
}

.box1 {
  height: 50px;
  background: red;
}

.box2 {
  height: 50px;
  background: green;
  width: 100%;
}

@media screen and (min-width:1180px) {
  .box2 {
    margin: 0calc(((100vw - 1180px) / 2) * -1);
    width: auto;
  }
}
<divclass="wrapper"><divclass="inner"><divclass="box1"></div><divclass="box2"></div></div></div>

As I say in my comments, it would be better to just move the green div outside your wrapper

Solution 3:

.wrapper {
  width: 100%;
  position: relative;
  background: #ccc;
}

.inner {
  width: 1180px;
  margin: 0 auto;
}

.box1 {
  height: 50px;
  background: red;
}

.box2 {
  height: 50px;
  background: green;
}
<divclass="wrapper"><divclass="inner"><divclass="box1"></div><divclass="box2"></div></div></div>

Solution 4:

Try this:

.wrapper {
  width: 100%;
  position: relative;
  background: #ccc;
}

.inner {
  width: 1180px;
  margin: 0 auto;
  background: pink;
}

.box1 {
  height: 50px;
  background: red;
}

.box2 {
  height: 50px;
  background: green;
  width: 100%;
}
<divclass="wrapper"><divclass="inner"><divclass="box1"></div><divclass="box2"></div></div></div>

Post a Comment for "How To Extend Section Width: 100% Using Css"