Making the last grid cell(s) inactive - css

I have a 4 column grid made with ReactJS. The grid contains a certain number of elements with pictures and text on them. If there's five elements filled, the last three need to be inactive, so there's always four columns on every row, no empty space lying around.
The last cell(s) (max 3.) should always be inactive. No hover, no focus, just an empty element.
The grid elements are fetched from a hardcoded JSON. At the moment my last cell is just {[name: '', img: ''}]
Im fairly new to ReactJS and can't come up with a solution or find one in stackoverflow. Pointing in the right direction would be appreciated.
const GridItem = ({ name, path, altText }) => {
return (
<li className={'grid-item'} key={name}>
<a className={'link'}>
<div className={'image'}>
<img src={path} alt={altText} />
</div>
<div className={'image-text'}>{name}</div>
</a>
</li>
);
};
Expected output:
[img] [img] [img] [img]
[img] [null] [null] [null]

This is more like a css question, and there're a lot of ways to achieve this.
I recommend to use display: grid and grid-template-columns to make grid-based layouts:
.grid {
width: 100%;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 2px;
}
.grid img {
width: 100%;
}
<div class="grid">
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
</div>
You don't have to create fake cells, but if you really want, you can just add some simple css to hide them:
.grid {
width: 100%;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-gap: 2px;
}
.grid img {
width: 100%;
}
.grid img[src=""] {
display: none;
}
<div class="grid">
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
<img src="https://picsum.photos/200/200" />
<img src="" />
<img src="" />
<img src="" />
</div>
Update
You can change your code like this, if the name is empty, give this cell some hint, so you can style it easily:
const GridItem = ({ name, path, altText }) => {
return (
<li className={'grid-item'} key={name}>
<a className={`${name ? 'link' : 'link link__empty'}`}>
<div className={'image'}>
<img src={path} alt={altText} />
</div>
<div className={'image-text'}>{name}</div>
</a>
</li>
);
Or just don't populate its contents at all:
const GridItem = ({ name, path, altText }) => {
return (
<li className={'grid-item'} key={name}>
<a className={'link'}>
(name &&
<React.Fragment>
<div className={'image'}>
<img src={path} alt={altText} />
</div>
<div className={'image-text'}>{name}</div>
</React.Fragment>)
</a>
</li>
);
The result should be like this:
* {
box-sizing: border-box;
}
.grid {
display: flex;
flex-wrap: wrap;
list-style: none;
}
.grid-item {
/* exclude the border, make it 4 columned grid */
flex: 0 1 calc(25% - 2px);
/* the border is 1px, so the equation above is minus 2px*/
border: solid #000 1px;
}
.link {
text-decoration: none;
}
.image {
width: 100%;
}
.image-text {
text-align: center;
}
.link__empty *{
/* if it's empty, do not display any of it's children */
display: none;
}
<ul class="grid">
<li class="grid-item">
<a class="link" href="javascript: void(0)">
<img class="image" src="https://picsum.photos/200/200" alt="image" />
<div class="image-text">image</div>
</a>
</li>
<li class="grid-item">
<a class="link" href="javascript: void(0)">
<img class="image" src="https://picsum.photos/200/200" alt="image" />
<div class="image-text">image</div>
</a>
</li>
<li class="grid-item">
<a class="link" href="javascript: void(0)">
<img class="image" src="https://picsum.photos/200/200" alt="image" />
<div class="image-text">image</div>
</a>
</li>
<li class="grid-item">
<a class="link" href="javascript: void(0)">
<img class="image" src="https://picsum.photos/200/200" alt="image" />
<div class="image-text">image</div>
</a>
</li>
<li class="grid-item">
<a class="link" href="javascript: void(0)">
<img class="image" src="https://picsum.photos/200/200" alt="image" />
<div class="image-text">image</div>
</a>
</li>
<li class="grid-item">
<a class="link link__empty" href="javascript: void(0)">
<img class="image" src="" alt="" />
<div class="image-text"></div>
</a>
</li>
<li class="grid-item">
<a class="link link__empty" href="javascript: void(0)">
<img class="image" src="" alt="" />
<div class="image-text"></div>
</a>
</li>
<li class="grid-item">
<a class="link link__empty" href="javascript: void(0)">
<img class="image" src="" alt="" />
<div class="image-text"></div>
</a>
</li>
</ul>

Related

How can I Fit Image inside Grid Layout

I am creating this layout
My code :
<div class="tour-grid">
<div class="tgItem tg-left">
<a href="" class="tgCard">
<div class="tgCard__image ro-4">
<img class="ro-4 js-lazy loaded" src="img/tours/3.png" alt="image" data-ll-status="loaded">
</div>
</a>
</div>
<div class="tgItem tgChild">
<a href="" class="tgCard">
<div class="tgCard__image ro-4">
<img class="ro-4 js-lazy loaded" src="img/blog/2/2.png" alt="image" data-ll-status="loaded">
</div>
</a>
</div>
<div class="tgItem tgChild">
<a href="" class="tgCard">
<div class="tgCard__image ro-4">
<img class="ro-4 js-lazy loaded" src="img/blog/2/3.png" alt="image" data-ll-status="loaded">
</div>
</a>
</div>
<div class="tgItem tgChild">
<a href="" class="tgCard">
<div class="tgCard__image ro-4">
<img class="ro-4 js-lazy loaded" src="img/tours/2.png" alt="image" data-ll-status="loaded">
</div>
</a>
</div>
</div>
Check my fiddle
But grid are not equal and image doesn't fit inside div.
Right div height should adjust to left col
what is rules with grid system ? should i wrap 3 div into one. ?
You can simplify your code like below
.grid {
display: grid;
grid-auto-flow: column;
grid-gap: 10px;
max-width: 600px;
margin: 20px auto;
}
.grid img:first-child {
grid-area: span 3/span 2;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="grid">
<img src="https://picsum.photos/id/1074/600/400">
<img src="https://picsum.photos/id/1074/300/200">
<img src="https://picsum.photos/id/1074/300/200">
<img src="https://picsum.photos/id/1074/300/200">
</div>
I have an article explaining this code and more: https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/#image-grid

How to set up img size in React.js

I have trouble resizing my image in specific
I set up .img in my index.css like this
.img {
display: block;
width: 100%;
height: 100%;
object-fit: contain;
}
my portfolio.jsx is
<div className="container portfolio_container">
<article className='portfolio_item'>
<div className="portfolio_item-image">
<img src={IMG1} alt="Pair Up" />
</div>
<h3>Pair Up</h3>
<a href='https://github.com/Adipure/pairUp' className='btn' target='_blank'>Github</a>
<a href='https://me0wmerz.github.io/pairUp/' className='btn btn-primary' target='_blank'>Live View</a>
</article>
<article className='portfolio_item'>
<div className="portfolio_item-image">
<img src={IMG2} alt="4AnimeArchive" />
</div>
<h3>4AnimeArchive</h3>
<a href='https://github.com/Adipure/anotherAnimeArchive' className='btn' target='_blank'>Github</a>
<a href='#' className='btn btn-primary' target='_blank'>Live View</a>
</article>
<article className='portfolio_item'>
<div className="portfolio_item-image">
<img src={IMG3} alt="Console.log" />
</div>
<h3>Console.log</h3>
<a href='https://github.com/Adipure/console.log' className='btn' target='_blank'>Github</a>
<a href='#' className='btn btn-primary' target='_blank'>Live View</a>
</article>
<article className='portfolio_item'>
<div className="portfolio_item-image">
<img src={IMG5} alt="weather" />
</div>
<h3>Weather Forecast</h3>
<a href='https://github.com/Adipure/WeatherHomwork' className='btn' target='_blank'>Github</a>
<a href='https://adipure.github.io/WeatherHomwork/' className='btn btn-primary' target='_blank'>Live View</a>
</article>
<article className='portfolio_item'>
<div className="portfolio_item-image">
<img src={IMG6} alt="snakeGame" />
</div>
<h3>Snake Game</h3>
<a href='https://github.com/Adipure/snakeGame' className='btn' target='_blank'>Github</a>
<a href='#' className='btn btn-primary' target='_blank'>Live View</a>
</article>
<article className='portfolio_item'>
<div className="portfolio_item-image">
<img src="#" alt="tbh" />
</div>
<h3>TBH</h3>
<a href='#' className='btn' target='_blank'>Github</a>
<a href='#' className='btn btn-primary' target='_blank'>Live View</a>
</article>
</div>
and I also set up .portfolio_item-image in portfolio.css like this
.portfolio_item-image {
border-radius: 1.5rem;
-webkit-border-radius: 1.5rem;
-moz-border-radius: 1.5rem;
-ms-border-radius: 1.5rem;
-o-border-radius: 1.5rem;
overflow: hidden;
}
and the result is the following,
I don't know why my second and 3rd image sizes are different from the others.
please help. thank you
I tried all CSS properties to resize them, but nothing changes.

What is causing these huge gaps?

I am trying to display several items from a query onto this page. I am trying to make them so that 6 images are on a line and evenly spaced. However, it seems that no matter how I change the css, I still keep ending up with huge gaps in some places.
Here is the code I used to create this section:
<div class="container2">
<div class="category"><h2>All Items</h2></div>
<div class="display">
<?php do { ?>
<ul>
<li><a href="details.php?recordID=<?php echo $row_Master['Master_ID']; ?>"><img class="thumb" src="img/<?php echo $row_Master['Img']; ?>"/>
<br />
<?php echo $row_Master['Name']; ?></a></li>
</ul>
<?php } while ($row_Master = mysqli_fetch_assoc($Master)); ?>
<!-- end .display --></div>
<?php
mysqli_free_result($Master);
?>
<!-- end .container2 --></div>
and the CSS associated with it:
.container2 {
margin:0 auto;
text-align: center;
width:95%;
background-color:#FFFFFF;
}
.display{
width:100%;
margin-left:auto;
margin-right:auto;
text-align:center;
overflow:hidden;
}
.display ul li {
float:left;
max-width:15.67%;
margin: 0 0 8% 1%;}
.display ul{
list-style:none;
}
img.thumb {
border:0;
max-height:150px;
min-width:16.67%;
vertical-align:middle;
padding:3px;}
Can anyone help me remove these huge gaps, plz?
Flexbox for the win.
You can get six across wrapping rows with this config. The flex container gets:
display: flex;
flex-wrap: wrap;
And the flex items get:
flex: 0 0 16.666%;
I added several comments in the CSS to explain what I did here. This approach takes a bit of practice but once you get it you will never use floats for layout again.
Here is a great resource that explains the flexbox approach really well: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
* { box-sizing: border-box; }
.display {
border: 2px solid red; /* just for demo visibility */
}
.display ul {
/* ul reset */
list-style: none;
padding: 0;
margin: 0;
/* flexbox layout - flex container config */
display: flex;
flex-wrap: wrap;
}
.display ul li {
border: 2px solid dodgerblue; /* just for demo visibility */
padding: 10px; /* use padding to create spacing between items, remove the borders to get the final look */
/* flex item config */
flex: 0 0 16.66%; /* 100 divided by 6 is 16.666... */
}
.display ul li span {
display: block;
text-align: center;
}
.display ul li img {
max-width: 100%;
height: auto;
}
<div class="container2">
<div class="category">
<h2>All Items</h2>
</div>
<div class="display">
<ul>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 1</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 2</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 3</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 4</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 5</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 6</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 7</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 8</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 9</span>
</a>
</li>
<li>
<a href="x">
<img class="thumb" src="https://picsum.photos/120" />
<span class="name">Thing 10</span>
</a>
</li>
</ul>
</div>
</div>

How to get a parent div to float/break into an L/wrap between children?

I thought I understood float, but clearly I don't.
I have a div (class="Sophia") made of a header ("h2"), and an unordered list ("ul") containing images that I want to collapse into an L-formation. Specifically I want the header (containg the text "sophia") to go into the gap that is under "Dorothy" and to the left of "Rose."
I realize that I could accomplish this by breaking the header out of the div, but for the sake of learning I want to know how to use CSS to style the div as a whole unit such that it "breaks" between elements into an L, such that the upper part of the L goes into empty space, just with CSS.
How things are now:
Kind of what the goal is:
If you know how to pull this off, I'd love it if you could show me how!
Thanks!
This is the codepen: http://codepen.io/ihatecoding/pen/WpBZpy
This is the snippet:
/* body formatting, not important */
body {font-family:"Gudea";
background-color: lightGray;
padding: 15px;}
.sophiaword{color:darkblue;}
h1 {text-align:center;}
a {text-decoration:none;
color:darkblue;}
.wrapper{
padding: 10px 25%;
width:50%;
}
/*this is the formatting that matters*/
.dorothy,
.rose,
.sophia {
float:left;
}
.dorothy,
.rose {
width:50%;
}
.sophia {
width:auto;
}
.dorothy li {
width:100%;
}
.rose li {
width:47%;
}
.sophia li {
width:20%;
display:block;
float:left;
}
.dorothy h2,
.rose h2,
.sophia h2{
color:black;
font-size: 40px;
margin:0;
}
li {
background-color: #f7f7f7;
margin:0 1% !important;
padding:0 !important;
width: 22%;
display: inline-block;
}
.cat-post-item span,
.cat-post-item a {
color: darkSlateGray;
font-weight: 700;
letter-spacing: -1px;
}
.cat-post-item img {
margin: 0 !important;
height: 150px;
object-fit: cover;
}
.cat-post-thumbnail span,
.cat-post-thumbnail img {
width:100% !important;
}
.sophia li {
width:20%;
display:block;
float:left;
}
<span><h1>I'd like to know how to use CSS and float to get the blue text "<span class="sophiaword">Sophia</span>" to flow so it breaks away from the row of sophia images, and ends up next to the left of the the bottom picture of rose. In the end I'd like the "sophia" div to make an "L"-shape, but stay as one div. Currently the div starts on a new line and I don't want that. </h1></span>
<div class="wrapper">
<div class=" dorothy">
<h2>
Dorothy
</h2>
<ul>
<li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span>
<img width="150" height="150" src="https://pbs.twimg.com/media/Buhe7x1CAAAIW02.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a class="cat-post-title" href="http://www.example.com" >Dorothy (down there ↓)</a>
</li>
</ul>
</div>
<div class="rose">
<h2 >
Rose
</h2><ul >
<li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span><img width="150" height="148" src="http://www.dvdizzy.com/images/g-i/ggs2-12.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" >Rose1</a>
</li>
<li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span>
<img width="150" height="112" src="http://www.dvdizzy.com/images/g-i/ggs2-12.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" >Rose2</a>
</li>
<li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span>
<img width="147" height="150" src="http://www.dvdizzy.com/images/g-i/ggs2-12.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" > ( ← over there ) Rose3 </a>
</li>
<li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span>
<img width="92" height="150" src="http://www.dvdizzy.com/images/g-i/ggs2-12.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" >Rose4 </a>
</li>
</ul>
</div>
<div class=" sophia">
<h2 class="widget-title">
Sophia
</h2>
<ul >
<li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span>
<img width="150" height="101" src="http://underscoopfire.com/wp-content/uploads/2013/02/sophia-petrillo.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" >Sophia1</a>
</li>
<li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span>
<img width="150" height="101" src="http://underscoopfire.com/wp-content/uploads/2013/02/sophia-petrillo.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" >Sophia2</a> </li><li class='cat-post-item'>
<a class="cat-post-thumbnail" href="http://www.example.com" >
<span> <img width="150" height="101" src="http://underscoopfire.com/wp-content/uploads/2013/02/sophia-petrillo.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" >Sophia3</a>
</li>
<li class='cat-post-item'><a class="cat-post-thumbnail" href="http://www.example.com" >
<span>
<img width="150" height="101" src="http://underscoopfire.com/wp-content/uploads/2013/02/sophia-petrillo.jpg" class="attachment-150x150 size-150x150 wp-post-image" />
</span>
</a>
<a href="http://www.example.com" >Sophia4</a>
</li>
</ul>
</div>
</div>

CSS tiles size tweak

i am trying to make my tiles look the same even text inside increase.but it does seems to be able to work even with word property.i want to make the "buy extended warranty" and ""List machines for service pack" to be same as others.
Anyone can help?
.tile {
width: 100%;
display: inline-block;
box-sizing: border-box;
background: #fff;
padding: 30px;
margin-bottom: 40px;
text-align:center;
}
.tile:hover
{
background:#F8F9F9;
}
<div class="tab-pane active" id="warranty">
<div class="col-md-4">
<a class="tile" style="background-color:#EBDEF0;" href="#">Show warranty </a>
</div>
<div class="col-md-4">
<a class="tile" style="background-color:#EBDEF0;" href="#">New Warranty</a>
</div>
<div class="col-md-4 ">
<a class="tile" style="background-color:#EBDEF0;" href="#">Delete Warranty</a>
</div>
<div class="col-md-4 ">
<a class="tile " style="background-color:#EBDEF0;" href="#">Enter Invoice</a>
</div>
<div class="col-md-4 ">
<a class="tile" style="background-color:#EBDEF0;" href="#">Enter Serial </a>
</div>
<div class="col-md-4">
<a class="tile" style="background-color:#EBDEF0;" href="#">Buy extended warranty </a>
</div>
<div class="col-md-4 ">
<a class="tile" style="background-color:#EBDEF0;" href="#">List Machines for service pack</a>
</div>
</div>
You could use flex-box for this:
css:
.tab-pane {
display: flex;
flex-wrap: wrap;
}
.tile {
width: 100%;
padding: 30px;
margin-bottom: 40px;
text-align: center;
display: block;
}
.tile:hover {
background: #F8F9F9 !important;
}
HTML
<div class="tab-pane active" id="warranty">
<div class="col-sm-4">
<a class="tile" style="background-color:#EBDEF0;" href="#">Show warranty </a>
</div>
<div class="col-sm-4">
<a class="tile" style="background-color:#EBDEF0;" href="#">New Warranty</a>
</div>
<div class="col-sm-4 ">
<a class="tile" style="background-color:#EBDEF0;" href="#">Delete Warranty</a>
</div>
<div class="col-sm-4 ">
<a class="tile " style="background-color:#EBDEF0;" href="#">Enter Invoice</a>
</div>
<div class="col-sm-4 ">
<a class="tile" style="background-color:#EBDEF0;" href="#">Enter Serial </a>
</div>
<div class="col-sm-4">
<a class="tile" style="background-color:#EBDEF0;" href="#">Buy extended warranty </a>
</div>
<div class="col-sm-4 ">
<a class="tile" style="background-color:#EBDEF0;" href="#">List Machines for service pack</a>
</div>
</div>
I adjusted the class on your col-md-4 to col-sm-4 so you could see it in action. I included the bootstrap CDN CSS, as you are using bootstrap class names in your HTML. And I added !important to override your inline color styles on the anchor tags.
Here's the fiddle to see it in action:
https://jsfiddle.net/mjqfjbh0/1/

Resources