简体   繁体   English

在Flex布局中将文本对齐为中心

[英]Align the text as center in flex layout

If we want to align the text as center or end of the div, we need to show the ellipsis, If text length was overflowed the div width. 如果要将文本对齐为div的中心或末端,则需要显示省略号。如果文本长度超出div宽度,则需要显示省略号。

Here below sample was work fine. 下面的示例工作正常。

My expectation is: If we add the item class to the parent div it will be worked as the same like which was applied in the span. 我的期望是:如果将项目类添加到父div,它将像在span中应用的一样工作。

Working Example: https://codepen.io/Muthupandi07/pen/oRVwqV 工作示例: https : //codepen.io/Muthupandi07/pen/oRVwqV

Thanks in advance. 提前致谢。

HTML: HTML:

<div class="controls">
<select id="justifyMe">
  <option value="flex-start">flex-start</option>
  <option value="flex-end">flex-end</option>
  <option value="center">center</option>
</select>    
</div>

<div class="container" id="container">
  <span class="item">How to center an item using flexbox</span>      
</div>

CSS: CSS:

body {
  padding: 20px;
  font: 1em Helvetica Neue, Helvetica, Arial, sans-serif;
}

* {box-sizing: border-box;}

p {
  margin: 0 0 1em 0;
}

.container {  
  border: 5px solid rgb(111,41,97);
  border-radius: .5em;
  padding: 10px;
  display: flex;
  width: 200px;
}

.item {
  text-overflow: ellipsis;
  white-space: pre;
  overflow: hidden;
}

.controls {
  background-color: rgba(0,0,0,.1);
  padding: 10px;
  border-radius: .5em;
  border: 1px solid rgba(0,0,0,.2);
  margin: 0 0 2em 0
}

.controls select {
  font-size: .9em;
}

JS: JS:

var justify = document.getElementById("justifyMe");
justifyMe.addEventListener("change", function (evt) {
  document.getElementById("container").style.justifyContent = evt.target.value;
});
  <div class="parent"> <span class="child"> center me </span> <div> .parent { display: flex; } .child { margin: auto; } 

Here you can apply max-width:90%; 在这里您可以应用max-width:90%; to span.item for viewing center and flex-end span.item用于查看中心和伸缩端

 var justify = document.getElementById("justifyMe"); justifyMe.addEventListener("change", function (evt) { document.getElementById("container").style.justifyContent = evt.target.value; }); 
 body { padding: 20px; font: 1em Helvetica Neue, Helvetica, Arial, sans-serif; } * {box-sizing: border-box;} p { margin: 0 0 1em 0; } .container { border: 5px solid rgb(111,41,97); border-radius: .5em; padding: 10px; display: flex; width: 200px; } .item { text-overflow: ellipsis; white-space: pre; overflow: hidden; max-width:90%; } .controls { background-color: rgba(0,0,0,.1); padding: 10px; border-radius: .5em; border: 1px solid rgba(0,0,0,.2); margin: 0 0 2em 0 } .controls select { font-size: .9em; } 
 <div class="controls"> <select id="justifyMe"> <option value="flex-start">flex-start</option> <option value="flex-end">flex-end</option> <option value="center">center</option> </select> </div> <div class="container" id="container"> <span class="item">How to center an item using flexbox</span> </div> 

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

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