Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Created March 19, 2025 07:21
Show Gist options
  • Save kidGodzilla/c6183bfd7f63e014e4bf83fefd35ca7a to your computer and use it in GitHub Desktop.
Save kidGodzilla/c6183bfd7f63e014e4bf83fefd35ca7a to your computer and use it in GitHub Desktop.
Flexbox center a div (wrong answers only)
<script>
class FlexCenter extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
const wrapper = document.createElement('div');
wrapper.classList.add('center');
const slot = document.createElement('slot');
wrapper.appendChild(slot);
const style = document.createElement('style');
style.textContent = `
.center {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
}
`;
shadow.appendChild(style);
shadow.appendChild(wrapper);
}
}
customElements.define('flex-center', FlexCenter);
</script>
<flex-center style="height: 100vh;">
<div>Centered Content</div>
</flex-center>
@kidGodzilla
Copy link
Author

Screenshot 2025-03-19 at 3 23 41 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment