html和css的项目实例(html模板下载)
大家好,今天小编来为大家解答html和css的项目实例这个问题,html模板下载很多人还不知道,现在让我们一起来看看吧!
css网页的几种布局实例
本文主要介绍了浅谈css网页的几种布局的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。
2018年已经过了一周,总结一下2017年在公司wiki上写的一篇关于css布局的知识,当时也借鉴了几个大神写的css布局知识,和自己在项目中遇到的坑。废话不多说。请看以下的干货。
1、左边固定,右边自适应布局的两种实现方式
效果图如下:
大屏展示:
小屏展示:
第一种实现方式通过负边距与浮动实现左边固定,右边自适应的布局。主要代码如下:
<style type="text/css">
.left{
float: left;
width: 100%;
height: 200px;
background-color: red;
}
.left-content{
margin-left: 30%;
}
.right{
float: left;
width: 30%;
margin-left:-100%;
height: 200px;
background-color: green;
}
.layout0{
clear: both;
width: 100px;
height: 100px;
background-color: yellow;
}
</style>
<body>
<p id="body">
<p class="left">
<p class="left-content">
设置子元素的margin,然后父元素必须浮动。
用父元素包裹,主要是因为right会覆盖left,从而导致left内容不可以看到,如果直接在left上设置margin或者padding会导致布局变化,因此只能再用一个p包裹内容,并且去除right覆盖的宽度。
</p>
</p>
<p class="right">-margin必须大于或等于自身的宽度才会上移</p>
<p class="layout0"></p>
</p>
</body>实现过程中需要注意的是:
1.自适应的容器需要容器包裹住,否则容器内的内容会被覆盖。
2.right容器的负边距必须大于或等于自身的宽度才会上移。
3.如果right容器负边距等于自身的宽度它会靠右对齐,如果负边距等于-100%,则会靠左对齐。
第二种通过浮动布局来实现左边固定,右边自适应的布局
主要的代码如下:
<style type="text/css">
.left{
float: left;
width: 200px;
height: 200px;
background-color: yellow;
}
.right{
padding-left: 200px;
height: 200px;
background-color: red;
}
@media(min-width: 650px) and(max-width: 1000px){
.left{
width: 150px;
}
.right{
margin-left: 150px;
}
}
@media(max-width: 640px){
.left{
width: 100px;
}
.right{
margin-left: 100px;
}
}
</style>
<body>
<p id="main">
<p class="left">左边固定宽度,右边自适应</p>
<p class="right"></p>
</p>
</body>实现过程中需要注意的是: 1. left需要脱离文档流,而right只需要正常显示就可以。
2.left只是覆盖在right上边,因此想要让right内容完整显示需要给right padding-left或者margin-left。
大屏展示:
小屏展示:
主要代码如下:
<style type="text/css">
#head{
height: 200px;
background-color: yellow;
}
#body{
width: 100%;
float: left;
}
.main{
background-color: green;
min-height: 200px;
margin: 0 210px;
}
.left{
float: left;
background-color: red;
width: 200px;
height: 200px;
margin-left:-100%;
}
.right{
float: right;
background-color: blue;
width: 200px;
height: 200px;
margin-left:-200px;
}
#footer{
clear: both;
height: 200px;
background-color: orange;
}
</style>
<body>
<p id="head">即左右固定,中间自适应,它可以利用margin-left为负数来实现,它的实现原理就是margin为负值可以改变float元素的排列位置</p>
<p id="body">
<p class="main">当多个元素同时从标准流中脱离开来时,如果前一个元素的宽度为100%宽度,后面的元素通过负边距可以实现上移。当负的边距超过自身的宽度将上移,只要没有超过自身宽度就不会上移</p>
</p>
<p class="left"></p>
<p class="right"></p>
<p id="footer"></p>
</body>实现过程中需要注意:
1.中间自适应的p需要放在left和right容器前面并且内容p需要用父容器包裹
2.left和right容器向同一个方向浮动。
主要代码如下:
<style type="text/css">
#head{
height: 200px;
background-color: yellow;
}
#body{
overflow: hidden;
}
.left{
float: left;
background-color: red;
width: 200px;
height: 200px;
}
.right{
float: right;
background-color: blue;
width: 200px;
height: 200px;
}
.main{
background-color: green;
height: 200px;
margin: 0 210px;
}
#footer{
clear: both;
height: 200px;
background-color: orange;
}
</style>
<body>
<p id="head">左右固定宽度并且向两边浮动,中间的p设置两边的margin</p>
<p id="body">
<p class="left"></p>
<p class="right"></p>
<p class="main">该方案有一个缺陷,在小屏幕情况下回导致right被挤下去,main没有了</p>
</p>
<p id="footer"></p>
</body>实现过程中需要注意:
1.该方式只需要注意中间自适应的p需要放在left和right容器的后面。
2.left和right容器向两边浮动。
主要代码如下:
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
<title>使用flex实现“双飞翼布局”</title>
</head>
<style type="text/css">
#main{
display: flex;
display:-webkit-flex;//谷歌浏览器加前缀
flex-flow: row nowrap;
justify-content: flex-start;
align-items: center;
}
.left{
flex: 0 0 auto;
width:100px;
height: 200px;
background-color: red;
word-wrap: break-word;
overflow: hidden;
}
.main{
flex: 1 1 auto;
height: 200px;
background-color: green;
}
.right{
flex: 0 0 auto;
width: 100px;
height: 200px;
background-color: yellow;
}
</style>
<body>
<p id="main">
<p class="left">flex语法我参照了阮一峰关于flex语法介绍 ;
<p class="main"></p>
<p class="right"></p>
</p>
</body>
</html>如果未了解过flex布局请移至文末点击链接查看阮一峰大神写的关于flex语法
3、定位布局
这边就不絮絮叨叨的讲一些基础的css定位知识了(ps:不会的请自行到w3c官网查阅),我主要来讲解一下工作中遇到的坑。以免其他人和我一样掉入坑中。
第一:使用多个fixed时,注意自己需要基于什么定位,因为如果父级有用transform属性时,可能会导致子元素的fixed基于父元素容器定位,而不是基于body定位。效果如下:
在上图中我可以发现中间黑色的小框是基于父级来定位,并且宽度也基于父容器的50%。详细的请看下面代码:
<!DOCTYPE html>
<html>
<head>
<title>关于position的定位的坑</title>
</head>
<style type="text/css">
body{
margin: 0;
padding: 0;
}
i{
font-style: normal;
cursor: pointer;
}
#delete-button{
position: absolute;
left: 45%;
top: 45%;
text-align: center;
vertical-align: middle;
height: 50px;
margin: auto;
cursor: pointer;
}
#delete-button> i{
display: inline-block;
width: 32px;
height: 32px;
border-radius: 16px;
background-color: orange;
color: red;
font-size: 32px;
vertical-align: middle;
line-height: 28px;
}
/*第一个模态框的样式*/
#layout{
display: none;
width: 100%;
height: 100%;
}
/*使用flex布局水平竖直居中*/
/*#layout-box{
position: fixed;
width: 100%;
height: 100%;
left: 0;
top: 0;
display: flex;
display:-webkit-flex;
flex-flow: column nowrap;
justify-content: center;
align-items: center;
background-color: rgba(0,0,0,0.3);
}*/
/*使用postion和 transform水平垂直居中*/
#layout-box{
position: fixed;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.3);
}
.modal-dialog{
position: absolute;
left: 50%;
top: 50%;
width: 500px;
height: 200px;
border-radius: 10px;
transform: translate(-50%,-50%);
-webkit-transform: translate(-50%,-50%);
-moz-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);
background-color:#fff;
}
.dialog-title{
text-align: center;
color:#333;
font-size: 28px;
margin-bottom: 10px;
}
.dialog-content{
text-align: center;
color:#666;
font-size: 18px;
}
.dialog-button{
margin-top: 20px;
width: 100%;
color:#333;
}
.dialog-button>.button-box{
display: inline-block;
width: 48%;
text-align: center;
}
.button-box span{
display: inline-block;
padding: 10px;
color:#fff;
border-radius: 6px;
cursor: pointer;
}
#confirm{
background-color:#27ad9a;
}
#cancel{
background-color: red;
}
/*添加按钮的样式*/
#add-button> i{
display: inline-block;
width: 32px;
height: 32px;
border-radius: 16px;
background-color:#27ad9a;
color:#fff;
font-size: 32px;
vertical-align: middle;
line-height: 28px;
text-align: center;
}
#add-button{
display: inline-block;
cursor: pointer;
}
/*第二个模态框的样式*/
.layout2{
display: none;
position: fixed;
width: 100%;
height: 100%;
left: 0;
top: 0;
background-color: rgba(0,0,0,0.2);
}
.modal-dialog2{
position: fixed;
left: 50%;
top: 50%;
width: 50%;
height: 50%;
border-radius: 10px;
transform: translate(-50%,-50%);
-webkit-transform: translate(-50%,-50%);
-moz-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);
background-color: rgba(0,0,0,0.2);
}
.modal-dialog2> span{
display: block;
}
.modal-text{
float: left;
}
#close{
color: red;
font-size: 24px;
float: right;
cursor: pointer;
}
</style>
<body>
<p id="delete-button"><i>-</i>删除</p>
<p id="layout">
<p id="layout-box">
<p class="modal-dialog">
<p class="dialog-title">提示</p>
<p class="dialog-content">是否删除该项,点击确定</p>
<p class="dialog-button">
<p class="button-box">
<span id="confirm">确定</span>
</p>
<p class="button-box">
<span id="cancel">取消</span>
</p>
</p>
<p id="add-butto
html和css学习总结
本周我大概用了六天的时间完成了html和css的复习、制作考核网页以及完成了学长布置的五个学习小任务,总的来说,虽然完成了这一周的学习,但是对于一些知识点掌握的不是很牢固,使用不太熟练,之后还需要多加练习,对于本周的学习我有如下总结:
对于写网页需要的基础知识一定要牢牢掌握,比如,每个网页都需要的导航栏,一定要熟练掌握;下拉菜单最常用的是 ul和 li标签,也就是列表元素;对于一些标签,一定要知道是块级元素还是行内元素,要掌握他们之间的转换方法,这个也是非常常用的。对于css的盒子模型,一定要认真地学习,并且能熟练的运用。
是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式。引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子元素进行排列、对齐和分配空白空间。
弹性容器通过设置 display属性的值为 flex或 inline-flex将其定义为弹性容器。
弹性容器内包含了一个或多个弹性子元素。
注意:弹性容器外及弹性子元素内是正常渲染的。弹性盒子只定义了弹性子元素如何在弹性容器内布局。
弹性子元素通常在弹性盒子内一行显示。默认情况每个容器只有一行。
当为父盒子设为flex布局,子元素的float、clear和vertical—align属性将失效
flex布局原理:通过给父盒子添加flex属性,来控制子盒子的位置好排列方式
flex布局父项常见的属性:
1.flex-diretion:设置主轴的方向
默认的主轴方向是x轴方向,水平方向;默认的侧轴方向是y轴方向,水平向下
属性值:row默认值从左到右
row-reverse从右到左
column从上到下
column从下到上
2.jusity-content:设置主轴上的子元素排列方式
属性值: flex-start默认值从头开始如果主轴是x轴,则从左到右
flex-end从尾部开始排列
center在主轴居中对齐(如果主轴是x轴则水平居中)
space-around平分剩余空间
space-between先两边贴边再平分剩余空间(重要)
3.flex-wrap:设置子元素是否换行
属性值: nowrap默认子元素不换行如果装不下,会缩小子元素的宽度,一块放到父元素中
wrap换行
4.align-content:设置侧轴的子元素排列方式(多行需要换行)单行无效
属性值: flex-start默认值在侧轴的头部开始排列
flex-end在侧轴的尾部开始排列
center在侧轴中间显示
space-around子轴在侧轴平分剩余空间
space-between轴在侧轴先分布在两头,再平分剩余空间
stretch设置子项元素高度平分父元素高度
5.align-items:设置侧轴的子元素排列方式(单行)
属性值: flex-start从上到下
flex-end从下到上
center挤在一起(垂直居中)
stretch拉伸(默认值)使用时子盒子不要给高度
例如:子元素居中默认主轴是x轴
主轴居中 jusity-content:center
侧轴居中 align-items:center
6.flex-flow:复合属性,相当于同时设置了flex-diretion和flex-wrap
flex布局子项常见属性
1.flex定义子项目分配剩余空间,用flex来表示占多少份数
flex:<number>;默认值 0
2.align-self控制子项自己在侧轴的排列方式
3.order属性定义子项的排列顺序(前后顺序)
给父元素添加 display:grid
display关于网格的取值分为两个,grid(块网格)和 inline-grid(行内网格行内块)
grid容器从上向下排列
inline-grid容器从左向右排列
grid-template-row;规定行属性
grid-template-column;规定列属性
1.绝对大小(根据列数或者行数确定值得个数)
grid-template-row:200px 200px 200px
grid-template-column:200px 200px 200px
2.百分比(根据列数或者行数确定值得个数)
grid-template-row:33.33% 33.33% 33.33%
grid-template-column:33.33% 33.33% 33.33%
3.repeat函数
grid-template-rows: repeat(3,33.3%);
grid-template-columns: repeat(3,33.3%);
4.repead auto-fill自动填充
grid-template-rows: repeat(auto-fill,33.3%);
grid-template-columns: repeat(auto-fill,33.3%);
5.fr片段
grid-template-rows: 100px 1fr 300px;
grid-template-columns: 100px 1fr 300px
6.minmax
grid-template-rows:minmax(100px,200px) 200px 300px
grid-template-columns:200px 200px 200px
7.auto
grid-template-rows: 100px auto 300px;
grid-template-columns: 100px auto 300px
列间距
1.grid-row-gap: 10px;
grid-column-gap: 10px;
复合写法
grid-gap: 10px 10px;
指定区域
1.grid-template-areas:'a b c'
'd e f'
'g h i';
区域合并时需要让合并的区域名字相同
grid-template-areas:'a a c'
'd e f'
'g h i';
.box div:nth-child(1){
grid-area: a;
}
以上两种网页布局方式是我制作网页时比较常用的,也是学习前端必须掌握的。
除了以上这些,还有很多是我们需要掌握的,加油吧继续学习!!!!!!下周分享js学习总结
css和html的作用和区别
Hypertext Markup Language,中文也就是超文本链接标示语言。HTML(HyperTextMark-upLanguage)即超文本标记语言,目的是为了能把存放在一台电脑中的文本或图形与另一台电脑中的文本或图形方便地联系在一起,形成有机的整体,人们不用考虑具体信息是在当前电脑上还是在网络的其它电脑上。我们只需使用鼠标在某一文档中点取一个图标,Internet就会马上转到与此图标相关的内容上去,而这些信息可能存放在网络的另一台电脑中。 HTML文本是由HTML命令组成的描述性文本,HTML命令可以说明文字、图形、动画、声音、表格、链接等。HTML是网络的通用语言,一种简单、通用的全置标记语言。它允许网页制作人建立文本与图片相结合的复杂页面,这些页面可以被网上任何其他人浏览到。
CSS语言是"Cascading Style Sheets"的缩写,中文翻译为"层叠式样式表单",它是由W3C协会制定并发布的一个网页排版式标准,是对HTML语言功能的补充。主要的用途是对网页中字体、颜色、背景、图像及其他各种元素的控制,使网页能够完全按照设计者的要求来显示。CSS语言是一个用于网页排版的标记性语言。css给html标记添加各种样式,用来告诉浏览器,因该如何显示这些标记里面的内容。
关于html和css的项目实例到此分享完毕,希望能帮助到您。