What's the difference between the HTML width / height attribute and the CSS width / height property on the img element?

前端 未结 7 1147
说谎
说谎 2020-12-02 01:34

The HTML element can have the width / height attribute, and it can also have the CSS width /

相关标签:
7条回答
  • 2020-12-02 01:58

    The difference is in semantic and general approach rather than in how the rendering will work. :) I personally prefer to concentrate all things like width and heights within CSS classes and avoid to use both attributes like "width" and inline styles like "style='width:100px'", just because it results in nice logical separation - HTML markup tells what should be displayed and CSS rules - how exactly it will look.

    0 讨论(0)
  • 2020-12-02 02:04

    They have the same effect.

    <img width="100" height="100" /> has been used for a long time, same with the widht/height properties of say.. an HTML table.

    There is no difference whether you specify it on the element itself or within the CSS, though I now prefer using CSS so I can keep the HTML clear and concise.

    Here's an example http://jsfiddle.net/N2RgB/1/

    I've loaded the same image 4 times, both proportional and non-proportional using HTML attributes AND CSS properties.

    There is absolutely NO difference.

    0 讨论(0)
  • 2020-12-02 02:09

    you can try this

    <img src="some_pic.jpg" width="100" />
    

    it's height will be auto-size.

    and this

    <img src="some_pic.jpg" style="width:100px" />
    

    it's height will not be auto-size.

    just try more,and you know the difference

    0 讨论(0)
  • 2020-12-02 02:10

    A hot debate about the subject can be found here: Width attribute for image tag versus CSS

    To sum it up:

    The gain from declaring a width value and an height value (which may not be the original physical dimensions of the image) or from css declarations (like width: [valueX]; height: [valueY];) is that it helps speed up the rendering of the page. The browser knows how much space to allocate a particular area of the page: it will even draw image placeholders on a first draw, a first parsing+rendering of the page. When one does not define any width and height, then the browser has to download the image and then figure out its dimensions, space to allocate and then redraw the page.

    This seems to be the most beneficial effect in my opinion.

    0 讨论(0)
  • 2020-12-02 02:10

    I can see a difference... Check the following code snippet out stolen from: http://jqueryui.com/resources/demos/button/icons.html

        <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title>jQuery UI Button - Icons</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    
    <!-- redefine source of background-image -->
    <style>
        .ui-icon { width: 64px; height: 64px; }
    </style>
    
    <script type="text/javascript" >
    $(function(){
        $("img").button({
            icons: {
                primary: "ui-icon-locked"
            },
            text: false
        });
    });
    </script>
    </head>
    <body>
    
    <img width="32px" height="32px"> <!-- size of img tag -->
    
    </body>
    </html>
    

    This is the result:

    enter image description here

    0 讨论(0)
  • 2020-12-02 02:19

    http://www.w3schools.com/tags/tag_IMG.asp

    • I haven't used the image tag in a while for this purpose. But there is a difference, the attributes can be relative to the images dimensions, CSS style properties are absolute (either %, px, em...)
    0 讨论(0)
提交回复
热议问题