
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- 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
Width and Height of Elements in CSS
The CSS height and width property are used to specify the height and width of an element respectively. We can also set the maximum and minimum values for these properties using the max-height, max-width, min-height, and min-width properties.
Syntax
The syntax of CSS height and CSS width property is as follows −
Selector { height: /*value*/ width: /*value*/ }
The actual width and height of elements is calculated as follows −
Box Size | Calculation |
---|---|
Total Width | width + padding-left + padding-right + border-left + border-right + margin-left + margin-right |
Total Height | height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin-bottom |
Example
The following examples illustrate CSS height and CSS width properties −
<!DOCTYPE html> <html> <head> <style> #demo { margin: auto; width: 400px; height: 80px; border: 2px solid black; display: flex; border-radius: 15px; } #demo div { flex: 1; border: thin dotted; border-radius: 50%; line-height: 60px; text-align: center; } #orange { box-shadow: inset 0 0 8px orange; } #green { box-shadow: inset 0 0 8px green; } #blue { box-shadow: inset 0 0 8px blue; } #red { box-shadow: inset 0 0 8px red; } </style> </head> <body> <div id="demo"> <div id="orange">Somebody</div> <div id="green">that I</div> <div id="blue">used</div> <div id="red">to know</div> </div> </body> </html>
Output
This gives the following output −
Example
<!DOCTYPE html> <html> <head> <style> article { margin: 10% 35%; box-shadow: 0 0 6px 1px black; max-width: 200px; max-height: 150px; overflow: auto; } </style> </head> <body> <h2>Java 8 Features</h2> <article> Lambda expression adds functional processing capability to Java. Referencing functions by their names instead of invoking them directly. Interface to have default method implementation. New compiler tools and utilities are added like ‘jdeps’ to figure out dependencies. New stream API to facilitate pipeline processing. Improved date time API. Emphasis on best practices to handle null values properly. Nashorn, a Java-based engine to execute JavaScript code. </article> </body> </html>
Output
This gives the following output −
- Related Articles
- The width and height properties in CSS
- Change the width and height of element using CSS
- How to set viewport height and width in CSS
- Set the height and width of an image using percent with CSS
- Add CSS transition effect for both the width and height property
- Set min-width and max-width of an element using CSS
- How to set height equal to the dynamic width (CSS fluid layout) in JavaScript?
- How to use height and width attributes in HTML?
- How to set cell width and height in HTML?
- How to get the height and width of the android.widget.ImageView?
- Get the width and height of a three-dimensional array
- How to get the Width and Height of the screen in JavaScript?
- Getting the Largest Window Height and Width of the Console in C#
- How to get the width and height of an android.widget.ImageView in Kotlin?
- Getting screens height and width using Tkinter Python

Advertisements