Is there an issue with images set at max-width: 100% displaying larger than their actual size when published?

I'm at my wits' end trying to figure this out. Here's the CSS code causing me trouble:

img {max-width:100%; height:auto; }

Whenever I upload an image to a blog post that is sized 200x200, like this:

<img width="200" title="photo-Trent-Dysmid.jpg" style="width: 200px; float: right;" alt="photo-Trent-Dysmid.jpg" src="http://inboundmarketing.digitalhive.buzz/hubfs/jan-2016-images/photo-Trent-Dysmid.jpg" data-constrained="true">

Upon previewing the blog post, it displays the image at 798x798, stretching it to fit the full width of the page. The only solution I've found is manually changing "width: 200px" to "max-width: 200px" each time. It's becoming quite a hassle! What am I missing here?

Appreciate any help! Rebekah

Answer №1

max-width: 100%

The percentage value in the CSS rule applies to the width of the container that holds the image, not the actual image itself!

For instance:

<div style="width: 500px"><img src="..." style="max-width: 100%" /></div>

In this example, if the container has a width of 500px, the image element will have a max-width of 500px too


If you want to set a custom max-width for the image based on its size, you can use JavaScript and the image.onload event like this:

<img onload="this.style.maxWidth = this.width + 'px'" src="..." />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activate simultaneous HTML5 videos on iOS devices with a simple click

I am currently in the process of developing a webpage that will play various videos when specific elements are clicked. Although the functionality works perfectly on desktop computers, it encounters an issue on iOS devices. The problem arises when the elem ...

Profile picture placeholder with conditional logic

After extensively searching online, I was unable to find a solution on how to add a default profile picture. Here is the code snippet I used for uploading an image chosen by the user: <td><img class="image" src="image/<?php echo $contact[&apos ...

Center the image within a Grid item using Material UI's alignment feature

I am currently utilizing the Grid component from Material UI. I'm facing an issue where the image inside a Grid item is aligned to the left instead of being centered. <Grid container direction="row" justify="center" alignIte ...

Place the div at the bottom of the page or viewport if it is bigger

Is there a way to ensure that a div with a background remains at the bottom of the page, regardless of whether the content on the page pushes it down or not? Currently, I am using the following code: #bottomBar { position: absolute; bottom: 0; } ...

Guide on Cordova: Playing MP4 videos in fullscreen and endless loop

I am looking to display a specific mp4 video file in fullscreen mode and on loop playback. Is there a plugin available that can help me achieve this? While researching, I came across the Videogular library designed for Angular-based applications. However ...

Is it possible for HTML to provide alternative text for unique characters, such as accented characters?

I have a vision for HTML support that may incorporate the following: <span alt="Antonin Dvorak">Anton&iacute;n Dvo&#345;&aacute;k</span> In this scenario, if a browser cannot display special characters, it would default to showing ...

How can I add opening quotation marks to every line of text using HTML and CSS?

Is it possible to display a quotation mark at the beginning of each line in a quote using HTML and CSS? In traditional typography, quotes were often marked at every linebreak in addition to the opening and closing of the quote. I want to replicate this on ...

Guide on extracting the string "152" from a textnode using Selenium

I'm having trouble getting the xpath for the number 152. The xpath I tried only extracts "Test" which comes after 152, but it excludes the actual number itself. How can I get this to work and extract the count of 152? My current xpath is: //div\ ...

What is a method to shift an element to the side without actually deleting it from the DOM (perhaps using a simulated slide

For nearly a month, I've been battling with a challenging issue and so far have had no success. Imagine a scenario where there's a button along with an element in the DOM (such as an image or something similar). Upon clicking this button, the e ...

Misaligned Div Content

Check out my jsfiddle here The text inside the <div> is shifting upwards Could someone explain why this is occurring? And suggest a solution? I would truly appreciate any assistance. Thank you! ...

Is there a way to submit a form without displaying the values in the URL?

For quite some time, I've been attempting to solve this issue, but none of the answers I've found seem to apply to my specific situation. The front end of my webapp consists of a very simple HTML page. It features two forms - one for creating an ...

Styling the sub-elements using CSS in JavaScript

Currently, I am dealing with two CSS classes: .dragbox and .removebutton. The .dragbox represents a div, while the .removebutton is a button nested within the div. On my page, there are multiple dynamically generated instances of .dragbox. I am seeking ...

Guide to creating a Bootstrap Select Dropdown that functions as a Tab List by utilizing option elements with role=tab

Can the functionality of a Bootstrap Select Dropdown menu be changed to act like a Tab List? Specifically, I would like the content in the Select to change based on the user's selection from the <option> value. While there is a bootstrap-select ...

Conflict between Angular's ng-repeat directive and Sass styling

Currently, I am working on a project and encountering an issue that is causing some difficulty: In order to create a navigation bar with equally distributed list elements based on the number of items, I am utilizing ng-repeat for data binding and Sass for ...

Can JQuery be used to specifically target attributes with vendor prefixes?

Is there a way to change the Thumb color for my slider without needing to select the thumb with a vendor prefix? I want to be able to dynamically pick the color without simply appending a class. $('.button').on('click', function (e) { ...

Modifying the alignment of Angular elements within the router-module to complement an established CSS grid

I'm facing an issue with my angular reactive-form project. I have successfully set up everything except routing. After defining all the routes in the app.module.ts file, things started getting messy. In my basic form, I had a responsive CSS grid with ...

Navigate through the page and once you reach a specific point, scroll the element

I'm feeling a bit stuck on how to achieve this particular effect. Imagine a web page where, as a user scrolls down, a specific element responds to the mouse scroll input by appearing to move along with the scrolling motion. Once that element reaches ...

Aligning text in the middle of an image both vertically and horizontally

I've been experimenting with different methods to vertically align caption text in the middle of an image, but I haven't had much luck. I've tried using table-cells and other techniques, without success! Currently, I have an <li> elem ...

Stopping setTimeout when leaving the current page: Is it possible?

Good evening, I am looking for advice on how to cancel a setTimeout function when a user navigates to other pages (for example, by clicking other links or pressing the back button in the browser, but not by changing tabs). I have attempted to use the windo ...

SassError: Variable not defined

I have a file containing variables with different themes that I need to override: _vars.scss body { $bg-color: #fff } body.theme-dark { $bg-color: #333 } In my Angular button component, I am referencing these variables: button.scss @import '_va ...