css 浮动(创建水平菜单栏,文档滚动,图片剪切等)

float 属性

其属性值有:left,right,none

ex1:将带有边框的边界的图像浮动与段落的右侧

<html>
<head>
<style>
div
{
    float:right;
    width:120px;
    margin:0  0 15px 20px;
    padding:15px;
    border:1px solid black;
    text-align:center;
}
</style>
</head>
<body>
<div>
<img src=url/></br>csdn
</div>
<p>
内容</p>
</body>
</html>

ex2:把段首的首字母浮动与左侧,以突出显示。

<html>
<head>
<style>
span
{
float:left;
width:0.7em;
font-size:400%;
font-family:courier;
line-height;
}
</style>
</head>
<body>
<p>
<span>T</span>his is some text.
This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</p>
</body>
</html>

ex3:创建水平菜单栏

<html>
<head>
<style>
ul
{
float:left;
width:100%;
padding:0;
margin:0;
list-style-type:none;
}
a
{
float:left;
width:7em;
text-decoration:none;
color:white;
background-color:purple;
padding:0.2em 0.6em;
border-right:1px solid white;
}
a:hover{backgroud-color:#ff3300;}
li{display:inline;}
</style>
</head>  
<body>
<ul>
<li><a href="#">Link one</a></li>
<li><a href="#">Link two</a></li>
<li><a href="#">Link three</a></li>
<li><a href="#">Link four</a></li>
</ul>
</body> 
</html>

在这里,要创建一个这样的水平菜单栏,要将<ul>和<a>元素向左浮动,而li元素显示为行内元素(元素前后没有换行)。

clear属性:

clear:both;则元素的左边和右边均不允许出现浮动元素。

clear:left;右边不允许

clear:right;左边不允许

***绝对定位

可以覆盖页面上的其他元素,通过z-index属性来。

设置z-index:-1;则该元素在其他元素之下。

position:absolute;

#display属性

display:none; 则该块元素不显示出来

display:block; 会有换行的行为

display:inline;在一行内,不会有换行的行为。

#如何设置滚动条来显示超出给定宽度和高度的内容

用overflow属性

可能的值

描述
visible 默认值。内容不会被修剪,会呈现在元素框之外。
hidden 内容会被修剪,并且其余内容是不可见的。
scroll 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。
auto 如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。
inherit 规定应该从父元素继承 overflow 属性的值。
<html>
<head>
<style>
div
{
background-color:green;
width:150px;
height:150px;
overflow:scroll;
</style>
</head>
<body>
<p>如果元素中的内容超出了给定的宽度和高度属性,overflow 属性可以确定是否显示滚动条等行为。</p>

<div>
这个属性定义溢出元素内容区的内容会如何处理。如果值为 scroll,不论是否需要,用户代理都会提供一种滚动机制。因此,有可能即使元素框中可以放下所有内容也会出现滚动条。默认值是 visible。
</div>
</body>
</html>

#剪切元素的形状

clip属性

唯一的合法的形状值:rect

clip:rect(上 ,右,下,左)

ex:剪切图像

img
{
position:absolute;
clip:rect(0px,60px,200px,0px);
}

$注意:这里position必须用absolute,因为要剪裁图像,需要完全删除,这一点十分重要。

猜你喜欢

转载自blog.csdn.net/weixin_41060905/article/details/81152923