第14章css样式设置小技巧

第15章 css样式设置小技巧
  
15.1水平居中设置-行内元素
    如果被设置元素为文本、图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的。
<html>
<head>
<meta charset="utf-8">
<title>定宽块状元素水平居中</title>
<style>
div{
    border:1px solid red;
    margin:20px;
}
div.txtCenter{
 text-align:center;
}
div.imgcenter{
    text-align:center;
}
</style>
</head>

<body>
<div class="txtCenter">我是文本,哈哈,我想要在父容器中水平居中显示。</div>
<!--下面是任务部分-->
<div class="imgCenter"><img src="http://img.mukewang.com/52da54ed0001ecfa04120172.jpg" /></div>
</body>
</html>
--------------------------------------------------------------------------------------------------------
15.2 水平居中设置-定宽块状元素

     满足定宽和块状两个条件的元素是可以通过设置“左右margin”值为“auto”来实现居中的。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>定宽块状元素水平居中</title>
<style>
div{
    border:1px solid red;
    width:200px;
 margin:20px auto;
}
</style>
</head>

<body>
<div>我是定宽块状元素,哈哈,我要水平居中显示。</div>
</body>
</html>
-------------------------------------------------------------------------------------------------
15.3 水平居中总结-不定宽块状元素方法(一)
<html>
<head>
<meta charset="utf-8">
<title>不定宽块状元素水平居中</title>
<style>
table{
    margin:0 auto;
}
ul{list-style:none;margin:0;padding:0;}
li{float:left;display:inline;margin-right:8px;}

/*下面是任务区代码*/
.wrap{
    background:#ccc;
}
</style>
</head>

<body>
<div>
<table>
  <tbody>
    <tr><td>
 <ul>
     <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
    </ul>
    </td></tr>
  </tbody>
</table>
</div>

<div class="wrap">
  设置我所在的div容器水平居中 
</div>
</body>
</html>

------------------------------------------------------------------------------------
15.4 水平居中总结-不定宽块状元素方法(二)
第二种方法:改变块级元素的 display 为 inline 类型,然后使用 text-align:center 来实现居中效果。
<html>
<head>
<meta charset="utf-8">
<title>不定宽块状元素水平居中</title>
<style>
.container{text-align:center;}
.container ul{list-style:none;margin:0;padding:0;display:inline;}
.container li{margin-right:8px;display:inline;}
</style>
</head>

<body>
<div class="container">
    <ul>
     <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
    </ul>
</div>
</body>
</html>
--------------------------------------------------------------------------------------------
15.5  水平居中总结-不定宽块状元素方法(三)
方法三:通过给父元素设置 float,然后给父元素设置 position:relative 和 left:50%,子元素设置 position:relative 和 left:-50% 来实现水平居中。
<html>
<head>
<meta charset="utf-8">
<title>不定宽块状元素水平居中</title>
<style>
.container{
    float:left;
 position:relative;
 left:50%
}

.container ul{
 list-style:none;
 margin:0;
 padding:0;
 
 position:relative;
 left:-50%;
}
.container li{float:left;display:inline;margin-right:8px;}


/*下面是代码任务区*/

.wrap-center{
    background:#ccc;
   
   
}
</style>
</head>

<body>
<div class="container">
 <ul>
     <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
    </ul>
</div>

<!--下面是代码任务区-->
<div class="wrap">
    <div class="wrap-center">我们来学习一下这种方法。</div>
</div>
</body>
</html>
----------------------------------------------------------------------------------
15.6 垂直居中-父元素高度确定的单行文本
父元素高度确定的单行文本的竖直居中的方法是通过设置父元素的 height 和 line-height 高度一致来实现的。如下代码
<html>
<head>
<meta charset="utf-8">
<title>垂直居中</title>
<style>

.wrap h2{
    margin:0;
    height:100px;
    line-height:100px;
    background:#ccc;
}
</style>
</head>

<body>

<!--下面是代码任务部分-->
<div class="wrap">
    <h2>hi,imooc!</h2>
</div>
</body>
</html>
-----------------------------------------------------------------------------------------------
15.7 垂直居中-父元素高度确定的多行文本(方法一)
方法一:使用插入 table (包括tbody、tr、td)标签,同时设置 vertical-align:middle。
<html>
<head>
<meta  charset="utf-8">
<title>父元素高度确定的多行文本</title>
<style>
  .wrap{height:200px;background:#ccc}
</style>
</head>

<body>
<table><tbody><tr><td class="wrap">
<div>
    <p>看我是否可以居中。</p>
    <p>看我是否可以居中。</p>
    <p>看我是否可以居中。</p>
    <p>看我是否可以居中。</p>
    <p>看我是否可以居中。</p>
</div>
</td></tr></tbody></table>

<!--下面是代码任务区-->
<div>
    <img src="http://img.mukewang.com/54ffac56000169c001840181.jpg" title="害羞的小女生"/>
</body>
</html>
</div>
-----------------------------------------------------------------------------------------------------
15.8垂直居中-父元素高度确定的多行文本(方法二)
在 chrome、firefox 及 IE8 以上的浏览器下可以设置块级元素的 display 为 table-cell,激活 vertical-align 属性,
但注意 IE6、7 并不支持这个样式。
<html>
<head>
<meta  charset="utf-8">
<title>父元素高度确定的多行文本</title>
<style>
.container{
    height:200px;
 background:#ccc;
 
 display:table-cell;/*IE8以上及Chrome、Firefox*/
 vertical-align:middle;/*IE8以上及Chrome、Firefox*/
}
</style>
</head>

<body>
<div class="container">
    <div>
        <p>看我是否可以居中。</p>
        <p>看我是否可以居中。</p>
        <p>看我是否可以居中。</p>
        <p>看我是否可以居中。</p>
        <p>看我是否可以居中。</p>
    </div>
</div>
<!--下面是代码任务区-->
<div>
    <img src="http://img.mukewang.com/54ffac56000169c001840181.jpg" title="害羞的小女生"/>
</div>
</body>
</html>
----------------------------------------------------------------------------------------------------------
15.9 隐性改变display类型
设置以下 2 个句之一: position : absolute  float : left 或 float:right
元素会自动变为以 display:inline-block 的方式显示.

<html>
<head>
<meta  charset="utf-8">
<title>隐性改变display类型</title>
<style>
.container a{
    position:absolute;
 width:200px;
 background:#ccc;
 
}

</style>
</head>

<body>
<div class="container">
    <a href="#" title="">进入课程请单击这里</a>
</div>
</body>
</html>
----------------------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值