为什么我的模态内容的背景色不显示?

时间:2019-02-07 03:37:12

标签: javascript css html5

// Get the modal
let modal = document.getElementById('login-modal');

// Get the button that opens the modal
let btn = document.getElementById("login");

// Get the <span> element that closes the modal
let span = document.getElementsByClassName("close")[0];

// When the user clicks on the button, open the modal 
btn.onclick = function() {
  modal.style.display = "flex";
}

// When the user clicks on the <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close the modal
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";}
}
html, body{
	box-sizing:border-box;
	overflow:hidden;
	height:100%;
}

body{
	min-height:100%;
	min-width:100%;
	background-color:blue;
	background-size:100% 100%;
	background-repeat: no-repeat;
	background-position:center center;
	position:relative;
 
}
.container{
	height:100%;
	width:100%;
	overflow:hidden;
}
#login-modal{
	width:100%;
	height:100%;
	background-color:rgba(0, 0, 0, 0.5);
	position: absolute;
	top:0;
	left:0;
	display:flex;
	justify-content: center;
	align-items: center;
	text-align:center;
}

.login-content{
	border: 10 px solid black;
	height:300px;
	width:500px;
	background-color:white;
	text-align:center;
}
input[type=text], input[type=password]{
	display:block;
	margin: 0 auto;
}
<header>
		<div class="container">
			<div  id="branding">
				<a href="indexresolve.html"><img src="images/lasttry.png" alt="resolvelogo"></a>
			</div>
			<nav>
				<li><a href="indexresolve.html">Home</a></li>
				<li><a href="howitworks.html">How It Works</a></li>
				<li><a href="contact.html">Contact</a></li>
				<li><a href="faq.html">FAQ</a></li>
				<li><button id="login" class="button">Log In</button></li>
				<div id="login-modal">
					<div id="login-content">
						<span class="close">&times;</span>
						<form>
							<input type="text" placeholder="username">
							<input type="password" placeholder="password">
							<button>Log In</button>
						</form>
					</div>
				</div>
			</nav>
	</header>

我已经准备好将模态内容div与基本CSS配合使用,但是白色的背景色没有显示。我希望内容背景的边框为200px x 300px。

<div id="login-modal">
<div id="login-content">
<span class="close">&times;</span>
<form>
<input type="text" placeholder="username">
<input type="password" placeholder="password">
<button>Log In</button>
</form>
</div>
</div>


#login-modal{
width:100%;
height:100%;
background-color:rgba(0, 0, 0, 0.5);
position:absolute;
top:0;
left:0;
display:flex;
justify-content: center;
align-items: center;
text-align:center;
}

.login-content{
border: 10 px solid black;
height:300px;
width:500px;
background-color:white;
text-align:center;
}
input[type=text], input[type=password]{
display:block;
margin: 0 auto;

我认为用rgba设置模态本身的背景色会有所帮助,但仍然没有白框。

1 个答案:

答案 0 :(得分:0)

在CSS中进行以下更改

#login-modal {
    width: 61%;
    height: 39%;
    background-color: rgba(0, 0, 0, 0.5);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
}