当子文本换行到2或更多行时,保持flexbox容器居中 [英] Keeping flexbox container centered when child text wraps to 2 or more lines

查看:110
本文介绍了当子文本换行到2或更多行时,保持flexbox容器居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用flexbox( display:flex )连续排列两个 div 元素。 。



第一个 div 元素在左边只有一个图片。



第二个 div 元素在右侧具有未指定长度的左对齐内联文本。

当文本行足够短以适合一行时,两个div都按照我的预期对齐并对齐中心。



当行的文本足够长,可以包装成两行,第二个 div 元素不会像我期望的那样包裹内容。相反,它会在 div 的右侧留下一个大的空白空间。



我在这里嘲笑了一个例子:



如何设置第二个 div 元素使其自身缩小以适应文本,这样 div 元素才会出现

HTML:

 < div class =flexbox -container> 
< div class =div1>
< img src =http://dreamatico.com/data_images/kitten/kitten-2.jpgwidth =150px>
< / div>
< div class =div2>
这是一行文本的示例。
< / div>
< / div>
< br>
< div class =flexbox-container>
< div class =div1>
< img src =http://dreamatico.com/data_images/kitten/kitten-2.jpgwidth =150px>
< / div>
< div class =div2>
这是一个很多loooooooooooooooonger文本行的例子。
< / div>
< / div>

CSS:

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

.div1 {
margin-right:30px;
}

.div2 {
font-size:48px;
line-height:48px;
text-align:left;
}

下面是一个Photoshop模型,展示了我正在尝试做的事情:



解决方案

要实现您的目标(如您的第二张图片中所述),我们需要对您的HTML和CSS进行一些调整。一切都可以通过flexbox完成。

HTML

 < div id =flex-container-main> 

< div class =flex-container-child>
< figure>
< img src =http://dreamatico.com/data_images/kitten/kitten-2.jpgwidth =150px>
< / figure>
< p>这是一行文字的示例。< / p>
< / div>

< div class =flex-container-child>
< figure>
< img src =http://dreamatico.com/data_images/kitten/kitten-2.jpgwidth =150px>
< / figure>
< p>这是一个很多loooooooooooooooonger文本行的例子。< / p>
< / div>

< / div><! - end#flex-container-main - >

CSS

 #flex-container-main {
display:flex;
flex-direction:column;
align-items:center;
}

.flex-container-child {
display:flex;
align-items:center;
min-height:127px;
宽度:75%;
margin:10px;
}

图{
margin:0 20px 0 0;
}

img {
width:150px;
height:127px;
vertical-align:bottom;
}

p {
font-size:48px;
保证金:0;
}



在图片1中,所有弹性项目实际上都居中。 Chrome Dev Tools的蓝色突出部分强调了这一点。每个项目都完美集中在屏幕上。



是的,当您重新缩小屏幕尺寸时,它确实有点笨重。主要是因为字体很大 - ndash;但是flex项仍然居中。

在图2中,flex项目不是均匀居中。您在模型中创建的内容更像是包含两个柔性盒的列,而则居中。但单独只有第一行在屏幕上居中。



关于您的代码的几个注释:


  1. 在flex容器上声明 justify-content 时,您正在将flex项目居中。由于两个flexbox都是< body> 和<$ c $的直接子元素,






所以为了达到你想要的效果,我们可以将所有现有的标记放在一个新的flex容器中(#flex-container-main )。这将原始的柔性容器转换为柔性项目,然后可以均匀为一个组。 .flex-container-child )会根据图像的高度给出一个宽度以创建空间和最小高度。每个Flex项目也被声明为flex父项( display:flex ),这允许我们在子元素上使用flex属性。特别是,这对垂直居中文本非常有用(如图中所示)。



(请注意,我使用HTML语义元素对代码不是必需的如果你更喜欢原来的 div 标记,只需将它们交换回来即可,重要的调整是新的父容器。)



<最后(这可能对您的布局并不重要,但以防万一),浏览器通常会在图片底部边框下留下一小部分空白。这是用来容纳下降器。用 vertical-align:bottom ,这个空间被删除。 (有关更多详细信息,请参阅关于后代的答案。)


I am trying to center two div elements side by side in a row with flexbox (display:flex).

The first div element, on the left, just has an image.

The second div element, on the right, has left-aligned inline text of unspecified length.

When the line of text is short enough to fit on one line, both divs are aligned and justified to the center as I expect.

When the line of text is long enough to wrap onto two lines, the second div element does not wrap itself around the content as I expect. Instead, it leaves a large white space on the right side of the div.

I mocked up an example here: http://codepen.io/anon/pen/NGqYQX?editors=110. Vary your browser window's width to see what I mean.

How can I set the second div element to shrink itself to fit the text, so that both div elements appear centered?

HTML:

<div class="flexbox-container">   
    <div class="div1">
        <img src="http://dreamatico.com/data_images/kitten/kitten-2.jpg" width="150px">
    </div>
    <div class="div2">
        This is an example of a line of text.
    </div> 
</div>
<br>
<div class="flexbox-container">
    <div class="div1">
        <img src="http://dreamatico.com/data_images/kitten/kitten-2.jpg" width="150px">
    </div>
    <div class="div2">
        This is an example of a much loooooooooooooonger line of text.
    </div>
</div>

CSS:

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

.div1 {
  margin-right: 30px;
}

.div2 {
  font-size: 48px;
  line-height: 48px;
  text-align: left;
}

Here is a Photoshop mockup showing what I am trying to do:

解决方案

In order to achieve your goal (as specified in your second image) we need to make a few adjustments to your HTML and CSS. Everything can be done with flexbox.

HTML

<div id="flex-container-main">

    <div class="flex-container-child">
        <figure>
            <img src="http://dreamatico.com/data_images/kitten/kitten-2.jpg" width="150px">
        </figure>
        <p>This is an example of a line of text.</p> 
    </div>

    <div class="flex-container-child">
        <figure>
            <img src="http://dreamatico.com/data_images/kitten/kitten-2.jpg" width="150px">
        </figure>
        <p>This is an example of a much loooooooooooooonger line of text.</p>
    </div>

</div><!-- end #flex-container-main -->

CSS

#flex-container-main {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.flex-container-child {
    display: flex;
    align-items: center;
    min-height: 127px;
    width: 75%;
    margin: 10px;
}

figure {
    margin: 0 20px 0 0;
    }

img {
    width: 150px;
    height: 127px;
    vertical-align: bottom;
} 

p { 
    font-size: 48px;
    margin: 0;
}

Revised Codepen Demo


Here's what's happening...

Your question asks:

Keeping flexbox centered when text wraps to 2 or more lines

I am trying to center two div elements side by side in a row with flexbox (display:flex).

Let's quickly go over your two images.

Image 1

Image 2

In image 1 all flex items are actually centered. The blue highlight from Chrome Dev Tools emphasizes this point. Each item is perfectly centered on the screen.

Yes, it does get a bit clunky as you re-size the screen smaller – mostly because of the large font size – but the flex items remain centered nonetheless.

In image 2, the flex items are not evenly centered. What you've created in your mock-up is more like a column containing both flexboxes, and the column is centered. But individually only the first row is centered on the screen.

A couple of notes about your code:

  1. With justify-content declared on the flex containers, you are centering the flex items. The flex container itself is not centered.
  2. Since both flexboxes are direct children of the <body>, and <body> has no defined width, the flexboxes align themselves in relation to the viewport.


So to achieve the effect you want we can wrap all your existing mark-up in a new flex container (#flex-container-main). This converts the original flex containers into flex items, which can then be evenly centered as a group.

The new flex items (now classed as .flex-container-child) are given a width to create space and a minimum height based on the height of the image. Each flex item is also declared a flex parent (display: flex) which allows us to use flex properties on child elements. In particular, this is useful for vertically centering the text (as shown in your images).

(Note that my use of HTML semantic elements is not necessary for the code to work. If you prefer the original div tags just swap them back. The important adjustment is the new parent container.)

Lastly (and this may not be important to your layout but just in case), browsers normally give images a small gap of whitespace under their bottom border. This is used to accommodate descenders. With vertical-align: bottom, this space is removed. (For more details see my answer about descenders.)

这篇关于当子文本换行到2或更多行时,保持flexbox容器居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆