Employ CSS flexbox and/or JavaScript for creating a customizable webpage

I am in the process of developing a basic IDE for an educational programming language that has similarities to Karel the Dog. One major issue I am encountering is creating the foundation HTML page.

The IDE consists of 4 main areas:

  • Toolbox (contains buttons like open, save as, run etc.)
  • Field (canvas where the executor can move and perform actions)
  • Code (uses CodeMirror editor for writing executor's commands)
  • Console (location for displaying messages such as compilation errors or runtime debug output)

I have outlined my requirements for each area in my code, so I will highlight what is currently not functioning properly:

  1. The page should occupy 100% of the screen height.
  2. I am struggling to make CodeMirror fill the entire available height within its parent container. Additionally, I want scrollbars to appear when the content exceeds the height of the parent element.
  3. Similar to CodeMirror, I am facing issues with the canvas element only on the vertical axis.
  4. I am looking for a way to create a separator between the code and field areas that can be used to adjust horizontal space distribution between these two sections.

One more challenge arises if addressing item number 4 requires JavaScript implementation. In this case, I prefer to utilize the WinJS 3.0 library instead of adding jQuery or other heavy frameworks solely for resizing functionality.

If anyone can offer guidance or assistance, it would be greatly appreciated.

I have uploaded my code to jsfiddle.net and included it here:

var ce = CodeMirror(document.getElementById('b-codemirror'), {
  value: "\n\n\nIt is CodeMirror element. [PARAMS ALL] " +
    "width: 100% of parent element, height: always 100% of" +
    " parent element + both scrollbars if needed\n\n\n",
  lineNumbers: true
});
var cc = document.getElementById("canvas").getContext("2d");
cc.font = "16px Helvetica";
cc.fillText("It is canvas. Can be resized from", 10, 30);
cc.fillText("JS. If it is larger than parent element,", 10, 60);
cc.fillText("corresponding scrollbar should appear.", 10, 90);
@import url("http://codemirror.net/lib/codemirror.css");

/* overriding default codemirror.css */
.CodeMirror {
  font-family: monospace;
  height: 100%;
}
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
.b-section {
  margin: 2px;
  padding: 4px;
  border-radius: 4px;
}
#b-fieldcode {
  min-height: 640px;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-flow: row;
  flex-flow: row;
}
#b-toolbox {
  background: #ffeebb;
}
#b-console {
  height: 100px;
  background: #ffeebb;
}
#b-field {
  background: #ccccff;
  overflow: auto;
  -webkit-flex: 1 1 40%;
  flex: 1 1 40%;
  -webkit-order: 1;
  order: 1;
}
#b-code {
  background: #dddd88;
  -webkit-flex: 1 1 60%;
  flex: 1 1 60%;
  -webkit-order: 2;
  order: 2;
}

@media all and (max-width: 1024px) {
  #b-fieldcode, #page {
    -webkit-flex-flow: column;
    flex-flow: column;
    flex-direction: column;
  }
  #b-code, #b-field {
    -webkit-order: 0;
    order: 0;
  }
  #b-field, #b-code {
    height: 500px;
  }
}
<script type="text/javascript" src="http://codemirror.net/lib/codemirror.js"></script>
<div id="b-toolbox" class="b-section">
  Here comes the space for buttons.
  [PARAMS ALL] width: 100% of screen, height: sized to content
</div>
<div id="b-fieldcode">
  <div id="b-field" class="b-section">
    Here comes canvas wrapper.<br />
    [PARAMS landscape] width: flex 40% of screen, height:
    100% of screen minus b-toolbox and b-console.
    <br />[PARAMS portrait] width: 100% of
    screen, height: fixed 400px.<br />
    <canvas width="300" height="300" id="canvas"
            style="background-color: green" />
  </div>
  <div id="b-code" class="b-section">
    Here comes CodeEditor wrapper.<br />
    [PARAMS landscape] width: flex 60% of screen, height:
    100% of screen minus b-toolbox and b-console.<br />
    [PARAMS portrait] width: 100% of
    screen, height: fixed 500px.
    <div id="b-codemirror"></div>
  </div>
</div>
<div id="b-console" class="b-section">
  Here comes output console.
  [PARAMS ALL] width: 100% of screen, height: fixed 120px.
</div>

Answer №1

To begin, it is important to differentiate between styles for portrait and landscape orientations. Creating styles for the portrait part is relatively straightforward, so we will focus on the landscape portion.

For the landscape layout, you'll need a fluid height header (containing buttons) and a fixed height footer (such as a console). This scenario perfectly fits the usage of CSS flexbox - allowing spare space to be allocated to the main content area. By setting

display: flex; flex-direction: column;
on the <body> element and flex: 1; on the main section (#fieldcode in your code snippet), you achieve this desired layout.

The #field should occupy 40% of the width within #fieldcode, while #code takes up the remaining 60%. To accomplish this, apply

display: flex; flex-direction: row;
to #fieldcode, and assign flex: 4; to #field and flex: 6; to #code, ensuring that the spare space distribution follows a 4:6 ratio. It's crucial to note the change in the flex-direction value from the previous step, indicating whether the separation occurs horizontally or vertically.

html, body {
  margin: 0;
  padding: 0;
}
#toolbox {
  background: #feb;
}
#field {
  background: #ccf;
  overflow: auto;
}
#code {
  background: #dd8;
  overflow: auto;
}
#codemirror {
  min-width: 100%;
  min-height: 100%;
}
#console {
  background: #feb;
  height: 120px;
}

@media screen and (orientation: portrait) {
  #field {
    height: 400px;
  }
  #code {
    height: 500px;
  }
}

@media screen and (orientation: landscape) {
  html, body {
    height: 100%;
  }
  body {
    display: flex;
    flex-direction: column;
  }
  #fieldcode {
    flex: 1;
    display: flex;
    flex-direction: row;
  }
  #field {
    flex: 4;
  }
  #code {
    flex: 6;
  }
}
<div id="toolbox">buttons here</div>

<div id="fieldcode">
  <div id="field">
    <canvas></canvas>
  </div>

  <div id="code">
    <div id="codemirror"></div>
  </div>
</div>

<div id="console">console here</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The functionality of ellipsis, which consists of three dots, allows the text to expand

I am trying to implement a feature where the extra text is represented by three dots (...) as an ellipsis, and upon clicking the dots, the text should expand and contract. However, the current code only contracts the text and does not expand it upon clicki ...

Is there a way to modify HTML using a C# program in a web browser?

I’m considering the possibility of editing the HTML document text through the web browser. I am currently working on an email client that utilizes an HTML template for its design. Everything seems to be in order, but now I need to customize the template ...

"Utilize jQuery to prevent the default action, perform a specific task, and

I am currently working on a Rails application where I want to enable users to tag blog posts with categories by typing the tags into a text field and separating them with a space (which is functional!). However, I am facing an issue when trying to submit ...

Improving the style of Google Maps InfoWindows for right-to-left languages

I'm currently working on a Google Maps marker infowindow and trying to adjust the padding specifically for RTL languages. Here's what I've attempted so far: google.maps.event.addListener(marker, 'click', function () { i ...

"Having issues with Django not properly applying the JavaScript and CSS files I've linked in

I have completed my project and organized all the necessary files, including index.html, css, js, and settings.py within the appropriate folders. I am encountering an issue with applying a pen from the following source: CodePen index.html <!DOCTYPE h ...

Can const variables be reassigned in JavaScript programming?

Can you reassign a const variable in JavaScript? In C++, we can cast variables to and from const. Is there something similar in JavaScript? My question is const a = 1; unconst(a); a = "xyz"; a === "xyz" // true I'm not referring to object prope ...

Type within a customizable div element located within an iframe

When I switched to using iframe driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@id='ContentFrame']"))); I attempted two different methods, but neither of them worked. Approach 1: WebElement webElement = driver.findElement(locat ...

Tips for extracting individual lines from a Buffer using streams?

There seems to be an issue with the file stream I'm receiving from STDIN where line breaks disappear within the buffers. I am looking for a way to process and parse these lines using the Stream approach. Can someone help me out? util.inherits(Parse ...

What are some techniques for animating SVG images?

Looking to bring some life to an SVG using the jQuery "animate" function. The plan is to incorporate rotation or scaling effects. My initial attempt with this simple code hasn't yielded the desired results: $("#svg").animate({ transform: "sc ...

Analyzing objects within an array for similarities

Suppose I have an array containing objects: var arr = [ { id: 1, pt: 0 }, { id: 2, pt: 12 }, { id: 3, pt: 7 }, { id: 4, pt: 45 }, { id: 5, pt: 123 }, ]; I am looking to loop through this array (possibly using array.forEach or array.map) ...

Global Path in Helmet Content Security Policy is not functioning as expected

I recently implemented Helmet to establish content security policies for my web application using Express in the backend. The specific policies are as follows: const express = require("express"); const app = express(); const helmet = require('helmet&a ...

Organizing outcomes from a for each function into an array using javascript

I have a form with multiple values of the same name, and I need to arrange this data in an array before sending it via AJAX. However, when I try to do this using the .push function, I encounter an error that says "Uncaught TypeError: dArray.push is not a f ...

The lower division remains static when the browser window is resized

Why does the div with class .lower not move to the bottom of the page when the screen is resized? Could this be because of its CSS property position:absolute;? Check out the code snippet here. #lower { width: 100%; position: absolute; bot ...

Contrasting VSCode Live Server and Node Live Server

Recently delving into the world of JS, I've come across the need to set up a live server using npm. One popular extension in VSCode known as Live Server by Ritwick Dey caught my attention. I'm curious about the distinctions between utilizing this ...

Utilizing React Javascript leaftlet library to implement functionality to zoom in on map using state

As a beginner in programming and leaflet, I've encountered a roadblock in my code. Check out my code snippet: import "./App.css"; import { MapContainer, TileLayer, Marker, Popup } from "react-leaflet"; import { useEffect, useState ...

Having trouble positioning the image at the center of the ion-slides

I'm currently working on designing a slide within an ion item. Everything seems to be functioning correctly, however, the image inside the slide is not appearing in the center. <ion-item style="height:45%; padding-left: 0;"> <ion-slides ce ...

Switch the checkbox attribute for multiple items within a carousel using Angular 2/Typescript

I am currently working on a carousel feature where each item has a checkbox above it. My goal is to be able to click on an item and have its corresponding checkbox checked. The current code successfully achieves this, but the issue I'm facing is that ...

"Ensuring Your SVG Icon is Perfectly Centered: A Step

My SVG icon is currently aligned to the left, but I want to center it. I tried using the following code, but it doesn't seem to be working: .parent{ display: flex; align-items: center; font-size: 13px; } span{ margin-right:10px } When I look at the S ...

The use of "runat="server" in the HTML Head tag is preventing C# embedded code blocks from being converted to client-side code

After I included runat="server" in the head tag and added a CSS href with the value of <%= WebAppInstance %>, I noticed that the <%= WebAppInstance %> was not being converted to client-side code. To provide clarity on my question, please refer ...

What causes the layout of an application to become distorted when I extend the theme in Tailwind.css?

I had the idea of incorporating an animation into my website design using tailwind.css for styling. module.exports = { content: ["./index.html", "./src/**/*.{js,jsx}"], theme: { colors: { primary: "#E51114", ...