Skip to content

Instantly share code, notes, and snippets.

@legoktm
Created March 2, 2025 07:36
Show Gist options
  • Select an option

  • Save legoktm/2857432ad490626462f85d56146d9c7c to your computer and use it in GitHub Desktop.

Select an option

Save legoktm/2857432ad490626462f85d56146d9c7c to your computer and use it in GitHub Desktop.
Resizing a Leaflet map to 80% of screen height

Resizing a Leaflet map to 80% of screen height

human (Feb 15, 2025, 05:53 PM)

I am embedding a map into a webpage using leaflet. How can I make the map take up about 80% of the height of the screen? I currently have it take 80% the width with the following CSS:

        .container {
            max-width: 80%;
            margin: 0 auto;
        }

        #map {
            height: 600px;
            width: 100%;
            border-radius: 8px;
            border: 2px solid #ddd;
        }
 

assistant (Feb 15, 2025, 05:54 PM)

To make the map take up 80% of the viewport height, you can use the vh (viewport height) unit instead of a fixed pixel value. Here's how to modify your CSS:

.container {
    max-width: 80%;
    margin: 0 auto;
}

#map {
    height: 80vh;  /* Changed from 600px to 80vh */
    width: 100%;
    border-radius: 8px;
    border: 2px solid #ddd;
}

The vh unit represents a percentage of the viewport height, where 100vh equals 100% of the visible screen height. So 80vh will make the map take up 80% of the screen height, and it will automatically adjust when the window is resized.

If you find that the map is getting cut off due to other elements on the page (like headers or footers), you might need to adjust the percentage to a slightly lower value, like 75vh or 70vh, to accommodate those elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment