Skip to content

Commit

Permalink
fix: Maintain img size regardless of its state via grid + svg was not…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
notacouch committed Oct 3, 2018
1 parent aa9d9b4 commit aaee80e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,13 @@ createRestaurantHTML = restaurant => {
const imageWrapper = document.createElement('div');
imageWrapper.className = 'restaurant-img-sizer';

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

const image = document.createElement('img');
Expand Down
2 changes: 1 addition & 1 deletion restaurant.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h2 id="restaurant-name"></h2>
<!-- maintain aspect ratio of img, credit:
@link https://codeburst.io/keeping-aspect-ratio-with-html-and-no-padding-tricks-40705656808b -->
<div class="restaurant-img-sizer">
<svg viewbox="0 0 4 3"></svg>
<svg viewBox="0 0 4 3"></svg>
<img alt="" id="restaurant-img">
</div>
<div id="restaurant-cuisine"></div>
Expand Down

0 comments on commit aaee80e

Please sign in to comment.