新手常见css问题

本文介绍了多种CSS布局技巧,包括文本换行控制、图片居中、循环动画设置、左右布局调整等,并提供了浮动导致的高度塌陷解决方案。

1、文本强制换行

word-break: break-all; 只对英文起作用,以字母作为换行依据。
word-wrap: break-word; 只对英文起作用,以单词作为换行依据。
white-space: pre-wrap; 只对中文起作用,强制换行。

2、文本禁止换行

white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;

3、图片水平+垂直居中

方案1

.title {
   height: 15%;
   font-size: 18px;
   position: relative;
}
.title img{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
方案2

.title {
  display: flex;
  justify-content: center;
  align-items: center;  
}

.img {
  width: 200px;
}
方案3

.title {
    width: 200px;
    height: 200px;
    vertical-align: middle;
    display: table-cell;
    text-align: center;
}

 4、css3循环动画

div
{
	width:100px;
	height:100px;
	background:red;
	position:relative;
	animation:myfirst 5s infinite;

}

@keyframes myfirst
{
	0%   {background:red; left:0px; top:0px;}
	25%  {background:yellow; left:200px; top:0px;}
	50%  {background:blue; left:200px; top:200px;}
	75%  {background:green; left:0px; top:200px;}
	100% {background:red; left:0px; top:0px;}
}

5、左右布局,左侧高度跟随右边,右边宽高不固定

方案1

.left{
  height: 100%;
  width: 100px;
  background: red;
  position: absolute;
}
.right{
  margin-left: 100px;
  background: blue;
}
.parent{
  position: relative;
}
方案2

  .parent{
     display: flex;
   }
   .left{
	 width:100px;
     background: red;
   }
   .right{
	 flex:1;
     background: blue;
   }

6、浮动导致父元素高度塌陷

方案1

.father{
	min-height:50px;
    width:300px;
    background:green;
}
.son {
    float:left;
    height:100px;
    width:100px;
    background:blue;
}
.father::after {
    content:"";
    clear:both;/*清除浮动*/
    display:block;/*确保该元素是一个块级元素*/
}
方案2

.father{
	min-height:50px;
    width:300px;
    background:green;
	overflow:hidden; /*子元素浮动父元素无法裁剪,只能包住*/
}
.son {
    float:left;
    height:100px;
    width:100px;
    background:blue;
}

7、white-space 属性

换行符空格和制表符文字换行行尾空格
normal合并合并换行删除
nowrap合并合并不换行删除
pre保留保留不换行保留
pre-wrap保留保留换行挂起
pre-line保留合并换行删除

8、border和padding不影响宽度

box-sizing: border-box;

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值