What is the best way to create an adaptive <pre> tag for ASCII art without using media queries?

Currently, my username is displayed as an ASCII art banner/header and I am looking to enhance its responsiveness. Instead of relying solely on media queries, I am exploring the possibility of making it scale within a CSS grid container. This approach would not only reduce code bloat but also make maintenance easier.

Here are the methods I have experimented with:
  • width using min(), max(), clamp(), and calc() functions incorporating various values of vw and vh.
  • width with just a value in vw, based on suggestions from other sources.
  • font-size with percentages in min(), max(), clamp(), and calc() functions.
  • white-space: pre-wrap;
  • white-space: pre-line;
  • word-wrap: break-word;

white-space: pre-wrap; and white-space: pre-line; somewhat work but they disrupt the appearance of the ASCII art.

Despite trying these approaches, I have been unable to achieve proper scaling for my username.

Below is my current code snippet:

body {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 7.25rem auto 5rem;
  grid-template-areas: "header header" "main sidebar" "footer footer";
  min-height: 100vh;
}

header {
  grid-area: header;
}

header pre {
  display: grid;
  justify-content: center;
  align-content: center;
  text-align: center;
  font-size: 70%;
}

@media only screen and (max-device-width: 700px) {
  body {
    grid-template-rows: 6rem auto 5rem;
  }
  header {
    font-size: 55%;
  }
}
<header>
  <pre>
 ___       __    ________   ___    _________   ________   _____ ______    ________   _______    ________      
|\  \     |\  \ |\   __  \ |\  \  |\___   ___\|\   __  \ |\   _ \  _   \ |\   ____\ |\  ___ \  |\   ___  \    
\ \  \    \ \  \\ \  \|\  \\ \  \ \|___ \  \_|\ \  \|\  \\ \  \\\__\ \  \\ \  \___|_\ \   __/| \ \  \\ \  \   
 \ \  \  __\ \  \\ \   __  \\ \  \     \ \  \  \ \  \\\  \\ \  \\|__| \  \\ \_____  \\ \  \_|/__\ \  \\ \  \  
  \ \  \|\__\_\  \\ \  \ \  \\ \  \____ \ \  \  \ \  \\\  \\ \  \    \ \  \\|____|\  \\ \  \_|\ \\ \  \\ \  \ 
   \ \____________\\ \__\ \__\\ \_______\\ \__\  \ \_______\\ \__\    \ \__\ ____\_\  \\ \_______\\ \__\\ \__\
    \|____________| \|__|\|__ \|_______| \|__|   \|_______ \|__|     \|__||\_________\\|_______| \|__| \|__|
                                                                            \|_________                              
  </pre>
</header>

I have implemented two additional media queries to improve mobile-friendliness and adjusted the font size of the header to scale accordingly. However, I am interested in exploring whether the header grid container/box can enforce scaling constraints on the ASCII art tag as the screen size decreases.

Thank you for taking the time to review my query!

Answer №1

Here are the revisions I implemented! All remaining code remains unchanged.

Initial solution: Functioning well but application of scaling is overly aggressive.
header pre {
 font-size: 0.5vw;
}
Second solution addresses the scaling concern, although not meeting desired outcome completely.
header pre {
 font-size: max(0.5vw, 0.25rem);
}
The third and most efficient solution offers a smooth and consistent scaling that works effectively on both small and large screens! Utilization of minmax and max functions to prevent excessive shrinking has been added.
body {
 grid-template-rows: minmax(2.5rem, 12vmin) 1fr 5rem;
}
header pre {
 font-size: max(0.2rem, 1vmin);
}

Special thanks to Adam and somethinghere, for their input!

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

Unable to apply 100% height to flex child element

When it comes to creating a layout for my website, I have a simple idea in mind. I want the body content to take up at least 100% of the height of the page. This setup works perfectly on desktop screens, but when I switch to a mobile view (which changes th ...

@media doesn't seem to be functioning properly when the screen width is below

I've been working on making my website fully responsive, but I've run into an issue where it won't recognize anything below 980px in width. Here is the CSS code I'm using: @media screen and (max-width:1029px){ h1{ font-size ...

Applying the document height to window height ratio to create a scale ranging from 1 to 100

My goal is to create a scroll bar using two divs with heights of 110px and 10px. The smaller one will be nested inside the taller one, allowing for adjustment of its margin-height from 0 to 100px while still fitting within the larger div. When I refer to ...

Execute a jQuery focus() function on a textarea element within a dynamically added form on the webpage

Whenever I dynamically insert a new form onto the page using clone(), my goal is to have the textarea within the form automatically focused. However, despite trying out the code below, it seems like it's not functioning as expected: .on('click&a ...

What is the method for aligning text to the right side within a ListItem?

Currently, I have these elements where the "YoYo" text is aligned to the left by default. However, I am looking to adjust the positioning of some "YoYo" texts so that they can appear on the right side. I attempted to provide a style object with justifyCon ...

What is the proper way to integrate HTML code into an .inc file that is being utilized by a php file?

My title may be a bit off, but I have a PHP file that needs to include a .inc file containing menu code. Here is how I'm attempting to do it in the PHP file: <?php include_once("php_menu.inc") ?> And here is my HTML code for the menu: < ...

Perfectly aligning the Update Progress Animation in real-time

I am currently utilizing the Ajax UpdateProgress with a loading gif attached. The issue I am facing is how to ensure that the loading.gif always appears in the center of the screen, even if the user is at the very top or bottom of the page. Is there a meth ...

Tips for improving the loading speed of my website

What are the best ways to improve website optimization and increase page speed? I have already minified JavaScript and CSS, as well as optimized images, but Google continues to penalize our pages. Any suggestions? ...

Tips for incorporating JavaScript into elements that have been modified using jQuery's .html() method

Consider this example: $('#key').on('click', function(){ $('.task').html("<button id='key'>Button</button>"+Date()); }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.j ...

What could be the reason for the presence of a white border in the footer section?

I am trying to create a div in the footer that sits side by side, but the current code is causing it to have an unwanted white border. <!DOCTYPE html> <html lang="en"> <head> <style type="text/css"> #botto ...

Using jQuery to make an element follow you as you scroll down a page inside a div

I've made some progress on my code: HTML <div id="header"></div> <div id="content"> <div class="sidebar-wrapper"></div> </div> <div class="session-wrapper"></div> <div id="footer"></div> ...

Is there a way to determine if the browser window is currently in use?

The code I am currently working with looks like this: let focus; function setFocus() { focus = true; } function hideWindow() { focus = false; } window.onfocus = setFocus(); window.onblur = hideWindow(); The intention behind this code is to use it in the ...

The automatic selection process for IE document modes

It may seem simple, but I'm a little perplexed about how IE decides which browser mode to use. In IE9, there are options such as IE9 Compatibility Mode, IE9, IE8, and IE7. These modes are selected automatically depending on the webpage. Can someone e ...

Reloading the page using ajax technology

What is the best way to handle logging out on a webpage without reloading the page? Thanks in advance!) My perspective: def logout(request): auth.logout(request) html = render(request, 'base.html') return html Using Ajax: $('a[hre ...

Achieve a sliding effect between two divs using CSS only

I'm trying to create a design with 2 divs that are the same size. The first one is visible by default, while the second one is hidden initially. My goal is to achieve an effect where when you hover over the first div, the second one slides upward and ...

Some elements are not visible when inspecting the source code

Hidden fields are essential in my jsp form. Interestingly, when I check the page source in a browser, only the first hidden field is displayed, not the second one. <input type="hidden" name="url" id="url" value="<%=url%>" /> <input type="hi ...

Creating a layered effect: Adding an image onto another image using HTML and CSS

Wondering if it's possible to create an overlaid effect with an image and text inside of an <img> element, similar to the example image below. Utilizing Bootstrap for this design. This is how I am attempting to achieve it: HTML <div class= ...

How to dynamically reduce the number of columns in a textarea using jQuery according to its content

Does anyone know how to make a textarea shrinkwrap the text inside on blur? The default number of columns is 10 or 15 and I want the textarea to adjust its width based on the text content. I have tried the following code: $('textarea').on(&apos ...

Rotating through classes with JQuery click event

I'm attempting to create a toggle effect for list items where clicking on an item will change its color to green, then red, and then back to its original state. Some list items may already have either the green or red class applied. However, my curren ...

Tips for Building Common Controller Logic in CakePHP

I am in the process of developing a website where I have set up dynamic content for the footer, which is retrieved from a database. I am looking to create a single controller logic code that can be used across all pages on the site. Any suggestions or gu ...