前端之背景与边框


渐变、背景图像和圆角,背景和边框的巧妙运用。

背景

背景颜色

background-color属性定义了CSS中任何元素的背景颜色。元素的内容和内边距

<style>
/* Background Colors  范围:内容和内边距
-------------------------------------------------- */

.box {
    background-color: #567895;
    border: 3px solid black;
    padding: 10px;
}

.box h2 {
    color: white;
    background-color: black;
}

.box span {
    color: yellow;
    background-color: black;
}
</style>
    <div class="box">
        <h2>Background Colors</h2>
        <p>Try changing the background <span>colors</span>.</p>
    </div>

在这里插入图片描述

背景图片

background-image
小图则是平铺以填充方框。——background-repeat
大图不会缩小以适应,只显示一部分。——background-size
背景图像将显示在背景颜色的顶部

<style>
body {
    background-image: url('../images/little-bg.png');
}
</style>

在这里插入图片描述

<style>
body {
    background-image: url('../images/little-bg.png');
    background-color: black;
}
</style>

在	这里插入图片描述

背景平铺

在这里插入图片描述

<style>

body {
    background-image: url('../images/little-bg.png');
    background-color: black;
    background-repeat: no-repeat;
}
</style>

在这里插入图片描述

图片大小

background-size
用数值设置

cover 使图像足够大,保持其高宽比的情况下完全覆盖。可能会跳出盒子外。保持比例拉伸。
contain 使图像的大小适合盒子内。图的长宽比与盒子的长宽比不同,可能在图像的任何一边出现间隙。

原图:
在这里插入图片描述

<style>
body {
    background-image: url('../images/big-bg.jpg');
    background-color: black;
    background-repeat: no-repeat;
}
</style>

在这里插入图片描述

<style>
body {
    background-image: url('../images/big-bg.jpg');
    background-color: black;
    background-size: cover;
}
</style>

在这里插入图片描述
在这里插入图片描述


<style>
body {
    background-image: url('../images/big-bg.jpg');
    background-color: black;
    background-size: contain
}
</style>

在这里插入图片描述

背景图像定位

background-position使用方式:

<style>
	background-attachment: fixed;
    background-position: 100px 100px;
</style>

在这里插入图片描述
在这里插入图片描述
有点无语。。。

在这里插入图片描述

默认的背景位置值是(0,0),也就是左上角。

背景图片建议配套方式:(大小铺满 不重复)

<style>
body {
    background-image: url('../images/big-bg.jpg');
    background-color: black;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-attachment: fixed;
    /* background-position: 0 0; */
}
</style>

但是当宽度缩小时,背景图片出现了变形。
建议在加上

<style>
       /* 页面缩小时, 背景图片上下,左右居中*/
       background-position-x: center;
       background-position-y: center;
</style>

去掉:

<style>
    background-repeat: no-repeat;
</style>

改:

<style>
    background-size: cover;
</style>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

渐变背景

渐变背景用CSS Gradient生成,然后复制代码。

<style>
body {
    background: rgb(255, 11, 0);
    background: linear-gradient(90deg, rgba(255, 11, 0, 1) 0%, rgba(0, 242, 255, 1) 0%, rgba(0, 30, 255, 1) 50%, rgba(14, 0, 255, 1) 100%, rgba(0, 212, 255, 1) 1000%, rgba(9, 9, 121, 1) 1000%);
}
</style>

在这里插入图片描述

多个背景图像

<style>
body {
    background-image: url('../images/little-bg.png'), url('../images/firefox1.png');
}
</style>

第一个url在第一层,第二个url在 第二层。
在这里插入图片描述

<style>
    background-image: url('../images/little-bg.png'), url('../images/firefox1.png');
    background-repeat: repeat-x, repeat-y;
</style>

background-image 和 background-repeat:一一对应。
在这里插入图片描述

边框

边框的 宽 样式 颜色
边框某部分的 宽 样式 颜色
在这里插入图片描述
混合边框:

<style>
{border-style: dotted dashed solid double;}
</style>

在这里插入图片描述

圆角

border-radius样例

<style>
.box2 {
    color: red;
    padding: .5em;
    border-style: dashed dotted double groove;
    border-color: red;
    border-width: .5em;
    border-radius: 10% 30%;
    background-color: black;
}
</style>
<div class="box2">
    <h2>Borders</h2>
    <p>Try changing the <span>span>borders</span>.</p>
</div>

推荐背景图片设置

<style>
	body {
            background-image: url('路径');
            background-color: black;
            background-repeat: no-repeat;
            background-size: cover;
            background-attachment: fixed;
            background-position: center;
        }
</style>

效果:
宽屏
在这里插入图片描述
窄频
在这里插入图片描述
手机屏
在这里插入图片描述
背景图片设置的事,终于让我踏实了。

网页实例
下一节:前端之书写模式

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值