Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

html - Position icon next to text

I have created a div which shows an icon and text but I need to get the icon next to the text instead of above it. Currently looks like this:

enter image description here

And I need the icon to be next to the text like this (the icon needs to be displayed to the left of the text without moving the text so the header still aligns with the phone number):

enter image description here

Code:

    <div fxLayout="row" fxLayoutAlign="space-around center">
            <div>
                <mat-icon svgIcon="phone-outline"> </mat-icon>
                <h3>Customer Support</h3>
                <label>123456</label>
            </div>
         <div>
            <mat-icon svgIcon="email-outline"> </mat-icon>
            <h3>Email</h3>
            <label>[email protected]</label>
        </div>
        <div>
            <mat-icon svgIcon="map-marker-outline"> </mat-icon>
            <h3>Address</h3>
            <label>address line 1</label>
        </div>
   </div>

I have three divs and using fxLayoutAlign aligns them next to each other accoss the page but if I put the icon into a seperate div the fxLayoutAlign moves it too far across the page. Is there a better way to align the divs?

Tried so far:

  1. Tried putting the icon on the same line as the text but that puts the icon too close to the text: <h3><mat-icon svgIcon="phone-outline"> </mat-icon> Customer Support</h3>

enter image description here

I need to have space between the icon and text and align it with the text.

  1. Using CSS to try align still causes the icon to be too close to the text (and not aligned correctly): .align-items { display: flex; justify-content: space-between; } .align-content { display: flex; align-items: center; }

enter image description here

Moving the icon in CSS using margins looks like its working but it has now moved the second line with the phone number:

enter image description here

The phone number should be directly under the header text (like in first screenshot above)

question from:https://stackoverflow.com/questions/65904280/position-icon-next-to-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is another approach with pure css.

css

.align-items {
  display: flex;
  justify-content: space-between;
}

.align-content {
  display: flex;
}

mat-icon {
  margin-right: 10px;
}

.align-text-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

h3 {
  margin-top: 0px;
}

html

<div class="align-items ">
    <div class="align-text-item">
        <div class="align-content">
            <mat-icon svgIcon="thumbs-up" aria-hidden="false" aria-label="Example thumbs up SVG icon"></mat-icon>
            <div>
                <h3>Customer Support</h3>
                123456
            </div>
        </div>
    </div>
    <div class="align-text-item">
        <div class="align-content">
            <mat-icon svgIcon="thumbs-up" aria-hidden="false" aria-label="Example thumbs up SVG icon"></mat-icon>
            <div>
                <h3>Email</h3>
                123456
            </div>

        </div>

    </div>
    <div class="align-text-item">
        <div class="align-content">
            <mat-icon svgIcon="thumbs-up" aria-hidden="false" aria-label="Example thumbs up SVG icon"></mat-icon>
            <div>
                <h3>Address</h3>
                123456
            </div>
        </div>
    </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...