I am using owl carousel 2 and my problem is that I am looking for a solution to maintain images' aspect ratio while users resizing the browser window (responsiveness) based on the graphist's design.
which makes the developing harder, each item is a carousel includes 3 pictures,
I use this function in Scss to make my images 16:9 :
#function aspect($width) {
#return $width * 9 / 16;
}
which is not very interactive and always returns the static size of the image (though I've written media queries in different scales, still it's not very stable)
here is my carousel init code:
$('.owl-carousel').owlCarousel({
nav: false,
dots: true,
items: 1,
center: true,
//autoWidth: true,
loop: true,
});
and my HTML code is :
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-12">
<a href="#Url.Action("Video", "Service")">
<div class="owl-carousel owl-theme">
<div class="item">
<img src="#service.serviceThumbpath" alt="#service.serviceName" title="#service.serviceName" />
</div>
#foreach (var thumb in videoThumbpath)
{
<div class="item">
<img src="#thumb" alt="#serviceName" title="#serviceName" />
</div>
}
</div>
<h5> #service.serviceName</h5>
<div class="details">
#{
_serviceRate = float.Parse(service.serviceRate);
}
<p class="pull-left">#service.VideoCount </p>
</div>
</a>
</div>
is there any solution that I can make it responsive images through owl carousel 2?
thanks a lot
I found the solution, I added these styles to my Scss file:
.outer {
position: relative;
&:before {
display: block;
content: "";
width: 100%;
padding-top: (9 / 16) * 100%;
}
> .inner {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 100%;
}
}
Then I changed my Html like this:
<div class="col-lg-2 col-md-3 col-sm-4 col-xs-12">
<a href="#Url.Action("Video", "Service")">
<div class="owl-carousel owl-theme">
<div class="item outer">
<img src="#service.serviceThumbpath" alt="#service.serviceName" title="#service.serviceName" class="inner"/>
</div>
#foreach (var thumb in videoThumbpath)
{
<div class="item outer">
<img src="#thumb" alt="#serviceName" title="#serviceName" class="inner"/>
</div>
}
</div>
<h5> #service.serviceName</h5>
<div class="details">
#{
_serviceRate = float.Parse(service.serviceRate);
}
<p class="pull-left">#service.VideoCount </p>
</div>
</a>
</div>
and it worked maintaining 16:9 aspect ratio with owl carousel 2 :)
for the grid system of the page, I used This CodePen which was really helpful for my custom sizes.
Something like this should work. Adding an fixed width to container. Then with the img css it should keep its aspect ratio, as you aspect.
.item {
width: 100px;
}
img {
display: block;
width: 100%;
height: auto;
}
<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p>
<div class="item">
<img width="400" height="400" src="#service.serviceThumbpath" alt="#service.serviceName" title="#service.serviceName" />
</div>
Just an example , you can use your own widths and heights. Could also add .item width only at certain #media query.
I set the height and width as below:
.owl-carousel {
height: 100%;
overflow: hidden;
}
.owl-carousel .owl-item img {
display:block;
height:100%;
width: auto;
}
jQuery:
owl.owlCarousel({
items:3,
autoWidth: true,
});
Related
I am new to react.
I a trying to setup a little app. On home page we have carousel.
My home page code is:
render(){
return (
<div className="main-page">
<div className="row full-height">
<div className="common-info col-lg-7">
<div className="row center-lg logo-container">
<LogoImg/>
</div>
<CarouselComponent/>
</div>
<div className="login-form col-lg-5">
<div className="justify-content-end">
</div>
</div>
</div>
</div>
)
}
In scss:
I set up
.main-page {
height: 100vh;
width: 100vw;
}
Everything works fine until adding
with
return (
<div className="carousel-outer">
<Carousel showArrows={true} showThumbs={false} showStatus={false}>
<div>
<p className="row center-lg learn-eng white-text">Выучи английский</p>
<p className="row center-lg easy-fast white-text">Легко и быстро!</p>
<p className="row center-lg go-abroad white-text">Для поездок за границу</p>
<img src={vacation}/>
</div>
<div>
<img src="assets/2.jpeg"/>
</div>
<div>
<img src="assets/3.jpeg"/>
</div>
</Carousel>
</div>
);
I tried to add styles to Carousel:
.carousel .carousel-slider {
max-width: 100%;
max-height: 100%;
}
.carousel-style {
height: auto;
width: auto;
}
But images are still outside parent container. Horizontal an vertical scrolls are there:
How can I fix the issue?
Work Demo
I could not modify your project but I made a sample .
tip:
.container {
width: 100%;
}
.container .carousel .slide img {
max-height: 100vh;
width: auto; /*100vw*/
}
Work Demo
Using Bootstrap 4 I'm trying to achieve an overlay effect with .png image which is also masking a part of bottom area of first section.
The height of .png image is 130px and it also should remain unscaled on mobile devices.
I've tried to use ::after pseudoelements with content as background image on first section, but this gives me a unwanted bottom margin.
See my example here: https://codepen.io/michalwyrwa/pen/EGbxXb
Is there a better way to do it?
CSS:
body {
color: #ecf0f1;
}
.welcome .col {
background-color: #3498db;
height: 50vh;
}
.welcome::after {
content: url(https://files.tinypic.pl/i/00976/nb1abpgxj5x3.png);
display: block;
margin: 0;
padding: 0;
}
.features .col {
background-color: #6ab04c;
height: 50vh;
}
HTML:
<section class="welcome">
<div class="container-fluid">
<div class="row text-center">
<div class="col-12">
<p class="my-3">Welcome text</p>
</div>
</div>
</div>
</section>
<section class="features">
<div class="container-fluid">
<div class="row text-center">
<div class="col-12">
<p class="my-3">Features</p>
</div>
</div>
</div>
</section>
I didn't find the root cause of your problem but I have the solution for you.
.welcome::after {
content: url(https://files.tinypic.pl/i/00976/nb1abpgxj5x3.png);
display: block;
padding: 0;
margin-bottom: -6px;
}
So if I set the height prop on img with vp, h2 (sibling element) has line breaks with respect to the img width (hence which is what I want). But if I set the image height with %, the h2 takes up as much width needed.
Really confused at what vp is doing other than setting image size relative to the browser.
HTML
<div class="container">
<div class="single-container">
<img src="https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ftimedotcom.files.wordpress.com%2F2014%2F11%2F84146440.jpg&w=800&q=85" alt="">
<h2>I'm trying to not make this more than image width.</h2>
</div>
<div class="single-container">
<img src="https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ftimedotcom.files.wordpress.com%2F2014%2F11%2F84146440.jpg&w=800&q=85" alt="">
<h2>I'm trying to not make this more than image width.</h2>
</div>
<div class="single-container">
<img src="https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ftimedotcom.files.wordpress.com%2F2014%2F11%2F84146440.jpg&w=800&q=85" alt="">
<h2>I'm trying to not make this more than image width.</h2>
</div>
</div>
CSS
.container {
display: flex;
}
img {
height: 30vh;
}
JSFiddle Link
https://jsfiddle.net/bqdxze94/9/
Maybe something like this will work:
Solution: display: table; width: 0;
Note: I added some space around each item and made the second image bigger to show the behaviour
View StackBlitz :D
.container {
display: flex;
justify-content: space-around;
}
img {
height: 30vh;
}
.img2 {
height: 60vh;
}
.single-container {
display: table;
width: 0;
}
<div class="container">
<div class="single-container">
<img src="https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ftimedotcom.files.wordpress.com%2F2014%2F11%2F84146440.jpg&w=800&q=85" alt="">
<h2>I'm trying to not make this more than image width.</h2>
</div>
<div class="single-container">
<img class="img2" src="https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ftimedotcom.files.wordpress.com%2F2014%2F11%2F84146440.jpg&w=800&q=85" alt="">
<h2>I'm trying to not make this more than image width.</h2>
</div>
<div class="single-container">
<img src="https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ftimedotcom.files.wordpress.com%2F2014%2F11%2F84146440.jpg&w=800&q=85" alt="">
<h2>I'm trying to not make this more than image width.</h2>
</div>
</div>
I need to align image to div (.md4 - boostrap) horizontal and vertical...
I try do this like:
<div class="front" style="height: 820px; width: 820px; vertical-align: middle;" >
<div class="inner" style="float:none; margin:0 auto;">
<img src="http://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg"/>
</div>
</div>
But it doesn't work. Its still align only in one way.
jsfiddle https://jsfiddle.net/tzvze7ew/
What i do wrong ? And how should its look for work?
You can use the CSS property transform to center both vertically and horizontally.
body {
margin: 0;
}
div:first-of-type {
background-color: blue;
height: 100vh;
}
img {
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="col-md-4">
<div class="main">
<div class="submain">
<div class="front">
<div class="inner">
<img src="http://placehold.it/100x100" />
</div>
</div>
<div class="back">
</div>
</div>
</div>
</div>
The above solution won't support all the browsers. Because transform is CSS3 property.
We can achieve through javascript. What we can do is lets calculate the height of the browser and divide by 2 and minus 50% height of the image.
Below is code
.img {
margin: 0 auto;
}
Javascript
Lets say your image height is 150px.
<script>
$(document).ready(function(){
var height = window.innerHeight;
var new_height = (height/2) - 75; // Half of 150 of 75.
$('.img').css('margin-top', new_height+'px');
});
</script>
<div>
<div class="row">
<div class="col-md-4">
<div class="cta-item cta-1">
<a href="#">
<div class="hover"><p>Serving food fine to fast. Come <span class="arrow">Hungry</span></p></div>
<p class="cta-title">Visit</p>
</a>
</div>
</div>
<div class="col-md-4">
<div class="cta-item cta-2">
<a href="#">
<div class="hover"><p>Serving food fine to fast. Come <span class="arrow">Hungry</span></p></div>
<p class="cta-title">Shop</p>
</a>
</div>
</div>
<div class="col-md-4">
<div class="cta-item cta-3">
<a href="#">
<div class="hover"><p>Serving food fine to fast. Come <span class="arrow">Hungry</span></p></div>
<p class="cta-title">Eat</p>
</a>
</div>
</div>
</div>
</div>
<script>
$(document).ready(cta);
$(window).resize(cta);
function cta() {
var ctaWidth = $('.cta-item').width();
var newHeight = ctaWidth - 40;
$('.cta-item').css( 'height', newHeight );
}
</script>
My question is how to do height = width - 40px for particular element by pure css?
Thanks.
If you need CSS to work in older browsers, too, there is a way utilizing the fact, that vertical padding is always calculated in relation to the parent element’s width. So you can do like this to enforce the height:
// additional styles when Bootstrap css is loaded
.cta-item {
position: relative;
background-color: #ffcccc;
*zoom: 1;
}
.cta-item::before {
width: 0;
float: left;
display: block;
content: ' ';
padding-bottom: 100%; /* makes height match width */
margin-top: -40px; /* value substracted from height */
}
// clearfix
.cta-item::after {
content: ' ';
display: table;
clear: both;
}
Working example: https://jsfiddle.net/7m5vyaqs/1/
It works like a min-height. If you want it to be a max-height, you have to wrap the .cta-item’s contents into a container and position it absolute. Like:
.cta-item > *:first-child {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}