分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

前端 css 垂直居中及自适应问题

发布时间:2023-09-06 01:12责任编辑:赖小花关键词:前端自适应

此css作用为将下面div结构中的Container-fluid背景自适应屏幕,content自适应居中

1、Div结构

all

  Head

  Container-fluid     

        Content

  Under

 

<div id="all">
   <div  class="head" style="height: 81px;width: 100%;min-width: 1000px;position: relative;">
      <img src="/caunion/image/title.png" />
   </div>
   <div  class="container-fluid" style="background:url(/caunion/image/backgroud.jpg) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  min-height: 480px;
    min-width:1000px;" >
         <div class="content" >

         </div>
   </div>
   <div id="under" style="margin:auto auto;bottom:0px;height:28px; line-height: 28px;min-width: 1000px;width: 100%;position: fixed;">
      <div style="margin:auto">
         <span style="font-size:20px;color:#848484"></span>
      </div>
   </div>
</div>

2、三个主要问题

 

1) container-fluid块背景设置

background-size:cover 使图片完全铺满  在ie8显示效果有所不同

    <div  class="container-fluid" style="background: url(image/ac_comp_pro/backgroud.png) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
">

 

使背景图能一直铺满浏览器  81为container-fluid以上元素的高度和;此处只有head

<script>
       $(document).ready(function () {
           $(".container-fluid").height($(window).height()-81);
       })
       $(window).resize(function () {
           $(".container-fluid").height($(window).height()-81);
       })
</script>

2) div上下左右居中

   父div一定要有position属性

   子div 样式position:absolute;top:50%;left:50%;margin-top为自身高度一半;margin-left为自身宽度一半;

 <div style="position: relative;">
    <div style="position: absolute;top: 50%;left: 50%;height:100px;margin-top: -50px;width:200px;margin-left:-100px; ">
    </div>
</div>

3) 根据内容设置min-height、min-width使样式不会被挤变形

<div  class="container-fluid" style="background:url(/caunion/image/backgroud.jpg) no-repeat center center fixed;
 -webkit-background-size: cover;
 -moz-background-size: cover;
 -o-background-size: cover;
 background-size: cover;
 min-height: 480px;
   min-width:1000px;" >

3、供参考css

body{
    font-family: arial;
    font-size: 25px;
    text-align: center;
    width:100%;
    height:100%;
}

#all{
    width: 100%;
    height: 100%;
}

.head{
    height: 81px;
    width: 100%;
    min-width: 1000px;
    position: relative;
}

.container-fluid{

background:url(/caunion/image/backgroud.jpg) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  min-height: 480px;
    min-width:1000px; 

}

 

#content{
    position: absolute;
    top:50%;
    left: 50%;
    height:400px;
    width: 720px;
    margin-left: -360px;
    margin-top: -200px;
}
.under{
    
    height: 29px;
    bottom: 0px;
    width: 100%;
    min-width: 1000px;
    position: fixed;
    margin: auto auto;
}

 

4、若content中有多个部件,需要添加百分比间隔

 

将content中的内容放到container-fluid,设置百分比高度;再将其中的内容置中。

 .row为间隔作用

<div  class="container-fluid" >


   <div class="row" ></div>

   <div class="TabContent" ></div>


   <div class="login" ></div>


   <div class="TabTitle" ></div>


   <!-- 选项卡结束 -->
</div>

 

.row{

  height:10%;

}

 

.TabTitle{
   height:36%;
   width: 1000px;
   margin: auto;
   position: relative;
}

.login{
    height: 8%;
   position: relative;
}

.TabContent{
   height: 36%;
   position: relative;
}

前端 css 垂直居中及自适应问题

原文地址:http://www.cnblogs.com/pemty/p/7560344.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved