按钮无法并排显示问题求助:CSS设置未生效
问题原因分析
你的按钮无法并排的核心问题是第二个按钮被包裹在<form>标签内,而<form>是块级元素,默认会独占一行,直接打断了前后元素的行内布局。另外,虽然<a>和<button>本身支持行内显示,但form的块级特性会强制换行。
解决方案
下面提供几种可行的解决方式,你可以根据实际场景选择:
方案1:修改form的显示属性(最简单)
给form添加display: inline-block样式,让它变成行内块元素,和其他按钮处于同一行:
#btn1, #btn2, #btn3 { width: 200px; height: 40px; } /* 新增form样式 */ form { display: inline-block; margin: 0; /* 清除form默认的上下外边距,避免间距过大 */ }
方案2:隐藏form,用链接触发提交(适合不想改变布局结构的情况)
把delete按钮换成<a>标签,通过JavaScript触发隐藏的form提交,这样三个元素都是行内的链接/按钮:
<a class="btn btn-success" id="btn1" href="{{route('wineshop.edit', compact('wineshop'))}}">EDIT</a> <a class="btn btn-danger" id="btn2" href="#" onclick="event.preventDefault(); document.getElementById('delete-form').submit();">DELETE</a> <form id="delete-form" action="{{route('wineshop.destroy', compact('wineshop'))}}" method="post" style="display: none;"> @method('delete') @csrf </form> <a class="btn btn-primary" id="btn3" href="{{route('wineshop.index')}}">BACK</a>
方案3:用Flex布局统一控制(最灵活)
给所有按钮的父容器添加Flex布局,强制子元素并排,还能方便控制间距:
<div class="btn-container"> <a class="btn btn-success" id="btn1" href="{{route('wineshop.edit', compact('wineshop'))}}">EDIT</a> <form action="{{route('wineshop.destroy', compact('wineshop'))}}" method="post"> @method('delete') @csrf <button type="submit" id="btn2" class="btn btn-danger">DELETE</button> </form> <a class="btn btn-primary" id="btn3" href="{{route('wineshop.index')}}">BACK</a> </div>
.btn-container { display: flex; gap: 12px; /* 可选:设置按钮之间的间距 */ } #btn1, #btn2, #btn3 { width: 200px; height: 40px; } form { margin: 0; }
内容的提问来源于stack exchange,提问作者Aurora
相关产品推荐
相关产品推荐

