我想要水平居中一个元素,并将其他元素放置在其两侧(但不包括它们在居中计算中)。我更喜欢使用纯CSS解决方案;最好不要使用脚本,除非是超级轻量级的。目前为止,这是我所能做到的最接近的结果:
https://jsfiddle.net/stupid_genius/5c0xnqmb/5/
"左"和"右"元素应该触碰到居中的"X"元素的左侧和右侧,而不改变它的位置。有没有一种方法可以做到这一点?
期望结果的模拟:https://jsfiddle.net/stupid_genius/5c0xnqmb/3/
期望结果的模拟:https://jsfiddle.net/stupid_genius/5c0xnqmb/3/
html, body {
margin: 0;
padding: 0;
}
#main {
position: absolute;
width: 100%;
height: 100%;
background: lightblue;
}
#centered {
position: relative;
margin: 0 auto;
width: 500px;
background: yellow;
}
#center {
margin: 0 auto;
width: 100px;
text-align: center;
background: gray;
}
#left,
#right {
position: absolute;
top: 0;
}
#left {
/* right: calc(50% + 50px); */
left: 0;
width: 50px;
background-color: green;
}
#right {
/* left: calc(50% + 50px); */
right: 0;
width: 200px;
background-color: red;
}