6+ Image Shapes in CSS with Example

Image Shapes in CSS with Example
Code Snippet:Image Shapes with CSS3
Author: Geoffrey Crofte
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 790
License: MIT
Edit Code online: View on CodePen
Read More

This CSS code example helps you to create various image shapes like diamonds, octagons, hexagons, and dodecagons. These shapes can be used to enhance your web design and layout. The code uses CSS transformations and overflow properties to achieve these beautiful shapes.

It simplifies the process of adding unique design elements to your portfolio. You can use these shapes for a photo gallery or slideshow on your website.

How to Create Image Shapes In CSS

1. To get started, create an HTML file and structure your web page as needed. You can use the following sample HTML structure as a starting point:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Stylish Shapes</title>
</head>
<body>
<!-- Your content goes here -->
</body>
</html>

2. In your project directory, create a CSS file (styles.css) and copy the following CSS code into it. This code defines the styles and transformations needed to create various shapes.

/*
 * ==== Losange
 */
.losange, .losange div {
  margin: 0 auto;
  transform-origin: 50% 50%;
  overflow: hidden;
  width: 250px;
  height: 250px;
}
.losange {
  transform: rotate(45deg) translateY(10px);
}
.losange .los1 {
  width: 355px;
  height: 355px;
  transform: rotate(-45deg) translateY(-74px);
}
.losange .los1 img {
  width: 100%;
  height: auto;
}

/*
 * ==== diamond 
 */

.diamond, .dia {
  margin: 0 auto;
  transform-origin: 50% 50%;
  overflow: hidden;
  width: 300px;
  height: 300px;
}
.diamond {
  transform: rotate(45deg) translateY(-25px) translateX(-25px);
}
.diamond .dia {
  width: 380px;
  height: 380px;
  transform: rotate(-45deg);
}
.diamond img {
  width: 100%;
  height: auto;
}

/* 
 * ==== octogone
 */
.octo, .octo div {
  margin: 0 auto;
  transform-origin: 50% 50%;
  overflow: hidden;
  width: 300px;
  height: 300px;
}
.octo, .octo div {
  width: 270px;
  height: 270px;
}
.octo {
  transform: rotate(45deg);
}
.octo1 {
  transform: rotate(-45deg);
}


/*
 * ==== hexagon
 */
.hexa, .hexa div {
  margin: 0 auto;
  transform-origin: 50% 50%;
  overflow: hidden;
  width: 300px;
  height: 300px;
}
.hexa {
  width: 325px;
  height: 230px;
}
.hexa div {
  width: 100%;
  height: 100%;
}
.hexa {
  transform: rotate(120deg);
}
.hex1 {
  transform: rotate(-60deg);
}
.hex2 {
  transform: rotate(-60deg);
}

/*
 * ==== dodecagon
 */
.dodeca, .dodeca div {
  margin: 0 auto;
  transform-origin: 50% 50%;
  overflow: hidden;
  width: 300px;
  height: 300px;
}
.dodeca {
  transform: rotate(120deg);
}
.dode1 {
  transform: rotate(-60deg);
}
.dode2 {
  transform: rotate(-60deg);
}

/* irregular dodecagon */
.irr, .irr div {
  width: 320px;
}

3. Now, let’s apply these shapes to elements on your webpage. For instance, to create a diamond shape for an image, you can use the following HTML structure:

<div class="diamond">
<div class="dia">
<img src="your-image.jpg" alt="Diamond Shape Image">
</div>
</div>

Replace "your-image.jpg" with the path to your image. You can use the following classes for other shapes:

  1. Diamond Shape:
    • .diamond and .dia
  2. Octagon Shape:
    • .octo and .octo div
  3. Hexagon Shape:
    • .hexa and .hexa div
  4. Dodecagon Shape:
    • .dodeca and .dodeca div
  5. Irregular Dodecagon Shape:
    • .irr and .irr div

You can customize the size and appearance of these shapes by adjusting the CSS properties like width, height, and colors. Experiment with different values to achieve the desired look.

That’s all! hopefully, you have successfully created image shapes in CSS. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X