伪类target实现纯css模态框

今天看到一个纯css模态框,觉得是很牛呀

看了下用了target伪类,

一直不知道有这么神奇的伪类

。。

用法是这样的,给模态框一个id,

<div id="pop" class="overlay">

然后通过锚链接的方法

<a class="button" href="#pop">Click!</a>

点击之后,伪类的css就能发生作用了

.overlay:target {
  visibility: visible;
  opacity: 1;
}
.overlay:target > .modal {
  transform: translateY(30%) scale(0.9);
  transition-timing-function: cubic-bezier(0.8, 0, 0, 1.5);
  opacity: 1;
}

顺便把别人的源码发一下

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSS模态框-jq22.com</title>
<style>
html, body {
  height: 100%;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  background-color: #FAFAFB;
  color: slategrey;
}

a.button {
  text-decoration: none;
  text-align: center;
  text-shadow: 0 1px 0 #fff;
  color: steelblue;
  font-weight: 500;
  padding: 8px 15px 8px 15px;
  border: 1px solid rgba(26, 53, 71, 0.1);
  border-radius: 4px;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  transition: .25s;
  background: linear-gradient(white, ghostwhite);
}
a.button:hover {
  border: 1px solid rgba(26, 53, 71, 0.2);
  background: white;
}

.wrapper {
  width: 600px;
  height: 100%;
  margin: auto;
  text-align: center;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  align-items: center;
  justify-content: center;
}

.overlay {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity .5s;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.modal {
  width: 20%;
  position: relative;
  margin: auto;
  padding: 1.5rem;
  background: #fff;
  border-radius: 4px;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  transition: .5s;
  opacity: 0;
}
.modal .content {
  margin-top: 1rem;
}
.modal .content a {
  max-width: 60%;
  margin: auto;
  display: block;
}

.overlay:target > .modal {
  transform: translateY(30%) scale(0.9);
  transition-timing-function: cubic-bezier(0.8, 0, 0, 1.5);
  opacity: 1;
}

h2 {
  text-align: center;
  margin-top: 2rem;
}

a.close {
  position: absolute;
  top: 15px;
  left: 15px;
  width: 22px;
  height: 22px;
  text-decoration: none;
  text-align: center;
  font-size: 20px;
  line-height: 21px;
  color: lightslategrey;
  background-color: lightgrey;
  border-radius: 50%;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  transition: .25s;
}
a.close:hover {
  background-color: tomato;
  color: white;
}

</style>
</head>
<body>
<div class="wrapper">
  <a class="button" href="#pop">Click!</a>
</div>

<div id="pop" class="overlay">
  <div class="modal">
    <h2>Hey there!</h2>
    <a class="close" href="#">×</a>
    <div class="content">
      So, here we are:<br/>
      I am a pure (S)CSS Modal and work with the <strong>:target</strong> element.<br/><br/>
      You can close me by clicking the x in the upper left corner or the button at the bottom.<br/><br/>
      See you next time:)<br/><br/>
      <a class="button" href="#">Close</a>
    </div>
  </div>
</div>
</body>
</html>
时间: 2024-08-29 17:56:01

伪类target实现纯css模态框的相关文章

使用基于关系的选择器和伪类选择器创建纯CSS无JavaScript的鼠标移动到上面即可显示的下拉菜单

html代码: <div class="menu-bar"> <ul> <li> <h3 class="text-warning"><a class="drop-decoration text-warning">CSS</a></h3> <ul> <li> html/css从入门到精通 </li> <li> html

【转】链接伪类(:hover)CSS背景图片有闪动BUG

来源:http://www.css88.com/archives/744 ------------------------------------------------------------------------------------------------- IE6下链接伪类(:hover)CSS背景图片有闪动BUG,主要原因ie会再一次请求这张图片,或者说图片没被缓存. 例如: CSS代码 a:hover{background:url(imagepath)} 常用的解决方案: 在页面

关于伪类:target

什么是target伪类?先看一下解释: URL后面跟锚点#,指向文档内某个具体的元素.这个被链接的元素就是目标元素(target element),:target选择器用于选取当前活动的目标元素. 举个例子: <html> <a href="#target">点我</a> <div id="target"><div> </html> 正常情况下,a标签里会跟一个url,但是在这里我们添加的不是u

【2048小游戏】——CSS/原生js爬坑之纯CSS模态对话框&amp;游戏结束

引言:2048小游戏的结束界面,使用纯CSS制作模态对话框,一般做模态对话框都会使用BootStrap自带的模态对话框组件方便使用,但在制作要运行在移动端的小项目时,就不能使用BootStrap,因为文件太大,下载耗时,耗费流量. 一.模态对话框的组成 2个Div,一个铺满整屛,一个显示内容 坑:如何让Div铺满整屛?解决:2个办法 宽  高 100%    →    position:absolute:  →   top=0;left=0; 四个方向  margin-top/left/righ

纯css单选框

1.没有用bootstrap时: 1 .has_sel,.un_sel{display:block; width:16px; height: 16px; border: 1px solid #B06A50; border-radius: 1px; position: relative; } 2 .has_sel:before{content: ""; width: 2px; height: 6px; background-color: #B06A50; border-radius: 1

伪类的用法以及css一些常用的标签

伪类的用法: :link 表示该超链接文字尚未被点选 :visited 表示该超链接文字已被点选过 :active 表示该超链接文字正被点选,但未被放开 :hover 表示当鼠标停留在文字上 css的一些常用属性标签: background:背景颜色 padding(内边框):padding-top(上边距) padding-left(左边距) text-align left 文字靠左right 文字靠右center 文字靠中 background-size:cover 背景填充 原文地址:ht

纯CSS气泡框实现方法探究

气泡框(或者提示框)是网页中一种很常见的元素,大多用来展示提示信息,如下图所示: 拆分来看,形如这种气泡框无外乎就是一个矩形框+一个指示方向的三角形小箭头,要制作出这样的气泡框,如果解决了三角形小箭头就容易了.一种方法就是制作这样一个三角形箭头的图片,然后定位在矩形框上.但这种解决办法在后期更改气泡框会很不方便,可能每修改一次气泡框都要重新制作一个三角形小图标.如果我们能够直接用HTML和CSS代码实现这样一个三角形小箭头一切都迎刃而解了. 首先我们来看一下border这个属性,当我们把一个di

纯CSS气泡框实现方法探究(转)

气泡框(或者提示框)是网页中一种很常见的元素,大多用来展示提示信息,如下图所示: 拆分来看,形如这种气泡框无外乎就是一个矩形框+一个指示方向的三角形小箭头,要制作出这样的气泡框,如果解决了三角形小箭头就容易了.一种方法就是制作这样一个三角形箭头的图片,然后定位在矩形框上.但这种解决办法在后期更改气泡框会很不方便,可能每修改一次气泡框都要重新制作一个三角形小图标.如果我们能够直接用HTML和CSS代码实现这样一个三角形小箭头一切都迎刃而解了. 首先我们来看一下border这个属性,当我们把一个di

利用target的特性,可以实现纯css的tab效果切换

基础知识: :target起作用的是href连接到的位置 如 <a href="#tab1">tab1</a> <div id="tab1" class="tab">This is a tab1</div> :target{ color:red; } 但点击a标签的时候,连接到id是tab1的div,对这个div起作用color:red; 如: <a href="#tab"