我正在嘗試使用Bootstrap制作一個表單,目前為止我做得很好,除了一件事。我想在表單中添加一個開關類型復選框,頂部有一個標簽,兩邊有文字。 我已經嘗試了很多方法來實現這一點,但我似乎無法將這兩個文本和復選框放在一行中。
目前看起來是這樣的:
下面是一個完整的工作示例:
<!DOCTYPE html>
<html>
<head>
<title>TestSite</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<!--link th:rel="stylesheet" th:href="@{/css/styles.css}"/-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body class="bodyClass sb-nav-fixed">
<div class="container">
<div class="card shadow-lg border-0 rounded-lg mt-5">
<div class="card-header">
<h2 class="text-left font-weight-light my-4">Add content</h2>
</div>
<div class="card-body">
<form>
<div class="row">
<div class="col">
<label class="small mb-1" for="price">Price:</label>
<div class="input-group mb-3">
<input id="price" name="price" type="text" class="form-control" placeholder="Price">
<span class="input-group-text">$</span>
</div>
</div>
<div class="col">
<label for="revenue">Revenue</label>
<div class="form-text">Fix</div>
<div class="form-check form-switch form-check-inline">
<input id="revenue" class="form-check-input form-check-inline" type="checkbox">
</div>
<div class="form-text">Percent</div>
</div>
</div>
<div class="btn btn-lg btn-secondary btn-block col-4">Cancel</div>
<button class="btn btn-lg btn-primary btn-block col-4" type="submit">Save</button>
</form>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/js/all.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>
</html>
你需要用flex的容器把它們放在一條線上,把它們放到一邊或者全部放在一條線上。我選擇了第一個。
剩下的就是稍微推動一下這些碎片。
.my-switch {
background: lightyellow;
padding-top: 6px;
}
.my-switch .form-switch {
margin: 3px 0px 0 15px;
}
<!DOCTYPE html>
<html>
<head>
<title>TestSite</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<!--link th:rel="stylesheet" th:href="@{/css/styles.css}"/-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body class="bodyClass sb-nav-fixed">
<div class="container">
<div class="card shadow-lg border-0 rounded-lg mt-5">
<div class="card-header">
<h2 class="text-left font-weight-light my-4">Add content</h2>
</div>
<div class="card-body">
<form>
<div class="row">
<div class="col">
<label class="small mb-1" for="price">Price:</label>
<div class="input-group mb-3">
<input id="price" name="price" type="text" class="form-control" placeholder="Price">
<span class="input-group-text">$</span>
</div>
</div>
<div class="col col-revenue">
<label for="revenue">Revenue</label>
<div class="d-flex my-switch">
<div class="form-text text-1">Fix</div>
<div class="form-check form-switch form-check-inline">
<input id="revenue" class="form-check-input form-check-inline" type="checkbox">
</div>
<div class="form-text text-2">Percent</div>
</div>
</div>
</div>
<div class="btn btn-lg btn-secondary btn-block col-4">Cancel</div>
<button class="btn btn-lg btn-primary btn-block col-4" type="submit">Save</button>
</form>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/js/all.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
</body>
</html>
類似的回答,因為它高盛,但沒有任何額外的風格。
使用bootstrap 5,添加一個& quotd-flex & quot;類在最外層,一個& quot浮點結束& quot在你的按鈕/開關上
<div class="d-flex">
<p>
My first sentence
</p>
<div class="form-check form-switch form-check-inline">
<input class="form-check-input float-end"
type="checkbox"
role="switch"
[title]="'Example button'"
[id]="'example'">
</div>
<p>
My second sentence
</p>
</div>