Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full-width image inside container in Bootstrap 4

I am using Bootstrap 4 and I have a template layout with

<div class="navbar">
  ...
</div>
<div class="container">
  {{content}}
</div>

This works in almost all cases. However, sometimes I want an image in the content to take up the full width, but this is not possible due to the .container which uses left-padding and right-padding.

I could solve this by adding the .container class in each view at the right places instead of in my template layout file, but this will become quite annoying. Especially if I have a CMS where different authors can write articles, and if they want a full-width image in the middle of their article, they have to write raw html to be something like

<div class="container">
  some text
</div>
<div class="full-width-image">
  <img src="" alt="">
</div>
<div class="container">
  more text
</div>
..

How could I solve this problem?

like image 222
Jamgreen Avatar asked Aug 16 '16 10:08

Jamgreen


People also ask

How can I set a full width image in Bootstrap?

“bootstrap full width image” Code Answer's you have to set the width of image to 100%, for that you can use Bootstrap class "w-100". keep in mind that "container-fluid" and "col-12" class sets left and right padding to 15px and "row" class sets left and right margin to "-15px" by default.

Which Bootstrap layout is 100% width?

Containers. Containers are the most basic layout element in Bootstrap and are required when using our default grid system. Choose from a responsive, fixed-width container (meaning its max-width changes at each breakpoint) or fluid-width (meaning it's 100% wide all the time).

Which Bootstrap layout class should be used for full width container?

container-fluid class provides a full width container, spanning the entire width of the viewport.


1 Answers

you can use vw units with negative margin. And it's responsive friendly.

.full-width-image {
   width: 100vw;
   position: relative;
   left: 50%;  
   margin-left: -50vw;
}

.full-width-image img {
  width: 100%;
}

See my fiddle: https://jsfiddle.net/ondrejruzicka/07y0o746/

like image 197
ondrejruzicka Avatar answered Nov 06 '22 03:11

ondrejruzicka