HTML、CSS - 将按钮与页面中心对齐
HTML, CSS - Aligning Buttons To The Center Of Page
我想将一些按钮对齐到页面的中心。我可以知道如何在我的 CSS 中做到这一点吗?
button {
background-color: #6495ED;
color: white;
padding: 16px 25px;
margin: 0 auto;
border: none;
cursor: pointer;
width: 100%;
border-radius: 8px;
display: block;
position: middle;
}
<button type="button" onclick="document.getElementById('id01').style.display='block'" style="width: auto;">User Login</button>
<br><br><br>
<button type="button" onclick="document.getElementById('id02').style.display='block'" style="width:auto; ">Admin Login</button>
如果 flexbox
是一个选项,您可以添加:
body {
margin: 0;
height: 100vh; // stretch body to the whole page
display: flex; // define a flex container
flex-direction: column; // arrange items in column
justify-content: center; // align vertically center
}
(注意position: middle
是无效的)
参见下面的演示:
body {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
}
button {
background-color: #6495ED;
color: white;
padding: 16px 25px;
margin: 0 auto;
border: none;
cursor: pointer;
width: 100%;
border-radius: 8px;
}
<button type="button" onclick="document.getElementById('id01').style.display='block'" style="width: auto;">User Login</button>
<br><br><br>
<button type="button" onclick="document.getElementById('id02').style.display='block'" style="width:auto; ">Admin Login</button>
如果您不想使用 Flexbox,只需将 button
包裹在 div
中并将其 position
设置为 absolute
、top
到 50%
、left
到 50%
和 transform
到 translate(-50%, -50%)
,还给父元素或 body
元素 position
relative
.
请注意 body
或包含 .buttons-holder
的父项必须定义为 height
。
html, body {
width: 100%;
height: 100%;
margin: 0;
}
body {
position: relative;
}
.buttons-holder {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
button {
background: #6495ED;
color: white;
padding: 16px 25px;
margin: 15px auto;
border: none;
cursor: pointer;
width: 100%;
border-radius: 8px;
display: block;
}
<div class="buttons-holder">
<button type="button" onclick="document.getElementById('id01').style.display='block'" style="width:auto">User Login</button>
<button type="button" onclick="document.getElementById('id02').style.display='block'" style="width:auto">Admin Login</button>
</div>
我想将一些按钮对齐到页面的中心。我可以知道如何在我的 CSS 中做到这一点吗?
button {
background-color: #6495ED;
color: white;
padding: 16px 25px;
margin: 0 auto;
border: none;
cursor: pointer;
width: 100%;
border-radius: 8px;
display: block;
position: middle;
}
<button type="button" onclick="document.getElementById('id01').style.display='block'" style="width: auto;">User Login</button>
<br><br><br>
<button type="button" onclick="document.getElementById('id02').style.display='block'" style="width:auto; ">Admin Login</button>
如果 flexbox
是一个选项,您可以添加:
body {
margin: 0;
height: 100vh; // stretch body to the whole page
display: flex; // define a flex container
flex-direction: column; // arrange items in column
justify-content: center; // align vertically center
}
(注意position: middle
是无效的)
参见下面的演示:
body {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
}
button {
background-color: #6495ED;
color: white;
padding: 16px 25px;
margin: 0 auto;
border: none;
cursor: pointer;
width: 100%;
border-radius: 8px;
}
<button type="button" onclick="document.getElementById('id01').style.display='block'" style="width: auto;">User Login</button>
<br><br><br>
<button type="button" onclick="document.getElementById('id02').style.display='block'" style="width:auto; ">Admin Login</button>
如果您不想使用 Flexbox,只需将 button
包裹在 div
中并将其 position
设置为 absolute
、top
到 50%
、left
到 50%
和 transform
到 translate(-50%, -50%)
,还给父元素或 body
元素 position
relative
.
请注意 body
或包含 .buttons-holder
的父项必须定义为 height
。
html, body {
width: 100%;
height: 100%;
margin: 0;
}
body {
position: relative;
}
.buttons-holder {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
button {
background: #6495ED;
color: white;
padding: 16px 25px;
margin: 15px auto;
border: none;
cursor: pointer;
width: 100%;
border-radius: 8px;
display: block;
}
<div class="buttons-holder">
<button type="button" onclick="document.getElementById('id01').style.display='block'" style="width:auto">User Login</button>
<button type="button" onclick="document.getElementById('id02').style.display='block'" style="width:auto">Admin Login</button>
</div>