Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
    <div class="content">
        <h1>Mondrian Art Generator</h1>
        <p class="byline">Click the button below to generate a Mondrian like painting using CSS-grid. Enjoy!</p>
        <div class="mondrian-controls">
            <h2>Settings</h2>
            <form name="size">
                <fieldset>
                    <legend>Painting size</legend>
                    <input type="radio" name="size" value="small" id="small-size"><label for="small-size">Small</label>
                    <input type="radio" name="size" value="medium" id="medium-size"><label for="medium-size">Medium</label>
                    <input type="radio" name="size" value="large" id="large-size" checked><label for="large-size">Large</label>
                </fieldset>
            </form>
            <form name="theme">
                <fieldset>
                    <legend>Feeling blue?</legend>
                    <input type="checkbox" name="theme-blue" value="blue" id="theme-blue"><label for="theme-blue">Yes</label>
                </fieldset>
            </form>
        </div>

        <button id="js-redraw" class="mondrian-button">Generate</button>
    </div>
    <div>
        <div id="mondrian-wrapper"></div>
    </div>
</main>
              
            
!

CSS

              
                :root {
    --mondrian-black: #010302;
    --mondrian-white: #FFF5FB;
    --mondrian-blue: #257CC1;
    --mondrian-red: #FC0002;
    --mondrian-yellow: #FEEE1F;

    --mondrian-block-size: 52px;
    --mondrian-border-width: 8px;

    --mondrian-font-family: 'Nunito', sans-serif;

    --radio-checkbox-size: 1.5rem;
    --spacing: 1.5rem;
}

* {
    box-sizing: border-box;
}

html {
    font-family: var(--mondrian-font-family);
    line-height: 1.5;
    font-size: 100%;
}

main {
    display: flex;
    flex-direction: column;
    max-width: 1020px;
    margin: 50px auto;
}

@media (min-width: 600px) {
    main {
        flex-direction: row;
    }
}

main > div {
    flex: 50%;
    padding: 20px;
    height: 510px;
}

h1 {
    font-weight: 300;
    font-size: 2.5rem;
    line-height: 1.1;
    margin: var(--spacing) 0;
}

h2 {
    margin-top: 0;
    margin-bottom: var(--spacing);
    font-size: 1.5rem;
    font-weight: 400;
    line-height: 1.1;
}

p {
    margin-top: 0;
    margin-bottom: var(--spacing);
}

.byline {
    font-size: 1.25rem;
}

/* Form controls */
form[name="size"] {
    margin-bottom: var(--spacing);;
}

fieldset {
    border: 0;
    padding: 0;
    margin: 0;
    min-width: 0;
}

legend {
    margin-bottom: 0.5rem;
    text-transform: uppercase;
    letter-spacing: 0.1rem;
    font-size: 0.85rem;
    font-weight: 700;
}

label {
    position: relative;
    display: inline-block;
    padding: 0 0 0 2rem;
    height: var(--radio-checkbox-size);
    line-height: 1.5;
    cursor: pointer;
}

label:not(:first-of-type) {
    margin-left: 1rem;
}

label::before,
label::after {
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    width: var(--radio-checkbox-size);
    height: var(--radio-checkbox-size);
}

label::before {
    content: " ";
    border: 4px solid var(--mondrian-black);
    box-sizing: border-box;
}

input[type="checkbox"],
input[type="radio"] {
    position: absolute;
    opacity: 0;
}

input[type="checkbox"] + label::after,
input[type=radio] + label::after{
    content: " ";
    color: var(--mondrian-black);
    line-height: 1.5;
}

/* :checked */
input[type="checkbox"]:checked + label::before,
input[type="radio"]:checked + label::before {
    background: var(--mondrian-blue);
    border-color: var(--mondrian-black);
    border-width: 4px;
}

.content {
    display: flex;
    flex-direction: column;
}

.mondrian {
    display: grid;
    grid-auto-columns: var(--mondrian-block-size);
    grid-auto-rows: var(--mondrian-block-size);
    grid-auto-flow: dense;
    grid-gap: var(--mondrian-border-width);
    grid-template-columns: repeat(auto-fit, minmax(var(--mondrian-block-size), 1fr));
    grid-template-rows: repeat(auto-fit, minmax(var(--mondrian-block-size), 1fr));
    background-color: var(--mondrian-black);
    border: var(--mondrian-border-width) solid var(--mondrian-black);
    overflow: hidden;
    box-sizing: border-box;
}

.mondrian.mondrian--small {
    max-height: 360px;
    max-width: 360px;
}

.mondrian.mondrian--medium {
    max-height: 420px;
    max-width: 420px;
}

.mondrian.mondrian--large {
    max-height: 480px;
    max-width: 480px;
}

.mondrian-controls {
    padding: var(--spacing);
    border: 2px solid var(--mondrian-black);
    background: var(--mondrian-white);
    margin-bottom: var(--spacing);
}

.mondrian-button {
    margin: auto auto 0;
    width: 200px;
    background-color: var(--mondrian-white);
    border: var(--mondrian-border-width) solid var(--mondrian-black);
    padding: 10px 20px;
    font-family: var(--mondrian-font-family);
    line-height: 1;
    font-weight: 700;
    text-transform: uppercase;
    cursor: pointer;
}

.mondrian-button:hover {
    background-color: var(--mondrian-blue);
}

.span-1 {
    grid-column: span 1;
}

.span-2 {
    grid-column: span 2;
}

.span-3 {
    grid-column: span 3;
}

.span-4 {
    grid-column: span 4;
}

.span-5 {
    grid-column: span 5;
}

.row-1 {
    grid-row: span 1;
}

.row-2 {
    grid-row: span 2;
}

.row-3 {
    grid-row: span 3;
}

.row-4 {
    grid-row: span 4;
}

.row-4 {
    grid-row: span 5;
}

.color-black {
    background-color: var(--mondrian-black);
}

.color-white {
    background-color: var(--mondrian-white);
}

.color-blue {
    background-color: var(--mondrian-blue);
}

.color-red {
    background-color: var(--mondrian-red);
}

.color-yellow {
    background-color: var(--mondrian-yellow);
}
              
            
!

JS

              
                interface IMondrianConfig {
    size?: string;
    theme?: string;
}

/** Class representing a Mondrian painting. */
class Mondrian implements IMondrianConfig {

    public size: string = 'medium';
    public theme: string = 'default';

    private element: string;
    private defaultConfig: IMondrianConfig = {
        size: 'medium',
        theme: 'default',
    }
    private sizes: Array<string> = ['small', 'medium', 'large'];
    private themes: Array<{ name: string, colors: Array<string> }> = [
        {
            name: 'default',
            colors: [
                'black',
                'red',
                'blue',
                'yellow',
                'white',
                'white',
                'white'
            ]
        },
        {
            name: 'blue',
            colors: [
                'black',
                'blue',
                'blue',
                'white',
                'white',
                'white',
                'white',
                'white'
            ]
        },
    ];

    /**
     * Initialize a mondrian.
     */
    constructor(element: string, config: IMondrianConfig) {
        this.element = element;
        const instanceConfig: IMondrianConfig = {
            ...this.defaultConfig,
            ...config
        }
        this.size = this.sizes.includes(instanceConfig.size) ? instanceConfig.size : this.size;
        this.theme = this.themes.some(obj => obj.name === instanceConfig.theme) ? instanceConfig.theme : this.theme;
    }

    /**
     * Create a mondrian in targetElement.
     */
    public draw() {
        const targetElement: HTMLElement = document.getElementById(this.element);
        const painting: string = `<div class="mondrian mondrian--${this.size}">${this.generateCells()}</div>`;

        targetElement.insertAdjacentHTML('beforeend', painting);
    }

    /**
     * Remove the mondrian and create a new one.
     */
    public redraw() {
        const element: HTMLElement = document.querySelector('.mondrian');
        element.parentNode.removeChild(element);

        this.draw();
    }

    /**
     * Set size option
     */
    public setSize(size: string) {
        if (this.sizes.includes(size)) {
            this.size = size;
        }
    }

    /**
     * Set theme option
     */
    public setTheme(theme: string) {
        if (this.themes.some(obj => obj.name === theme)) {
            this.theme = theme;
        }
    }

    /**
     * Generate the colored dom elements for the Mondrian based on the theme colors.
     */
    private generateCells() {
        let cells: string = ``;
        const selectedTheme: { name: string, colors: Array<string> } = this.themes.find(obj => obj.name === this.theme);
        for (let i = 0; i < 50; i++) {
            let span: number = this.randInt(1, 5);
            let row: number = this.randInt(1, 5);
            let colorIndex: number = this.randInt(1, selectedTheme.colors.length);
            cells += `<div class="span-${span} row-${row} color-${selectedTheme.colors[colorIndex]}"></div>`
        }

        return cells;
    }

    /**
     * Get random int
     */
    private randInt(min: number, max: number): number {
        return Math.floor(Math.random() * (max - min) + min);
    }
}

/**
 * Initialize the Mondrian class in the document
 */
const config = {
    size: 'large',
};

const mondrian = new Mondrian('mondrian-wrapper', config);
mondrian.draw();

/**
 * Event Listeners
 */
document.getElementById("js-redraw").addEventListener("click", () => {
    mondrian.redraw();
});

const sizeRadios = document.forms["size"].elements["size"];
for (let i = 0, max = sizeRadios.length; i < max; i++) {
    sizeRadios[i].addEventListener("click", (event) => {
        mondrian.setSize(event.target.value);
    });
}

const themeCheckbox = document.querySelector("input[name=theme-blue]");
themeCheckbox.addEventListener('change', function () {
    this.checked ? mondrian.setTheme(this.value) : mondrian.setTheme('default');
});
              
            
!
999px

Console