javascript - Responsive design for CSS grids -
i trying place 2 divs occupying 1:2 ratio inside container div. when on wider screen 768px or landscape mode on smartphones, side side. on narrower screen portrait mode on smartphones, stacked n top of another. how accomplish media queries? here's have tried(note: divs needs created js):
let colors = ['#1f82d2', "#f86a25", "#1697d5"]; let keys = [], l = colors.length; for(let = 0; < l; i++) { keys.push(`title${i}`) } let main = document.queryselector('main'); (let = 1; <= l; i++) { const attributes = {style: `background: ${colors[i-1]}`} , colon = elementfactory('span', {attributes: {style: `color: ${colors[i-1]}`}, text: ":"}) , h2 = elementfactory('h2', {text: keys[i-1]}, colon) , div_header = elementfactory('div', {class: 'topic_head',}, h2) , div_content = elementfactory('div', {class: 'topic_content', attributes}) , div = elementfactory(`div`, { class: 'topic', id: `items${i}`}, div_header, div_content); main.appendchild(div); } function elementfactory(el, options = {}, ...children) { el = document.createelement(el); if (options.id) el.id = options.id; if (options.class) el.classlist.add(options.class); if (options.text) el.innertext = options.text; if (options.html) el.innerhtml = options.html; if (options.attributes) object.entries(options.attributes).map(([k, v]) => el.setattribute(k, v)); children.foreach(child => { if (typeof child === 'string') { el.appendchild(document.createtextnode(child)) } else { el.appendchild(child) } }); return el }
.topic { display: grid; height: 100vh; font-size: 3.3rem; grid-template-columns: 1fr 2fr; } @media screen , (max-device-width : 719px) { .topic { display: grid; height: 100%; font-size: 3.3rem; grid-template-rows: 1fr 3fr; } } .topic_head { justify-content: center; display: flex; font-style: italic; font-weight: bold; font-family: "playfair display"; font-size: 7rem; align-items: center; } .topic_content { font-family: "overlock"; display: flex; align-items: center; line-height: 1.2; color: white; } body { font-size: 10vh; }
<main></main>
@media screen , (max-width : 768px) { /*when screen width equal or smaller 768px*/ .topic { display: list-item; /* display list item*/ height: auto; /* height adjust automatically new content.*/ } .topic_head { width: 100%; /*adjust need*/ height: 100vh; /*adjust need*/ } .topic_content { width: 100%; /*adjust need*/ height: 100vh; /*adjust need*/ } }
let colors = ['#1f82d2', "#f86a25", "#1697d5"]; let keys = [], l = colors.length; for(let = 0; < l; i++) { keys.push(`title${i}`) } let main = document.queryselector('main'); (let = 1; <= l; i++) { const attributes = {style: `background: ${colors[i-1]}`} , colon = elementfactory('span', {attributes: {style: `color: ${colors[i-1]}`}, text: ":"}) , h2 = elementfactory('h2', {text: keys[i-1]}, colon) , div_header = elementfactory('div', {class: 'topic_head',}, h2) , div_content = elementfactory('div', {class: 'topic_content', attributes}) , div = elementfactory(`div`, { class: 'topic', id: `items${i}`}, div_header, div_content); main.appendchild(div); } function elementfactory(el, options = {}, ...children) { el = document.createelement(el); if (options.id) el.id = options.id; if (options.class) el.classlist.add(options.class); if (options.text) el.innertext = options.text; if (options.html) el.innerhtml = options.html; if (options.attributes) object.entries(options.attributes).map(([k, v]) => el.setattribute(k, v)); children.foreach(child => { if (typeof child === 'string') { el.appendchild(document.createtextnode(child)) } else { el.appendchild(child) } }); return el }
.topic { display: grid; height: auto; font-size: 3.3rem; grid-template-columns: 1fr 2fr; } @media screen , (max-width : 768px) { .topic { display: list-item; height: auto; } .topic_head { width: 100%; } .topic_content { width: 100%; height: 100vh; } } .topic_head { justify-content: center; display: flex; font-style: italic; font-weight: bold; font-family: "playfair display"; font-size: 7rem; align-items: center; } .topic_content { font-family: "overlock"; display: flex; align-items: center; line-height: 1.2; color: white; } body { font-size: 10vh; }
<main></main>
Comments
Post a Comment