
- 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
The width and height properties in CSS
We can define the height and width exclusively for the element’s content, though these properties do not include margins, paddings or borders.
Syntax
The syntax of CSS height property is as follows −
Selector { height: /*value*/ }
The syntax of CSS width property is as follows −
Selector { width: /*value*/ }
Let us see an example of width and height properties −
Example
<!DOCTYPE html> <html> <head> <title>CSS height and width</title> </head> <style> * { padding: 2px; margin:5px; } button { border-radius: 10px; } #containerDiv { width:70%; margin: 0 auto; padding:20px; background-image: linear-gradient(62deg, #fbab7e 0%, #f7ce68 100%); text-align: center; border-radius: 10px; } #contentDiv2{ width:200px; height: 200px; opacity: .5; border:1px solid black; } </style> <body> <div id="containerDiv"> <div id="contentDiv1"> This is paragraph 1 with some dummy text. </div> <div id="contentDiv2"> This is paragraph 2 with some dummy text. </div> </div> <script> </script> </body> </html>
Output
Let us see another example of width and height properties −
Example
<!DOCTYPE html> <html> <head> <title>CSS height and width</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } #containerDiv { margin: 0 auto; height: 150px; width:250px; } </style> </head> <body> <form> <fieldset> <legend>CSS height and width</legend> <div id="containerDiv"> <img id="image" src="https://www.tutorialspoint.com/tensorflow/images/tensorflow-mini-logo.jpg"> </div> </fieldset> </form> </body> </html>
Output
- Related Articles
- Width and Height of Elements in CSS
- Change the width and height of element using CSS
- How to set viewport height and width in CSS
- Add CSS transition effect for both the width and height property
- Set the height and width of an image using percent with CSS
- Set the width, line style and color properties for an outline in a single statement with CSS
- How to set height equal to the dynamic width (CSS fluid layout) in JavaScript?
- How to get the height and width of the android.widget.ImageView?
- How to use height and width attributes in HTML?
- How to set cell width and height in HTML?
- The border-width Property in CSS
- The max-width Property in CSS
- The outline-width Property in CSS
- The min-width Property in CSS
- How to get the Width and Height of the screen in JavaScript?

Advertisements