Hi,

I am working with small academic project and I am stuck on creating presentation of single product from list of ptoducts.
I want to achieve something like that: (in attached graphic file) - may be without rounded corners.

I wrote code but it is not working properly...

<div style="width:405px; background-color:red; border:solid 1px; black">
	<div style="width:80px; background-color:yellow; float: left;">
		<div>
		there will be an image
		</div>
		description of an image
	</div>

	<div style="width:320px; background-color:lime; height:100%;">
		<div style="width:250px; background-color:blue; float:left; height:70%;">
		name of the product
		</div>
		<div style="width:65px; background-color: violet; float:right;">
		quantity
		</div>
		<div style="width:250px; background-color:navy; float:left;">
		description of the product
		</div>
		<div style="width:65px; background-color: grey; float:right;">
		price
		</div>
	</div>
</div>

Dani AI

Generated

Combining 's pragmatic layout fixes and 's call for semantic markup gives a clean, future-proof result: use semantic HTML for the image and key/value product data, and CSS Flexbox for layout instead of float-heavy rules. That removes the fragile height/clear hacks, improves readability without CSS, and makes the card responsive. Keep styling out of the HTML, add box-sizing: border-box, and give images an intrinsic aspect ratio to avoid layout shifts. See MDN for Flexbox and the figure element for details: Flexbox basics and figure element.

Example HTML (semantic and accessible):

<article class="product-card" aria-labelledby="prod-name-1">
  <figure>
    <img src="product.jpg" alt="Product name" loading="lazy">
    <figcaption>Image caption</figcaption>
  </figure>

  <dl class="product-meta" id="prod-name-1">
    <dt>Name</dt><dd>Example Widget</dd>
    <dt>Quantity</dt><dd>3</dd>
    <dt>Description</dt><dd>Short product blurb.</dd>
    <dt>Price</dt><dd>$9.99</dd>
  </dl>
</article>

Example CSS (modern, responsive):

.product-card{ display:flex; gap:1rem; align-items:flex-start;
  max-width:405px; padding:16px; border:1px solid #ccc; box-sizing:border-box;
}
.product-card figure{ margin:0; flex:0 0 120px; }
.product-card img{ display:block; width:100%; height:auto; aspect-ratio:4/3; object-fit:cover; }
.product-meta{ margin:0; display:flex; flex-direction:column; gap:0.5rem; }
@media (max-width:520px){ .product-card{ flex-direction:column; } .product-card figure{ flex:unset; } }

Quick troubleshooting notes: move inline styles into a stylesheet; use correct property order (for example border: 1px solid #000;); percentage heights fail unless a parent has an explicit height—prefer flexible sizing instead. For accessibility, always include meaningful alt and use semantic tags so the card still makes sense when CSS is off.

Recommended Answers

All 5 Replies

* {
			  margin: 0;
			  padding: 0;
			  border: 0;
			  font-family: sans-serif;
			  font-size: 1em;
			  font-weight: normal;
			  font-style: normal;
			  text-decoration: none;
			}
			
			
			
			.Productcontainer {
				width: 405px;
				height: 220px;
				border: 1px solid black;
				position:relative;
				margin: 10px auto;
				padding: 15px;
				
			}
			
			.Productimage {
				background: blue;
				width: 200px;
				height:200px; /* add 20px for desciption */
				position:relative;
				float:left;
			}
			
			.Productimage_desc {
				position:relative;
				height:20px;
			}
			
			.Productdetails {
				position:relative;
				margin-left: 210px; /* add 10px to img width, will give us decent padding */
			}
			
			.Productdetails p { /* Since i removed styling, this will add padding inbetween each of our paragraphs */
				padding-bottom:10px;
			}
<div class="Productcontainer">
        	<div class="Productimage">
        		<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Blue_Box_in_museum.jpg/200px-Blue_Box_in_museum.jpg">
        		<div class="Productimage_desc">This is the description</div>
        	</div>
			<div class="Productdetails">
				<p>Name of the product</p>
				<p>quantity</p>
				<p>description of the product</p>
				<p>price</p>
			</div>
		</div>
Member Avatar for Member #120589

This would be my markup:

<div class="card">
	<img src="whatever"  />
	<p>description of an image</p>
        <ul>
             <li>name of the product</li>
	     <li>quantity</li>
	     <li>description of the product</li>
	     <li>price</li>
         </ul>
</div>

Notice the lack of presentation divs (no divitis).

This should make reading of the data in the absence of css a bit more 'real'.

You can achieve almost everything you need with CSS descendants:

/*do a reset css - i.e. give everything a value of 0/none etc. - including paddings and margins. Eric Meyer has an excellent version*/
/*assume the images in the markup will be 120px width X 80px height (change values accordingly)*/

.card{
  background-image: url(images/card.jpg); /*this gives the nice visuals/rounded edges etc without the hard work - BUT - text needs to ensure that it doesn't overrun the dimensions available to it*/
  background-repeat:no-repeat;
  width: 300px;
  height: 180px;
  padding: 20px; /*workable interior width = 300 - 2X20 = 260, height = 140px*/
}
.card p{
  width: 120px; /*set this to the width of the image so it fits nicely underneath*/
  margin-top: 5px; /*separate paragraph from image*/
}
.card ul{
  float:right;
  width: 120px /* this means that the 'data' will have a 20px gap between it and the image/paragraph*/
}
.card li{
 margin-bottom: 15px; /* just separates the list items from one another*/
}

.card ul li:first-child{
 font-size: 20px;/*make first list item BIGGER*/
 font-weight:bold;
}

CAVEAT: I haven't tried this - off the top of my head. You may find a considerable amount of tinkering is required here!

Cool approach. Should work. A few things i have noticed though.

IE 6 likes spaces when it comes to pseudo or it wont work

.card ul li:first-child/*note space */ {
font-size: 20px
font-weight:bold;
}

Secondly, ensure you dont get bullets

.card li{
margin-bottom: 15px
list-style: none
}

Fix some structure to get float

<div class="card">
			<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Blue_Box_in_museum.jpg/200px-Blue_Box_in_museum.jpg"  />
		        <ul>
		             <li>name of the product</li>
				     <li>quantity</li>
				     <li>description of the product</li>
				     <li>price</li>
		         </ul>
			<p>description of an image</p>		 
		</div>

finally add a clear both to the p

.card p{
			  width: 200px; /*set this to the width of the image so it fits nicely underneath*/
			  margin-top: 5px; /*separate paragraph from image*/
			  clear:both;
			}
commented: Great couple of catches Mac - really appreciate it. +5
Member Avatar for Member #120589

Yes well spotted Mac - rep to you. Like what you've done with it too.
Wrote it in a bit of a hurry, and missed a few things.

I had a little play with it and found that I forgot to float the image and paragraph. Doh! Unfortunately, this messed up the whole image -paragraph thingy. So I made these changes:

.card p img{
  display:block; /* in case image less than width of paragraph - don't want text wrapping*/
}
.card p{
  width: 120px; 
  margin-top: 5px; /*separate paragraph from image*/
  float:left;
}

Because I changed the image - paragraph relationship:

<div class="card">
	<p><img src="whatever"  />description of an image</p>
        <ul>
             <li>name of the product</li>
	     <li>quantity</li>
	     <li>description of the product</li>
	     <li>price</li>
         </ul>
</div>

I really don't know how you feel about that guys. Is it kosher? Image inside a p tag? If so, perhaps it's a little bit better than moving the p tag to below the list items. But I suppose that's just a matter of opinion.

Don't see why it shouldnt validate. A job well done!

Now it would be great, if "questionary" would log in, mark as solved and continue his small academic project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.