css面板宽度匹配最大文字宽度

时间:2017-02-06 11:22:36

标签: html css panel

如何使整个浮动面板宽度与其中的最大文本宽度相匹配? 它应该是这样的:

Sample

pannel html是:



.pannel{    
    width: auto;
    float: left;
    margin: 10px 10px 10px 0px;
    border: #88F 4px solid;
    padding: 10px;
    background-color: rgb(240,240,255);
    border-radius: 8px;
    font-size: 150%;
  }
.pannel>a>div
  {  
    width: auto;
    text-align: center;
    border: #AAF 1px solid;
    margin: 0 auto 0;
    overflow: hidden;
    border-radius: 9px;
    margin: 3px;
  }

<div class="pannel">
  <a href="someref1">
    <div>
     <img src="http://www.w3schools.com/css/img_fjords.jpg">
     <div>Some text</div>
    </div>
  </a>
  <a href="someref2">
    <div>
     <img src="https://camo.mybb.com/e01de90be6012adc1b1701dba899491a9348ae79/687474703a2f2f7777772e6a71756572797363726970742e6e65742f696d616765732f53696d706c6573742d526573706f6e736976652d6a51756572792d496d6167652d4c69676874626f782d506c7567696e2d73696d706c652d6c69676874626f782e6a7067">
     <div>Some text2</div>
    </div>
  </a>
  <a href="someref3">
    <div>
     <img src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg">
     <div>Text with a maximum width</div>
    </div>
  </a>
</div>
&#13;
&#13;
&#13;

如您所见,面板尺寸与最大图像尺寸相匹配,但我希望图像适合元素的宽度。

2 个答案:

答案 0 :(得分:1)

我不确定您是否要将图片作为整个内容放置,如果您想使用此代码

<style>
.pannel {
    width: auto;
    float: left;
    margin: 10px 10px 10px 0px;
    border: #88F 4px solid;
    padding: 10px;
    background-color: rgb(240, 240, 255);
    border-radius: 8px;
    font-size: 150%;
}

.pannel > a > div {
    width: auto;
    text-align: center;
    border: #AAF 1px solid;
    margin: 0 auto 0;
    overflow: hidden;
    border-radius: 9px;
    margin: 3px;
}

.pannel > a > div img {
    width: 100%;

}
</style>

和html代码

<div class="pannel">
<a href="someref1">
    <div>
        <img src="http://www.w3schools.com/css/img_fjords.jpg">

        <div>Some text</div>
    </div>
</a>
<a href="someref2">
    <div>
        <img
            src="https://camo.mybb.com/e01de90be6012adc1b1701dba899491a9348ae79/687474703a2f2f7777772e6a71756572797363726970742e6e65742f696d616765732f53696d706c6573742d526573706f6e736976652d6a51756572792d496d6167652d4c69676874626f782d506c7567696e2d73696d706c652d6c69676874626f782e6a7067">

        <div>Some text2</div>
    </div>
</a>
<a href="someref3">
    <div>
        <img
            src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg">

        <div>Text with a maximum width</div>
    </div>
</a>
</div>

答案 1 :(得分:1)

使用纯css执行此操作的唯一方法是让所有图像具有相同的宽高比 - 如果是这种情况,那么您可以使用具有绝对定位和填充顶部的技巧。

在下面的例子中,我使用的比例为w:600px x h:400px(66.6666%)

&#13;
&#13;
.pannel {
  display: inline-block;
}

.pannel > a {
  display: block;
  width: 100%;
  text-align: center;
  margin-bottom: 10px;
}

.image {
  display: block;
  position:relative;
  width:100%;
  padding-top:66.6666%; /* padding = height / width of image - 400px / 600px */
}

.image > img {
  position:absolute;
  left:0; 
  right:0;
  top:0; 
  max-width:100%;
  
}
&#13;
<div class="pannel">
  <a href="someref1">
    <div class="inner">
      <div class="image">
        <img src="http://www.w3schools.com/css/img_fjords.jpg">
      </div>
      <div>Some text</div>
    </div>
  </a>
  <a href="someref2">
    <div class="inner">
      <div class="image">
        <img src="http://lorempixel.com/600/400/">
      </div>
      <div>Some text2</div>
    </div>
  </a>
  <a href="someref3">
    <div class="inner">
      <div class="image">
        <img src="http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg"> </div>
      <div>Text with a maximum width</div>
    </div>
  </a>
</div>
&#13;
&#13;
&#13;

如果您无法让所有图像具有相同的宽高比,那么使用纯css是不可能的,并且您需要一些小的j来调整图像大小 - 除非您使用背景图像路径。

这与上面几乎相同,但不是绝对定位图像,而是使用背景图像和封面(这意味着您必须选择宽高比,所有法师的大小都相同 - 可能看起来这样更好)

&#13;
&#13;
.pannel {
  display: inline-block;
}

.pannel > a {
  display: block;
  width: 100%;
  text-align: center;
  margin-bottom: 10px;
}

.image {
  display: block;
  position:relative;
  width:100%;
  background-repeat:no-repeat;
  background-position:center center;
  background-size: cover;

  padding-top:66.6666%; /* for simplicity I have chose the same aspect ratio as above */
}

.image > img {
  position:absolute;
  left:0; 
  right:0;
  top:0; 
  max-width:100%;
  
}
&#13;
<div class="pannel">
  <a href="someref1">
    <div class="inner">
      <div class="image"style="background-image:url(http://www.w3schools.com/css/img_fjords.jpg);"></div>
      <div>Some text</div>
    </div>
  </a>
  <a href="someref2">
    <div class="inner">
      <div class="image"style="background-image:url(https://camo.mybb.com/e01de90be6012adc1b1701dba899491a9348ae79/687474703a2f2f7777772e6a71756572797363726970742e6e65742f696d616765732f53696d706c6573742d526573706f6e736976652d6a51756572792d496d6167652d4c69676874626f782d506c7567696e2d73696d706c652d6c69676874626f782e6a7067);"></div>
      <div>Some text2</div>
    </div>
  </a>
  <a href="someref3">
    <div class="inner">
      <div class="image"style="background-image:url(http://keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg);"></div>
      <div>Text with a maximum width</div>
    </div>
  </a>
</div>
&#13;
&#13;
&#13;

您会注意到,在上面的代码段中,第二张图片看起来与另外两张图片相似,即使它与它们的宽高比不同