SlideShare a Scribd company logo
7
ADDING IMAGES
OVERVIEW
• Web image formats
• The img element
• Attributes for the img element
• Adding an SVG to a page
Web Image Formats
Image formats appropriate for web pages:
• PNG
• JPEG
• GIF
• SVG
• WebP (up and coming, not yet widely supported)
Bitmapped vs. Vector Formats
PNG, JPEG,
GIF, and WebP
are bitmapped
formats.
SVG is a vector
format.
The img Element
<img src="" alt="">
• Embed images on the page with the empty img element.
• The src and alt attributes are required.
• img can be used for PNG, JPEG, GIF, and SVG.
NOTE: Special markup is recommended for experimenting with cutting-
edge image formats like WebP.
The img Element (cont’d)
<p>This summer, try making pizza <img src="pizza.png" alt="">
on your grill.</p>
The img element tells the browser to make a server request for
the image and display it in its place:
The src attribute
• The value is the location (URL) of the image file.
• Use the relative pathname conventions to point to the image
on the server.
PERFORMANCE TIP: Take advantage of caching (temporary storage).
For the same image used repeatedly, using the same pathname
reduces the number of calls to the server.
<img src="/images/icon/next.png" alt="Next">
The alt Attribute
• The alt attribute provides
alternative text for those who
are not able to see it.
• It is required for every img
element in order for the HTML
to be valid.
• It is used by screen readers,
search engines, and graphical
browsers when the image fails
to load.
<p> If you're <img src="/images/smiley.png" alt="happy"> and
you know it, clap your hands.</p>
Alternative Text Tips
• Alternative text (alt text) should convey the same information and
function as the image.
• If the image is purely decorative and shouldn’t be read aloud, include
the alt attribute with an empty value:
<img src="flowers.svg" alt="">
• Consider what the alt text would sound like when read aloud by a
screen reader. Is it helpful or a hindrance?
• When an image is used as a link, the alt text serves as the linked text.
Write what you’d want the link to say, don’t just describe the image.
• Avoid starting alt text with “An image of” or “Graphic of”.
width and height Attributes
• The width and height attributes set the dimensions of the image
on the page in number of pixels.
• They help the browser maintain space for the image in the layout
while the files load.
• Don’t use width and height attributes if you are sizing the image
with style sheets or if the size changes in a responsive layout.
• If the attribute values do not match the dimensions of the image,
the image will resize and may be distorted or blurry.
<img src="flowers.svg" alt="" width="120" height="160">
Adding SVG Images
SVG images are unique:
• SVG is a vector format, made up of shapes and paths.
• Those shapes and paths are described in a text file using the
Scalable Vector Graphic markup language.
• The elements in an SVG can be styled with CSS and scripted
for interactivity.
• Because they are vector, SVGs can resize with no loss of
quality.
SVG Example
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 180">
<rect width="300" height="180" fill="purple" rx="20" ry="20"/>
<text x="40" y="114" fill="yellow" font-family="'Verdana-Bold'"
font-size="72">
hello!
</text>
</svg>
Compare the SVG source to the image. The svg element
contains a rectangle (rect element) and a text element:
Adding SVG to a Page
There are several ways to add an SVG image to a web page:
• <img> element
• <object> element
• <svg> element directly in HTML (inline SVG)
Adding SVG with the img Element
You can add an .svg file to the page with the img element:
<img src="/images/circle.svg" alt="">
PROS:
• Easy and familiar
• Universally supported
CONS:
• Can’t manipulate the SVG with scripts or styles.
• The SVG can’t contain any external resources such as
images or fonts.
Embedding SVGs with object
The content of the object element is a fallback text or image that
displays if the SVG is not supported:
<object type="image/svg+xml" data="pizza.svg">
<img src="pizza.png" alt="pizza">
</object>
PROS:
• SVG can be scripted and use eternal files (images and fonts).
CONS:
• You can’t change styles with the same CSS used for the document.
• May be buggy in some browsers.
Inline SVGs with the svg Element
You can paste the content of the SVG text file directly into the HTML
source. This is called using the SVG inline.
PROS:
• Can take full advantage of scripting and styling the SVG because
the elements in the SVG are part of the DOM for the document.
CONS:
• Code can get extremely long and unwieldy.
• Harder to maintain images embedded in the source
• Can’t take advantage of caching repeated images
• Not universally supported
Example of an Inline SVG
<p>This summer, try making pizza
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 72 72" width="100"
height="100">
<circle fill="#D4AB00" cx="36" cy="36" r="36"/>
<circle opacity=".7" fill="#FFF" stroke="#8A291C" cx="36.1" cy="35.9" r="31.2"/>
<circle fill="#A52C1B" cx="38.8" cy="13.5" r="4.8"/>
<circle fill="#A52C1B" cx="22.4" cy="20.9" r="4.8"/>
<circle fill="#A52C1B" cx="32" cy="37.2" r="4.8"/>
<circle fill="#A52C1B" cx="16.6" cy="39.9" r="4.8"/>
<circle fill="#A52C1B" cx="26.2" cy="53.3" r="4.8"/>
<circle fill="#A52C1B" cx="42.5" cy="27.3" r="4.8"/>
<circle fill="#A52C1B" cx="44.3" cy="55.2" r="4.8"/>
<circle fill="#A52C1B" cx="54.7" cy="42.9" r="4.8"/>
<circle fill="#A52C1B" cx="56" cy="28.3" r="4.8"/>
</svg>
on your grill.</p>

More Related Content

PPTX
Chapter 7: Images
PPTX
SVG - Scalable Vector Graphic
PPT
PDF
SVG For Web Designers (and Developers)
PPTX
HTML Images
PPTX
Inserting imagesin html
PPTX
SVG and the web
PPTX
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Chapter 7: Images
SVG - Scalable Vector Graphic
SVG For Web Designers (and Developers)
HTML Images
Inserting imagesin html
SVG and the web
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...

Similar to ClassJavaScript 1: Language Fundamentals06 .pdf (20)

PDF
The Image that called me - Active Content Injection with SVG Files
PPTX
Html images and html backgrounds
PPTX
Deep Dive into SVG and Its Applications.pptx
PPTX
SVG Icons and Screen Reader Accessibility
PDF
Accessibility on SVG and SEO
PDF
Accessibility on SVG and SEO
PDF
Intro to SVG
PPTX
PDF
Use SVG to Bring the Web to Life (Quinton Jason Jr)
PDF
SVG - Scalable Vector Graphics
PDF
Before Going Vector
PDF
Learn svg
PDF
SVG 101
PDF
Standard svg
PDF
"Responsive Web Design: Clever Tips and Techniques". Vitaly Friedman, Smashin...
PPTX
Html images syntax
PPTX
Session no 4
PPSX
Font icons vs inline svg - 2016
PPTX
Scalable vector graphics(svg)
PPTX
Accessibility Hacks Version 2
The Image that called me - Active Content Injection with SVG Files
Html images and html backgrounds
Deep Dive into SVG and Its Applications.pptx
SVG Icons and Screen Reader Accessibility
Accessibility on SVG and SEO
Accessibility on SVG and SEO
Intro to SVG
Use SVG to Bring the Web to Life (Quinton Jason Jr)
SVG - Scalable Vector Graphics
Before Going Vector
Learn svg
SVG 101
Standard svg
"Responsive Web Design: Clever Tips and Techniques". Vitaly Friedman, Smashin...
Html images syntax
Session no 4
Font icons vs inline svg - 2016
Scalable vector graphics(svg)
Accessibility Hacks Version 2
Ad

Recently uploaded (20)

PDF
Caterpillar CAT 311B EXCAVATOR (8GR00001-UP) Operation and Maintenance Manual...
PPTX
Paediatric History & Clinical Examination.pptx
PPT
Kaizen for Beginners and how to implement Kaizen
PDF
3-REasdfghjkl;[poiunvnvncncn-Process.pdf
PDF
Volvo EC290C NL EC290CNL Hydraulic Excavator Specs Manual.pdf
PDF
book-slidefsdljflsk fdslkfjslf sflgs.pdf
PDF
Renesas R-Car_Cockpit_overview210214-Gen4.pdf
PDF
Marketing project 2024 for marketing students
PDF
Caterpillar CAT 312B L EXCAVATOR (2KW00001-UP) Operation and Maintenance Manu...
PPTX
Intro to ISO 9001 2015.pptx for awareness
PDF
Volvo EC290C NL EC290CNL Excavator Service Repair Manual Instant Download.pdf
PPTX
1. introduction-to-bvcjdhjdfffffffffffffffffffffffffffffffffffmicroprocessors...
PDF
How Much does a Volvo EC290C NL EC290CNL Weight.pdf
PDF
Caterpillar Cat 315C Excavator (Prefix CJC) Service Repair Manual Instant Dow...
PPTX
Gayatri Cultural Educational Society.pptx
PPT
ACCOMPLISHMENT REPOERTS AND FILE OF GRADE 12 2021.ppt
PPTX
laws of thermodynamics with complete explanation
PPTX
building_blocks.pptxdcsDVabdbzfbtydtyyjtj67
PPTX
IMMUNITY TYPES PPT.pptx very good , sufficient
PDF
industrial engineering and safety system
Caterpillar CAT 311B EXCAVATOR (8GR00001-UP) Operation and Maintenance Manual...
Paediatric History & Clinical Examination.pptx
Kaizen for Beginners and how to implement Kaizen
3-REasdfghjkl;[poiunvnvncncn-Process.pdf
Volvo EC290C NL EC290CNL Hydraulic Excavator Specs Manual.pdf
book-slidefsdljflsk fdslkfjslf sflgs.pdf
Renesas R-Car_Cockpit_overview210214-Gen4.pdf
Marketing project 2024 for marketing students
Caterpillar CAT 312B L EXCAVATOR (2KW00001-UP) Operation and Maintenance Manu...
Intro to ISO 9001 2015.pptx for awareness
Volvo EC290C NL EC290CNL Excavator Service Repair Manual Instant Download.pdf
1. introduction-to-bvcjdhjdfffffffffffffffffffffffffffffffffffmicroprocessors...
How Much does a Volvo EC290C NL EC290CNL Weight.pdf
Caterpillar Cat 315C Excavator (Prefix CJC) Service Repair Manual Instant Dow...
Gayatri Cultural Educational Society.pptx
ACCOMPLISHMENT REPOERTS AND FILE OF GRADE 12 2021.ppt
laws of thermodynamics with complete explanation
building_blocks.pptxdcsDVabdbzfbtydtyyjtj67
IMMUNITY TYPES PPT.pptx very good , sufficient
industrial engineering and safety system
Ad

ClassJavaScript 1: Language Fundamentals06 .pdf

  • 2. OVERVIEW • Web image formats • The img element • Attributes for the img element • Adding an SVG to a page
  • 3. Web Image Formats Image formats appropriate for web pages: • PNG • JPEG • GIF • SVG • WebP (up and coming, not yet widely supported)
  • 4. Bitmapped vs. Vector Formats PNG, JPEG, GIF, and WebP are bitmapped formats. SVG is a vector format.
  • 5. The img Element <img src="" alt=""> • Embed images on the page with the empty img element. • The src and alt attributes are required. • img can be used for PNG, JPEG, GIF, and SVG. NOTE: Special markup is recommended for experimenting with cutting- edge image formats like WebP.
  • 6. The img Element (cont’d) <p>This summer, try making pizza <img src="pizza.png" alt=""> on your grill.</p> The img element tells the browser to make a server request for the image and display it in its place:
  • 7. The src attribute • The value is the location (URL) of the image file. • Use the relative pathname conventions to point to the image on the server. PERFORMANCE TIP: Take advantage of caching (temporary storage). For the same image used repeatedly, using the same pathname reduces the number of calls to the server. <img src="/images/icon/next.png" alt="Next">
  • 8. The alt Attribute • The alt attribute provides alternative text for those who are not able to see it. • It is required for every img element in order for the HTML to be valid. • It is used by screen readers, search engines, and graphical browsers when the image fails to load. <p> If you're <img src="/images/smiley.png" alt="happy"> and you know it, clap your hands.</p>
  • 9. Alternative Text Tips • Alternative text (alt text) should convey the same information and function as the image. • If the image is purely decorative and shouldn’t be read aloud, include the alt attribute with an empty value: <img src="flowers.svg" alt=""> • Consider what the alt text would sound like when read aloud by a screen reader. Is it helpful or a hindrance? • When an image is used as a link, the alt text serves as the linked text. Write what you’d want the link to say, don’t just describe the image. • Avoid starting alt text with “An image of” or “Graphic of”.
  • 10. width and height Attributes • The width and height attributes set the dimensions of the image on the page in number of pixels. • They help the browser maintain space for the image in the layout while the files load. • Don’t use width and height attributes if you are sizing the image with style sheets or if the size changes in a responsive layout. • If the attribute values do not match the dimensions of the image, the image will resize and may be distorted or blurry. <img src="flowers.svg" alt="" width="120" height="160">
  • 11. Adding SVG Images SVG images are unique: • SVG is a vector format, made up of shapes and paths. • Those shapes and paths are described in a text file using the Scalable Vector Graphic markup language. • The elements in an SVG can be styled with CSS and scripted for interactivity. • Because they are vector, SVGs can resize with no loss of quality.
  • 12. SVG Example <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 180"> <rect width="300" height="180" fill="purple" rx="20" ry="20"/> <text x="40" y="114" fill="yellow" font-family="'Verdana-Bold'" font-size="72"> hello! </text> </svg> Compare the SVG source to the image. The svg element contains a rectangle (rect element) and a text element:
  • 13. Adding SVG to a Page There are several ways to add an SVG image to a web page: • <img> element • <object> element • <svg> element directly in HTML (inline SVG)
  • 14. Adding SVG with the img Element You can add an .svg file to the page with the img element: <img src="/images/circle.svg" alt=""> PROS: • Easy and familiar • Universally supported CONS: • Can’t manipulate the SVG with scripts or styles. • The SVG can’t contain any external resources such as images or fonts.
  • 15. Embedding SVGs with object The content of the object element is a fallback text or image that displays if the SVG is not supported: <object type="image/svg+xml" data="pizza.svg"> <img src="pizza.png" alt="pizza"> </object> PROS: • SVG can be scripted and use eternal files (images and fonts). CONS: • You can’t change styles with the same CSS used for the document. • May be buggy in some browsers.
  • 16. Inline SVGs with the svg Element You can paste the content of the SVG text file directly into the HTML source. This is called using the SVG inline. PROS: • Can take full advantage of scripting and styling the SVG because the elements in the SVG are part of the DOM for the document. CONS: • Code can get extremely long and unwieldy. • Harder to maintain images embedded in the source • Can’t take advantage of caching repeated images • Not universally supported
  • 17. Example of an Inline SVG <p>This summer, try making pizza <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 72 72" width="100" height="100"> <circle fill="#D4AB00" cx="36" cy="36" r="36"/> <circle opacity=".7" fill="#FFF" stroke="#8A291C" cx="36.1" cy="35.9" r="31.2"/> <circle fill="#A52C1B" cx="38.8" cy="13.5" r="4.8"/> <circle fill="#A52C1B" cx="22.4" cy="20.9" r="4.8"/> <circle fill="#A52C1B" cx="32" cy="37.2" r="4.8"/> <circle fill="#A52C1B" cx="16.6" cy="39.9" r="4.8"/> <circle fill="#A52C1B" cx="26.2" cy="53.3" r="4.8"/> <circle fill="#A52C1B" cx="42.5" cy="27.3" r="4.8"/> <circle fill="#A52C1B" cx="44.3" cy="55.2" r="4.8"/> <circle fill="#A52C1B" cx="54.7" cy="42.9" r="4.8"/> <circle fill="#A52C1B" cx="56" cy="28.3" r="4.8"/> </svg> on your grill.</p>