how to draw a rectangle in HTML or CSS?
Categories:
Drawing Rectangles in HTML and CSS: A Comprehensive Guide

Learn various techniques to create and style rectangles using HTML elements and CSS properties, from basic divs to advanced pseudo-elements and SVG.
Rectangles are fundamental shapes in web design, used for everything from layout containers and buttons to intricate UI components. While HTML doesn't have a direct <rectangle>
tag, you can easily create them using standard HTML elements combined with CSS styling. This article explores several methods to draw and customize rectangles, catering to different use cases and complexity levels.
Method 1: The Basic <div>
Element
The most common and straightforward way to create a rectangle is by styling a <div>
element. By default, a <div>
is a block-level element that occupies 100% of its parent's width. You can define its dimensions using CSS width
and height
properties, and add visual characteristics like background-color
, border
, and border-radius
.
<div class="basic-rectangle"></div>
.basic-rectangle {
width: 200px;
height: 100px;
background-color: #3498db;
border: 2px solid #2980b9;
border-radius: 5px;
}
%
), viewport units (vw
, vh
), or em
/rem
for width
and height
instead of fixed pixel values.Method 2: Using Pseudo-elements (::before
and ::after
)
Pseudo-elements like ::before
and ::after
allow you to insert content before or after the content of an element without adding extra markup to your HTML. This is particularly useful for creating decorative rectangles, overlays, or complex shapes that are visually tied to another element but don't need to be part of its semantic structure.
<button class="button-with-rectangle">
Click Me
</button>
.button-with-rectangle {
position: relative;
padding: 10px 20px;
background-color: #2ecc71;
color: white;
border: none;
cursor: pointer;
overflow: hidden; /* Important for containing pseudo-element */
}
.button-with-rectangle::before {
content: '';
position: absolute;
top: -10px;
left: -10px;
width: 50px;
height: 50px;
background-color: rgba(255, 255, 255, 0.2);
transform: rotate(45deg);
z-index: 0;
}
graph TD A[HTML Element] --> B{Has ::before or ::after?} B -->|Yes| C[Pseudo-element created] C --> D[Styled as rectangle with CSS] B -->|No| E[No pseudo-element rectangle]
Flowchart of pseudo-element rectangle creation
Method 3: SVG for Scalable Rectangles
Scalable Vector Graphics (SVG) offers a powerful way to draw shapes, including rectangles, directly within your HTML. SVG rectangles are resolution-independent, meaning they scale perfectly without pixelation, making them ideal for logos, icons, and complex graphics. You can embed SVG inline or reference it as an external file.
<svg width="200" height="100">
<rect x="0" y="0" width="200" height="100"
fill="#e74c3c" stroke="#c0392b" stroke-width="3"
rx="10" ry="10" />
</svg>
rx
and ry
attributes in SVG's <rect>
element control the x-axis and y-axis radius of the corners, respectively, allowing you to create rounded rectangles directly in SVG.Choosing the Right Method
The best method for drawing a rectangle depends on your specific needs:
<div>
with CSS: Best for general layout, content containers, buttons, and most UI elements where the rectangle is a primary structural component.- Pseudo-elements: Ideal for decorative elements, hover effects, subtle backgrounds, or when you want to add a visual rectangle without cluttering your HTML with extra tags.
- SVG: Perfect for scalable graphics, icons, logos, and when you need precise control over vector shapes that must look sharp at any resolution.

Comparison of different methods for drawing rectangles