简体   繁体   English

文本位于图像旁边,如果没有空格,则与顶部对齐

[英]Text centered next to image, and if no space, aligned to top

Let's check this fiddle : 让我们看看这个小提琴

img {
    float: left;
}

#inner {
    height: 128px; 
    background-color: yellowgreen; 
    display: table-cell;
    vertical-align: middle;
}

#content {
    background-color: red;
}

<img src="http://cdn.memegenerator.net/instances/250x250/37934290.jpg" width="128" height="128" />
<div id="inner">
    <div id="content">text text tertkl elknr tlken lsl kdmfsldkfmsldkfmslkd mfkndfln dflkfndg lkn</div>
</div>

this works so far as I expect - text is centered, and as you shrink the width, text goes underline: but then its "too far" from the image. 这个工作到目前为止我认为 - 文本居中,当你缩小宽度时,文字会下划线:但是它与图像“相距太远”。 The best would be if the vertical-align: middle; 最好的是如果vertical-align: middle; became vertical-align: top; 变得vertical-align: top; when it needs to jump. 什么时候需要跳。 How to do it without possibly jQuery? 如何在没有jQuery的情况下做到这一点?

A simple way to achieve this is to use a CSS Media Query . 实现此目的的一种简单方法是使用CSS Media Query

Your markup would stay the same and your CSS would only need to have the following added: 您的标记将保持不变,您的CSS只需要添加以下内容:

@media screen and (max-width: 290px) {
    #inner {
        vertical-align: top;   
    }
}

in action: http://jsfiddle.net/uWMkH/1/ 在行动: http//jsfiddle.net/uWMkH/1/

What that says is, "When the viewport's width is no more than 290px, do this stuff to #inner . 这就是说,“当视口的宽度不超过290px时,请将此内容写入#inner

Take a look at these links for more information: 请查看这些链接以获取更多信息:

The caveat with using media queries to do this is that they aren't supported in IE8 and below. 使用媒体查询执行此操作的警告是,IE8及更低版本不支持它们。 I hope you don't have to deal with those headaches! 我希望你不必处理那些令人头疼的问题!

Look here for a complete list of browsers with support: 在此处查看支持的浏览器的完整列表:

You can do this without media queries, but it requires a browser that supports the entire CSS Flexible box module (most browsers are missing support for wrapping). 可以在没有媒体查询的情况下执行此操作,但它需要一个支持整个 CSS Flexible框模块的浏览器(大多数浏览器都缺少对包装的支持)。 At this point in time, support is limited to Opera, Chrome, and IE10. 目前,支持仅限于Opera,Chrome和IE10。

http://codepen.io/cimmanon/pen/rFdkt http://codepen.io/cimmanon/pen/rFdkt

figure {
  display: -ms-flexbox;
  display: -webkit-flex;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
  background-color: yellowgreen;
}
@supports (flex-wrap: wrap) {
  figure {
    display: flex;
  }
}

figcaption {
  -webkit-flex: 1 15em;
  -ms-flex: 1 15em;
  flex: 1 15em;
  background-color: red;
}

What Flexbox offers over media queries is the ability to reflow the content based on the available space, not just the browser width. Flexbox通过媒体查询提供的功能是能够根据可用空间重排内容,而不仅仅是浏览器宽度。

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

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