I would like the content of the .container to be centered.
I am using the float:left property to create the gallery. It is also part of the code to resize and crop the thumbnails.
Any way to do this at all?
CSS:
.grid-item {
float: left;
width: 175px;
height: 120px;
overflow: hidden;
}
.grid-item img {
display: block;
max-width: 100%;
border: 2px solid #fff;
border-radius: 5px;
margin: 0 auto;
}
HTML:
<div class="container text-center">
<div id="links">
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
</div>
</div>
You should use flexbox to align your contents and you can read the comments in the CSS as to why which property is meant for. Hope, it helps.
#links{
display: flex; /*Generates a flexbox layout with default flex direction as row */
width: 100%; /* Not really required */
align-items: center; /*Aligns contents vertically */
justify-content: space-around; /*Aligns contents horizontally */
text-align: center; /*Aligns further text in the center */
flex-direction:row; /*By default its row, you can change to column for vertical alignment */
flex-flow:row wrap; /*To wrap items in other row when no more can be fit in the same row*/
}
.grid-item {
/* flex:1; If you need them to grow or shrink flexibly */
width: 175px;
height: 120px;
overflow: hidden;
}
.grid-item img {
display: block;
max-width: 100%;
border: 2px solid #fff;
border-radius: 5px;
margin: 0 auto;
}
<div class="container text-center">
<div id="links">
<a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
<img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
</a>
<a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
<img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
</a>
<a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
<img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
</a>
</div>
</div>
Usually when I run into this situation I will nest everything in the container in a sub-container and tweak that to my liking:
.container .center {
/*
This is just a way of centering the element,
you can use whatever technique.
*/
position:absolute;
top:50%;
left:50%;
transform:translateX(-50%) translateY(-50%);
}
<div class="container text-center">
<div class="center">
<div id="links">
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
</div>
</div>
</div>
Related
This is the actual design I need to achieve, having 3 divs in a row and another 3 in next row
Instead I'm having this kind of div, can anyone help me to achieve the above styling ?
MY HTML :
<div class="category-food">
<div class="food-item" *ngFor="let art of meals">
<img class="card-img-top center" [src]="art.strMealThumb" style="max-width:300px;" role="button">
<div>
<h1 class="content-text">{{art.strMeal}}</h1>
</div>
</div>
</div>
STYLE SHEET :
I have tried the below styling to achieve the expected output but I didn't get the actual output
.category-food {
display: flex;
}
.category-food .food-item {
align-items: center;
margin: 20px;
width: 100%;
border: 1px solid grey;
}
.card-img-top {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
border-radius: 6px;
}
.content-text {
font-size: 24px !important;
text-align: center;
}
So assuming that .food-itemis the div in question that you have 6 of, you can achive this using display: grid; on the parent container of all of those div which would be .category-food
So adding this:
.category-food {
display: grid;
grid-template-columns: auto auto auto;
}
to your CSS (i've also copied and pasted the .food-item div 6 times since you provided code didn't have 6 divs. This is what you get:
.category-food {
display: grid;
grid-template-columns: auto auto auto;
gap: 50px 50px;
margin: 20px;
}
.category-food .food-item {
align-items: center;
width: 100%;
border: 1px solid grey;
}
.card-img-top {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
border-radius: 6px;
}
.content-text {
font-size: 24px !important;
text-align: center;
}
<div class="category-food">
<div class="food-item" *ngFor="let art of meals">
<img class="card-img-top center" [src]="art.strMealThumb" style="max-width:300px;" role="button">
<div>
<h1 class="content-text">{{art.strMeal}}</h1>
</div>
</div>
<div class="food-item" *ngFor="let art of meals">
<img class="card-img-top center" [src]="art.strMealThumb" style="max-width:300px;" role="button">
<div>
<h1 class="content-text">{{art.strMeal}}</h1>
</div>
</div>
<div class="food-item" *ngFor="let art of meals">
<img class="card-img-top center" [src]="art.strMealThumb" style="max-width:300px;" role="button">
<div>
<h1 class="content-text">{{art.strMeal}}</h1>
</div>
</div>
<div class="food-item" *ngFor="let art of meals">
<img class="card-img-top center" [src]="art.strMealThumb" style="max-width:300px;" role="button">
<div>
<h1 class="content-text">{{art.strMeal}}</h1>
</div>
</div>
<div class="food-item" *ngFor="let art of meals">
<img class="card-img-top center" [src]="art.strMealThumb" style="max-width:300px;" role="button">
<div>
<h1 class="content-text">{{art.strMeal}}</h1>
</div>
</div>
<div class="food-item" *ngFor="let art of meals">
<img class="card-img-top center" [src]="art.strMealThumb" style="max-width:300px;" role="button">
<div>
<h1 class="content-text">{{art.strMeal}}</h1>
</div>
</div>
</div>
I want to get something like this using grid in CSS:
How can I reach it?
I made this so far and I have no idea what further..
#gallery {
width: 80%;
height: 200px;
border: 1px solid;
margin-top: 50px;
display: grid;
grid-template-columns: repeat(7, auto);
grid-template-rows: repeat(2, auto);
grid-auto-flow: row;
}
#gallery img {
border: 1px solid black;
height: 100%;
width: 100%;
min-width: 100px;
}
#gallery img:first-child {
grid-column: 3;
grid-row: 1/3
}
#gallery img:nth-child(2) {
grid-column: 7;
grid-row: 1/3
}
<div id="gallery">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="../images/.jpg" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
<img src="" alt="">
</div>
One solution here using 2 fixed rows and auto-generating columns.
Problem: you loose an image every 6, as columns always contain 2 images, but one in 3 only shows one. You could insert a filler every 6 in html to compensate.
I used variables to avoid repeating the same values (image height and width) in several places.
I also made the margin-top for offsets in columns 0 and 1 a calculation for symmetry.
:root {
--img-width: 90px;
--img-height: 120px;
--img-offset: 50px;
}
#gallery {
display: inline-grid;
grid-auto-flow: column;
grid-template-rows: repeat(2, var(--img-height));
grid-auto-columns: var(--img-width);
height: var(--img-height);
overflow: hidden;
border: 1px solid gray; border-left: none;
}
#gallery img {
border-top: none;
width: var(--img-width);
height: var(--img-height);
background: radial-gradient(circle, rgba(255,175,0,1) 0%, rgba(0,106,190,1) 100%);
border: 1px solid gray; border-left: none;
}
#gallery .lost-pic {
background: red; /* you will not see those */
}
/* column 0 */
#gallery img:nth-child(6n + 1),
#gallery img:nth-child(6n + 2) {
margin-top: calc(0px - var(--img-height) + var(--img-offset));
}
/* column 1 */
#gallery img:nth-child(6n + 3),
#gallery img:nth-child(6n + 4) {
margin-top: calc(0px - var(--img-offset));
}
<div id="gallery">
<!--column 0-->
<img src="" alt="">
<img src="" alt="">
<!--column 1-->
<img src="" alt="">
<img src="" alt="">
<!--column 2-->
<img src="" alt="">
<img src="" alt="" class="lost-pic"> <!-- This will be invisible -->
<!--column 0-->
<img src="" alt="">
<img src="" alt="">
<!--column 1-->
<img src="" alt="">
<img src="" alt="">
<!--column 2-->
<img src="" alt="">
<img src="" alt="" class="lost-pic"> <!-- This will be invisible -->
</div>
I am trying to get a grid of cards with a text and an image where the image cover all card spare space. But I can't get the text to show, the text is scrolled off the card which has an overflow: hidden. (https://jsfiddle.net/s8gxuza0/)
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(275px, 1fr));
gap: 24px;
}
.cell {
position: relative;
overflow: hidden;
border-radius: 6px;
padding-top: 100%;
background: red;
}
.link {
text-decoration: none;
color: inherit;
}
.content {
position: absolute;
top: 0px;
width: 100%;
height: 100%;
}
.image {
width: 100%;
height: 100%;
object-fit: cover;
}
.text {
padding: 16px;
}
<div class="grid">
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<img class="image" src="https://images.unsplash.com/photo-1499492568083-9115ab12d0d2?auto=format%2Ccompress&fit=crop&w=400" />
<div class="text">
text 1
</div>
</div>
</a>
</div>
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<img class="image" src="https://images.unsplash.com/photo-1533743409942-b91130480a7a?auto=format%2Ccompress&fit=crop&w=400" />
<div class="text">
text 2<br>
more text
</div>
</div>
</a>
</div>
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<img class="image" src="https://images.unsplash.com/photo-1505569127510-bde1536937bc?auto=format%2Ccompress&fit=crop&w=400" />
<div class="text">
text 3
</div>
</div>
</a>
</div>
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<img class="image" src="https://images.unsplash.com/photo-1531852855896-94f2949b34fe?auto=format%2Ccompress&fit=crop&w=400" />
<div class="text">
text 4<br>
more text
</div>
</div>
</a>
</div>
</div>
I too tryed with display: flex and flex: 1 1 auto but i didn't get it to work either.
Something like this, but keeping the aspect ratio of the whole card (image + text = aspect-ratio 1/1):
For example, as a diagram:
AND THIS FINAL VERSION
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(275px, 1fr));
gap: 24px;
}
.cell {
position: relative;
overflow: hidden;
border-radius: 6px;
padding-top: 100%;
background: red;
}
.link {
text-decoration: none;
color: inherit;
}
.content {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
display: flex;
flex-direction: column;
}
.image {
width: 100%;
height: 100%;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.text {
padding: 16px;
height: fit-content;
box-sizing: border-box;
}
<div class="grid">
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<div class="image" style="background-image: url(https://images.unsplash.com/photo-1499492568083-9115ab12d0d2?auto=format%2Ccompress&fit=crop&w=400);"></div>
<div class="text">
text 1
</div>
</div>
</a>
</div>
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<div class="image" style="background-image: url(https://images.unsplash.com/photo-1533743409942-b91130480a7a?auto=format%2Ccompress&fit=crop&w=400);"></div>
<div class="text">
text 2<br> more text
</div>
</div>
</a>
</div>
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<div class="image" style="background-image: url(https://images.unsplash.com/photo-1505569127510-bde1536937bc?auto=format%2Ccompress&fit=crop&w=400);"></div>
<div class="text">
text 3
</div>
</div>
</a>
</div>
<div class="cell">
<a class="link" href="https://stackoverflow.com/questions/65760464/grid-aspect-ratio-with-text-and-image-covering-all-spare-space" target="_blank">
<div class="content">
<div class="image" style="background-image: url(https://images.unsplash.com/photo-1531852855896-94f2949b34fe?auto=format%2Ccompress&fit=crop&w=400);"></div>
<div class="text">
text 4<br> more text
</div>
</div>
</a>
</div>
</div>
I have a bootstrap 4 header and I'm using flex in it for some custom layout.
Here is the html:
<nav class="navbar">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="logo.png" alt="">
</a>
<div class="box">
<div class="title">TITLE HERE</div>
<div class="flex-inline-container">
<div class="icon"><img src="icon1.png" alt="" /></div>
<div class="icon"><img src="icon2.png" alt="" /></div>
<div class="icon"><img src="icon3.png" alt="" /></div>
<div class="icon"><img src="icon4.png" alt="" /></div>
<div class="icon"><img src="icon5.png" alt="" /></div>
</div>
</div>
</div>
</nav>
And the CSS:
.box {
display: flex;
border-right: 2px solid #bfbfbf;
width: min-content;
flex-direction: column;
}
.box .flex-inline-container {
display: inline-flex;
}
.box .icon {
margin-right: 20px;
cursor: pointer;
}
.box .title {
position: relative;
left: 7px;
font-size: 1.40rem;
font-weight:300;
color: #727272 !important;
}
My issue is that the "flex-inline-container" div is going all the way to the right and I need it to be on the left.
How can I make it go to the left?
The .container-fluid class of bootstrap has a Justify-content:space-between property. Which distributes items evenly (The first item is flush with the start,
the last is flush with the end).
In your case the <a class="navbar-brand"><a> will be moved to first and elements inside <div class="box"></div> moved to last.
If you want to override Justify-content property of .Container-fluid and move to left, Then add a .justify-content-start along with .Container-fluid.
.box {
display: flex;
border-right: 2px solid #bfbfbf;
width: min-content;
flex-direction: column;
}
.box .flex-inline-container {
display: inline-flex;
}
.box .icon {
margin-right: 20px;
cursor: pointer;
}
.box .title {
position: relative;
left: 7px;
font-size: 1.40rem;
font-weight:300;
color: #727272 !important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<nav class="navbar">
<div class="container-fluid flex-row-reverse">
<a class="navbar-brand" href="#">
<img src="logo.png" alt="">
</a>
<div class="box">
<div class="title">TITLE HERE</div>
<div class="flex-inline-container">
<div class="icon"><img src="icon1.png" alt="" /></div>
<div class="icon"><img src="icon2.png" alt="" /></div>
<div class="icon"><img src="icon3.png" alt="" /></div>
<div class="icon"><img src="icon4.png" alt="" /></div>
<div class="icon"><img src="icon5.png" alt="" /></div>
</div>
</div>
</div>
</nav>
I have this code that will display image carousel.
For desktops, I want to display 3 thumbnails per row;
For mobiles, just one or two thumbnail per row horizontally.
The code in working fine in desktopview but in mobileview how do i display in horizontally with only one or two image and use the indicator to see next image ?
i tried using this..but is not working
<div class="col-sm-6 col-md-4">
html
<div class="container">
<div class="row">
<h2>Media Slider Carousel BS3</h2>
</div>
<div class='row'>
<div class='col-md-8'>
<div class="carousel slide media-carousel" id="media">
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
<div class="col-md-4">
<a class="thumbnail" href="#"><img alt="" src="http://placehold.it/150x150"></a>
</div>
</div>
</div>
</div>
<a data-slide="prev" href="#media" class="left carousel-control">‹</a>
<a data-slide="next" href="#media" class="right carousel-control">›</a>
</div>
</div>
</div>
</div>
CSS
/* carousel */
.media-carousel
{
margin-bottom: 0;
padding: 0 40px 30px 40px;
margin-top: 30px;
}
/* Previous button */
.media-carousel .carousel-control.left
{
left: -12px;
background-image: none;
background: none repeat scroll 0 0 #222222;
border: 4px solid #FFFFFF;
border-radius: 23px 23px 23px 23px;
height: 40px;
width : 40px;
margin-top: 30px
}
/* Next button */
.media-carousel .carousel-control.right
{
right: -12px !important;
background-image: none;
background: none repeat scroll 0 0 #222222;
border: 4px solid #FFFFFF;
border-radius: 23px 23px 23px 23px;
height: 40px;
width : 40px;
margin-top: 30px
}
/* Changes the position of the indicators */
.media-carousel .carousel-indicators
{
right: 50%;
top: auto;
bottom: 0px;
margin-right: -19px;
}
/* Changes the colour of the indicators */
.media-carousel .carousel-indicators li
{
background: #c0c0c0;
}
.media-carousel .carousel-indicators .active
{
background: #333333;
}
.media-carousel img
{
width: 250px;
height: 100px
}
/* End carousel */
js
$(document).ready(function() {
$('#media').carousel({
pause: true,
interval: false,
});
});