New Title: Compressed HTML Matrix Layout Design Using Pure CSS and HTML
P粉458913655
P粉458913655 2023-09-07 21:27:33
0
1
443

I'm trying to create a logo using pure HTML and CSS. The design is very intuitive, being a [13x13] matrix containing some colored items.

CSS is very simple and minor. However, the HTML code has many repeated parts and needs to be DRY (Don't Repeat Yourself).

body { width: 100vmin; margin: auto }
logo {
  display: grid;
  grid-template-columns: repeat(13, 1fr);
}

s { }
w { background-color: #d6d6d6; aspect-ratio: 1 }
l { background-color: #d6d6d6; aspect-ratio: 1 }
f { background-color: #d6d6d6; aspect-ratio: 1 }
<logo>
<f></f><f></f><f></f><s></s><f></f><f></f><f></f><s></s><f></f><s></s><f></f><f></f><f></f>
<f></f><s></s><f></f><s></s><s></s><s></s><f></f><s></s><f></f><s></s><s></s><s></s><s></s>
<f></f><s></s><f></f><s></s><f></f><f></f><f></f><s></s><f></f><s></s><f></f><s></s><f></f>
<f></f><s></s><s></s><s></s><s></s><s></s><s></s><s></s><f></f><s></s><s></s><s></s><f></f>
<f></f><s></s><l></l><l></l><l></l><l></l><l></l><s></s><f></f><s></s><f></f><s></s><f></f>
<f></f><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s><f></f><s></s><f></f>
<f></f><s></s><l></l><l></l><l></l><l></l><l></l><l></l><s></s><f></f><f></f><s></s><f></f>
<f></f><s></s><l></l><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s>
<f></f><s></s><l></l><s></s><l></l><l></l><l></l><s></s><w></w><s></s><w></w><s></s><w></w>
<s></s><s></s><l></l><s></s><s></s><s></s><l></l><s></s><s></s><s></s><w></w><s></s><s></s>
<l></l><l></l><l></l><s></s><l></l><l></l><l></l><w></w><w></w><w></w><w></w><s></s><w></w>
<l></l><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s><s></s><w></w><s></s>
<l></l><l></l><l></l><l></l><l></l><l></l><l></l><l></l><l></l><l></l><s></s><w></w><w></w>
</logo>

I've tried using <f/> instead of <f></f> without success.

Is there any way to reduce this code?

Note: The color should remain unchanged.

P粉458913655
P粉458913655

reply all(1)
P粉713846879

If javascript is available, you can concatenate the cell type into a string object and use it to dynamically build the flag. Sample code is as follows.

let logo = document.getElementById("logo"); 
logo.dataset["cells"].split('').forEach(a => logo.append(document.createElement(a)));
body { width: 100vmin; margin: auto }
logo {
  display: grid;
  grid-template-columns: repeat(13, 1fr);
}

w { background-color: #d6d6d6; aspect-ratio: 1 }
l { background-color: #d6d6d6; aspect-ratio: 1 }
f { background-color: #d6d6d6; aspect-ratio: 1 }
<logo id="logo" data-cells="fffsfffsfsffffsfsssfsfssssfsfsfffsfsfsffsssssssfsssffslllllsfsfsffsssssssssfsffsllllllsffsffslssssssssssfslslllswswswsslssslssswsslllslllwwwwswlssssssssssswllllllllllsww"></logo>

Based on the need for HTML/CSS and @Temani Afif's comment, here is an SVG solution:

    .w * { stroke:red;stroke-width:1;;fill:none;stroke-linejoin="miter"; }
    .l * { stroke:green;stroke-width:1;;fill:none;stroke-linejoin="miter"; }
    .f * { stroke:blue;stroke-width:1;;fill:none;stroke-linejoin="miter"; }
<logo>
    <svg height="300" width="300" viewBox="-1 -1 15 15">
        <g class="w">
            <path d="M12.5,10 L12.5,12.5 L11,12.5" />
            <path d="M6.5,10.5 L10.5,10.5 L10.5,8" />
            <path d="M8,8.5 L9,8.5" />
            <path d="M12,8.5 L13,8.5" />
        </g>
        <g class="l">
            <path d="M4,8.5 L6.5,8.5 L6.5,10.5 L4,10.5" />
            <path d="M7,4.5 L2,4.5" />
            <path d="M8,6.5 L2.5,6.5 L2.5,10.5 L0.5,10.5 L0.5,12.5 L10,12.5" />
        </g>
        <g class="f">
            <path d="M0.5,9 L0.5,0.5 L2.5,0.5 L2.5,3" />
            <path d="M4,0.5 L6.5,0.5 L6.5,2.5 L4,2.5" />
            <path d="M8.5,0 L8.5,5" />
            <path d="M10,0.5 L13,0.5" />
            <path d="M10,2.5 L11,2.5" />
            <path d="M12.5,2 L12.5,7" />
            <path d="M10.5,4 L10.5,6.5 L9,6.5" />
        </g>
    </svg>
</logo>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!