P粉6048485882023-09-05 00:33:10
让我们尝试这个选项。您对这种方法有什么不喜欢的地方?
div { background-color: rgba(0, 0, 0, 0.1); } .button-container { padding: 16px; display: flex; gap: 16px; } .horizontal-button { padding: 16px; flex: 1; text-align: center; word-wrap: hypens; } @media (max-width: 360px) { .button-container { flex-direction: column; } }
<div class="button-container"> <div class="horizontal-button" data-role="yes">Yes</div> <div class="horizontal-button" data-role="no">No, sorry - I cannot make it</div> </div>
A: 在这种情况下,容器/按钮占据了整个页面的宽度...它们不会根据文本的宽度进行调整。
Q: 好的,那么:
div { background-color: rgba(0, 0, 0, 0.1); } .button-container { margin: auto; padding: 16px; box-sizing: border-box; display: flex; align-items: stretch; gap: 16px; max-width: max-content; } .horizontal-button { display: flex; flex-direction: column; justify-content: center; padding: 16px; flex: 1; text-align: center; max-width: 50%; } @media (max-width: 360px) { .button-container { flex-direction: column; } .horizontal-button { hyphens: auto; max-width: unset; } }
<div class="button-container"> <div class="horizontal-button" data-role="yes">Yes</div> <div class="horizontal-button" data-role="no">No, sorry - I cannot make it</div> </div>