How one can Override width and top HTML attributes with CSS


One of many HTML components that often comes into collision with CSS is the img ingredient. As we realized in Request Metrics’ Fixing Cumulative Format Shift Issues on DavidWalshBlog article, offering picture dimensions throughout the picture tag will assist to enhance your web site’s rating. However in a world the place responsive design is king, we’d like CSS and HTML to work collectively.

Most responsive design type changes are achieved through max-width values, however while you present a top worth to your picture, you may get a distorted picture. The aim ought to all the time be a show photographs in relative dimensions. So how will we make sure the top attribute does not battle with max-width values?

The reply is as simple as top: auto!

/* assuming any media question */
img {
  /* Make sure the picture does not go offscreen */
  max-width: 500px;
  /* Make sure the picture top is responsive no matter HTML attribute */
  top: auto;
}

The dance to please customers and engines like google is all the time a enjoyable steadiness. CSS and HTML have been by no means meant to battle however in some instances they do. Use this code to optimize for each customers and engines like google!

  • MooTools 1.2 Image Protector: dwProtector

    Picture safety is a sizzling matter on the web today, and why should not or not it’s? In the event you spent two hours designing an superior graphic, would you need it ripped of in matter of seconds? Hell no! That is why I’ve created a picture…

  • New MooTools Plugin:  ElementFilter

    My new MooTools plugin, ElementFilter, supplies a good way so that you can permit customers to look by the textual content of any mixture of components. Merely present a textual content enter field and ElementFilter does the remainder of the work. The XHTML I’ve used an inventory for this instance…