CSS: First row of image gallery coming out diagonal? - php

I have an array of images in php that I am trying to display as a gallery. It is working pretty well, except that the first row of the gallery comes out diagonal, with each image slightly below the one before it.
Here is the code I am using:
CSS
.gallery {
margin: 5px;
border: 1px solid #ccc;
float: left;
display: inline;
width:180px;
height:180px;
}
.gallery:hover {
border: 1px solid #777;
}
.gallery img {
max-height:100%;
max-width:100%;
margin:0 auto;
}
HTML/PHP
<div class="gallery-container">
<?php
$count = 0;
foreach($images as $image) {
echo "<div class='gallery'><a href=''><img src='".$dir.$image."'/></a></div>"; //$dir is the path to the image
if(count % 4 == 0) {
echo "<br>";
}
$count = $count + 1;
}
?>
</div>
Here is a screenshot of the result on the first row:
All the other rows come out fine. Thank you very much for any help.

If you want to do it using float, you would better do that like this:
CSS
.gallery{
margin: 5px;
border: 1px solid #ccc;
float: left;
display: block;
width:180px;
height:180px;
}
.gallery.clear-left{
clear: left;
}
PHP/HTML
<div class="gallery-container">
<?php
$count = 0;
foreach( $images as $image ){?>
<div class="gallery <?php echo( 0 == $count % 4 ? 'clear-left' : '' );?>">
<a href="">
<img src="<?php echo $dir.$image;?>">
</a>
</div>
<?php
$count++;
}
?>
</div>
But there is an even better solution to this, called flex:

Instead of br element render "clear" div
<div style="clear:both"></div>

Have you tried starting the count at 1 instead of 0?

Checkout the resulting html. It is likely that you count is off and the br is being put to the dom too often. This fiddle shows that your desired html and css should work.
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<div class='gallery'><a href=''><img src='https://upload.wikimedia.org/wikipedia/commons/b/b2/Hausziege_04.jpg'/></a></div>
<br>
Though, I would consider not using the br, but instead using a containing div:
like this

Related

How can i create 6 <div> in php and arrange in a div wrapper?

I want to create 6 <div> in php by a loop with size of 200x200 px and arrange these 6 div in 3 rows and 2 columns inside a div wrapper.
Thanks in advance
<div class="wrapper">
<?php
$i = 1;
for ( $i=1 ; $i<=6; $i++){
div1 div2
div3 div4
div5 div6
}
?>
</div>
The generating of the divs is the easy bit - use the clear property in css and assign it using the nth-child syntax like so:
<?php
echo "<div class='wrapper'>";
for( $i=0; $i < 6; $i++ ) echo "<div>$i</div>";
echo "</div>";
?>
To see the wrapper background colour it needs a height -the 2n+1 ~ this is defined in the css specification so you need only use it like it is. Have a look on css-tricks.com etc for use of nth-child, nth-of-type(odd) etc
<style>
.wrapper{
background:blue!important;
display:block;
width:80%;
min-height:calc( 600px + 7rem );
float:none;
clear:both;
margin:1rem auto;
box-sizing:border-box;
border:2px solid black;
border-radius:1rem;
}
.wrapper div{
width:200px;
height:200px;
border:1px solid black;
float:left;
margin:1rem;
display:block;
background:whitesmoke;
}
.wrapper div:nth-child(2n+1){
clear:left;
}
</style>

Display database content in 3 blocks

I am using a table to display information from my database but I'm having trouble formatting the CSS. Right now I have 12 icons and the respective name on the right and the problem is that everything is displayed like a list, one below the other and I wanted to break the list in three blocks, like this:
EDIT I don't need necessarily to do it with tables, I just need to display the values like this, dynamically. Because later I'd probably going to add more plants to database and I want them to be always listed like this.
This is my html:
<table id="plantslist">
<?php
foreach($resultado as $planta){ ?>
<tr>
<td><img src="http://www.coisas.pt/coisas/backoffice/<?php echo $planta['icon']; ?>"></img></td>
<td><a href="blabla.php?name=<?php echo $planta['name']?>&img=<?php echo $planta['img']?>&tagline=<?php echo $planta['tagline']?>&hum=<?php echo $planta['hum']?>&lum=<?php echo $planta['lum']?>&temp=<?php echo $planta['temp']?>&seed=<?php echo $planta['seed']?>&spacing=<?php echo $planta['spacing']?>&info1=<?php echo $planta['info1']?>&info2=<?php echo $planta['info2']?>&info3=<?php echo $planta['info3']?>&info4=<?php echo $planta['info4']?>">
<?php echo $planta['name'];?></a></td>
</tr>
<?php } ?>
</table>
CSS:
#plantslist {
border: 3px solid white;
overflow: scroll;
font-size: 1.7vw;
line-height: 2.8vw;
font-family: Robotomedium;
margin-top: 11%;
text-align: left;
}
#plantslist img {width: 2.3vw;}
#plantslist tr td {
display: inline-block;
vertical-align: top;
}
You can do a container with width: 100%; then make child elements with width: 33% each one. To make them place one next to other you can use float: left;. Here is an example.
.table {
width: 100%;
float: left;
}
.table > .items {
width: 33%;
float: left;
}
.table > .items:before {
content: "";
display: block;
background: url(http://placehold.it/20/000) no-repeat;
width: 20px;
height: 20px;
float: left;
margin: 0 6px 0 0;
}
<div class="table">
<div class="items">1</div>
<div class="items">2</div>
<div class="items">3</div>
<div class="items">4</div>
<div class="items">5</div>
<div class="items">6</div>
<div class="items">7</div>
<div class="items">8</div>
<div class="items">9</div>
</div>
Look that your icon it's placed with before pseudo element. You should change background value with the correct url.
EDIT
Use this if you have different images per plant.
.table {
width: 100%;
float: left;
}
.table > .items {
width: 33%;
float: left;
}
.table > .items img {
width: 20px;
height: 20px;
margin: 0 10px 0 0;
float: left;
}
<div class="table">
<div class="items">
<img src="http://placehold.it/20/dfa9a9" alt=""> 1
</div>
<div class="items">
<img src="http://placehold.it/20/dfa9de" alt=""> 2
</div>
<div class="items">
<img src="http://placehold.it/20/b1a9df" alt=""> 3
</div>
<div class="items">
<img src="http://placehold.it/20/a9cadf" alt=""> 4
</div>
<div class="items">
<img src="http://placehold.it/20/dfd8a9" alt=""> 5
</div>
<div class="items">
<img src="http://placehold.it/20/5cff46" alt=""> 6
</div>
<div class="items">
<img src="http://placehold.it/20/dfa9a9" alt=""> 7
</div>
<div class="items">
<img src="http://placehold.it/20/a9cadf" alt=""> 8
</div>
<div class="items">
<img src="http://placehold.it/20/a9cadf" alt=""> 9
</div>
</div>
Using your PHP code
HTML | PHP
<div class="table">
<?php
foreach($resultado as $planta){
?>
<div class="items">
<img src="http://www.growbox.pt/growbox/backoffice/<?php echo $planta['icon']; ?>" alt="<?php echo $planta['name'];?>">
<a href="intips.php?name=<?php echo $planta['name']?>&img=<?php echo $planta['img']?>&tagline=<?php echo $planta['tagline']?>&hum=<?php echo $planta['hum']?>&lum=<?php echo $planta['lum']?>&temp=<?php echo $planta['temp']?>&seed=<?php echo $planta['seed']?>&spacing=<?php echo $planta['spacing']?>&info1=<?php echo $planta['info1']?>&info2=<?php echo $planta['info2']?>&info3=<?php echo $planta['info3']?>&info4=<?php echo $planta['info4']?>"><?php echo $planta['name'];?>
</a>
</div>
<?php
}
?>
</div>
CSS
.table {
width: 100%;
float: left;
}
.table > .items {
width: 33%;
float: left;
}
.table > .items img {
width: 20px;
height: 20px;
margin: 0 10px 0 0;
float: left;
}
See if this works. I'm just creating more elements so that you have 3 sets of tds per row. Ideally, though, I wouldn't do this with tables. I'd do something like arglab suggested with divs, but here I'm just showing you how I'd do it with tables since that was your question. Make your divs inline blocks with width 33% and you should be good. You may also need to do box-sizing: border-box so that way any padding and margins get automatically put into the calculation for you.
<table id="plantslist">
<?php $count = 0; ?>
<tr>
<?php foreach($resultado as $planta) {
if (count === 2) {
count = 0; ?>
<td><img src="http://www.growbox.pt/growbox/backoffice/<?php echo $planta['icon']; ?>"></img></td>
<td><a href="intips.php?name=<?php echo $planta['name']?>&img=<?php echo $planta['img']?>&tagline=<?php echo $planta['tagline']?>&hum=<?php echo $planta['hum']?>&lum=<?php echo $planta['lum']?>&temp=<?php echo $planta['temp']?>&seed=<?php echo $planta['seed']?>&spacing=<?php echo $planta['spacing']?>&info1=<?php echo $planta['info1']?>&info2=<?php echo $planta['info2']?>&info3=<?php echo $planta['info3']?>&info4=<?php echo $planta['info4']?>">
<?php echo $planta['name'];?></a></td>
</tr>
<tr>
<?php }
else {
$count += 1;
// If this doesn't work, try $count = $count + 1;
<td><img src="http://www.growbox.pt/growbox/backoffice/<?php echo $planta['icon']; ?>"></img></td>
<td><a href="intips.php?name=<?php echo $planta['name']?>&img=<?php echo $planta['img']?>&tagline=<?php echo $planta['tagline']?>&hum=<?php echo $planta['hum']?>&lum=<?php echo $planta['lum']?>&temp=<?php echo $planta['temp']?>&seed=<?php echo $planta['seed']?>&spacing=<?php echo $planta['spacing']?>&info1=<?php echo $planta['info1']?>&info2=<?php echo $planta['info2']?>&info3=<?php echo $planta['info3']?>&info4=<?php echo $planta['info4']?>">
}
?>
</table>
SOLUTION WITH DIVS: See if this works...
<div id="plantslist">
<?php foreach($resultado as $planta) { ?>
<div class="plant">
<img src="http://www.growbox.pt/growbox/backoffice/<?php echo $planta['icon']; ?>"></img>
<a href="intips.php?name=<?php echo $planta['name']?>&img=<?php echo $planta['img']?>&tagline=<?php echo $planta['tagline']?>&hum=<?php echo $planta['hum']?>&lum=<?php echo $planta['lum']?>&temp=<?php echo $planta['temp']?>&seed=<?php echo $planta['seed']?>&spacing=<?php echo $planta['spacing']?>&info1=<?php echo $planta['info1']?>&info2=<?php echo $planta['info2']?>&info3=<?php echo $planta['info3']?>&info4=<?php echo $planta['info4']?>">
<?php echo $planta['name'];?></a>
</div>
<?php } ?>
</div>
CSS
#planstslist{
width: 100%;
/* this may help if the items don't stack properly */
white-space: nowrap;
}
.plant{
width: 33.33%
display: inline-block;
/* this may help if the items don't stack properly */
white-space: normal;
}
You may need to make the images inline blocks and set them and the links to have percentage width values adding up to no more than 100%. You may also need to add box-sizing: border-box; to the "plant" divs. I usually get in the habit of making the following declaration on all my CSS files: * {box-sizing: border-box};
I think it's enough if you simply erase display: inline-block; from this CSS rule;
#plantslist tr td {
display: inline-block;
vertical-align: top;
}
displayshould be table-cell, not inline-block, which is the default anyway in a <td> element. Also vertical-align: top; wouldn't be necessary, so you might as well erase the whole rule...

Float left not working

I'm currently working on a "webshop" kind of project. I am creating a product panel with a
float:left; property but when I loop the products they are not coming next to each other but underneath the previous one.. What am I missing?
Code:
$web_string = "
<div class='product'>
<div class='image'>
<img class='image' src='iphone.jpg' alt='iPhone' height='100%' width='100%' />
</div>
<div class='under'>
<div class='info'>
<ul>
<li>iPhone</li>
<li>iOS</li>
<li>16 GB</li>
<li>Black & White</li>
</ul>
<a href='#'>more info...</a>
</div>
<div class='extra'>
<p>price: 588</p>
<button>put in cart!</button>
</div>
<div>
</div>";
CSS:
#wrapper{
width: 100%;
height:100%;
background-color: blue;
}
.product
{
float:left;
width:250px;
height:250px;
background-color:red;
}
.onder
{
height:50%;
}
.afbeelding
{
background-color: green;
}
.info
{
background-color:yellow;
float:left;
width:50%;
height:100%;
}
.extra
{
background-color: purple;
float:right;
width:50%;
height:100%;
}
PHP Loop:
<?
echo $web_string;
for($i = 0; $i < 4; $i++)
{
echo $web_string;
if($i == 2)
{
echo "Hello World";
}
}
?>
EDIT: Changed ID's to Classes
You're using CSS ID selector which can be used to point only ONE element. So the float property is applied only on the first one. Use class instead : <div class='product'> and then in your CSS : .product { float: left; }
Same thing for "image" and "under"
Here is a jfiddle fix:
link
Had to add float: left; on image and under ID too
#image {
float: left;
}
#under {
float: left;
}
Let me know if this is what you needed

How to show 4 <li> on one row and the others 4 <li> on the other one [duplicate]

This question already has answers here:
Dividing long list of <li> tags into columns?
(3 answers)
Closed 9 years ago.
I have the query on php which takes a lot of images from database, I need to show 4 images on the first row, the other 4 images on the next row and so on.
I thought I could do that with the help css, but I can't =(
HTML
<div id="photo_block" style="border:2px dotted <?echo $color;?>; height:1000px;" >
<?
while ($item=$select_items->fetch()) {
?>
<ul class="items">
<li>
<div class="item_block">
<img src="<?echo $item['src']?>">
</div>
</li>
</ul>
<?
}
?>
</div>
CSS
#photo_block{
width:800px;
}
.items li{
display: inline;
}
.items li .item_block img{
width: 100px;
height: 100px;
}
thank you for helping me!
you should reset the padding and margin of the ul element
your img size is set to 100px so they will fit inside your #photo_block 8 times in a row
How about: http://jsfiddle.net/4Ntzq/
Why are you creating a new list with one item for every separate image?
I would not use a list at all, and just put them in divs four at a time, then scale the images to (a little under) 25%.
<div class='imagerow'>
<?
$i = 0;
while ($item=$select_items->fetch()) {
if($i > 0 && $i % 4 == 0) {
?>
</div>
<div class='imagerow'>
<?
} // end if
?>
<img src="<?echo $item['src']?>">
<?
} // end while
?>
</div>
with CSS
.imagerow img { width: 24.75%; }
where the width is just short of 25% because experience taught me that if you take exactly 25% the last one often still overruns onto the next line.
HTML
<div id="photo_block" style="border:2px dotted <?echo $color;?>; height:1000px;" >
<ul class="items">
<? while ($item=$select_items->fetch()) { ?>
<li>
<div class="item_block">
<img src="<?echo $item['src']?>">
</div>
</li>
<? } ?>
</ul>
</div>
CSS
#photo_block{
width:500px;
}
.items li{
width:100px;
margin-right:5px;
float: left;
list-style: none;
}
.items li .item_block img{
width: 100px;
height: 100px;
}
Use float: left; instead of display: inline, then add a new rule that does a clear: left every 4th <li> element.
HTML:
<div id="photo_block" style="border:2px dotted <?echo $color;?>; height:1000px;" >
<ul class="items">
<? while ($item=$select_items->fetch()): ?>
<li>
<div class="item_block">
<img src="<?echo $item['src']?>">
</div>
</li>
<? endwhile ?>
</ul>
</div>
CSS:
#photo_block{
width:800px;
}
.items li{
float: left;
}
.items li:nth-child(4n)
{
clear: left;
}
.items li .item_block img{
width: 100px;
height: 100px;
}

Showing multiple of the same <divs> on the same line

I am retrieving a list of products from a database and want to display them all in a rows of 3 columns not using a table though. So I want 3 divs to be displayed side by side. then below.
<div class="productindividualdisplay">
<div class="productphoto">
<img src="http://3.bp.blogspot.com/-_xP-UUa4D0c/UfAo1eYxURI/AAAAAAAAAT4/xsibNtxZceQ/s320/Books.jpg" alt="Smiley face" width="250" height="250"></p>
</div>
<div class="producttitle">
<?php echo $row['title'] ?>
</div>
<div class="productprice">
<?php echo "<div id='productrrp'> €" . $row['rrp'] . "</div>";
if(is_null($offeringprice)) {
echo "Not Available";
} else {
echo "€" . $offeringprice['price'];
}
?>
</div>
That is my code but it is just displaying the divs below each other. Is it possible so it fills up the row before starting another one?
Try using display: inline-block; on the divs's css.
A <div> is a block-level element. Block-level elements, like <h1>, <p>, <table> etc. will (by default) span the entire width of their parent elements, so they can't be positioned next to eachother.
You can change this behavior, however, using the following CSS rule:
div.column {
display: inline-block;
}
This will render the <div>s as inline blocks.
Now you can give it a certain width so that three divs fit into a row. Do note that, when you leave whitespace between two <div> elements, there will be some visual whitespace. If you give all div's a width of 33.333333333%, the extra whitespace will cause their combined width to exceed 100%, so the third div will move to the next line.
You can simply prevent this by making sure there is no whitespace between the HTML elements:
<div class="column">
<p>Some contents here</p>
</div><div class="column">
<p>As you can see, no whitespace between the two div elements.</p>
</div>
Of course you can then use margins to control whitespace manually:
div.column {
display: inline-block;
width: 30%;
margin-right: 3.33333333%;
margin-bottom: 10px;
}
You might wanna take a look at this article: Using inline-block to Display a Product Grid View (it uses <li>s instead of <div>s, but the idea is essentially the same)
Here's a FIDDLE
<div class="product-wrapper">
<div class="productindividualdisplay">
<div class="productphoto">
<img src="http://3.bp.blogspot.com/-_xP-UUa4D0c/UfAo1eYxURI/AAAAAAAAAT4/xsibNtxZceQ/s320/Books.jpg" alt="Smiley face" width="250" height="250">
</div>
<div class="producttitle">
Product Title
</div>
<div class="productprice">
<span>$100</span>
</div>
</div>
...more products...
</div>
.product-wrapper {
width: 960px;
padding: 10px;
}
.productindividualdisplay {
background: #fff;
display: inline-block;
width: 260px;
margin: 5px 5px 15px 5px;
padding: 10px;
text-align: center;
border: 1px solid #999;
box-shadow: 0 5px 6px -3px #333;
}
.productphoto {
width: 95%;
margin: 10px auto;
border-bottom: 1px solid #999;
}
.producttitle a {
font-size: 18px;
text-decoration: none;
}
.productprice {
font-size: 18px;
font-weight: 600;
}

Categories