CSS Width

CSS Width Examples

The entire div has a width of auto so it will adjust according to browser while the table width is set as 90% of the div width

Element Description
div div has a width of auto in pink background with yellow border-width of 3px
p paragrpah below has a width of 90% of main width and is in powderblue background with orange border-width of 3px

This element has border width of 3px

CSS width

“css width” property sets the width of the elements. The important width elements are –

  • border width
  • CSS max-width
  • CSS  min-width
  • div width

CSS border width

The border width is the width of the border of the element.

“CSS Borders” is an important topic.

Syntax of border-width:
border-width: units|%|auto|initial;

Where units can take values in units like px, em, length, %, etc.

auto: This will automatically calculate the value for width based on the browser.

initial: initial set the value to the initial default value.

CSS Border width

Example of CSS Border width

<style>
.border-width { 
   border: 3px solid aqua;
   width:300px;
   background-color: pink;
} 
</style>

CSS div width

The div tag creates division or sections.  width property sets the width of the div.

You can also set the maximum & minimum width of the div using max-width and min-width property.

Example of CSS div width

<style>
.div_width {
   width:600px;
   max-width:700px;
   min-width:200px;
   background-color: tomato;
} 
</style>

CSS Layout - max-width & min-width

A CSS Layout defines the width of the element is the actual width of the element under normal circumstances.

  • The max-width is the maximum width of the element in a particular device.
  • The min-width is the minimum width of the element in a particular device.

max-width & min-width are important when the size of the device is different from a normal computer. For example – Tablets, mobile Devices etc.

Note/Info:

Normally,

  1. max-width >= width
  2. min-width <= width

The standard rules are-

If max-width >= width

For Example:
width: 180px;max-width:200px;

 Then, normal width of element is 180px but maximum width of element is 200px.

If max-width < width

For Example:
width: 200px;max-width:180px ;

Then, the normal width of the element is 180px and it is maximum width also.

If min-width <= width.

For Example:
width: 180px;min-width:100px;

Then, normal width of element is 180px but minimum width element is 100px.

If min-width > width.

For Example:
width: 100px;min-width:180px ;

 Then, the normal width of the element is 180px and it is minimum width also.

CSS Interview Questions and Answers

CSS border is the area around elements. Read complete details on CSS borders.

Table width is set using width property.

Example:
In Inline CSS: 

<table style="width:500px">

Similarly, you can set the width for a table using a class or id as well. You can also set the Table column width using width property.

CSS media max-width property is an extremely important width property of media queries. Example:

@media only screen and (max-width:400px) {
  body {
  color:blue;
  }
}

Some other width properties in CSS are:-

  • css:background width
  • css:text width
  • background image height and width in css
  • css:outline width
  • css:font width
  • css:white space width
  • css:float width

Width property is used to set div width. We can also set a maximum and a minimum width of div.

Example

.div_width {
   width:600px;
   max-width:700px;
   min-width:200px;
   border:2px solid red;
   } 

Max-width defines the maximum width of the specified device.
Syntax:

width: 180px;
min-width:200px;

Min-width defines the minimum width of the specifies device.
Syntax:

width:200px;
max-width:180px ;

Tutorials for all brains!