简体   繁体   English

CSS-使内容的div宽度(inline-flex div)

[英]CSS - Make div width of content (inline-flex divs)

I have the following code: 我有以下代码:

HTML: HTML:

<div class="log">

  <div class="entry">
    <img class="icon" src="https://apixel.me/static/apixel.png" />
    <p class="text">
      filler text
      <span class="bullet">•</span>
      filler text
      <span class="bullet">•</span>
      filler text
      <span class="bullet">•</span>
      filler text
    </p>
  </div>

</div>

CSS: CSS:

.log {
    display: inline-block;
    margin-left: 50%;
    transform: translate(-50%, 0);
    border: thin red solid;
}

.entry {
    display: inline-flex;
    height: 25px;
    color: #2e2e2e;
    font-family: "Roboto";
    font-size: 20px;
    margin-top: 10px;
    border: thin orange solid;
}

.icon {
    display: inline-block;
    width: 25px;
    height: auto;
}

.text {
    display: inline-block;
    color: #2e2e2e;
    font-family: "Roboto";
    font-size: 15px;
    white-space: nowrap;
    margin: auto;
    margin-left: 15px;
}

.bullet {
    color: #a1a1a1;
    margin-left: 10px;
    margin-right: 10px;
}

JSFiddle demo JSFiddle演示

In the JSFiddle demo, if you resize the result to be wide enough, the div with the red border is no longer the size of its content. 在JSFiddle演示中,如果将结果调整为足够宽的大小,则带有红色边框的div不再是其内容的大小。 What's causing this strange behavior and how can I fix it? 是什么导致这种奇怪的行为,我该如何解决?

Try changing the .entry {display: inline-flex; 尝试更改.entry {display: inline-flex; to .entry {display: flex; .entry {display: flex;

You can fix this by adding this CSS width: min-content; 您可以通过添加以下CSS width: min-content;来解决此问题width: min-content; in log class. log类中。 JS Fiddle Link JS小提琴链接

 .log { display: inline-block; margin-left: 50%; transform: translate(-50%, 0); border: thin red solid; width: min-content;/*Add this line*/ } .entry { display: inline-flex; height: 25px; color: #2e2e2e; font-family: "Roboto"; font-size: 20px; margin-top: 10px; border: thin orange solid; } .icon { display: inline-block; width: 25px; height: auto; } .text { display: inline-block; color: #2e2e2e; font-family: "Roboto"; font-size: 15px; white-space: nowrap; margin: auto; margin-left: 15px; } .bullet { color: #a1a1a1; margin-left: 10px; margin-right: 10px; } 
 <div class="log"> <div class="entry"> <img class="icon" src="https://apixel.me/static/apixel.png" /> <p class="text"> filler text <span class="bullet">•</span> filler text <span class="bullet">•</span> filler text <span class="bullet">•</span> filler text </p> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM