A common problem when updating articles on your own website is that the article illustrations are too wide, causing the entire web page to be distorted. It would be too troublesome to scale each illustration before inserting it.
I encountered this kind of thing in an article I wrote some time ago. Later, I used CSS overflow and max-width properties to temporarily solve the problem of page deformation. The advantage of this method is that it is simple, but the disadvantage is that it will destroy the effect of some details.
For example, overflow:hidden means that when the width of the internal element is greater than the parent frame, the excess width is hidden. Doing so may cause some content to be suddenly cut off and hidden, which is a pity for the audience.
If you limit the maximum width of article illustrations through the max-width attribute, you need to consider the compatibility of each browser. IE6 does not support this attribute. In my impression, although some browsers support this attribute, the images are not scaled proportionally (it seems to be Safari and Opera, I can’t remember clearly). If this is done, it will be harmful to users of these browsers. Very unfair.
Therefore, what I finally chose was to dynamically change the image size through JavaScript. This method has good compatibility with various browsers (very few people would disable JavaScript nowadays, right?), and if the theme is changed, the maximum size of the article illustrations can also be flexibly changed.
There are two solutions. Since the theme I am using is loaded with the jQuery library, it can be implemented with the following code:
The following is the quoted content:$ ( document ) . ready ( function ( ) { |
If you don't want to load the jQuery library, you can use the following code:
The following is the quoted content:function ResizeImage ( image , maximum width of illustration , maximum height of illustration ) |
Using pure JavaScript is a little more troublesome, as you need to manually add class="Thumbnail" to the image, but the final effect is the same.
Original text: Dynamically adjust image size
Thanks bolo for your contribution