Skip to content

Commit aaee80e

Browse files
committed
fix: Maintain img size regardless of its state via grid + svg was not working bc of lowercase b in viewbox
This was in part due to HTML linter. It only has error level severity, eslint style warn severity support is in the works, see: htmlhint/HTMLHint#279 (comment) The other part was XML vs HTML fragment, latter's attributes are always set as lowercase! Which broke the SVG! Reference for fix is found in inline comment in code.
1 parent aa9d9b4 commit aaee80e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

js/main.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,13 @@ createRestaurantHTML = restaurant => {
188188
const imageWrapper = document.createElement('div');
189189
imageWrapper.className = 'restaurant-img-sizer';
190190

191-
const svgAspectRatio = document.createElement('svg');
192-
svgAspectRatio.setAttribute('viewbox', '0 0 4 3');
191+
// Create XML fragment, not HTML, otherwise setAttribute forces lowercase, though viewBox is case-sensitive
192+
// @link https://stackoverflow.com/a/28734954/781824
193+
const svgAspectRatio = document.createElementNS(
194+
'http://www.w3.org/2000/svg',
195+
'svg'
196+
);
197+
svgAspectRatio.setAttribute('viewBox', '0 0 4 3');
193198
imageWrapper.append(svgAspectRatio);
194199

195200
const image = document.createElement('img');

restaurant.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ <h2 id="restaurant-name"></h2>
4040
<!-- maintain aspect ratio of img, credit:
4141
@link https://codeburst.io/keeping-aspect-ratio-with-html-and-no-padding-tricks-40705656808b -->
4242
<div class="restaurant-img-sizer">
43-
<svg viewbox="0 0 4 3"></svg>
43+
<svg viewBox="0 0 4 3"></svg>
4444
<img alt="" id="restaurant-img">
4545
</div>
4646
<div id="restaurant-cuisine"></div>

0 commit comments

Comments
 (0)