0% found this document useful (0 votes)
12 views49 pages

CSS Box Model

The document provides an overview of the CSS Box Model, explaining the components such as content, padding, border, and margin. It also covers CSS background properties, border properties, and the box-shadow property, including examples for each. Additionally, it discusses how to set transparency and use shorthand properties for more efficient CSS coding.

Uploaded by

roshannramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views49 pages

CSS Box Model

The document provides an overview of the CSS Box Model, explaining the components such as content, padding, border, and margin. It also covers CSS background properties, border properties, and the box-shadow property, including examples for each. Additionally, it discusses how to set transparency and use shorthand properties for more efficient CSS coding.

Uploaded by

roshannramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

CSS

CSS Box model


CSS Box model

 All HTML elements can be considered as boxes.

 In CSS, "box model" - design and layout.


CSS Box Model
1. Content - text and images appear.

2. Padding - Clears an area around the

content. It is transparent.

3. Border - goes around the padding and


content.

4. Margin - Clears an area outside the


border. It is transparent.
 <div> tag

- defines a division or a section in an HTML document.

- used as a container for HTML elements - which is then styled with


CSS or manipulated with JavaScript.

- easily styled by using the class or id attribute.


Example
 <html>
 <head>
 <style>
 div {
 width: 320px;
 padding: 10px;
 border: 5px solid red;
 margin: 0;
 }
 </style>
 </head>
 <body>

 <h2>Calculate the total width:</h2>

 <img src="/home/mahi/Desktop/html/download.jpeg" width="350"


height="263" alt="computer">
 <div>The picture above is having the width of 350.</div>

 </body>
 </html>
Width and Height of an Element

 Set the width and height properties of an element with CSS, it


can be set for content area.

 To calculate the full size of an element- padding, borders and


margins.
CSS Background
CSS Background
 The CSS background properties are used to define the
background effects for elements.

 Background properties
- background-color
- background-image
- background-repeat
- background-attachment
- background-position
CSS background-color

 The background-color property specifies the background color


of an element.

Example
The background color of a page is set like this:

body {
background-color: lightblue;
}
CSS Background…

With CSS, a color is most often specified by:

 a valid color name - like "red“

 a HEX value - like "#ff0000“

 an RGB value - like "rgb(255,0,0)"


Other Elements

Background color for any HTML elements


Example

<h1>, <p>, and <div> elements will have different background colors

h1 {
background-color: green;
}

div {
background-color: lightblue;
}

p{
background-color: yellow;
}
Opacity / Transparency

 The opacity property specifies the opacity/transparency of an


element.

 It can take a value from 0.0 - 1.0.


Example
 <html>
 <head>
 <style>
 div {
 background-color: red;
 }
 div.first {
 opacity: 0.1;
 }
 div.second {
 opacity: 0.4;
 }
 div.third {
 opacity: 0.86;
 }
 </style>
 </head>
 <body>
 <h1>Transparent Box</h1>
 <p>Display with opacity</p>

 <div class="first">
 <h1>display with 0.1</h1>
 </div>

 <div class="second">
 <h1>display with 0.4</h1>
 </div>

 <div class="third">
 <h1>display with 0.86</h1>
 </div>
 </body>
 </html>
 When using the opacity property to add transparency to the
background of an element, all of its child elements inherit the
same transparency.

 This can make the text inside a fully transparent element hard
to read.
Transparency using RGBA

 For not to apply opacity to child elements, use RGBA color


values

 sets the opacity for the background color and not the text

 div {
 background: rgba(0, 128, 0, 0.2)
 }
CSS Background Image

 The background-image property specifies an image to use as


the background of an element.

 By default, the image is repeated so it covers the entire


element.

Example
body {
background-image: url("p1.gif");
}
 When using a background image, use an image that does not
disturb the text.
CSS Background Repeat

 By default, the background-image property repeats an image both


horizontally and vertically.

 Some images should be repeated only horizontally or vertically.

Example
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
To repeat an image vertically, set background-repeat: repeat-y;
CSS background-repeat: no-repeat

 Showing the background image only once is also specified by


the background-repeat property:

Example
Show the background image only once:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
CSS background-position

 The background-position property is used to specify the


position of the background image.

Example
Position the background image in the top-right corner

body {
background-image: url("img1.png");
background-repeat: no-repeat;
background-position: right top;
}
CSS Background Attachment

 The background-attachment property specifies whether the background


image should scroll or be fixed (will not scroll with the rest of the page)
Example

body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}

 background-attachment: scroll - image will scroll


CSS background - Shorthand property
 To shorten the code, it is also possible to specify all the background
properties in one single property.

body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
The above can be written as follows:
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
CSS Borders
CSS Border Properties

 Allows to specify the style, width, and color of an element's border.


CSS Border Style

The border-style property specifies what kind of border to display.

The border-style property can have from one to four values (for
the top border, right border, bottom border, and the left border).
Border styles:

- p.dotted {border-style: dotted;}


- p.dashed {border-style: dashed;}
- p.solid {border-style: solid;}
- p.double {border-style: double;}
- p.groove {border-style: groove;}
- p.ridge {border-style: ridge;}
- p.inset {border-style: inset;}
- p.outset {border-style: outset;}
- p.none {border-style: none;}
- p.hidden {border-style: hidden;}
- p.mix {border-style: dotted dashed solid double;}
Example
 <html>
 <head>
 <style>
 p.dotted {border-style: dotted;}
 p.solid {border-style: solid;}
 p.mix {border-style: dotted dashed solid double;}
 </style>
 </head>
 <body>
 <h2>The border-style Property</h2>
 <p>This property specifies what kind of border to display:</p>
 <p class="dotted">A dotted border.</p>
 <p class="solid">A solid border.</p>
 <p class="mix">A mixed border.</p>
 </body>
 </html>
CSS Border Width

 The border-width property specifies the width of the four


borders.

 The width can be set as a specific size (in px, pt, cm, em, etc)
or by using one of the three pre-defined values: thin, medium,
or thick:
Example
p.one {
border-style: solid;
border-width: 5px;
}
p.two {
border-style: solid;
border-width: medium;
}
p.three {
border-style: dotted;
border-width: 2px;
}
p.four {
border-style: dotted;
border-width: thick;
}
CSS Border Color

 The border-color property is used to set the color of the four borders.
 The color can be set by:

 name - specify a color name, like "red"


 HEX - specify a HEX value, like "#ff0000"
 RGB - specify a RGB value, like "rgb(255,0,0)"
 HSL - specify a HSL value, like "hsl(0, 100%, 50%)"
 Transparent
Example Result
p.one {
border-style: solid;
border-color: red;
}

p.two {
border-style: solid;
border-color: green;
}

p.three {
border-style: dotted;
border-color: blue;
}
Specific Side Colors
The border-color property can have from one to four values (for
the top border, right border, bottom border, and the left border).

Example
p.one {
border-style: solid;
border-color: red green blue yellow; /* red top, green
right, blue bottom and yellow left */
}
HEX Values

 The color of the border can also be specified using a


hexadecimal value (HEX):

Example
p.one {
border-style: solid;
border-color: #ff0000; /* red */
}
RGB Values

By using RGB values:

Example
p.one {
border-style: solid;
border-color: rgb(255, 0, 0); /* red */
}
CSS Border - Individual Sides

To specify each side of the borders (top, right, bottom, and left)

Result
Example

p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
Border-style property has four values

 border-style: dotted solid double dashed;

- top border is dotted


- right border is solid
- bottom border is double
- left border is dashed
Example

 /* Four values */

p{
border-style: dotted solid double dashed;
}

/* Three values */

p{
border-style: dotted solid double;
}

/* Two values */

p{
border-style: dotted solid;
}

/* One value */

p{
border-style: dotted;
}
CSS Border – Shorthand Property

 There are many properties to consider when dealing with borders.

 To shorten the code, it is also possible to specify all the individual border
properties in one property.

 The border property is a shorthand property for the following individual


border properties:

- border-width
- border-style (required)
- border-color
Example Result

p{
border: 5px solid red;
}
CSS Rounded Borders

 The border-radius property is used to add rounded borders to an element

Example
p{
border: 2px solid red;
border-radius: 5px;
}
Box Shadow
The CSS box-shadow property is used to apply one or more shadows to an element.

In its simplest use, specify a horizontal and a vertical shadow. The default color of the shadow is the current text-color.

The color parameter defines the color of the shadow.

The blur parameter defines the blur radius. The higher the number, the more blurred the shadow will be.

The spread parameter defines the spread radius. A positive value increases the size of the shadow, a negative value decreases the size
of the shadow.

Example:
A <div> element with a 5px blurred, lightblue box-shadow with a spread radius of 12px.

div {
box-shadow: 10px 10px 5px 12px lightblue;
}
The inset parameter changes the shadow from an outer shadow (outset) to an inner shadow.

Example:
div {
box-shadow: 10px 10px 5px lightblue inset;
}
Example

<html>
<head>
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
background-color: coral;
box-shadow: 10px 10px 5px 12px lightblue;
}
</style>
</head>
<body>

<h1>The box-shadow Property</h1>

<div>
A div element with a blurred, lightblue box-shadow, with a spread radius of 12px.
</div>

</body>
</html>
 The position property specifies the type of positioning method
used for an element (static, relative, absolute, fixed, or sticky).
Resource

 w3schools.com
 w3resource.com

Prof. N. Maheswari, VIT Chennai 49

You might also like