- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS Data Type - <percentage>
The CSS <percentage> data type represents a proportion of another value, typically relative to the parent element or some other reference value. Percentages are often used in various CSS properties (such as font-size, width, height, margin, and padding) to specify dimensions, spacing, or other proportional values.
Syntax
<number>%
A <number> and the percentage symbol (%) come together to form the <percentage> data type. A single + or - sign is an optional way to start it, albeit not all properties allow negative values. Unlike other dimension kinds, CSS dimensions do not have a gap between the symbol and the number.
CSS <percentage> - Sizing Font
The following example demonstrates the usage of <percentage> datatype to size the font.
<html> <head> <style> body, html { margin: 0; padding: 0; } .container { width: 90%; /* Set container width to 90% of the viewport width */ background-color: #f0f0f0; margin: 40px auto; } .text { font-size: 100%; /* Default font size (16px) */ text-align: center; margin: 20px 0; } .text-smaller { font-size: 80%; /* Font size 80% of the default size */ color: red; } .text-larger { font-size: 120%; /* Font size 120% of the default size */ color: blue; } </style> </head> <body> <div class="container"> <p class="text">Font Size with Percentage</p> <p class="text text-smaller">Smaller Font Size</p> <p class="text text-larger">Larger Font Size</p> </div> </body> </html>
CSS <percentage> - Sizing A Box
The following example demonstrates the usage of <percentage> datatype to alter the size of container and box.
<html> <head> <style> body, html { margin: 0; padding: 0; } .container { width: 70%; /* Set container width to 70% of the viewport width */ margin: 20px auto; background-color: #f4f4f4; padding: 20px; } .box { width: 40%; /* Box width set to 40% of its container */ height: 200px; background-color: #3498db; color: white; text-align: center; line-height: 200px; margin: 0 auto 20px; } .box:nth-child(2) { width: 60%; /* Box width set to 60% of its container */ height: 160px; background-color: #e74c3c; line-height: 160px; } </style> </head> <body> <div class="container"> <div class="box"> <p>Box 1 - 40% of container width</p> </div> <div class="box"> <p>Box 2 - 60% of container width</p> </div> </div> </body> </html>
To Continue Learning Please Login
Login with Google