简体   繁体   English

如何使用flexbox将文本与图像水平对齐?

[英]How to horizontally align text with images using flexbox?

I am trying to horizontally align text with three vertically aligned images on each side of the text. 我试图在文本的每一侧水平对齐文本与三个垂直对齐的图像。 However, when I apply "display:flex;" 但是,当我申请“display:flex;”时 to the parent element of the three, it changes the alignment of the three divs from vertical to horizontal, changes the size of the images and causes white space inbetween the first div and the text, rather than pushing all the divs to the left of the screen. 到三者的父元素,它改变三个div从垂直到水平的对齐方式,改变图像的大小,并在第一个div和文本之间产生空白,而不是将所有div推到左边的屏幕。

 .text { font-family: 'font', cursive; font-size: 18px; text-align: left; width: 35em; } .image > img { width: 20% } .image { display: flex; flex-direction: column; } .flex { display: flex; } 
 <div class="flex"> <div class='image'> <img src='image.jpg'> <img src='image.jpg'> <img src='image.jpg'> </div> <div class='text'> <p>text <br><br>text <br><br>text <br><br>text</p> </div> <div class='image'> <img src='image.jpg'> <img src='image.jpg'> <img src='image.jpg'> </div> </div> 

 .text { font-family: 'font', cursive; font-size: 18px; text-align: left; flex: 1; display: flex; flex-flow: row nowrap; justify-content: space-evenly; align-items: center; } .image > img { width: 20%; display: block; margin: 0 auto; } .image { display: flex; flex-flow: column nowrap; } .flex { display: flex; flex-flow: row nowrap; justify-content: space-evenly; align-items: center; } 
 <div class="flex"> <div class='image'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> </div> <div class='text'> <p>text <br><br>text <br><br>text <br><br>text</p> </div> <div class='image'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> </div> </div> 

Is that good? 这样好吗?

set your flex to your divs and remove the display and flex-direction from images 将您的flex设置为div并从图像中移除显示和弯曲方向

 .text { font-family: 'font', cursive; font-size: 18px; text-align: left; flex:0 0 10%; } img { width: 100%; } .image { flex: 0 0 10%; } .flex { display: flex; } 
 <div class="flex"> <div class='image'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> </div> <div class='text'> <p>text <br><br>text <br><br>text <br><br>text</p> </div> <div class='image'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> <img src='https://images.pexels.com/photos/33109/fall-autumn-red-season.jpg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940'> </div> </div> 

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

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