- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
ul {
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
li {
display: flex;
margin: 1px;
padding: 5px;
background: #aaa;
}
li:last-child {
background: #ddd;
/* magic to throw to the right*/
}
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
最佳答案
以下是实现此布局的五个选项:
CSS定位
带有不可见DOM元素的Flexbox
带有不可见伪元素的Flexbox
带flex: 1
的Flexbox
CSS网格布局
方法1:CSS定位属性
将position: relative
应用于flex容器。
将position: absolute
应用于项目D。
现在,此项目已完全放置在flex容器中。
更具体地说,项目D从文档流中删除,但停留在nearest positioned ancestor的范围内。
使用CSS偏移属性top
和right
将此元素移到位置。
li:last-child {
position: absolute;
top: 0;
right: 0;
background: #ddd;
}
ul {
position: relative;
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
li {
display: flex;
margin: 1px;
padding: 5px;
background: #aaa;
}
p {
text-align: center;
margin-top: 0;
}
span {
background-color: aqua;
}
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<p><span>true center</span></p>
auto
margins和新的,不可见的flex项目,可以实现布局。
visibility: hidden
从视图中删除了新项目。
D
元素的副本。
auto
边距使
A
,
B
和
C
居中,两个
D
元素从两端创建相等的平衡。
visibility: hidden
应用于重复的
D
li:first-child {
margin-right: auto;
visibility: hidden;
}
li:last-child {
margin-left: auto;
background: #ddd;
}
ul {
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
li {
display: flex;
margin: 1px;
padding: 5px;
background: #aaa;
}
p { text-align: center; margin-top: 0; }
span { background-color: aqua; }
<ul>
<li>D</li><!-- new; invisible spacer item -->
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<p><span>true center</span></p>
D
的宽度。
D
相同的伪元素。
::before
将其放在容器的开头。
auto
边距可保持
A
,
B
和
C
完美居中,伪元素和
D
元素可在两端产生相等的平衡。
ul::before {
content:"D";
margin: 1px auto 1px 1px;
visibility: hidden;
padding: 5px;
background: #ddd;
}
li:last-child {
margin-left: auto;
background: #ddd;
}
ul {
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
li {
display: flex;
margin: 1px;
padding: 5px;
background: #aaa;
}
p { text-align: center; margin-top: 0; }
span { background-color: aqua; }
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<p><span>true center</span></p>
flex: 1
添加到左侧和右侧的项目
flex: 1
。这将迫使它们都占用可用空间,从而使中间项居中。
display: flex
添加到各个项目以使其内容对齐。
min-height
结合使用的注意事项:当前在Chrome,Firefox,Edge和其他浏览器中,速记规则
flex: 1
细分为:
flex-grow: 1
flex-shrink: 1
flex-basis: 0%
flex-basis
时,
min-height
上的百分比单位(%)导致此方法中断。这是因为,通常来说,子级上的百分比高度要求在父级上有显式的
height
属性设置。
#flex-container {
display: flex;
flex-direction: column;
background: teal;
width: 150px;
min-height: 80vh;
justify-content: space-between;
}
#flex-container>div {
background: orange;
margin: 5px;
}
#flex-container>div:first-child {
flex: 1;
}
#flex-container::after {
content: "";
flex: 1;
}
<div id="flex-container">
<div>very long annoying text that will add on top of the height of its parent</div>
<div>center</div>
</div>
px
或根本不尝试(
which is what the spec actually recommends,尽管事实上至少有一些主要的浏览器出于某种原因附加了百分比单位)。
#flex-container {
display: flex;
flex-direction: column;
background: teal;
width: 150px;
min-height: 80vh;
justify-content: space-between;
}
#flex-container > div {
background: orange;
margin: 5px;
}
/* OVERRIDE THE BROWSER SETTING IN THE FLEX PROPERTY */
#flex-container > div:first-child {
flex: 1;
flex-basis: 0;
}
#flex-container::after {
content: "";
flex: 1;
flex-basis: 0;
}
/* OR... JUST SET THE LONG-HAND PROPERTIES INDIVIDUALLY
#flex-container > div:first-child {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}
#flex-container::after {
content: "";
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}
*/
<div id="flex-container">
<div>very long annoying text that will add on top of the height of its parent</div>
<div>center</div>
</div>
ul {
display: grid;
grid-template-columns: 1fr repeat(3, auto) 1fr;
grid-column-gap: 5px;
justify-items: center;
}
li:nth-child(1) { grid-column-start: 2; }
li:nth-child(4) { margin-left: auto; }
/* for demo only */
ul { padding: 0; margin: 0; list-style: none; }
li { padding: 5px; background: #aaa; }
p { text-align: center; }
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
</ul>
<p><span>| true center |</span></p>
关于html - 使flexbox元素居中和右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059550/
我想知道最终用户按下了什么,所以我使用了 getch() 。 如果用户按右,我可以获得0xE0 0x4D。 如果用户按下Ctrl+右,我可以获得0xE0 0x47。 如果用户按下Shift+右,我可以
我已经构建了一个应用程序来搜索我的位置。 这是代码 var map; var gdir; var geocoder = null; var addressMarker; function init
我想为我的元素设计布局 View 。布局 View 在左 Angular 和右 Angular (同一行)有一个图像,将有 2 行单词,一行在第 1 行,另一行在第 2 行。我该如何实现? It
我有一个很长的线性(分支不多)流程图,在 graphviz 中显示为要么太高而无法放在单个页面上,要么太宽(如果方向是从左到右) 是否有一种简单的方法可以让 graphviz 以从左到右,然后向下,然
我一直摸不着头脑,但运气不好。设计器有一个包含 3 栏的站点、两个侧边栏和一个主要内容区域。 专为桌面设计,左栏、主要内容、右栏。但是,在较小的设备上,我们希望首先堆叠主要内容。 所以通常情况下,你可
我想要从上到下和从左到右组织的 css block 。 为了更好地解释这是一张图片,其中包含我到目前为止所获得的内容以及我希望使用 CSS 实现的内容: 代码如下: HTML: 1 2 3 4 5
当我问this question时,答案之一(现已删除)建议Either类型对应Curry-Howard correspondence中的XOR而不是OR,因为它不能同时是Left和Right。 真相
如果一行中六个观察值中至少有三个是 != NA,我想计算该行的平均值。如果存在四个或更多 NA,则平均值应显示为 NA。 给出平均值的例子,忽略了 NA: require(dplyr) a % mut
我有一个由 9 列组成的数据框,其中包含一个因素 list 。每行可以填充所有 9 列(因为在该行中包含 9 个“事物”),但大多数没有(大多数有 3-4 个)。列也不是特定的,就像第 1 列和第 3
这是我第一次尝试使用 R 构建函数。基本上我的预期目标如下。 使用 RoogleVision 包与 Google Cloud Vision API 通信 函数遍历目录中的图片 从每张图片的 Googl
使用: mean (x, trim=0.05) 从分布的每一侧移除 2.5%,这对于对称的双尾数据来说很好。但是如果我有一个尾部或高度不对称的数据,我希望能够只删除分布的一侧。有没有这个功能,还是我自
我想保留重复的列,并删除唯一的列。这些列将具有相同的值,但名称不同。 x1 = rnorm(1:10) x2 = rnorm(1:10) x3 = x1 x4 = rnorm(1:10) x5 = x
是否可以使WPF工具栏中的元素的Right水平对齐方式正确? 我尝试将内部元素添加到Grid中,并将ColumnDefinition分配给Left / Right。我
datatable(head(iris)) 如何将我的列居中,使其位于我的列名称的正下方? 最佳答案 您可以使用options 下的columnDefs 自变量。将 className 设置为 dt-
我是 R 的新手,但我正在尝试在 R 中制作滑动窗口。 使用循环我可以像这样,但这变得非常低效。 results=c(1:7) letters=c("A","B","C","D","E","F","G
假设我有这个 .txt 文件: here is line 1 here is line 2 here is line 3 here is line 4 我想将此字符串粘贴到第 3 行和第 4 行之间:
假设我有这个 .txt 文件: here is line 1 here is line 2 here is line 3 here is line 4 我想将此字符串粘贴到第 3 行和第 4 行之间:
我想知道我的环境中有什么类型的对象。 我可以像这样显示谁在那里: ls() 但是运行类似的东西 sapply(ls(), class) (显然)不会告诉我们我们拥有什么类型(类)的对象(函数、数字、因
我想创建一个带有水平标签的树状图,但让叶子根据它们的高度悬挂,而不是仅仅下降到图的边缘。 例子: par(mfrow = c(1,2)) hc <- hclust(dist(USArrests), "
我的 CSS 中有一个元素,如下所示 .xyz{ position:absolute; left:50%; } 现在正如预期的那样,当我减小浏览器窗口的宽度时,这个元素向左移动
我是一名优秀的程序员,十分优秀!