Certainly! You can achieve this by utilizing CSS variables for customizable width, height, and vertex (dot) size. We'll use the ::before pseudo-element along with box-shadow to create the four square dots at each corner of the rectangle.
Here's how you can implement it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rectangle with Corner Dots</title>
<style>
.mysquare {
/* CSS Variables for customization */
--width: 200px; /* Width of the rectangle */
--height: 100px; /* Height of the rectangle */
--vertex-size: 15px; /* Size of the corner squares */
--vertex-color: #3498db;/* Color of the corner squares */
position: relative;
width: var(--width);
height: var(--height);
border: 2px solid #000; /* Optional: Border for the rectangle */
margin: 50px; /* Optional: Centering the rectangle */
}
.mysquare::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: var(--vertex-size);
height: var(--vertex-size);
background-color: var(--vertex-color);
/* Creating three more squares using box-shadow */
box-shadow:
calc(var(--width) - var(--vertex-size)) 0,
calc(var(--width) - var(--vertex-size)) calc(var(--height) - var(--vertex-size)),
0 calc(var(--height) - var(--vertex-size));
}
</style>
</head>
<body>
<div class="mysquare"></div>
</body>
</html>
Explanation:
-
CSS Variables:
--width: Defines the width of the rectangle.
--height: Defines the height of the rectangle.
--vertex-size: Defines the size (both width and height) of the square dots at each corner.
--vertex-color: (Optional) Defines the color of the square dots. You can change this to any color you prefer.
-
.mysquare Styles:
- Positioning & Sizing:
position: relative; ensures that the pseudo-element (::before) is positioned relative to .mysquare.
width and height are set using the CSS variables.
- Border & Margin:
border: Adds an optional border to visualize the rectangle.
margin: Centers the rectangle on the page with some spacing.
-
.mysquare::before Styles:
-
Creating the First Square:
- Positioned at the top-left corner (
top: 0; left: 0;).
- Sized using
--vertex-size.
- Colored using
--vertex-color.
-
Creating Additional Squares with Box-Shadow:
- Top-Right Corner:
calc(var(--width) - var(--vertex-size)) 0: Positions the shadow to the top-right.
- Bottom-Right Corner:
calc(var(--width) - var(--vertex-size)) calc(var(--height) - var(--vertex-size)): Positions the shadow to the bottom-right.
- Bottom-Left Corner:
0 calc(var(--height) - var(--vertex-size)): Positions the shadow to the bottom-left.
The box-shadow property allows us to add multiple shadows, each acting as an additional square dot at the specified corner.
Customization:
You can easily customize the rectangle and the corner dots by modifying the CSS variables. For example:
.mysquare {
--width: 300px;
--height: 150px;
--vertex-size: 20px;
--vertex-color: #e74c3c;
}
This will create a larger rectangle with bigger, red corner dots.
Demo:
Here's a live demonstration for you to see it in action:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Rectangle with Corner Dots</title>
<style>
.mysquare {
--width: 200px;
--height: 100px;
--vertex-size: 15px;
--vertex-color: #3498db;
position: relative;
width: var(--width);
height: var(--height);
border: 2px solid #000;
margin: 50px;
}
.mysquare::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: var(--vertex-size);
height: var(--vertex-size);
background-color: var(--vertex-color);
box-shadow:
calc(var(--width) - var(--vertex-size)) 0,
calc(var(--width) - var(--vertex-size)) calc(var(--height) - var(--vertex-size)),
0 calc(var(--height) - var(--vertex-size));
}
</style>
</head>
<body>
<div class="mysquare"></div>
</body>
</html>
You can copy and paste this code into an HTML file and open it in your browser to see the result.