简体   繁体   English

图标旁边的文字未正确对齐

[英]The text along side the icons are not aligning properly

My question is is flexbox necessary or is there any easy way around to get it done. 我的问题是必须使用flexbox,还是有任何简单的方法来完成它。 it been a long time since I practised , so I do not recall anything. 我练习已经很久了,所以我不记得了。

 .sadaka-contacts p { color: #115c9b; font-size: 14px; line-height: 1.42; } .sadaka-contacts li { list-style: none; width: 35px; height: 35px; background: #1f76bd; margin-bottom: 5px; display: flex; justify-content: center; align-items: center; } .sadaka-contacts li i { color: white; } 
 <div id="contact-area" class="sadaka-contacts"> <h3>SADAKA CONTACTS</h3> <p>Sadaka ipsum dolor sit amet, consectetur adipiscing elit. Ut at eros rutrum turpis viverra elementum semper quis ex. Donec lorem nulla .</p> <ul> <li> <i class="fa fa-map-marker"></i> <p>135 Hay el nahda, Rabat, Morocco</p> </li> <li> <i class="fa fa-phone"></i> <p>00 210 25 55 55 11</p> </li> <li> <i class="fa fa-envelope"></i> <p>mail@domain.com</p> </li> </ul> </div> </div> 

this is how its looking now 这就是它现在的样子

this is how I want it to look like 这就是我想要它的样子

] 2 ] 2

So I have done the below changes to your code: 所以我对你的代码进行了以下更改:

  1. Moved the background and dimensions from li element to i . 将背景和尺寸从li元素移动到i
  2. Removed justify-content: center from li 删除了justify-content: center来自li justify-content: center
  3. Center the icon in i by using a centered flexbox. 使用居中的弹性框将i的图标居中
  4. Reset the default padding of ul to zero. ul的默认padding重置为零。

See demo below - I guess you can take it forward from here: 请参阅下面的演示 - 我想你可以从这里开始:

 .sadaka-contacts p { color: #115c9b; font-size: 14px; line-height: 1.42; } .sadaka-contacts li { list-style: none; /*width: 35px;*/ /*height: 35px;*/ /*background: #1f76bd;*/ /*margin-bottom: 5px;*/ display: flex; /*justify-content: center;*/ align-items: center; } .sadaka-contacts li i { color: white; /* ADDED THE BELOW */ /* These style were applied to li before */ width: 35px; height: 35px; background: #1f76bd; margin-bottom: 5px; /* Adding a separation margin */ margin-right: 5px; /* Centering the icon */ display: flex; justify-content: center; align-items: center; } .sadaka-contacts ul { padding: 0; } 
 <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <div id="contact-area" class="sadaka-contacts"> <h3>SADAKA CONTACTS</h3> <p>Sadaka ipsum dolor sit amet, consectetur adipiscing elit. Ut at eros rutrum turpis viverra elementum semper quis ex. Donec lorem nulla .</p> <ul> <li> <i class="fa fa-map-marker"></i> <p>135 Hay el nahda, Rabat, Morocco</p> </li> <li> <i class="fa fa-phone"></i> <p>00 210 25 55 55 11</p> </li> <li> <i class="fa fa-envelope"></i> <p>mail@domain.com</p> </li> </ul> </div> </div> 

Look at this: 看这个:

Use background color for only icon container. 仅将背景颜色用于图标容器。

  .youclass { display: inline; width: 35px; background: #1f76bd; height: 35px; margin-right: 10px; } .sadaka-contacts p { color: #115c9b; font-size: 14px; line-height: 1.42; } .sadaka-contacts li { list-style: none; height: 35px; margin-bottom: 5px; display: flex; align-items: center; } .sadaka-contacts li i { color: white; } 
 <div id= "contact-area" class="sadaka-contacts"> <h3>SADAKA CONTACTS</h3> <p>Sadaka ipsum dolor sit amet, consectetur adipiscing elit. Ut at eros rutrum turpis viverra elementum semper quis ex. Donec lorem nulla .</p> <ul> <li> <div class="youclass"> <i class="fa fa-map-marker"></i> </div> <p>135 Hay el nahda, Rabat, Morocco</p> </li> </ul> </div> </div> 

Following are the steps I took to generate the UI you want it to look like: 以下是我为生成您希望它看起来像的UI所采取的步骤:

  1. Consider everything is a flex with items aligned on column. 考虑一切都是flex,列上对齐的项目。 So style the parent class 'sadaka-contacts' as, 因此,将父类'sadaka-contacts'设为样式,

    .sadaka-contacts { border: 1px solid black; color: #4040a1; display: flex; height: 240px; flex-direction: column; justify-content: space-between; }

  2. Add bottom border for the title: 添加标题的下边框:

    .sadaka-contacts h3 { border-bottom: 1px solid blue; }

  3. Since a web browser automatically adds a margin of 16px for p elements, remove it and add remaining styles for body as: 由于Web浏览器会自动为p元素添加16px的边距,因此将其删除并为body添加剩余样式:

    .sadaka-contacts p { color: #115c9b; font-size: 14px; line-height: 1.42; margin: 0; }

  4. Browser also adds padding and margin for ul elements. 浏览器还为ul元素添加填充和边距。 Remove it and add remaining styles as: 删除它并添加剩余样式:

    .sadaka-contacts ul { margin: 0; list-style: none; padding: 0; }

  5. Now we come to the part where you're seeing the overlap of icons and text. 现在我们来到你看到图标和文字重叠的部分。 For this, we will make the li element as a flex in itself with row as flex direction (as against column direction of parent). 为此,我们将li元素作为flex本身,将row作为flex方向(与parent的列方向相对)。 Note that, I'm using alphabets in place of actual images. 请注意,我使用字母代替实际图像。 You can replace that as you like. 您可以根据需要替换它。 The styles for list items goes like this: 列表项的样式如下:

    .sadaka-contacts ul > li { display: flex; flex-direction: row; }

  6. As mentioned earlier, I'm using boxed alphabets which are styled as: 如前所述,我使用的是盒装字母,其格式如下:

    .sadaka-contacts ul > li > i { border: 1px solid #115c9b; border-radius: 12px; color: white; background: #115c9b; margin: 4px; min-width: 24px; padding: 4px; }

  7. Finally we horizontally align the list items text exactly at center using 'align-self' property set to 'center'. 最后,我们使用'align-self'属性设置为'center',将列表项文本正确地水平对齐。 The style for the text is as: 文本的样式如下:

    .sadaka-contacts ul > li > p { align-self: center; }

Hope this helps. 希望这可以帮助。

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

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