• Stars
    star
    1,821
  • Rank 25,497 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A modal built with pure CSS, enhanced with JavaScript

CSS Modals Build Status

Modals built out of pure CSS

Please visit the website to read more about this project and refer to the FAQ in case of a question.

What is it

Built with pure CSS: CSS Modal is built out of pure CSS. JavaScript is only for sugar. This makes them perfectly accessible.

Optimized for mobile: The modals are designed using responsive web design methods. They work on all screen sizes from a small mobile phone up to high resolution screens.

Use as Sass plugin: You can use CSS Modal as Sass plugin and apply it to your custom classes. No need to understand all the code.

A few other advantages: Accessible, cross-browser, media-adaptive, small and fast!

How to use

Please be aware that modals get stacked above each other if you open one modal from within another. You can add a data-attribute data-stackable="false" to the modal in order to make it non-stackable.

Markup

You need to include the markup and content for modals in your website. This has a positive effect on SEO. The example code:

<section class="modal--show" id="modal-text" tabindex="-1"
        role="dialog" aria-labelledby="modal-label" aria-hidden="true">

    <div class="modal-inner">
        <header id="modal-label"><!-- Header --></header>
        <div class="modal-content"><!-- The modals content --></div>
        <footer><!-- Footer --></footer>
    </div>

    <a href="#!" class="modal-close" title="Close this modal" data-close="Close"
        data-dismiss="modal">?</a>
</section>

The id attribute is the one which identifies the modal. You can link to this ID from everywhere.

Please note that the ID cannot include the / character since this one is needed for identifying stacked modals.

Using header and footer is optional. Just remove the tags if you don't want them in a modal.

You should leave the link's href attribute that way to close the modal in order to prevent the page from scrolling to top when clicking on it.

Please remember to set a unique ID for the header and change the aria-labelledby attribute to the same value.

You link to a modal by simply setting the ID to a link element's href like this:

<a href="#modal">Modal</a>

If you want to decouple the modal call from the location's hash you need to add data-cssmodal-nohash to the link.

JavaScript

As stated above you don't need JavaScript to get a good experience out of CSS Modals. But there are some issues where JavaScript helps:

  • IE 8 compatibility (please include jQuery if you need full compatibility).
  • Pressing escape: If you press ESC on your keyboard while the modal is visible it closes itself. This behavior cannot be achieved with CSS only.
  • Preventing background page from scrolling: If you scroll within the modal and you reach the end you don't want the page in the background to scroll. To prevent this JavaScript pushes a CSS class selector to the body element.
  • Being accessible: To get the browser's focus to the modal and back after closing.
  • Firing events: When a modal opens a custom event is fired called cssmodal:show. When the modal is hidden, an event called cssmodal:hide is triggered.
  • To add this behavior to your website, please include the JavaScript file modal.js right before the closing body-tag:
<script src="js/modal.js"></script>

Browser Support

This modal is designed to work on all modern browsers. Unfortunately this does not include Internet Explorer 7 or lower. But we deal with IE 8 โ€“ well,โ€ฆ at least it works.

On mobile Safari for iOS and Android 4+ it is tested pretty well, while Android 2.3 has some problems (biggest issue is scrolling). It's also working on Windows Phone 8.

In numbers:

  • Chrome
  • Firefox
  • Safari 6.x
  • Opera 12+
  • Internet Explorer 8 (functional, include jQuery if you want support for events)
  • Internet Explorer 9+
  • iOS 6
  • Android 2.3 (functional)
  • Android 4.x
  • Windows Phone 8

Media

Please be aware that you need to stop playing videos or audio manually after hiding the modal. We have a plugin for this though.

Events

There is an event cssmodal:show fired on the modal itself after the modal is shown. Another event cssmodal:hide is fired after the modal is hidden.

You can use the events by subscribing to them as if they were click events or something. Here is an example using jQuery:

$(document).on('cssmodal:show', function (event) {
    console.log(event);
});

There events are not fired in IE8. Please be aware of that and use jQuery or something else to create custom events.

Plugins

We have a couple for the modal to enhance it:

  • Resize - Resizes modal to size of input elements
  • Gallery - A lightbox plugin (in connection with resize)
  • HTML5 Video - Load videos within the modal
  • Maximum Width - Set a custom maximum width on a per-modal basis

Bug reports and feature requests

If you got something that's worth including into the project please open an issue for further discussion.

Please see the section on contributing on the website.

Contributors

This is a project by Hans Christian Reinl. Thanks goes out to all other contributors.

More Repositories

1

Sass-Mixins

Sass mixins for general use
CSS
523
star
2

checklist

๐Ÿ“‹ A Frontend Checklist for Websites
277
star
3

less-mixins

LESS mixins for general use
CSS
99
star
4

contentful-to-algolia

โšก๏ธ Transmit content from any Contentful type to Algolia indexes
JavaScript
52
star
5

hongkong

A parallax scroll effect plugin
HTML
44
star
6

PubSub

A JS pub/sub implementation
JavaScript
43
star
7

jsFiddle-Chrome-App

The jsFiddle Chrome-App
17
star
8

slider

Wrecking Slide โ€“ A jQuery slider.
JavaScript
12
star
9

resumerx

ResumerX is a CV enhancement tool utilizing OpenAI's GPT-3 API
TypeScript
8
star
10

isMothereffinjQueryUp

6
star
11

WordPress-Admin-Kit

Some functions and hooks to add to a WordPress theme
PHP
6
star
12

drublic-blog

This is my blog
PHP
5
star
13

gulp-css-background-remove

Remove `background-image` from CSS file to load asynchron
JavaScript
5
star
14

drublic

My vCard on the web
TypeScript
4
star
15

dotfiles

My Mac OSX dotfiles
Shell
4
star
16

TYPO3.PackageBuilder

PHP
4
star
17

dabblet-chrome-app

A Chrome App for Dabblet
JavaScript
4
star
18

Store

A JavaScript lib for storing stuff
JavaScript
3
star
19

jQuery.baseSVG

A jQuery-Plugin for SVG encoding into Base64
JavaScript
3
star
20

EB-modeller-prototype

Compare jsPlumb and JointJS
JavaScript
3
star
21

socoded-init

My slides for SoCoded Conference on Sept. 19 2013 - Starting Front-end Projects
JavaScript
3
star
22

h5bp-stylus

The HTML5Boilerplate's CSS in Stylus
2
star
23

component-based-development

My slides from Beyond Tellerrand 2013 - Component Based Development
JavaScript
2
star
24

takeoff-awesome-code

The slides of my talk at TakeOff Conf in January 2013 on
JavaScript
2
star
25

uxm-styleguides

Slides for talk at UX Munich 2013
JavaScript
1
star
26

EB

JavaScript
1
star
27

fetch-rest-connect

Opinionated fetch interface for RESTful APIs
TypeScript
1
star
28

code-examples

Tooling- and code-examples
JavaScript
1
star
29

jquery.modals

A jQuery-plugin for lightboxes
JavaScript
1
star
30

Templates

Template parsing with Hogan
JavaScript
1
star
31

kjs-components

My slides for Karlsruhe JS on Sept. 18 2013 - Component Based Webdevelopment
CSS
1
star