Tips for swapping text with an image or icon during mobile scaling

As a newcomer to this field, I am facing challenges in finding specific answers. My current objective is to convert text labels into images when the viewport shrinks to mobile sizes. The complexity arises from the fact that I am employing Leaflet, a JavaScript library for Open Street Maps, which structures elements differently and requires custom styling injections.

For an interactive display showcasing icons, you can visit my live page: (please note the ongoing work on SSL certification).

The complete set of inline styles can be found towards the end of the page, addressing various aspects of my project:

  • I have directly styled the input because Leaflet inserts a div between label and input, preventing me from triggering animations on the label based on the state changes of the "checked" input.

  • By utilizing JavaScript, I inject a class into my labels. This customization is facilitated by Leaflet's functionality, as exemplified below:

var overlayMaps = {
       "All Playgrounds": dummy[0],
       "<img class='icons' src='https://static.tildacdn.com/tild6162-6637-4663-b262-356661343562/IconsMedium_WC.png' alt='Restrooms'>": dummy[1],
       "<img class='icons' src='https://static.tildacdn.com/tild6238-6432-4162-a161-333539326537/IconsMedium_Grill.png' alt='Public grills'>": dummy[2],
       "<img class='icons' src='https://static.tildacdn.com/tild3939-3338-4237-b732-346131313435/IconsMedium_Access.png' alt='Accessible equipment'>": dummy[3],
       "<img class='icons' src='https://static.tildacdn.com/tild3634-3833-4338-b834-626437613735/IconsMedium_Indoor.png' alt='Indoor area'>": dummy[4],
       "<img class='icons' src='https://static.tildacdn.com/tild3331-3632-4239-a333-343137356133/IconsMedium_Full_Fen.png' alt='Fully fenced'>": dummy[5],
       "<img class='icons' src='https://static.tildacdn.com/tild3339-3038-4032-a638-626137393039/IconsMedium_Partial_.png' alt='Partially fenced'>": dummy[6],
       "<img class='icons' src='https://static.tildacdn.com/tild3336-3333-4137-a562-346663633031/IconsMedium_Horse.png' alt='Animals'>": dummy[7],
       "<img class='icons' src='https://static.tildacdn.com/tild3332-6230-4731-a138-373630383130/IconsMedium_Water.png' alt='Pool or beach'>": dummy[8],
       "<img class='icons' src='https://static.tildacdn.com/tild3761-3935-4338-b861-336231626433/IconsMedium_Toddler.png' alt='Toddler Area'>": dummy[9]
  };
  • To tackle layout issues on smaller screens caused by unmanageable margins within the flex container, check out the visual representation of the problem here: photo of current mobile view

Exploring options such as jQuery (although unfamiliar to me) could potentially resolve the dilemma of adapting the flex container design on mobile devices while seamlessly transitioning from text-based content on desktops to icon-driven representations on smaller screens. Here's a reference link showcasing the text-only version: . My ultimate aim is to optimize the display of the control box (filters) on mobile interfaces without triggering overflow scroll boxes or forcing users to navigate through excessive scrolling distances to reach the map itself.

Please feel free to provide any insights or assistance regarding these challenges.

<div class="flexcontainer">
    <div id="new-parent">
    </div>
    </div>

<script>// Create the control and add it to the map;

var control = L.control.layers(null, overlayMaps,{collapsed:false});
control.addTo(map);

// Call the getContainer routine.
var htmlObject = control.getContainer();
// Get the desired parent node.
var a = document.getElementById('new-parent');

// Finally append that node to the new parent.
function setParent(el, newParent)
{
    newParent.appendChild(el);
}
setParent(htmlObject, a);


</script>


<style>
*, *::before, *::after {
  box-sizing: border-box;
  vertical-align: middle;
}

body {
    color: #435757;
    background: radial-gradient(#fff, #dac4cd);
    font: min(3vw, 16px) 'Montserrat';
    justify-content: center;
    align-items: center;
    flex-direction: column;
}
label:first-of-type {
    border: 3px solid #689c93;
    margin: min(1vw,10px) 75%;
    flex: 0 0 30%;
    border-radius: 100px;
}
label {
    display: block;
    position: relative;
    padding: min(.5vw, 5px) min(3vw, 15px) min(.5vw, 5px) min(.5vw, 5px);
    color: #000;
    background-color: transparent;
    white-space: nowrap;
    cursor: pointer;
    user-select: none;
    transition: background-color .2s, box-shadow .2s;
    flex: 0 0 20%;
    border-radius: 100px;

}

.flexcontainer {
    display: flex !important;
    flex-wrap: wrap;
}

.icons {
    width: 70px;
}

#new-parent {
    position: relative;
}

</style>

Answer №1

Below is the solution:

Step 1: Conceal the text when the width of the browser is below 991px or on mobile screens with a width less than 991px;

Step 2: Define a background image.

@media all and (max-width:991px){

    
      
#tst1{
      background-image:url('https://static.examplecdn.com/example123.png');     
       transition:all 0.3s;  
       color:transparent;
       background-size:50px 25px;
       background-repeat:no-repeat;
       background-position:center;



}
#tst2{
      background-image:url('https://static.examplecdn.com/example456.png');  
       transition:all 0.3s;       
       color:transparent;
       background-size:50px 25px;
       background-repeat:no-repeat;
       background-position:center;



}

... similarly for other elements ...

}
<span id="tst1">Restrooms</span>

<span id="tst2">Public grills</span>

... likewise for remaining items ...

I have made some slight adjustments to your code for functionality. Please expand the browser to full screen and resize it to observe the changes.

Additionally, substitute the corresponding sections of your code with this script and implement CSS. Although I used 991px as the breakpoint, you may choose a different breakpoint if desired.

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

performing a query on two tables with sequelize

There must be a more efficient way to accomplish this task with fewer lines of code. I am not very experienced with databases and I am new to sequelize and node. The user id is passed as a parameter and I need to check if there is a corresponding user in ...

Incorporate a "Back" button following the removal of the navigation bar in a Meteor-Ionic

When working on a Meteor-Angular-ionic app, I encountered a situation where I needed to hide the nav-bar in a template to create a full-screen view using the following code: <ion-view hide-nav-bar="true"> However, I then faced the challenge of addi ...

how to attach a function to a dynamically generated element

Currently, I am in the process of developing a placeholder-enabling feature. var t=document.createElement("input"); "placeholder" in t||$("input").each(function(){ if("submit"!==$(this).attr("type")){ var n=$(this),l=n.attr("placeholder"); ...

Incapable of choosing every radio button within the initial three rows of a table

In my table, each row contains approximately 11 radio buttons and a few other attributes. I am attempting to make the radio buttons in the first three rows non-editable using the provided code. However, it seems that the code is not functioning as intend ...

Tips for identifying whether a form contains any empty fields and, if it does, directing focus to an anchor element

Is it possible to determine if a specific form contains any input fields? What about if it doesn't have any input fields? Additional Query: Also, how can I ensure that the focus is returned to a button when the page loads if the specified condition ...

AngularJS: Establishing effective communication channels among directives

I am currently developing a custom directive for an audio player that supports mp3 files. The challenge I'm facing is how to handle multiple instances of the player on a single page. My goal is to ensure that when one player is active, starting anothe ...

Experiencing issues with the fadeIn() and fadeOut() methods while constructing a slider using Object-Oriented Programming in JavaScript and jQuery

I'm currently trying to develop a slider using jQuery and JavaScript. I have successfully implemented the slide change functionality, but I am facing difficulties in smoothly adding fadeIn() and fadeOut() effects... No matter where I insert these eff ...

Bootstrap 4 Carousel with Multiple Items

I am looking to create a carousel that can display multiple items or images at once. The carousel should show 4 items initially, and when I click on 'next', the next item should appear, and so forth. Below is an image depicting what I have in min ...

Verifying file types with HTML5 drag and drop feature

Is it possible to change the drop zone's background color to green or red based on whether the dragged payload contains supported file types (JPEG)? Do Gecko and Webkit browsers have the ability to determine the file type of drag and drop files? ...

Error: Module 'electron-prebuilt' not found

I am encountering an issue with my Electron app that utilizes Nightmare.js after compiling it into an .exe file using electron-packager. Everything functions properly until I click a button that triggers Nightmare.js, at which point I receive the followi ...

Has xlink:href become outdated for svg <image> elements?

In the realm of SVG attributes, it is noted by MDN xlink:href that href should be used over xlink:href. Interestingly, on the svg example page, last revised on July 6, 2017, the attribute utilized in the given example is xlink:href. The question arises: ...

Prevent content overlap in Tumblr's sidebars by using CSS positioning techniques

I'm facing an issue with my blog on Tumblr where the sidebar keeps overlapping the main content. I have modified a premade template and am trying to position the sidebar in the top right corner using absolute or fixed positioning: #sidebar{ position: ...

CKeditor dialog plugin allows for the inclusion of dynamic content elements that can be set at runtime, similar to the onShow method

When a user clicks or selects an element, I want to display its attributes in a dialog box. Everything works fine initially, but when I close the dialog and open it again for another element, it still shows the previous attributes list. The issue lies in t ...

The improper incorporation of the jQuery bootstrap-table plugin into Vue

Just diving into VUE and seeking guidance on how to successfully integrate the bootstrap table jQuery plugin. After loading jQuery globally and verifying its functionality, I'm faced with a scenario where I have an existing table (id="tableTest") wit ...

Converting an object with a combination of different index types into a string representation

I'm dealing with a unique object structure that behaves similarly to an array, except it contains a mix of index types (numbers and strings). Here's an example: var myObj = []; myObj[0] = 'a'; myObj[1] = 'b'; myObj[2] = &apos ...

Understanding how Regular Expression loops behave in TypeScript within an Angular environment

I have encountered a peculiar issue with regex. I am attempting to validate user input in a contentEditable div using regex after each keydown event. The goal is to modify the text by highlighting specific words such as "hello" or "status" with <span st ...

Order JSON Array Using LoDash Library

Recently I encountered a JSON array with the following structure: var json = [ { key: 'firstName', value: 'Bill' }, { key: 'lastName', value: 'Mans' }, { key: 'phone', value: '123.456.7890&apo ...

Is there a way to send an array of objects using axios-http?

Currently, I am utilizing react-dropzone for uploading mp3 files and a metadata npm to extract all the file contents. However, upon sending it to axios.post(), an error is encountered stating "Body Exceeded 1mb limit" Here is the snippet where the new dat ...

Is There a Lack of 'Contains' Support in JavaScript's ScriptEngine?

Below is some code that I am working with. ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); engine.eval("[1, 2, 3].contains(1)"); However, when I run the code, it throws the following e ...

Destructuring is not supported in useParams when using TypeScript

During my exploration of this video entitled "JWTUser Sessions with ReactJS & GraphQL...", I encountered a part at this timestamp where the presenter destructures the useParams() method from the react-router-dom library. However, when I tried to implement ...