分享好友 前端技术首页 频道列表

CSS 高级布局技巧

html教程  2016-12-23 15:260

  随着 IE8 逐渐退出舞台,很多高级的 CSS 特性都已被浏览器原生支持,再不学下就要过时了。

 

 用 :empty 区分空元素

兼容性:不支持 IE8

  Demo

  假如我们有以上列表:

<div class="item">a</div>
<div class="item">b</div>
<div class="item"></div>

  我们希望可以对空元素和非空元素区别处理,那么有两种方案。

  用 :empty 选择空元素:

.item:empty {
  display: none;
}

  或者用 :not(:empty) 选择非空元素:

.item:not(:empty) {
  border: 1px solid #ccc;
  /* ... */
}

 用 :*-Of-Type 选择元素

兼容性:不支持 IE8

  举例说明。

  给第一个 p 段落加粗:

p:first-of-type {
  font-weight: bold;
}

  给最后一个 img 加边框:

img:last-of-type {
  border: 10px solid #ccc;
}

  给无相连的 blockquote 加样式:

blockquote:only-of-type {
  border-left: 5px solid #ccc;
  padding-left: 2em;
}

  让奇数列的 p 段落先死红色:

p:nth-of-type(even) {
  color: red;
}

  此外,:nth-of-type 还可以有其他类型的参数:

/* 偶数个 */
:nth-of-type(even)

/* only 第三个 */
:nth-of-type(3)

/* 每第三个 */
:nth-of-type(3n)

/* 每第四加三个,即 3, 7, 11, ... */
:nth-of-type(4n+3)

 用 calc 做流式布局

兼容性:不支持 IE8

  Demo

  左中右的流式布局:

nav {
  position: fixed;
  left: 0;
  top: 0;
  width: 5rem;
  height: 100%;
}

aside {
  position: fixed;
  right: 0;
  top: 0;
  width: 20rem;
  height: 100%;
}

main {
  margin-left: 5rem;
  width: calc(100% - 25rem);
}

 用 vw 和 vh 做全屏滚动效果

兼容性:不支持 IE8

  Demo

  vw 和 vh 是相对于 viewport 而言的,所以不会随内容和布局的变化而变。

section {
  width: 100vw;
  height: 100vh;

  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;

  background-size: cover;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

section:nth-of-type(1) {
  background-image: url(https://unsplash.it/1024/683?image=1068);
}
section:nth-of-type(2) {
  background-image: url(https://unsplash.it/1024/683?image=1073);
}
section:nth-of-type(3) {
  background-image: url(https://unsplash.it/1024/683?image=1047);
}
section:nth-of-type(4) {
  background-image: url(https://unsplash.it/1024/683?image=1032);
}

body {
  margin: 0;
}
p {
  color: #fff;
  font-size: 100px;
  font-family: monospace;
}

 用 unset 做 CSS Reset

兼容性:不支持 IE

  Demo

body {
  color: red;
}
button {
  color: white;
  border: 1px solid #ccc;
}

/* 取消 section 中 button 的 color 设置 */
section button {
  color: unset;
}

 用 column 做响应式的列布局

兼容性:不支持 IE9

  Demo

nav {
  column-count: 4;
  column-width: 150px;
  column-gap: 3rem;
  column-rule: 1px dashed #ccc;
  column-fill: auto;
}

h2 {
  column-span: all;
}

查看更多关于【html教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Bootstrap.css 中请求googleapis.com/css?family 备忘录
问题描述: Web中引入bootstrap.css中头部有访问Google服务器的请求 @import url("//fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700");     国内访问不稳定,页面反应慢解决:将请求结果保存下来,放到本地,重新声明请求1.请求http:

0评论2023-03-16312

Vue PostCSS的使用介绍
目录PostCSS使用安装依赖运行使用第三方插件autoprefixer使用第三方插件postcss-preset-env使用第三方插件postcss-pxtorem运行的新方式PostCSSpostcss 一种对css编译的工具,类似babel对js的处理,常见的功能如:1 . 使用下一代css语法2 . 自动补全浏览器前缀

0评论2023-03-08315

css实现弹出框 css弹出菜单
弹出框也是前端里面经常使用的一个应用场景了,最开始想到的是用js实现这个效果,看到codepen上面有用css实现的。其实就是用到了css里面的一个:target选择器+visibility属性。URL 带有后面跟有锚名称 #,指向文档内某个具体的元素。这个被链接的元素就是目标

0评论2023-03-08519

jfinal框架页面找不到相关css,js文件404
在JFinalConfig中添加配置: @Overridepublic void configHandler(Handlers handlers) {handlers.add(new ContextPathHandler());}页面中添加:base href="${CONTEXT_PATH}/"/之前页面找不到资源,把tomcat工程路径去掉了(path=""),这样暂时解决了问题推荐

0评论2023-03-08576

移动端CSS底部固定和fixed定位在ios上的bug
fixed定位在ios上的bugcss三栏布局假设我们页面的 HTML 结构是这样: div class="wrapper"div class="content"!-- 页面主体内容区域 --/divdiv class="footer"!-- 需要做到 Sticky Footer 效果的页脚 --/div/div方法1.:absolute通过绝对定位处理应该是常见的

0评论2023-03-08624

css实现图片翻转 css使图片旋转
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"html xmlns="http://www.w3.org/1999/xhtml"headmeta http-equiv="Content-Type" content="text/h

0评论2023-03-08577

CSS3中的px,em,rem,vh,vw辨析 css3 vw
1、px:像素,精确显示2、em:继承父类字体的大小,相当于“倍”,如:浏览器默认字体大小为16px=1em,始终按照div继承来的字体大小显示,进场用于移动端          em换算工具:http://www.runoob.com/tags/ref-pxtoemconversion.html3、rem:与em类似,

0评论2023-03-08902

gulp自动化构建工具--自动编译less为css--学习笔记
 1.安装      命令:npm install gulp-less 或者 cnpm install gulp-less2.编写文件 //获取gulpvar gulp = require('gulp')//获取gulp-less模块var less = require("gulp-less")//编译less//在命令行输入gulp less启动此任务gulp.task('less',function(){

0评论2023-03-08411

关于动画Animate.css的使用方法
    首先贴个官网:https://daneden.github.io/animate.css/  1、引入animate css文件 1 head2 link rel="stylesheet" href="animate.min.css"3 /head   2、给指定的元素加上指定的动画样式名div class="animated bounceOutLeft"/div    这里包

0评论2023-03-08673

selenium Firefox禁用js,css.falsh,缓存,设置请求头和代理ip
1 profile = FirefoxProfile() 2 #请求头 3 #profile.set_preference("general.useragent.override", util.http_agent_insert(xinhuaUtil.agent_path)) 4 # 激活手动代理配置(对应着在 profile(配置文件)中设置首选项) 5 profile.set_preference("network

0评论2023-03-08777

CSS--容易混淆的.class1.class2与.class1 .class2的区别
容易混淆的.class1.class2与.class1 .class2的区别:第一个中间没空格的,匹配的是同时拥有class1和class2的元素。第二个中间有空格的,匹配的是父节点是class1类,子节点是class2类的元素。

0评论2023-03-08554

页面css样式找不到问题 页面css样式找不到问题怎么解决
出现了一个页面没有样式的问题:问题:  1.路径不对, 可以打开页面f12看样式是否找到  检查路径是否正确。  2.样式没引全或者没引对。查看引入的样式是否正确或缺少样式。  3.路径明明写对了却404找不到。可以试一试把${pageContext.request.context

0评论2023-03-08414

学习前端页面css定位 网页前端设计css
一、相对定位:absolute  相对定位是一个非常容易掌握的概念。如果对一个元素进行相对定位,它将出现在它所在的位置上。然后,可以通过设置垂直或水平位置,让这个元素“相对于”它的起点进行移动。position:relative;同时可以设置上下左右位置偏移;rela

0评论2023-03-08782

css-移动端模拟hover效果 css移动端适配
:hover 在移动端中,点击后一直处于hover状态,不会切换。为此解决:通过:active模拟!-- * @Author: lingxie * @Date: 2020-04-23 13:35:57 * @Descripttion: -- templatediv class="about"h1This is an about page/h1div class="btn-hole"按钮/div/div/templ

0评论2023-03-08409

更多推荐