脱离文档流,内联元素的宽度问题

1,018 阅读1分钟

本文只测试常见的几种内联元素,不具有普适应,参考请慎重

正常情况下内联元素的宽高

内联元素一般情况下无法设置宽高例如:

 a {
     width: 100px;
 }

这样的代码不会产生任何效果。原则上内联元素没有宽高属性。

想要设置内联元素的宽高需要将内联元素更改为块状元素,例如:

a {
    display: inline-block;
    width: 100px;
}
a {
    display: block;
    width: 100px;
}

脱离文档流之后的内联元素

脱离文档流之后,例如:absolute、float、fixed等情况时。 内联元素可以设置宽高,但是不会占用正常文档流的空间。

 <div class="inline">
    <label>宽度</label>
    <a>absolute</a>
 </div>
  <p>Lorem ipsum dolor sit amet, per in nusquam nominavi periculis, sit elit oportere ea.Lorem ipsum dolor sit am</p>
 
 .label {
    position: relative;
  }
  
  label {
    width: 200px;
    position: absolute;
    background-color: #999;
  }
  
  a {
    width: 240px;
    position: absolute;
    top: 20px;
    background-color: #888;

效果
}