gpt4 book ai didi

css INLINE-BLOCK div 居中对齐但保持内部元素仍然左对齐

转载 作者:行者123 更新时间:2023-11-28 16:58:22 24 4
gpt4 key购买 nike

我需要这个简单示例的帮助,我设计这个示例只是为了理解 INLINE-BLOCK 相关布局。很简单:左右两部分,右部分里面有上下元素。我希望右侧部分位于 [屏幕宽度减去左侧部分] 的中心,而右侧部分 h3 和 p 元素向左对齐。我添加了一个边框只是为了了解我是否已经充分利用了右侧屏幕的空间。有时我的双手被束缚,所以我不能改变 html,所以我需要纯 css 方法。我确实在这里读过一些类似的问题,但经过一些测试后我仍然迷路了----例如,给右边的部分一个 width:500px ?但我需要正确的部分以不同的屏幕为中心!另一个问题:右侧部分 text-align:center 也会影响内部元素;内部元素 text-align:left 也会影响右侧部分。请注意:这是关于内联 block ......和纯CSS。感谢任何回应。

.left{
display:inline-block;
background:aqua;
}
.right{
border:solid;
display:inline-block;
}
<OCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<div class="whole">
<div class="left"><p>lefter</p></div>

<div class="right">
<h3>upperright</h3>
<div class="lowerright"><p>sdfsdfsdfsdfsdfsdd</p>
</div>
</div>
</div>

</body>
</html>

所附图片显示了我的想法。 enter image description here

最佳答案

添加了

margin-left: 50%;
transform: translatex(-50%);

向右。希望这可以帮助。谢谢

.left {
display: inline-block;
background: aqua;
}

.right {
border: solid;
display: inline-block;
margin-left: 50%;
transform: translatex(-50%);
}
<OCTYPE html>
<html>

<head>
<title></title>
</head>

<body>

<div class="whole">
<div class="left">
<p>lefter</p>
</div>
<div class="right">
<h3>upperright</h3>
<div class="lowerright">
<p>sdfsdfsdfsdfsdfsdd</p>
</div>
</div>
</div>

</body>

</html>

关于css INLINE-BLOCK div 居中对齐但保持内部元素仍然左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55114594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com