bootstrap 4 使警报出现在行的顶部
bootstrap 4 make alert appear on top of row
我有一个警报 div 和容器内的一行,当我关闭警报时,行内容向上移动。我想让警报出现在行内容上,这样当我关闭它时什么也不会发生。
<div class="container">
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>TEXT</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="row align-items-center">
CONTENT
</div>
</div>
提前致谢
一种方法是添加以下 CSS:
position:absolute !important;
top:0px;
警报 div。
这是一个示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Alerts</h2>
<div class="alert alert-success alert-dismissable" style="position: absolute; top:0px;">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> This alert box could indicate a successful or positive action.
</div>
</div>
</body>
</html>
您可以尝试使用 jQuery 来更改警报的可见性而不是它的显示:
$('.alert').on('close.bs.alert', function (e) {
e.preventDefault()
$(this).css('visibility', 'hidden')
})
如果您将容器设置为 position: relative;
,Muhammad 的建议可能会奏效
.container {position: relative;}
.alert {position: absolute; top: 0;}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar">
<div class="navbar-brand">Brand</div>
</nav>
<div class="container">
<h2>Alerts</h2>
<div class="alert alert-success alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> This alert box could indicate a successful or positive action.
</div>
</div>
</body>
</html>
我有一个警报 div 和容器内的一行,当我关闭警报时,行内容向上移动。我想让警报出现在行内容上,这样当我关闭它时什么也不会发生。
<div class="container">
<div class="alert alert-success alert-dismissible fade show" role="alert">
<strong>TEXT</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="row align-items-center">
CONTENT
</div>
</div>
提前致谢
一种方法是添加以下 CSS:
position:absolute !important;
top:0px;
警报 div。 这是一个示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Alerts</h2>
<div class="alert alert-success alert-dismissable" style="position: absolute; top:0px;">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> This alert box could indicate a successful or positive action.
</div>
</div>
</body>
</html>
您可以尝试使用 jQuery 来更改警报的可见性而不是它的显示:
$('.alert').on('close.bs.alert', function (e) {
e.preventDefault()
$(this).css('visibility', 'hidden')
})
如果您将容器设置为 position: relative;
,Muhammad 的建议可能会奏效.container {position: relative;}
.alert {position: absolute; top: 0;}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar">
<div class="navbar-brand">Brand</div>
</nav>
<div class="container">
<h2>Alerts</h2>
<div class="alert alert-success alert-dismissable">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Success!</strong> This alert box could indicate a successful or positive action.
</div>
</div>
</body>
</html>