{"id":1084,"date":"2018-08-03T23:55:15","date_gmt":"2018-08-03T15:55:15","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1084"},"modified":"2018-09-05T11:17:26","modified_gmt":"2018-09-05T03:17:26","slug":"flexible-css-rule-sets-to-keep-your-images-within-their-parent-element","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/flexible-css-rule-sets-to-keep-your-images-within-their-parent-element\/","title":{"rendered":"Flexible CSS rule-sets to keep your images within their parent element"},"content":{"rendered":"<p>Since an image is worth a thousand words, we use images in your webpage to express complex ideas succinctly.<\/p>\n<p>By default, if we do not specify any dimensions for an image, most browsers will show the image according to its actual size. In this situation, an image will appear to spill over the boundaries of its parent element if it is bigger.  <\/p>\n<p>For example, the following screenshot shows the image at <a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:LARGE_elevation.jpg\" rel=\"noopener\" target=\"_blank\">https:\/\/commons.wikimedia.org\/wiki\/File:LARGE_elevation.jpg<\/a> spilling over its parent element:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/example-of-large-image-overspilling-the-parent-container.jpg\" alt=\"Example of large image overspilling the parent container\" \/><\/p>\n<p>One way to prevent a larger image from going beyond the boundaries of its parent element is to give a standard width to all images. However, if the actual width of an image is smaller than the given width, the image will be stretched:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/example-of-small-image-being-stretched-to-fill-parent-container.jpg\" alt=\"Example of small image being stretched to fill parent container\" \/><\/p>\n<p>This post shows the CSS rule-sets that you can use for flexibly keeping images within their parent element. <\/p>\n<h2>Example CSS rule-sets and HTML codes that keep an image within its parent element<\/h2>\n<p>When an image is smaller that the parent element, we want the browser to show the image in its actual dimensions instead of stretching to fill the parent element.<\/p>\n<p>On the other hand, if an image is too large for the parent element, we want the browser to show the image in a dimension that nicely fit the parent element.<\/p>\n<p>With this purpose in mind, we can use the <code>max-width<\/code> CSS property to indicate the maximum size that an image can be displayed:<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\nimg {\n    max-width: 100%;\n}\n<\/pre>\n<p>In order to see the effect of the <code>max-width<\/code> CSS property on images, we can create an example for flexibly keeping an image within its parent element:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n  &lt;head&gt;\n    &lt;title&gt;Flexible CSS rule-sets to keep your images within their parent element&lt;\/title&gt;\n    &lt;style&gt;\n      img {\n        max-width: 100%;\n      }\n    \t.container {\n    \t\tbackground-color: #CCC;\n    \t\tpadding: 2rem;\n    \t\tmargin: auto;\n    \t\twidth: 50%;\n    \t}\n    &lt;\/style&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;div class=&quot;container&quot;&gt;\n    \t&lt;h1&gt;Flexible CSS rule-sets to keep your images within their parent element&lt;\/h1&gt;\n    \t&lt;img src=&quot;https:\/\/sites.google.com\/site\/tcperpetual\/bigimage\/1280px-LARGE_elevation.jpg&quot;\/&gt;\n    &lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>When rendered by a browser, the above codes will look like the following:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/example-of-a-large-image-staying-within-boundary-of-its-parent-element.jpg\" alt=\"Example of a large image staying within boundary of its parent element\" \/><\/p>\n<p>You can try to resize your browser to see if the image is always displayed within the parent element. <\/p>\n<p>In addition, you can try to change the image to one with a smaller width than the parent element. In this situation, the image will be displayed in its actual dimensions.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-hu\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-hu&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-hu&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1084&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Since an image is worth a thousand words, we use images in your webpage to express complex ideas succinctly.<\/p>\n<p>By default, if we do not specify any dimensions for an image, most browsers will show the image according to its actual size. In this situation, an image will appear to spill over the boundaries of its parent element if it is bigger.  <\/p>\n<p>For example, the following screenshot shows the image at <a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:LARGE_elevation.jpg\" rel=\"noopener\" target=\"_blank\">https:\/\/commons.wikimedia.org\/wiki\/File:LARGE_elevation.jpg<\/a> spilling over its parent element:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/example-of-large-image-overspilling-the-parent-container.jpg\" alt=\"Example of large image overspilling the parent container\" \/><\/p>\n<p>One way to prevent a larger image from going beyond the boundaries of its parent element is to give a standard width to all images. However, if the actual width of an image is smaller than the given width, the image will be stretched:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/example-of-small-image-being-stretched-to-fill-parent-container.jpg\" alt=\"Example of small image being stretched to fill parent container\" \/><\/p>\n<p>This post shows the CSS rule-sets that you can use for flexibly keeping images within their parent element. <\/p>\n","protected":false},"author":1,"featured_media":1087,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[17,16,206,207],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/example-of-a-large-image-staying-within-boundary-of-its-parent-element.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-hu","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1084"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1084"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1084\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1087"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}