- A width-responsive perfect square in pure CSS
- How the square is made
- How the circle is made
- Quick geometry recap
- CSS Shapes With Text Inside
- Step By Step Guide On CSS Shapes With Text Inside :-
- Conclusion :-
- How to Create a Responsive Square with CSS
- The Problem
- The Solution
- Adding Content
- Responsive CSS Squares and Rectangles (Simple Examples)
- TLDR – QUICK SLIDES
- TABLE OF CONTENTS
- DOWNLOAD & NOTES
- QUICK NOTES
- EXAMPLE CODE DOWNLOAD
- RESPONSIVE SQUARES & RECTANGLES
- 1) SQUARES & RECTANGLES USING RELATIVE UNITS
- HTML & CSS
- THE RESULT
- THE EXPLANATION
- 2) ASPECT RATIO TRICK
- HTML & CSS
- THE RESULT
- HOW IT WORKS
- 3) ADDING & CENTERING TEXT
- HTML & CSS
- THE RESULT
- THE EXPLANATION
- 4) OVERFLOW TEXT
- HTML & CSS
- THE RESULT
- THE EXPLANATION
- EXTRA BITS & LINKS
- LINKS & REFERENCES
- INFOGRAPHIC CHEAT SHEET
- THE END
- Leave a Comment Cancel Reply
- Search
- Breakthrough Javascript
- Socials
- About Me
A width-responsive perfect square in pure CSS
The resize property — I wasn’t aware you could actually control this past textareas, but stumbled upon it when looking for a way to make a user-resizable div without using JavaScript.
How the square is made
#square width: 100%; padding-bottom: 100%; position: relative; background-color: #2980b9; >
- width: 100% is to make sure it’s defined as having the same width as its outer, resizable container.
- padding-bottom: 100% is based on a trick in the CSS spec. When specifying a margin or padding by percentage, it’s always based on the width, even if it’s a -top or -bottom property. In this way we get a property that’s equal to the width.
- position: relative is so that I can put stuff inside it without disrupting the padding; any additional content would add content to the box, which would make it a rectangle
- background-color is there so you can see where it is ¯\_(ツ)_/¯
How the circle is made
Once you have any square, making a circle is easy enough. The only trick you really need is border-radius: 50% , but let’s break down the responsive circle a bit more.
#circle position: absolute; top: 0; bottom: 0; width: 100%; height: 100%; border-width: 4px; border-color: #27ae60; border-style: solid; border-radius: 50%; >
- position: absolute makes sure that this circle stays within the position: relative square while not adding any in-flow content to it.
- top: 0; bottom: 0; width: 100%; height: 100%; constrains the circle to the exact dimensions of the responsive square. At this point, what we have is a position: absolute container that is an inner copy of our square.
- The border-* properties are used to build the outline of the circle. We have a 4px green solid border, and it has a radius of half the edge of the square.
Quick geometry recap
Here’s an illustration of why 50% is the magic number to make a circle from a square:
The radius of the circle is equal to half the side of the square.
CSS Shapes With Text Inside
In this tutorial we will show you the solution of CSS shapes with text inside, as we know css used to style html elements, here we used internal style method to show demo.
We can create any shape in html and css here we will make square and triangle shape with respective shape name on it.
Here we used border style to create triangle shape and square we can make easily with just ‘width and height’ style property but we don’t have inbuilt method to make any shapes in html.
Step By Step Guide On CSS Shapes With Text Inside :-
We can implement styles using three types (i.e inline,external or internal) Internal means we have to define our style within tag in head block and inline means we have use style within element definition, external means we defined our css at separate file with .css extension we have to import by tag within head block.
In our program we defined two tags with attributes of classes with value ‘tri,sqaure’ and within that we defined two para
tag with attribute classes of ‘t,sq’ and texts.
Using them we defined some block of styles with property ‘border-bottom’ value ‘100px solid #555’ none of them we needs to make left and right side border to be ‘transparent’ then only we can get triangle shape and for square shape we just define width and height of div to be equal size that’s all.
.tri < width: 0; height: 0; border-left: 65px solid transparent; border-right: 65px solid transparent; border-bottom: 100px solid #555; >.square < width: 50px; height: 50px; background-color: green; position: absolute; margin-left: 20%; >.sq < text-align: center; >.tTriangle
Square
- tag which is instruct the web browser about what version of HTML file written in.
- The tag is used to indicate the beginning of HTML document.
- As above shown tag is containing information about webpage and if you need any external file those links are declared here. tag is used for set the webpage title.
- Within tag we defined block of styles. We created triangle shape by styles ‘width and height’ value to ‘0’, right and left side border to ‘65px thickness, solid border style,color to be transparent’ and bottom of border to be ‘100px width, solid border style, grey color’.
- Finally we gets triangle shape. Using class ‘t’ we can display text on triangle shape and for that we defined styles of ‘position:absolute’ makes text to be sticks on shape, for align that text to center of shape we defined ‘margin-top: 40px; and margin-left: -22px;’.
- For creates square shape we used class ‘.square’ within that we defined width and height to be same size so it’s ‘50px’, ‘position:absolute’ is must because without this empty div will show up on display. When we fills them with text so it will show up but it is not properly make shape because lack of text width.
- After that we sets ‘background-color’ to ‘green’ and ‘margin-left’ property to ‘20%’ makes available square shape after particular space.
- In class ‘.sq’ we defined ‘text-align’ property to value ‘center’ so we can move text to the center area of shape.
- Both and tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- Here two tags we defined with some classes ‘tri,square’ and also we puts ‘triangle,square’ texts on between tag then its styled by as we seen at point 4.
- Both , tags closed respectively.