I would like to display the image outside the containers width
, filling up to right edge of the browser.
At the moment I’m doing this with JavaScript, but I feels and looks clumsy, and SEO is unfriendly because of missing ALT-tags and also introduces problems when the css triggers different viewport settings for smaller screens.
Is there a documentation chapter I’ve missed?
My current set up:
<div class="container-fluid screen-image" style="background: url('image.png') no-repeat;" data-position-object="screen1">
<div class="container">
<div class="col-md-6">
my content
</div>
<div class="col-md-6" id="screen1">
</div>
</div>
</div>
Javscript:
$(document).ready(function () {
setScreenPositions();
$(window).on('resize', function() {
setScreenPositions();
});
function setScreenPositions() {
$('.screen-image').each(function() {
var $this = $(this),
$positionObject = $('#' + $this.attr('data-position-object'));
$this.css('background-position', $positionObject.position().left + 'px 60px');
});
}
});
To better illustrate what I’m after, I’ve put my Word drawing skills to the test since I’m not at my own computer at the moment 😉
9
Answers
Try the following HTML structure:
Then use this CSS:
Here is a quick explanation:
position: absolute
on the background image container. We don’t want it to push the content down..screen-image
element..screen-image .image-content
we need to set.col-sm-12
to make it cover the whole screen on mobile. We don’t have 2 columns there so it makes sense to cover the whole screen with the background image.EDIT (img tag)
Example with SEO friendly image tag: http://codepen.io/avladov/pen/bpKvYd
It’s not the prettiest solution but it does work. It’s not as fluid as I would like to see.
http://codepen.io/iMarketingGuy/pen/BKVmMj
CSS
JavaScript
It looks like this happening because you have used container inside container-fluid. Please try replacing your container class with row class. It will solve your problem for sure.
Best luck.
end container before full size width section, dont mix container with container-fluid. you can have multiple container( container and container fluid one after another. Dont use js if you can use css. Use bootstrap grid. Also you can use flex grid for more complicated aligned grids if you dont have to cover older browsers. If you want full screen background, use backgroung with cover property on higher elemrnt ex. on body tag.
If you want to have full width solution under centered grid, put container inside the div with image bg.
html:
css:
also you can put img just like regular content inside the col-xs-6 – it will overflow, and use overflow hidden on ex body to remove the horizontal sliders.
the solution i would choose would be background image set as 50% of the window size in css. – it will be aligned with the grid then.
also you can manage how it works on different resolutions by media queries.
According to Bootstrap Docs, your HTML is incorrect,
.container
right after a.container-fluid
.container
you should have a.row
With the correct HTML and from what I’ve understood, you just need to reset the bootstrap default
padding
given in thecol-
classes.UPDATE
You will need to give a
width
to yourcontainer-fluid
and then useposition:relative
(which bootstrap already have in-col
) and usingcalc()
along with viewport units.Note I use
!important
just for the demo get simpler. in Production avoid using!important
by getting more specific on the CSS rule.I wouldn’t think you need javascript. When it comes to bootstrap you just need to think of all the pieces in a puzzle.
container-fluid
makes it possible for full width screen.Adding
container col-md-6
makes one column with max width. Then acontainer-fluid col-md-6
makes a half page column with no max width for images to the edge. Here is my codepen, a more concise version from the guy above.http://codepen.io/MrNagoo/pen/jqvyeP
Of course, it’s pretty easy to make your own containers with css while still using bootstrap.
First, I’d recommend not use two containers (which doubles the grid gutter width), but use only one container and also use a row in order to have every column aligned as the were initially made.
Something like below:
As you can see, I quite changed a bit how the divs should be ordered.
Now, how I would solve your issue, is putting a div inside that column that you want make an image outside the container.
You can use an img tag instead of the div.
I will show you an example by using the div and applying background-image to it.
We can achieve the desired effect, if we apply the CSS below:
NOTE: We can use viewport width unit to the div inside the column and just take off the grid gutter width by by calculating it the way above.
Explanation: Viewport width (vw) is a unit which calculates the whole width of your current screen, never minding what size does the parent div have.
Can I Use it? vw is now supported in most modern browsers.
http://caniuse.com/#feat=viewport-units
As for the @media query, I assumet in smaller screens than md image should be applied 100%.
You can see this working, in the Codepen below:
http://codepen.io/ArdianPerlaska/pen/dMqWKp
You can use the
vw
unit to set the image width to 50% of the viewport width. You will need to zero out the padding on grid cell which wraps the image. Thevw
unit is supported by all major browsers:i acomplised it with a absolute position element as a sibling of the container.
see codepen:
working pen here