简体   繁体   English

如何在离子项目中并排对齐离子标签和ion-img

[英]How to align ion-label and ion-img side by side in ion-item

Ionic 4: 离子4:

I have an image and a label in tag and I want both of them to appear in the center and side by side. 我在标签中有一个图像和一个标签,我希望它们都出现在中间并排。

HTML: HTML:

 <ion-item lines="none" [color]="'#F1F9FF'" class='all'>
            <ion-img [class]="'vehicle-img'"
                [src]="'./assets/icons/enem.png'">
            </ion-img>
            &nbsp;
            <ion-label text-center text-wrap class='header-text'>This is my heading
            </ion-label>   
        </ion-item>

Sass 萨斯

.vehicle-img
  height: 80px
  width: 80px

.header-text
  font-size: 18px;
  font-style: roboto medium

Present condition: Right now, only the label appears in the center, while the image appears on the extreme left. 现状:目前,只有标签出现在中间,而图像则出现在最左端。 How can I align both of them in the center and side by side? 如何将它们居中对齐并排对齐?

What I tried? 我尝试了什么? I added div tag around image and added text-center to it, but still the image does not appear in the center. 我在图像周围添加了div标签,并在其中添加了文本中心,但是图像仍未出现在中心。 Moreover, I tried align-content: center in class of image tag and still it does not work. 而且,我尝试了align-content:在图像标签类中居中,但仍然无法正常工作。

Code Pen: https://codepen.io/anon/pen/RdjQKr?editors=1111 密码笔: https //codepen.io/anon/pen/RdjQKr? editors = 1111

Any solutions? 有什么办法吗?

Marium what is happening is that your ion-label is by default a flex child with flex:1; Marium正在发生的事情是,默认情况下,您的离子标签是带有flex:1的flex子级; which makes it stretch to all the space it gets pushing the image to the side just like any block level element would. 就像所有块级元素一样,它可以延伸到将图像推向侧面的所有空间。 What you need to do is pass your image tag inside the ion-label so that it acts as a part of that block, then your text-align center can do the rest of the work. 您需要做的是将图像标签传递到离子标签内,使其充当该标签的一部分,然后您的文本对齐中心就可以完成其余的工作。 If further modification is needed you can make ion-label a flex parent and justify/align using flex properties. 如果需要进一步修改,您可以将ion-label设为flex父级,并使用flex属性进行对齐/对齐。 The issue of breaking on mobile devices can be resolved by using flex-wrap: nowrap; 可以使用flex-wrap来解决移动设备中断的问题: I hope it helps! 希望对您有所帮助!

I would try making the wrapper a flexbox wrapper: 我会尝试使包装器成为flexbox包装器:

.input-wrapper {
  display: flex;
  align-items: center;
  justify-content: center; 
}

The display: flex rule makes the wrapper into a flex container and the child elements flex items. display: flex规则使包装器进入flex容器,子元素将flex项目。 align-items affects the vertical alignment, and justify-content affects the horizontal alignment. align-items影响垂直对齐方式, justify-content影响水平对齐方式。

Then add this to the title CSS: 然后将其添加到标题CSS:

.header-text {
  flex: 0 0 auto;
}

The flex shorthand says flex-grow: 0 flex-shrink: 0 and initial width auto based on content. flex的缩写表示flex-grow: 0 flex-shrink: 0并且初始宽度根据内容auto

Great article on all things flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 关于flexbox的所有文章都很棒: https ://css-tricks.com/snippets/css/a-guide-to-flexbox/

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

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