I have a jumbotron at the top of my site, with a background image which resizes fine when I resize the browser window.
I have another background image that lays on top of the jumbotron background image. I have been setting different breakpoints for that overlaying image using position: relative; and passing the appropriate pixel amount to top, left, width and height styles.
The reason I do this, is because the overlaying image needs to decrease as the web browser size decreases, and the reason I use top and left is because I want to have the bottom of the overlaying image line up with the bottom of the jumbotron background image at that breakpoint.
I know if I set the overlaying image to use a .make-sm-column(5) and the appropriate offset, it may work, but I am having trouble lining the bottom of the overlaying image to the bottom of the jumbotron background image.
Example html:
<div id="jumbotron">
<div id="jumbotron-man"></div>
</div>
The css just sets the background-image and position to relative, and for each breakpoint it sets the top, left, height, width of the image, I do not currently use .make-sm-column or offset for the overlay. There are other overlays and text divs on the jumbotron, but I thought just asking about this will give me push in the direction and allow me to apply the same to the other elements overlaying the jumbotron.
As seen in the image, you can see how I want it lined up
I feel like I am not taking advantage of bootstrap in this situation, any guidance is appreciated, thank you.
There are some different ways, a common way is to set position:relative; on the parent and position:absolute; on the child, to position the child at the bottom set bottom:0; for the child.
So:
CSS:
#jumbotron {
position: relative;
}
#jumbotron-man {
position: absolute;
bottom: 0;
}
HTML:
<div id="jumbotron">
<div id="jumbotron-man"></div>
</div>
Related
I would like a div with a background-image that keeps the aspect ratio of the image, with a fixed height of 500px and i want no "padding" on the background of that div.
Is this possible to do?
I Can get a div with a fixed height and a background-image that keeps aspect ratio :
<div style="background: url(something.png) 50% 50% / cover #D6D6D6;background-size: contain;background-repeat: no-repeat;height:500px"></div>
This makes the image centered in the middle of the div ( either vertically or horizontally ) but gives some padding to the background of the div ...
Can anybody help me out ?
What you are trying to achieve is not possible using only CSS, you could use JavaScript to detect the width of the image and then set the width of the div to be the same. Or alternatively you could simply remove the background-image property and rather add the image as an img tag into your HTML. If you do that you can display the div as inline-block which will take care of making the div as wide as the width of the image.
body
{
text-align:center;
}
div
{
background-color:#666;
display:inline-block;
}
div img
{
height:500px;
}
<div>
<img src="http://lorempixel.com/200/500" alt="">
</div>
background-size: contain; will always display the whole image (without cutting off anything), thereby leaving some space either vertically or horizontally.
On the other hand, background-size: cover; will fill the whole DIV in a way that the shorter side of the image corresponds exactly to the length or height of the DIV (depending on the relation of the proportions between DIV and image) and the longer one is cut off on the sides or on top and bottom.
If you don't want a distorted image, those are the options you have.
I'm creating a simple one-page layout in Bootstrap, however I'm struggling with the background color of a div on a large screen and on a mobile. When on a large screen, the size of the div is 100vw wide and 100vh high - it's all fine. The problem is when I scale down the viewport, the div doesn't have the desired color underneath all it's contents, the contents overflow into the second div underneath the first one.
How can I make the div have always one color underneath all it's contents no matter what the viewport size is? (width or height of 100% did not help really, there were borders on sides).
Thanks
You mean just add a background color to the div?
CSS
.my__div {
background: red; // add background color
}
HTML
<div class="my__div">Some content here</div>
I'm facing a rather challenging html/css problem. I'm trying to build an image gallery with thumbnails below. The design needs to be fluid and able to scale down for mobile.
The requirements,
Container needs to maintain 4:3 aspect ratio regardless of image
size within
Container max-width 665px and the min-width:300px
Image within needs to align center / middle
When the browser scales down the container to the point in which it meets one of the image sizes, the image must scale down
with the container.
I've successfully been able to get the container to scale correctly with the code below, but the image doesn't maintain vertical middle nor does it scale with the container. The container scales behind the image as if the image is just floating on top of the container.
JS Fiddle Example
http://jsfiddle.net/2kmtmzxv/18/
Example code
<div id="image-container">
<div id="dummy"></div>
<div id="image">
<div>
<img src="http://www.gannett-cdn.com/-mm-/d3038439ef7e9ad854298da49122ea72ad452f6a/c=186-0-2724-1908&r=x513&c=680x510/local/-/media/USATODAY/USATODAY/2014/08/22/1408738143000-2015-Chevrolet-CorvetteZ06-026.jpg"/>
</div>
</div>
</div>
css
#image-container {display:inline-block;position:relative;width:100%;max-width:665px;min-width:300px}
#dummy {padding-top:75%/* 4:3 aspect ratio */}
#image {position:absolute;top:0;bottom:0;left:0;right:0;background-color:grey}
#image div img{display:block;margin:auto;vertical-align:middle;width:100%;max-width:400px}
UPDATE
I was able to get the image to scale within by adding width:100% to the image. I still can't get it to vertically align middle though.
To center the image, on the img css add
positon:absolute; top:0; bottom:0; left:0; right:0;
This will absolutely position the image relative to its closest non static element (which in this case is #image)
Here is a fiddle: http://jsfiddle.net/dzgvh453/
You have this options
Background image instead of actual image
Simply have a thumbnail that at-least have a min-height and width. then use the image as background, center, and no-repeat.
Scalable image width:
Simply have a thumbnail that at-least have a min-height and width, then put your inside it with 100% width.
Your second option is the easiest way to do it. I simply added width:100% to #image div img
http://jsfiddle.net/3e90xxge/#image div img { width: 100%}
containerdiv has two images, I make them display left and right:
.container {
background-image: url('img/left.png'),url('img/right.png');
background-position: left,130px;
overflow: visible;//this line doesn't work
}
currently, right.png is out of the right boundary of the container div and is hidden behind another named div2 which is at the right side of the container div.
How to make right.png image display on top of div2?
see below structure:
[left.png------ 'I am container div'--------- ][right.png-----I am div2 -------]
for some reason, it is not possible to change the css of div2, so I am wondering if it is possible to set some attribute inside container div then right.png can show up.
see below I draw a picture: I set right.png 125px to show, ideally, it would cover the grey triangle.
Can not add padding to the container, can not change position of div2 (because there are other menus share this part, whole part of container div would turn grey if other menu was clicked.)
Is div2 positioned absolute? If so, you can only place .container higher by positioning it absolute as well and setting the z-index higher than div2, or by placing .container after div2 in the DOM.
I have an image that I need to put divs on top of for links. I did this fine when I initially created the website, but now I am tasked with making it responsive, so I can no longer use -top and left values to position the overlay divs because they don't move with the image.
Trying to put the image as a background image so that the position of the overlays can be set and contained within the parent container.
I have tried using the background size property (cover, contain, 100%), but they will not make the div show all 400px of its height.
setting a max height, using auto, or 100% do not work either.
I would use min-height, but then the div would not scale down on mobile devices.
Does anyone know how I can get my parent div to be the full size of the background image?
One recommendation is to position the links using top and left percentages, ie:
#link1 {
position: relative;
top:2%;
left:2%;
}