I have a gallery slider in bootstrap. It must be RWD so I must use eg. max-height.
The problem is that the images in this gallery can be different. They can be bigger/smaller - with different ratio.
In this example a .carousel div has height: 450px. To be RWD friendly I want use max-height, but images in this carousel must be 100% to entire div with keeping ratio. Even if some image will be overflowing outsdie this div (or conversely). On this case img must be centered vertically and horizontally (maybe with flexbox?).
When I do max-height the carousel will be jumping because photos can be bigger or smaller. When I set height to carousel she didn't jumping, but it isn't RWD option and some images can be smaller than div (check example below)
Here is example
Try using object-fit: cover.
These will keep your image ration no matter what size you set it. It sort of act like cropping the image. You can add object-position: center too to position your image on the center.
.carousel-inner > .item > img {
object-fit: cover;
object-position: center;
// then your height and/or width
}
Refer on these link for more info: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
Your best bet is really to add your images as background images in your css you can either do this in the actual css stylesheet or just add a style tag to the .item itself. Then add your height and such to the .carousel .item instead of the entire carousel itself along with background position and background size then remove the image tag completely from your .item in your markup. In the following example I added the !Imporatant to the background size and background position. You can remove these if you are going to just state the background in the css stylesheet but if you want you can do somthething like put them in the html markup in a style tag like
<div class="item active" style="background:url('path-to-image');"></div>
If you do this you would need to keep the important statements in your css in order for this to work otherwise there is no need for them.
Here is a revised fiddle Fiddle Demo
So your css would look something like the following:
.carousel .item{
width: 100%;
height: 450px;
overflow: hidden;
background-position:center !Important;
background-size:cover !important;
}
.carousel .item:nth-of-type(1){
background:url('https://pixabay.com/static/uploads/photo/2014/03/29/09/17/cat-300572_960_720.jpg')
}
.carousel .item:nth-of-type(2){
background:url('https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-349976.png')
}
.carousel .item:nth-of-type(3){
background:url('https://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-80082.jpg')
}
/* Indicators list style */
.article-slide .carousel-indicators {
width: auto;
white-space: nowrap;
margin: 0;
left: 50%;
transform: translate(-50%, -50%);
}
/* Indicators list style */
.article-slide .carousel-indicators li {
border: medium none;
border-radius: 0;
float: left;
height: 54px;
margin-bottom: 5px;
margin-left: 0;
margin-right: 5px !important;
margin-top: 0;
width: 100px;
}
/* Indicators images style */
.article-slide .carousel-indicators img {
border: 2px solid #FFFFFF;
float: left;
height: 54px;
left: 0;
width: 100px;
}
/* Indicators active image style */
.article-slide .carousel-indicators .active img {
border: 2px solid #428BCA;
opacity: 0.7;
}
Related
My site has full-width hero images. There are currently served as a CSS background-image with a single image URL that is 2000px wide. Loading one or more of these images on a small device is definitely sub-optimal. I would like to serve a 2000px image, a 1440px image, a 992px image, etc to improve page loading time on mobile.
It seems that I can use -webkit-image-set and image-set, but these only take dpi and 1x, 2x, etc. sizes, so that won't help in this case, as I understand it because this won't ever show smaller images on smaller screens. Alternately, I could serve different images with a media query, but that gets complicated fast if you want to serve images based on viewport and if a device is 2x or 3x. Or I could switch to a img tag instead and use srcset, but I have not been able to find a solution to get a full width image that scales both up and down and fills a fixed height. It seems like this should be possible with object-fit: cover, but no combination of max-width, height, max-height, etc that I have tried has worked.
Here's an example of the CSS background-image that I'm trying to make adaptive:
.hero {
display: block;
min-height: 80px;
width: 100%;
float: left;
position: relative;
padding: 0;
-webkit-background-size: cover;
background-size: cover;
background-position: center center;
background-image: url("https://place-hold.it/500x100");
}
<div style="width: 500px;"> <!-- for this example, is 100% on our page -->
<a class="hero"></a>
</div>
This is on Wordpress with Boostrap.
Here is how you can do that with a picture tag. I like using this tag because i think it gives you more control over the breakpoints.
Look it up here:
https://www.w3schools.com/tags/tag_picture.asp
/* Basic styling for body. not important */
body,
html {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
/* The parent container needs to have position:relative */
.example-container {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
color: #fff;
}
/* The content element needs position:relative, and z-index higher than the BG element */
.example-content {
position: relative;
z-index: 10;
margin: 0;
}
/* The picture element */
.image-full,
.image-full img {
display: block;
/* Absolute position the element with full width/height, and a low z-index */
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
/* Object-fit:cover will make the image fill the space */
object-fit: cover;
object-position: center;
}
<div class="example-container">
<h2 class="example-content">Content goes here</h2>
<picture class="image-full">
<source media="(min-width:1200px)" srcset="https://i.picsum.photos/id/1002/4312/2868.jpg?hmac=5LlLE-NY9oMnmIQp7ms6IfdvSUQOzP_O3DPMWmyNxwo">
<source media="(min-width:500px)" srcset="https://i.picsum.photos/id/10/2500/1667.jpg?hmac=J04WWC_ebchx3WwzbM-Z4_KC_LeLBWr5LZMaAkWkF68">
<img src="https://i.picsum.photos/id/100/2500/1656.jpg?hmac=gWyN-7ZB32rkAjMhKXQgdHOIBRHyTSgzuOK6U0vXb1w">
</picture>
</div>
I am trying to display a div within a background-image. I have tried to do it in the same way that I would do it using an img but it does not work:
WITHIN IMG
HTML
<img class="scale" src="../../../assets/images/van-landing-2.JPG">
CSS
.scale {
max-width: 100%;
height: auto;
width: auto\9;
/* ie8 */
}
This is perfectly working. It shows an image that takes 100% of my screen and the height that should have to be proportional.
Photo
Nevertheless, when I try the same using a div it does not work. It displays nothing to cause it does not set a specific height, so It shows nothing.
WITHIN DIV
HTML
<div class="i-van"></div>
CSS
.i-van {
background-image: url("../../../assets/images/van-landing.JPG");
max-width: 100%;
height: auto;
width: auto\9;
/* ie8 */
}
Photo
How could I do it? I have tried using min-height and it shows it but just the minimum height. I would like to show it all.
The background-image property is used to add a background to an element. This means that the content with in that element is what dictates its size. Additionally, height:auto is interpreted by the elements content.
What you can try is to use height:100% providing that the parent elements also have defined height values. This will stretch the element to the tallest height and scale your background image accordingly.
If you are looking to display the image at the exact size or aspect ratio of the image itself, then the element will need to be defined with the exact width and height of the image.
From a semantics perspective however, you should decide if the image you are displaying is part of the content of your page or a decorative part of the design. In general, you should use a <img /> tag for images that are content and background-image for images that are not.
You can learn more about background-image here
You have to declare a valid height for your background div. As Maneesh has said height:auto takes the element content's height.
They key is to specify a height in vh or px.
After that you can easily place your text div inside it and set it
around with either flexbox or position absolute
Check the snippets! :)
CODE WITH FLEXBOX
body {
margin: 0;
}
.i-van {
display: flex;
align-items: center;
justify-content: center;
background-image: url(https://www.planwallpaper.com/static/images/3865967-wallpaper-full-hd_XNgM7er.jpg);
background-size: cover;
background-position: center;
max-width: 100%;
height: 100vh;
}
#div-inside {
font-size: 100px;
color: white;
}
<div class="i-van">
<div id="div-inside">
HELLO
</div>
</div>
CODE WITH POSITION ABSOLUTE
body {
margin: 0;
}
.i-van {
position: relative;
background-image: url(https://www.planwallpaper.com/static/images/3865967-wallpaper-full-hd_XNgM7er.jpg);
background-size: cover;
background-position: center;
max-width: 100%;
height: 100vh;
}
#div-inside {
position: absolute;
top: 50%;
/* position the top edge of the element at the middle of the parent */
left: 50%;
/* position the left edge of the element at the middle of the parent */
transform: translate(-50%, -50%);
/* This is a shorthand of translateX(-50%) and translateY(-50%) */
font-size: 100px;
color: white;
}
<div class="i-van">
<div id="div-inside">
HELLO
</div>
</div>
I want to center an full screen image vertically.
I can't define image in CSS because the image depends on URL parameters.
<div>
<img src="photo.jpg">
</div>
div {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
If I define my image CSS like this:
div img {
width: 100%;
height: 100%;
}
My image will stretch and be deformed in height to fit on screen.
If I define my image CSS like this (just without defining height):
div img {
width: 100%;
}
My image will not stretch/be deformed, but it will start at top: 0 of the image. What I want is the image to be centered vertically and the overflow of it's height to be hidden.
Basically I want the same behaviour I would get in CSS with background centered:
background: url(photo.jpg) no-repeat center;
background-size: cover;
EDIT: I forgot to mention that CSS object-fit: cover works on this but I'm looking for a more cross-browser solution since this property does not work in every browsers.
Try this css
div {
position: fixed;
top: 50%;
left: 0;
width: 100%;
height: 100%;
-webkit-transform: translateY(-50%);
transform: translateY(-50%);
}
EDIT
also its a bad practice to give the image both height and width. this will always override the aspect ratio of the image and stretch it in some direction.
use this for img
div img {
width: 100%;
}
This will first position the division 50% form top. i.e. the image will now have its topmost part at 50% of the page height then the translate property will move the image upward by 50% of its height essentially centering the image
How about this:
div {
position:fixed;
top:0;
left:0;
width: 100px;
height: 100px;
border:1px solid red;
text-align:center;
line-height:100px;
overflow:hidden;
}
img {
vertical-align:middle;
border:1px solid black;
}
<div>
<img src="https://www.smallbusinesssaturdayuk.com/Images/Small-Business-Saturday-UK-Google-Plus.gif">
</div>
If you allow js, you can do this (assuming the image has id 'img'):
#img {
width: 100%;
position: absolute;
top: 50%;
}
A negative margin top needs to be set using js or jQuery (on resize):
$('#img').css('margin-top', '-'+($('#img').height()/2)+'px');
Hey guys I am working on this responsive horizontal gallery, but now I have a dilemma.
I am trying to get 4 list items on a single shot (viewport).
The images are somehow not stretched to the full height.
Have a look and please tell me if my image size is not fine or something like that.
I need at least 4 images on a single shot.
Thanks
FIDDLE
li {
display: inline-block;
max-height: 100%;
width: 25%;
}
li img {
max-width: 100%;
}
Code looks something like this. Please check fiddle. thanks.
Something like this Link
Here's my answer:
http://jsfiddle.net/SKEL2/14/
I set all the widths to be 100% of the window (INCLUDING html and body), this might be a problem if you have other scrollable elements inside the page.
after that I just positioned the images inside the with position absolute, and gave them a height of 100% (this will stretch them to the bottom of the container, keeping the aspect ratio constant).
To keep it centered I used the
top: 50%;
transform: translateX(-50%);
trick, that will keep an absolutely positioned element in the center of the container (as long as the container has position: relative)
I hardcoded a
min-width 420px;
to avoid the white borders around the images when the window became too small.
Hope this helps
Check this fiddle: http://jsfiddle.net/SKEL2/15/
*{
height:100%;
}
body {
width:100%;
margin: 0;
padding: 0;
}
slides {
width: 100%;
overflow: auto;
margin: 0;
padding: 0;
}
ul {
width:100%;
display: inline-block;
white-space: nowrap;
margin: 0;
padding: 0;
}
li {
display: inline-block;
max-height: 100%;
width: 25%;
background-image:url('http://beautifulsoftwares.com/scroll/img/2.jpg');
background-size:cover;
background-position:50% 50%;
}
li img {
max-width: 100%;
}
background-image:url(''); - Sets an image to the element
background-size:cover; - Property to cover the entire width and height with image
background-position:50% 50% - Centers the image
I'm building a web site based on a theme built with Twitter Bootstrap: http://demo.graphikaria.com/agilis/theme.
Whenever I decrease the width of my browser the background image for the home page carousel becomes distorted.
For the default faded background image the template uses, this isn't a big deal, but if you want to use a clear image or logo instead it will appear distorted or squished.
If you use an IMG tag, it will always end up no wider than its container DIV. Because bootstrap re-sizes fixed images for fluid layout, especially on mobile, the images are squished to the screen width.
An alternative that seems to work well so far for me is to use a <div> tag with a background image.
The class:
.carousel-inner > .item > .carousel-image {
width: 100%;
height: 500px;
text-align: center;
align: center;
}
The Item Code:
#csl-item1 {
background:url(img/carousel_image1.jpg);
background-repeat: no-repeat;
background-position: center top;
}
The Item:
<div class="item active">
<div id="csl-item1" class="carousel-image"> </div>
<div class="container">
<div class="carousel-caption">
<!-- Caption -->
</div>
</div>
</div>
I'd be curious to see if anyone has any bugs with this method tho, so let me know if you think its a bad idea...
The cleanest solution I've found is adding this css to your image in the slide:
object-fit: cover;
overflow: hidden;
You can even just add it inline to the img tag:
<img style="object-fit: cover; overflow: hidden;" class="first-slide" src="/images/beach.jpg" alt="sunny day at the beach">
There's a great write-up that shows examples of CSS3 properties and their impact on image aspect ratios here: http://www.creativebloq.com/css3/control-image-aspect-ratios-css3-2122968
You have:
.carousel .item>img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 500px;
}
Change the height to 100%.
Looking at it in Chrome, the default max-width: 100% seems to not be what you want.
In your CSS, you can add to the already defined rules to get the browser to use the default.
.carousel .item > img { max-width: none }
It's worth noting that you specify min-width: 100% combined with an absolute height, so on large screens (like mine, which is 1080p), it will still distort the image if it gets too wide, thus changing the aspect ratio and again distorting the image.
OK, the best I could do was this…
/* Carousel base class */
.carousel {
height: auto;
margin-bottom: 60px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
z-index: 35;
}
/* Declare heights because of positioning of img element */
.carousel .item {
background:#e64c6c;
border:.5px solid #e7c1af;
overflow:hidden;
padding:3px;
min-height: 500px;
}
.carousel-inner > .item > img {
float:right;
padding: 2px;
padding-left: 3px;
min-width: 500px;
}
.carousel-inner > .item > img:hover {
border: 1px solid #e7c1af;
}
With bootstrap 3.3.7, I could fix this issue by simply removing the 'height' line in carousel.css here:
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
/*height: 500px;*/
}
In my case the image was distorted when the screen size increased. I was working off one of the start Bootstrap free templates (https://startbootstrap.com/) using Bootstrap 4.
Based on the answer above from Ira Herman (and I would comment if I had enough credit points), I ended up with the code below to solve my problem:
.carousel-item {
height: 32rem;
background-color: #777;
}
.carousel-item > img {
position: absolute;
top: 0;
right: 0;
min-width: 100%;
height: 32rem;
object-fit: cover;
overflow: hidden;
object-position: 20% 19%;
}
The object-position x and y coordinates can be tweaked depending how you want the image to increase, decrease. Adjusting the image from the right also helped in my case. I hope that helps.