我希望一个整个区块在其父容器中居中,但该区块的内容应左对齐。
最好提供一些示例:
在这个页面上:
ASCII 艺术应该居中(如所示),但它应该对齐并看起来像 "YAML"。
或者这个:
错误消息应该与控制台中的格式保持一致。
我希望一个整个区块在其父容器中居中,但该区块的内容应左对齐。
最好提供一些示例:
在这个页面上:
ASCII 艺术应该居中(如所示),但它应该对齐并看起来像 "YAML"。
或者这个:
错误消息应该与控制台中的格式保持一致。
首先,创建一个父级 div
,并使用 text-align:center
居中其子内容。接下来,创建一个子级 div
,使用 display:inline-block
以适应其子元素的宽度,并使用 text-align:left
使其持有的内容按所需左对齐。
<div style="text-align: center;">
<div style="display: inline-block; text-align: left;">
Centered<br />
Content<br />
That<br />
Is<br />
Left<br />
Aligned
</div>
</div>
如果您希望确保长行不会使一切都变得太宽,您还可以将max-width
属性(具有您选择的值)应用于内部标签:
max-width: 250px;
重新发布另一个问题中的有效答案:如何水平居中具有可变宽度的浮动元素?
假设需要被浮动和居中的元素是具有id="content"的div...
<body>
<div id="wrap">
<div id="content">
This will be centered
</div>
</div>
</body>
然后应用以下 CSS
#wrap {
float: left;
position: relative;
left: 50%;
}
#content {
float: left;
position: relative;
left: -50%;
}
这里有一个很好的参考资料,关于如何居中浮动元素。http://dev.opera.com/articles/view/35-floats-and-clearing/#centeringfloats
This will be centered
一旦不能在一行上容纳,就会换行,导致块的宽度为全宽,但文本不是全宽,因此即使块居中,也无关紧要,因为文本不是包含块的全宽。有关示例,请参见https://dev59.com/7moy5IYBdhLWcg3wN7UX的图2a。 - user3338098<div>
<p>Some interesting text.</p>
</div>
P {
width: 50%; //or whatever looks best
margin: auto; //top and bottom margin can be added for aesthetic effect
}
margin-left: auto;
margin-right: auto;
左对齐其内容:
text-align: left;
<div>
<div style="text-align: left; width: 400px; border: 1px solid black; margin: 0 auto;">
<pre>
Hello
Testing
Beep
</pre>
</div>
</div>
对于仍在使用旧版浏览器的人,这里提供了一些扩展的向后兼容性:
<div style="text-align: center;">
<div style="display:-moz-inline-stack; display:inline-block; zoom:1; *display:inline; text-align: left;">
Line 1: Testing<br>
Line 2: More testing<br>
Line 3: Even more testing<br>
</div>
</div>
部分灵感来自于这篇帖子:https://dev59.com/RWjWa4cB1Zd3GeqPuM16#12567422。
.container{
display: flex;
flex-flow: row wrap;
justify-content: center;
align-content: center;
align-items: center;
}
.inside{
height:100px;
width:100px;
background:gray;
border:1px solid;
}
<section class="container">
<section class="inside">
A
</section>
<section class="inside">
B
</section>
<section class="inside">
C
</section>
</section>
.format-pre pre {
font-size: 18px;
text-align: left;
display: inline-block;
}
在 ts 文件中
showPasswordHints(){
var message = 'Your password mist contain:<br>'+
'1. At least 8 characters in length<br>'+
'2. At least 3 Lowercase letters<br>'+
'3. At least 1 Uppercase letter<br>'+
'4. At least 1 Numbers<br>'+
'5. At least 1 Special characters<br>'+
'5. Maximum 16 characters in length';
Swal.fire({
html:'<pre>' + message + '</pre>',
customClass: {
popup: 'format-pre'
}
,
showClass: {
popup: 'animate__animated animate__fadeInDown'
},
hideClass: {
popup: 'animate__animated animate__fadeOutUp'
},
icon: 'info',
confirmButtonText: 'Got it',
confirmButtonColor: '#3f51b5',
});
}
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
justify-items: center;
align-items: center;
}
.width {
width: 12ch;
}
.title {
text-align: center;
}
<span class="grid">
<span class="width title">Title 1</span>
<span class="width title">Title 2</span>
<span class="width title">Title 3</span>
</span>
<span class="grid">
<span class="width">13 characters</span>
<span class="width">18 characters text</span>
<span class="width">5 cha</span>
</span>
<span class="grid">
<span class="width">3 c</span>
<span class="width">9 charact</span>
<span class="width">35 characters text and so on goes a</span>
</span>
<span class="grid">
<span class="width">6 char</span>
<span class="width">12 character</span>
<span class="width">13 characters</span>
</span>
<span class="grid">
<span class="width">7 chara</span>
<span class="width">22 characters text and</span>
<span class="width">15 characters g</span>
</span>
我使用每行的grid
和每列的width
,接近了上述可接受的结果。
这是原始文本:
span {
border: 1px solid;
}
<span class="grid">
<span class="width title">Title 1</span>
<span class="width title">Title 2</span>
<span class="width title">Title 3</span>
</span>
<span class="grid">
<span class="width">13 characters</span>
<span class="width">18 characters text</span>
<span class="width">5 cha</span>
</span>
<span class="grid">
<span class="width">3 c</span>
<span class="width">9 charact</span>
<span class="width">35 characters text and so on goes a</span>
</span>
<span class="grid">
<span class="width">6 char</span>
<span class="width">12 character</span>
<span class="width">13 characters</span>
</span>
<span class="grid">
<span class="width">7 chara</span>
<span class="width">22 characters text and</span>
<span class="width">15 characters g</span>
</span>
我将它转换为一个完全居中的具有3列的网格
:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
justify-items: center;
align-items: center;
}
span {
border: 1px solid;
}
<span class="grid">
<span class="width title">Title 1</span>
<span class="width title">Title 2</span>
<span class="width title">Title 3</span>
</span>
<span class="grid">
<span class="width">13 characters</span>
<span class="width">18 characters text</span>
<span class="width">5 cha</span>
</span>
<span class="grid">
<span class="width">3 c</span>
<span class="width">9 charact</span>
<span class="width">35 characters text and so on goes a</span>
</span>
<span class="grid">
<span class="width">6 char</span>
<span class="width">12 character</span>
<span class="width">13 characters</span>
</span>
<span class="grid">
<span class="width">7 chara</span>
<span class="width">22 characters text and</span>
<span class="width">15 characters g</span>
</span>
宽度
。这导致了一个居中的块,其中文本左对齐,除了标题之外:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
justify-items: center;
align-items: center;
}
.width {
width: 12ch;
}
.title {
text-align: center;
}
span {
border: 1px solid;
}
<span class="grid">
<span class="width title">Title 1</span>
<span class="width title">Title 2</span>
<span class="width title">Title 3</span>
</span>
<span class="grid">
<span class="width">13 characters</span>
<span class="width">18 characters text</span>
<span class="width">5 cha</span>
</span>
<span class="grid">
<span class="width">3 c</span>
<span class="width">9 charact</span>
<span class="width">35 characters text and so on goes a</span>
</span>
<span class="grid">
<span class="width">6 char</span>
<span class="width">12 character</span>
<span class="width">13 characters</span>
</span>
<span class="grid">
<span class="width">7 chara</span>
<span class="width">22 characters text and</span>
<span class="width">15 characters g</span>
</span>
针对每列的内容计数器,变化每列的width
,可以更好地调整边缘情况。