I am new to using css grid. I am attempting to make a responsive grid of images that differ in sizes. The example that I came up with works perfectly but it seems like I need to define a size repeat(auto-fit, minmax(84px, max-content)); instead of something like repeat(auto-fit, minmax(min-content, max-content)); if I use auto-fit. This makes the a href attribute extend the 84px even if the image itself isn't 84px.
Is there a way to use auto-fit while also having the rows and columns fit perfectly like they do now, without having to define the size as 84px? Or is there a better way to do this while keeping it simple?
a {
border: 2px dashed pink;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(84px, max-content));
align-items: center;
grid-gap: 2rem;
}
.ef {
background: url(https://fakeimg.pl/84x84/) no-repeat 0px 0px;
width: 84px;
height: 84px;
}
.eft {
background: url(https://fakeimg.pl/16x16/) no-repeat 0px 0px;
width: 16px;
height: 16px;
}
.ytt {
background: url(https://fakeimg.pl/44x44/) no-repeat 0px 0px;
width: 44px;
height: 44px;
}
<p>
The grid is fine but the a href extends too far for some reason.
</p>
<section class="grid">
<a href="/ytt">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
</section>
Simply change justify-items. By default items will get stretched to fill the grid area since the default alignment is stretch. You already changed this on the cross axis using align-items. We do the same for the main axis using justify-items. You can notice that both properties accept stretch as value
a {
border: 2px dashed pink;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(84px, max-content));
align-items: center;
justify-items:center;
grid-gap: 2rem;
}
.ef {
background: url(https://fakeimg.pl/84x84/) no-repeat 0px 0px;
width: 84px;
height: 84px;
}
.eft {
background: url(https://fakeimg.pl/16x16/) no-repeat 0px 0px;
width: 16px;
height: 16px;
}
.ytt {
background: url(https://fakeimg.pl/44x44/) no-repeat 0px 0px;
width: 44px;
height: 44px;
}
<p>
The grid is fine but the a href extends too far for some reason.
</p>
<section class="grid">
<a href="/ytt">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
</section>
Related
This question already has answers here:
Why are flex items not wrapping?
(2 answers)
Closed 1 year ago.
So am new to learning/using the "display: flex" property.
*{
margin: 10px;
padding: 0;
-webkit-margin:0;
-webkit-padding:0;
}
.container {
display: flex;
}
.imagebox {
position: relative;
flex: 1;
margin: 15px;
}
.imagebox img {
max-height: 200px;
max-width: 100%;
position: absolute;
}
<div class="container">
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
</div>
Here are 4 boxes of a certain size but if I insert one more, the size gets smaller as it tries to fit the 5th box in the same line, how to get the 5th box in the next line?
*{
margin: 10px;
padding: 0;
-webkit-margin:0;
-webkit-padding:0;
}
.container {
display: flex;
}
.imagebox {
position: relative;
flex: 1;
margin: 15px;
}
.imagebox img {
max-height: 200px;
max-width: 100%;
position: absolute;
}
<div class="container">
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
</div>
I want the images to automatically go on to the next line when it reaches the end of the main container, I have tried reducing the width as well, it didn't work.
It's probably something very simple but I can't really seem to find the way, also am not fully accustomed with all the flexbox properties
change width of .imagebox {} accordingly
*{
padding: 0;
-webkit-margin:0;
-webkit-padding:0;
}
.container {
display: flex;
flex-wrap: wrap;
}
.imagebox {
position: relative;
margin: 15px;
width: 20%;
}
.imagebox img {
max-height: 200px;
max-width: 100%;
}
<div class="container">
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
</div>
You can use the flex-wrap property:
.container {
display: flex;
flex-wrap: wrap;
flex-direction: row;
}
Flexbox doesn't play nicely with absolutely-positioned children. By taking the images out of the normal flow, they no longer take part in the flexbox calculations. The remaining imagebox element is not wide enough to trigger wrapping.
.container {
display: flex;
flex-wrap: wrap;
gap: 15px;
}
.imagebox {
flex: 0 0 calc((100% - 45px) / 4);
}
.imagebox img {
max-height: 200px;
max-width: 100%;
}
<div class="container">
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
<div class="imagebox">
<a href="#" class="images">
<img class="active" src="https://fakeimg.pl/350x200/000/?text=Image" />
</a>
</div>
</div>
Please note that to simplify the code I removed some list items but they all have class="footerstyle"
This is what the list currently looks like:
This is what I want it to look like however they have to be in the centre of a bootstrap grid:
This is my code:
.footer {
position: absolute;
bottom: 0;
width: 100%;
height: 50px;
}
footer {
position: relative;
}
.list-unstyled {
margin-bottom: 20px;
}
.footerstyle {
font-size: 18px;
text-align: center;
}
.footerstyle:hover {
border-radius: 25px;
text-align: center;
border-bottom:solid 1px transparent;
}
.dropright {
flex-wrap: wrap;
}
.col-md {
height: 200px;
display: flex;
}
<div class='footer'>
<footer class="page-footer font-small pt-4">
<div class="row">
<div class="col-sm">
<ul class="list-unstyled">
<div class="col-sm">
</div>
<div class="col-sm">
<li class="footerstyle">
<a>Homepage</a>
</li>
</div>
<div class="col-sm">
<div class="dropdown footerstyle">
<a class="footer-dropdown dropdown-toggle" id="dropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Team
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" >Team members</a>
<a class="dropdown-item" >Our Story</a>
<a class="dropdown-item" >Join the team</a>
</div>
</div>
</div>
</ul>
</div>
<div class="col-sm socialmedia">
<a href="">
<img src="linkedin.png">
</a>
<a href="">
<img src="twitter.png">
</a>
</div>
<div class="col-sm cookies">
A
</div>
</div>
<div class="footer-copyright text-center py-3">
<a> B </a>
</div>
</footer>
</div
The other items in the footer are in the right place and don't have this problem as they aren't list items. How can I make it so that the list items align to the left whist still being on the centre of the bootstrap grid?
To align text left just add this code to your css:
.page-footer a {
text-align:left;
}
I am using an image gallery. Each image has different sizes, masonry style. How can I place a button inside each image in the same position with different sizes? I want to put the button in the upper right corner.
css
.EyeG{
cursor: pointer;
position: absolute;
background-color: white;
background-image: url("/assets/eye.svg");
background-position: 13px 13px;
background-repeat: no-repeat;
height: 15%;
width: 15%;
margin-top: -329px;
right: 13px;
border-radius: 8px;
opacity: 1;
}
HTML
<ngx-masonry [options]="masonryOptions" [useImagesLoaded]="true">
<ngxMasonryItem *ngFor="let p of products" class="masonry-item">
</div>
<img [src]="imagMap.get(p.id)">
<figcaption class="EyeG"></figcaption>
</ngxMasonryItem>
</ngx-masonry>
Try this
.masonry-item { position: relative }
.EyeG{
cursor: pointer;
position: absolute;
background-color: white;
background-image: url("/assets/eye.svg");
background-position: 13px 13px;
background-repeat: no-repeat;
height: 15%;
width: 15%;
border-radius: 8px;
opacity: 1;
top:50%;
left:50%;
transfomrm: translate(-50%, -50%);
}
Attached codepen link, please have look.
Let me know if you not got your solution here. Will help you
<div class="header">
<h1>Responsive Masonry.js Grid</h1>
<p>A horizontal image card grid built with masonry.js</p>
</div>
<div class="container">
<div class="grid-sizer"></div>
<div class="gutter-sizer"></div>
<a class="item" href="#">
<img src="https://placehold.it/350x300">
<sapn class="EyeG"></span>
</a>
<a class="item" href="#">
<img src="https://placehold.it/350x200">
<sapn class="EyeG"></span>
</a>
<a class="item" href="#">
<img src="https://placehold.it/300x290">
<sapn class="EyeG"></span>
</a>
<a class="item" href="#">
<img src="https://placehold.it/400x300">
<sapn class="EyeG"></span>
</a>
<a class="item" href="#">
<img src="https://placehold.it/350x350">
<sapn class="EyeG"></span>
</a>
<a class="item" href="#">
<img src="https://placehold.it/400x300">
<sapn class="EyeG"></span>
</a>
<a class="item" href="#">
<img src="https://placehold.it/400x500">
<sapn class="EyeG"></span>
</a>
</div>
https://codepen.io/pgurav/pen/zYYXoNj
This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Closed 3 years ago.
My probleme is difficult to explain but i will try.
I develop a web site. For my nav-bar i have 3 div in a header (burger-menu / loogo / social-networks). I use flex for display with justify-content: between.
My probleme is i want to center my second div by the viewport and not by my two other divs. Someone know how to do ?
(Please excuse my bad English)
<header>
<div class="burger">
<img src="assets/img/cutlery.svg" alt="icône couteau cuillère burger menu">
</div>
<div class="logo">
<img src="assets/img/bio.svg" alt="">
</div>
<div class="network">
<a href="#">
<img src="assets/img/facebook.svg" alt="">
</a>
<a href="#">
<img src="assets/img/linkedin.svg" alt="">
</a>
<a href="#">
<img src="assets/img/instagram.svg" alt="">
</a>
<a href="#">
<img src="assets/img/france.svg" alt="">
</a>
<a href="#">
<img src="assets/img/english.svg" alt="">
</a>
</div>
</header>
You can add
position: absolute;
width: 100%;
display: flex;
justify-content: center;
to your second div. Look here:
header{
display: flex;
width: 100%;
justify-content: space-between;
}
.logo{
position: absolute;
width: 100%;
display: flex;
justify-content: center;
}
<header>
<div class="burger">
Burger
</div>
<div class="logo">
Logo
</div>
<div class="network">
<a href="#">
Fb
</a>
<a href="#">
Ln
</a>
<a href="#">
I
</a>
<a href="#">
Fr
</a>
<a href="#">
En
</a>
</div>
</header>
Just tested the code below, and it works. This is to give an idea; CSS attributes have to be in separate file of course:
<div style="display:flex;flex-direction:row;width:100%">
<div style="flex:1 1 0px">
<span style="float:right;background-color:lime">Left Element</span>
</div>
<div style="background-color:blue">Central element</div>
<div style="flex:1 1 0px">
<span style="background-color:red">Right 1</span>
<span style="background-color:gray">Right 2</span>
<span style="background-color:magenta">Right 3</span>
</div>
</div>
Or you can have like this.
header{
display: flex;
width: 100%;
justify-content: space-between;
}
.right-menu{
display: flex;
flex: 0 0 55%;
justify-content: space-between;
}
.right-menu ul{
list-style:none;
margin:0;
}
.right-menu ul li{
display:inline-block;
}
<header>
<div class="burger">
Burger
</div>
<div class="right-menu">
<div class="logo">
Logo
</div>
<ul>
<li>
<a href="#">
Fb
</a>
</li>
<li>
<a href="#">
Ln
</a>
</li>
<li>
<a href="#">
I
</a>
</li>
<li>
<a href="#">
Fr
</a>
</li>
<li>
<a href="#">
En
</a>
</li>
</ul>
</div>
</header>
Give all the child elements of the flexbox a flex: 1 to give them the same width, then give your .logo div a text-align: center.
header {
display: flex;
width: 960px;
background: lightblue;
}
header>* {
flex: 1;
}
header .logo {
text-align: center;
}
header .network {
text-align: right;
}
<header>
<div class="burger">
<img src="http://placekitten.com/40/40" alt="icône couteau cuillère burger menu">
</div>
<div class="logo">
<img src="http://placekitten.com/200/100" alt="">
</div>
<div class="network">
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
</div>
</header>
This answer well be helpful.
header {
display: flex;
flex-direction: row;
justify-content: space-between;
flex: 1;
}
header > div {
display: inline-flex;
flex-basis: 33.33%;
align-items: center;
}
.burger {
justify-content: flex-start;
background-color: #AAAAAA;
}
.logo {
justify-content: center;
background-color: #DDDDDD;
}
.network {
justify-content: flex-end;
background-color: #AAAAAA;
}
<header>
<div class="burger">
<span>01</span>
<img src="assets/img/cutlery.svg" alt="">
</div>
<div class="logo">
<span>02</span>
<img src="assets/img/bio.svg" alt="">
</div>
<div class="network">
<a href="#">
<span>03</span>
<img src="assets/img/facebook.svg" alt="">
</a>
<a href="#">
<span>04</span>
<img src="assets/img/linkedin.svg" alt="">
</a>
<a href="#">
<span>05</span>
<img src="assets/img/instagram.svg" alt="">
</a>
<a href="#">
<span>06</span>
<img src="assets/img/france.svg" alt="">
</a>
<a href="#">
<span>07</span>
<img src="assets/img/english.svg" alt="">
</a>
</div>
</header>
I have the following code and want the anchor everywhere in the box not only at the image:
HTML:
<p style="clear:left" />
<div class="reference_container">
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
</div>
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
</div>
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
</div>
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
</div>
</div>
<p style="clear:left" />
CSS:
.reference_container {
display: flex;
}
.reference_box {
border: 1px solid #ddd;
border-radius: 5px;
margin: 1%;
position:relative;
overflow: hidden;
padding: 15px;
white-space: nowrap;
display:inline-block;
}
.reference_box_geraete {
width: 22%;
/*box-sizing: border-box;*/
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
.reference_box_geraete img {
vertical-align: middle;
max-width: 100%;
max-height: 100%;
}
The problem is that I don't get the anchor to stretch to the whole surrounding div.
I tried to remove the div and make the anchor as block-element, but then the images are not stretched correctly (doesn't keep aspect-ratio) when resizing the browser window.
How can I achieve this.
set width , height and display:block for element a
check this:
<p style="clear:left" />
<div class="reference_container">
<a class="reference_box reference_box_geraete" href="google.de">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
<a href="google.de" class="reference_box reference_box_geraete">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
<a href="google.de" class="reference_box reference_box_geraete">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
<a href="google.de" class="reference_box reference_box_geraete">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
</div>
<p style="clear:left" />
Demo: http://www.cssdesk.com/p5Cqd