Make Bootstrap Collapse button and content position fixed - html

Trying to position the button that triggers the bootstrap collapse in a fixed position so it is always visible when the user is scrolling on the page. However the position of the content that is shown when the button is clicked does not change with the button position. How do I show the collapsed content in the same default collapse format after changing the button position?
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<button style="position: fixed; top: 50% ;left: 50%", class="btn btn-primary" type="button" data-toggle="collapse" data-target="#butcollapse"
aria-expanded="false" aria-controls="butcollapse">
+
</button>
<div class="collapse" id="butcollapse">
<div class="card card-body">
hello
</div>
</div>

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<button style="position: fixed; top: 50% ;left: 50%", class="btn btn-primary" type="button" data-toggle="collapse" data-target="#butcollapse"
aria-expanded="false" aria-controls="butcollapse">
+
</button>
<div style="position: fixed; top: 75% ;left: 50%;" class="collapse" id="butcollapse">
<div class="card card-body">
hello
</div>
</div>

Related

Bootstrap Dropdown left / right position bug on mobile

I have an incomprehensible problem in our web application. I have reduced the page to a minimum, here it is:
https://www.aschemeier.net/bug1.html
Everything works fine on the desktop. When I test the page either in the Firefox browser (screen size set to iPhone) or on an Android smartphone (Chrome browser), display errors occur when I open the drop-down menu at the bottom. In the mobile emulation on the desktop the touch input is registered after a few clicks about 200px further up, on the smartphone the picture jumps up and you can't scroll down anymore. If I reduce the height of the red framed container it works from a certain limit. If I don't open the menu to the left (dropleft) there are no problems either, but I need this function at this point.
I hope someone can help me. Thanks
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Alle globalen, externen Javascript Dateien -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js" integrity="sha384-1CmrxMRARb6aLqgBO7yyAxTOQE2AKb9GfXnEo760AUcUmFx3ibVJJAzGytlQcNXd" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<title>bug sample</title>
</head>
<body>
<div class="container mb-5 mt-4">
<div class="card">
<div class="card-body" style="height:1000px; border:2px solid #f00;"></div>
</div>
<div class="mt-4">
<div class="row">
<div class="col-md-1 col-4">AB</div>
<div class="col-md-2 col-4">AB</div>
<div class="col-md-1 col-4">
<!-- Split dropleft button -->
<div class="btn-group">
<div class="btn-group dropleft" role="group">
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">Toggle Dropleft</span>
</button>
<div class="dropdown-menu">
<!-- Dropdown menu links -->
</div>
</div>
<button type="button" class="btn btn-secondary">
Split dropleft
</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
I would handle it by assigning an expander class into the parent row and toggle expander class on each button click.
Sample CSS:
.expanded-row {
min-height:30vh;
}
Toggling the class:
$( ".dropdown-toggle" ).click(function() {
$( ".row" ).toggleClass( "expanded-row" );
});
I hope it helps.

Bootstrap 4 Nested Collapse Style Issue

there is a collapse (with button 2) inside of a another collaspe (with button 1).
when parent is collapsed the background color of button 1 is blue (bg- primary) and when its not collapsed the backpround color is yellow (bg- warning). when we click button 2, the color stays yellow but when button 2 clicked again and its collapsed, the color of button 1 changes to blue. I want to button 1 stays yellow when it is not collapsed whether her child is collapsed or not.
thank you and sorry for my bad english.
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="my-2">
<button class="btn btn-primary" id="btn1" type="button" name="button" data-toggle="collapse" data-target="#collapsebtn" aria-expanded="false" aria-controls="collapsebtn">
button 1
</button>
</div>
<div class="collapse" id="collapsebtn">
<div class="mb-2">
<button class="btn btn-dark btn-sm" type="button" name="button" data-toggle="collapse" data-target="#collapsebtn2" aria-expanded="false" aria-controls="collapsebtn2">
button 2
</button>
</div>
<div class="collapse border border-primary" id="collapsebtn2">
Lorem ipsum dolor text....
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#collapsebtn").on("show.bs.collapse", function() {
$("#btn1").removeClass("btn-primary");
$("#btn1").addClass("btn-warning");
});
$("#collapsebtn").on("hidden.bs.collapse", function() {
$("#btn1").addClass("btn-primary");
$("#btn1").removeClass("btn-warning");
});
});
</script>
</body>
</html>
add this code:
$("#collapsebtn").on("hidden.bs.collapse", function() {
if(!$('#collapsebtn').hasClass('show')){
$("#btn1").addClass("btn-primary");
$("#btn1").removeClass("btn-warning");
}
});

bootstrap 4 make alert appear on top of row

I have an alert div and a row inside a container, when I close the alert the row content moves up. I want to make the alert appear over the row content so that when I close it nothing happens.
<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>
thanks in advance
Muhammad's suggestion could be made to work if you set the container to position: relative;
.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">
×
<strong>Success!</strong> This alert box could indicate a successful or positive action.
</div>
</div>
</body>
</html>
You could try using jQuery to change the visibility of the alert rather than it's display:
$('.alert').on('close.bs.alert', function (e) {
e.preventDefault()
$(this).css('visibility', 'hidden')
})
Here is a demo:
https://www.bootply.com/TazOvYZQyC
One way of doing this is to add the following CSS:
position:absolute !important;
top:0px;
to the alert div.
Here is a sample code:
<!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;">
×
<strong>Success!</strong> This alert box could indicate a successful or positive action.
</div>
</div>
</body>
</html>

How to center text on customized and resizeable button using bootstrap 4 only

I have a problem with button text. I have customized the width and height of a button and when I resize, its text remains no more in center on some screen sizes in a debugging mode.
Html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ICT</title>
<base href="/">
<meta name="viewport" charset="utf-8" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div #fade class="img-wrapper">
<img [src]="imgPath" class="img-fluid">
<div class="img-overlay">
<button routerLink="/test"
class="btn btn-success fill-container text-center testButton">
TEST
</button>
</div>
</div>
</div>
</div>
</body>
</html>
CSS
.img-wrapper {
position: relative;
}
.img-overlay {
position: absolute;
bottom: 31%;
left: 13.5%;
width: 34%;
height: 4%;
}
.testButton {
font-size: 3vw;
line-height: 20px;
}
.fill-container {
width: 100%;
height: 100%;
}
Update
Here is the image where I want html button to replace the button on image
Please have a look I have added the image on which I want to place an html button with same width and height so that it hides the image button.
Here's the code (using your HTML as a basis) for a large button that remains centered on all devices/screens. Click the "run code snippet" button below:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div #fade class="img-wrapper">
<img [src]="imgPath" class="img-fluid">
<div class="img-overlay text-center">
<button routerLink="/test"
class="btn btn-lg btn-success fill-container text-center testButton" style="width: 34%;">
TEST
</button>
</div>
</div>
</div>
</div>
</div>
This should do the trick : Adding class text-center to parent div of the button tag.
<div class="row">
<div class="col-sm-12">
<div class="text-center">
<button routerLink="/test" class="btn btn-success">TEST</button>
</div>
</div>
</div>
Also make sure to include Bootdtrap V4 in head tag.
<head>
<meta charset="utf-8">
<title>ICT</title>
<base href="/">
<meta name="viewport" charset="utf-8" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
Have a look at the this link as well, adding bootstrap to your page: click me
To center the text on button: Use
class="text-center"
You need to Apply "align-center" class on .img-overlay {} not on Button then it will work

Bootstrap alertinfo getting merged with button in header

I have provided the code snippet
here i want to display the marque below logo but it is covering the whole header.
Visit here
use display: inline-block and width: 100%; on alert-info div it will work fine that you want.
UPDATED
You missed at least a container-fluid style declaration. See your snippet updated, here https://www.w3schools.com/code/tryit.asp?filename=FEL4BAHI14YZ.
Code:
<!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.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body id="bootstrap-overrides">
<header class="container-fluid">
<div class="header">
<img width="300" class="img-responsive" style="height: 120px; float:left" src="images/logo.jpg" />
<button class="btn btn-primary pull-right">BUTTON</button>
</div>
</header>
<div class="container-fluid">
<form id="frm" runat="server" class="form-horizontal">
<div class="alert alert-info" role="alert" style="color: red">
<marquee>Welcome To IDB WEB APPLICATION. All (*) Fields Are Mandatory</marquee>
</div>
</form>
</div>
</body>
</html>
Hope this helps.
Updated- Just wrap your header and alert-form individually inside a container-fluid with a row. Thats the best solution using bootstrap.
below is the wokring code. For logo I am using dummy image
<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.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<header>
<div class="container-fluid">
<div class="row">
<div class="header">
<img width="300" class="img-responsive" style="height:120px; float:left;" src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150" />
<button class="btn btn-primary pull-right">BUTTON</button>
<!--- Remove this inline style style="height: 120px; float:left" -->
</div>
</div>
</div>
</header>
<div class="container-fluid">
<div class="row">
<form id="frm" runat="server">
<div class="alert-info" role="alert" style="color: red">
<marquee>Welcome To IDB WEB APPLICATION. All (*) Fields Are Mandatory</marquee>
</div>
</form>
</div>
</div>