Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>jmgModal</h1>
<button onclick="jmgModal('miventana',{
				title: 'Ventana modal',
				width: 600,
				height: 50,
				content: 'Este es el contenido de la ventana'});"
        >Modal Simple</button>
<button onclick="jmgModal('miventana',{
				title: 'Ventana modal',
				width: 1000,
				height: 60,
        content: '<iframe src=\'https://escss.blogspot.com\'></iframe>'});"
        >Modal con Iframe</button>
<button onclick="jmgModal('miventana',{
				title: 'Ventana modal',
				width: 100,
				height: 20,
        content: '<img src=\'https://1.bp.blogspot.com/-iYDMJD5wZ6Y/WR3R-CrcejI/AAAAAAAANjU/oAO4TVbqJ9IUtmYY4Dn2AkckJ5Zjv3F0QCLcB/s600/esCss-fav.jpg\'>'});"
        >Modal Imagen</button>
<button onclick="jmgModal('miventana',{
				title: 'Ventana modal',
				width: 400,
				height: 40,
        content: 'Esta es una ventana informativa'},
        ['Aceptar',function(){
             alert('Has pulsado Aceptar. La ventana se cierra automáticamente');
        }]);"
        >Modal con botón "Aceptar"</button>
<button onclick="jmgModal('miventana',{
				title: 'Ventana modal',
				width: 400,
				height: 40,
        content: 'En esta ventana se puede realizar una pregunta'},
        ['Aceptar',function(){
             alert('Has pulsado Aceptar. La ventana se cierra automáticamente');
        }],
        ['Cancelar',function(){
             alert('Has pulsado Cancelar. La ventana se cierra automáticamente');
        }]);"
        >Modal con botones "Aceptar" y "Cancelar"</button>
<button onclick="jmgModal('miventana',{
				title: 'Ventana modal',
				width: 400,
				height: 40,
        content: 'Esta ventana es equivalente a la función prompt de javascript. Para mostrar el input es imprescindible que haya un botón de Aceptar. El botón Cancelar es opcional'},
        ['Aceptar',function(input){
             alert('Has escrito: '+input);
        }], /* obligatorio */
        null, /* boton cancelar, opcional */
        ['Placeholder del input']);"
        >Modal con "Aceptar" y un Input</button>
<button onclick="jmgModal('miventana',{
				title: 'Ventana modal',
				width: 400,
				height: 40,
        content: 'Cambia la id de la ventana (primer parámetro) para mostrar varias ventanas anidadas.<br><button onclick=\'metaventana()\'>Abrir otra ventana</button>'});"
        >METAMODAL: Ventana dentro de Ventana</button>
<script>
// función aparte para no complicar el código
function metaventana() {
  jmgModal('ventana2',{
				title: 'Sub-ventana',
				width: 300,
				height: 20,
        content: 'Contenido de la sub-ventana'});
}
</script>

<p>Pen original de <a href='https://codepen.io/jorgemg/'><b>Jorge mg</b></a> para su artículo como autor invitado en EsCss</p>
              
            
!

CSS

              
                .jmgmodal {
position: fixed; margin: auto;
top:0;left:0;right:0;bottom:0;
background: rgba(34,34,68,.8);
box-sizing: content-box;
visibility: hidden; opacity: 0;
transition: all .12s; z-index: 99;
}
.jmgmodal.visible {
visibility: visible; opacity: 1;
}
.jmgmodal * {box-sizing: inherit}
.jmgmodal .panel {
position: absolute; margin: auto;
top:0;left:0;right:0;bottom:0;
max-width: 600px; max-height: 70vh;
background: #0ebeff; border-radius: 10px;
color: #000; padding: 50px 0;
transform: translateY(-25%);
transition: all .12s;
}
.jmgmodal.nofooter .panel {
padding: 50px 0 0 0;
}
.jmgmodal.visible .panel {
transform: none;
}
.jmgmodal .title {
position: absolute; top: 0;
width: 100%; height: 50px;
line-height: 50px;
background: orange;
font-weight: bolder; padding: 0 2em;
box-shadow: 2px 0 10px rgba(0,0,0,.6);
border-radius: 10px 10px 0 0;
box-sizing: border-box;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.jmgmodal .panel .close {
position: absolute; top: .25em; right: .75em;
cursor: pointer; font-size: 25px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.jmgmodal .panel .content {
max-height: 100%; overflow: auto;
padding: 1em; box-sizing: border-box;
}
.jmgmodal .panel .content iframe:only-child {
border: 0; width: 100%;
height: 100%; height: 80vh;
min-height: 200px;
}
.jmgmodal .panel .content img:only-child {
width: 100%; max-width: 100%;
}
.jmgmodal .panel .footer {
position: absolute; bottom: 0;
width: 100%; background: orange;
font-weight: bolder; padding: 0 .5em;
box-shadow: 2px 0 10px rgba(0,0,0,.6);
border-radius: 0 0 10px 10px;
box-sizing: border-box;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
text-align: right;
}
.jmgmodal .panel .footer .action {
border-radius: 10px; color: #000;
background: #fff; border: 0;
min-width: 80px; min-height: 35px;
font-weight: bold; cursor: pointer;
margin-left: 20px;
transition: all .12s;
}
.jmgmodal .panel .footer .action:hover {
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.6);
}

/* estilos del pen, ornamentales */
html {
  background: #fff;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
  color: #224;
  text-align: center;
  font-variant: small-caps;
}
h1 {
  font-weight: 400;
}
button {
  border: 3px solid #224;
  background: #fff;
  padding: 1rem;
  margin: 1rem .5rem;
  font-size: 1.2rem;
  font-variant: small-caps;
  transition: all .5s;
  cursor: pointer;
  box-shadow: inset 0 0 0 rgba(0,0,0,1);
}
button:hover {
  color: #fff;
  box-shadow: inset 0 0 0 10rem rgba(34, 34, 65, .68);
}

              
            
!

JS

              
                /************
* Autor:
*          JorgeMG
*          (https://codepen.io/jorgemg/pen/xEwGEr)
* Licencia: 
*         Creative Commons Reconocimiento-NoComercial-CompartirIgual
*         (https://creativecommons.org/licenses/by-nc-sa/4.0/deed.es_ES)    
* Finalidad:
*         Post divulgativo
*         (https://escss.blogspot.com/2016/09/ventana-modal-javascript-puro.html)
************/


function jmgModal(id, data, ok, cancel, input) {
	data=data || {};
	id="modal-"+id;
	if (document.getElementById(id)==null) {
		var d=document.createElement("div");
		d.className="jmgmodal";
		d.id=id;
		var p=document.createElement("div");
		p.className="panel";
		var t=document.createElement("div");
		t.className="title";
		var cl=document.createElement("div");
		cl.className="close";
		cl.innerHTML='&times;';
		cl.addEventListener('click',function(ev) {
			ev.preventDefault();
			var dTop=this.parentNode.parentNode;
			dTop.classList.remove("visible");
			dTop.querySelector(".panel .content").innerHTML='';
		});
		var ct=document.createElement("div");
		ct.className="content";
		var f=document.createElement("div");
		f.className="footer";
		p.appendChild(t);p.appendChild(cl);p.appendChild(ct);p.appendChild(f);
		d.appendChild(p);
		document.body.appendChild(d);
	}
	var mod=document.getElementById(id),
	p=mod.querySelector(".panel"),
	t=mod.querySelector(".panel .title"),
	ct=mod.querySelector(".panel .content"),
	f=mod.querySelector(".panel .footer");
	if (f==null) {
		mod.classList.remove("nofooter");
		var f=document.createElement("div");
		f.className="footer";
		p.appendChild(f);
	}
	t.innerHTML=data.title || '';
	ct.innerHTML=data.content || '';
	f.innerHTML='';
	if (!isNaN(data.width)) p.style.maxWidth=data.width+'px';
	if (!isNaN(data.height)) p.style.maxHeight=data.height+'vh';
	if (ok && ok.length>1) {
		var param={value:null};
		if (input && input.length>0) {
			var ph=document.createElement("p");
			ph.className="action";
			var txt=document.createElement("input");
			txt.className="action";
			txt.setAttribute("placeholder",input[0]);
			txt.addEventListener('keydown',function(ev) {
				if (ev.keyCode==13 || ev.key=="Enter") {
					ev.preventDefault();
					mod.classList.remove("visible");
					ok[1](param.value);
				}
			});
			ph.appendChild(txt); ct.appendChild(ph);
			param=ct.querySelector("p.action > input.action");
			setTimeout(function(){
				param.focus();
			},100);
		}
		var bOk=document.createElement("button");
		bOk.className="action";
		bOk.innerHTML=ok[0];
		bOk.addEventListener('click',function(ev) {
			ev.preventDefault();
			mod.classList.remove("visible");
			ok[1](param.value);
		});
		f.appendChild(bOk);
	}
	if (cancel && cancel.length>1) {
		var bCancel=document.createElement("button");
		bCancel.className="action";
		bCancel.innerHTML=cancel[0];
		bCancel.addEventListener('click',function(ev) {
			ev.preventDefault();
			mod.classList.remove("visible");
			cancel[1]();
		});
		f.appendChild(bCancel);
	}
	if (f.innerHTML=='') {
		p.removeChild(f);
		mod.classList.add("nofooter");
	}
	setTimeout(function(){
		mod.classList.add("visible");
	},50);
}

              
            
!
999px

Console