Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Implementing CSS Shapes for Creative Text Wrapping
CSS Shapes allow text to flow around custom shapes, breaking free from traditional rectangular boundaries. This powerful feature enables designers to create magazine-style layouts and visually appealing designs with dynamic text wrapping around circles, polygons, and irregular images.
Syntax
selector {
shape-outside: circle() | ellipse() | polygon() | url();
float: left | right;
clip-path: circle() | ellipse() | polygon();
shape-margin: value;
}
Core CSS Properties
shape-outside Property
The shape-outside property defines the area around which text should wrap. It works in conjunction with the float property to position elements for text wrapping.
float Property
The float property is essential for shape-outside to work. It positions the element so text can wrap around it.
clip-path Property
The clip-path property visually clips elements into custom shapes, working as a cropping tool for creative visual effects.
shape-margin Property
The shape-margin property adds space between text and the shape to prevent overcrowding and improve readability.
Example: Circle Text Wrapping
The following example demonstrates text wrapping around a circular shape
<!DOCTYPE html>
<html>
<head>
<style>
.circle-shape {
float: left;
shape-outside: circle();
width: 150px;
height: 150px;
border-radius: 50%;
background: #f206fa;
clip-path: circle();
margin: 0.5rem;
}
p {
text-align: justify;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="circle-shape"></div>
<p>This text flows dynamically around the circle shape defined using CSS Shapes. Lorem ipsum dolor sit amet consectetur adipisicing elit. Aut, nulla amet velit distinctio quos corporis atque vel maxime laborum fuga enim ad quod dolorum vitae laboriosam natus unde repellendus soluta!</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum voluptatem minus ratione quas consequatur laudantium modi quod nulla, beatae eaque hic repudiandae doloribus nam facilis rem odio, nemo assumenda.</p>
</body>
</html>
A purple circle appears on the left side of the page, with text flowing smoothly around its curved edge, creating a dynamic layout that breaks away from traditional rectangular text boxes.
Example: Ellipse Text Wrapping
An ellipse provides more control over shape dimensions with separate x and y axis values
<!DOCTYPE html>
<html>
<head>
<style>
.ellipse-shape {
float: left;
shape-outside: ellipse(60px 100px at 50% 50%);
width: 120px;
height: 200px;
background: #4CAF50;
clip-path: ellipse(60px 100px at 50% 50%);
shape-margin: 20px;
}
p {
text-align: justify;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="ellipse-shape"></div>
<p>This text wraps around an elliptical shape with custom dimensions. The ellipse function allows for precise control over the horizontal and vertical radii, creating more varied and interesting text flow patterns.</p>
<p>Notice how the text follows the curved edge of the ellipse, maintaining proper spacing thanks to the shape-margin property.</p>
</body>
</html>
A green elliptical shape appears on the left, taller than it is wide, with text flowing around its curved edges and maintaining a 20px margin for readability.
Example: Polygon Text Wrapping
Polygons enable creation of complex angular shapes using coordinate points
<!DOCTYPE html>
<html>
<head>
<style>
.polygon-shape {
float: right;
shape-outside: polygon(50% 0%, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0% 60%, 10% 20%);
width: 150px;
height: 150px;
background: #FF5722;
clip-path: polygon(50% 0%, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0% 60%, 10% 20%);
margin: 20px;
}
p {
text-align: justify;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="polygon-shape"></div>
<p>This text flows around a custom polygon shape created using coordinate points. The polygon function accepts multiple coordinate pairs that define the vertices of your custom shape.</p>
<p>Complex geometric shapes like this star-like polygon create unique and eye-catching layouts that would be impossible with traditional rectangular text flows.</p>
</body>
</html>
An orange star-like polygon shape appears on the right side, with text wrapping around its angular edges, demonstrating how complex geometric shapes can create distinctive layout patterns.
Browser Support and Best Practices
Most modern browsers support CSS Shapes, but some features like the path() function are no longer supported. Always test across different browsers and provide fallbacks when necessary.
For optimal performance, avoid overly complex shapes with too many coordinate points. Use percentage-based dimensions for responsive layouts and implement media queries to ensure shapes work well across different screen sizes.
Accessibility Considerations
Ensure adequate spacing between text and shapes using shape-margin. Avoid overly complex designs that might hinder readability, especially for users with visual impairments or on smaller screens.
Conclusion
CSS Shapes provide a powerful way to create dynamic, magazine-style layouts that break free from rectangular constraints. Whether using simple circles or complex polygons, these techniques enable more creative and engaging web designs while maintaining proper text flow and readability.
