gpt4 book ai didi

html - 如何使黑框垂直和水平居中?

转载 作者:太空宇宙 更新时间:2023-11-03 17:46:49 24 4
gpt4 key购买 nike

我正在尝试将黑盒垂直和水平居中!对于水平居中,我尝试过 margin: auto 但它不适用于高分辨率。

我正在尝试让我的英雄区域响应。当我减小窗口的宽度时,黑框会居中,但在宽度较大时它会保持在左侧。

另外请帮我水平对齐!还请帮我调整背景!

因此,我的问题是:-

  1. 我想使黑框垂直和水平居中!
  2. 帮助我调整红色背景以使其具有响应性

#header{
width: 100%;
height: 100px;
background: rgba(0,0,0,0.7);
}

#heroarea{
max-width: 100%;
height: calc(100vh - 100px);
background:red;
position: relative;
}

#hero-intro-box{
background: rgba(0,0,0,0.8);
width: 100%;
max-width: 800px;
height: auto;
border: 1px solid white;
border-radius: 15px;
padding: 10px;
padding-bottom: 30px;
margin: auto;

}

#hero-intro-box h1{
color: white;
text-align: center;
padding-top: 3%;
}

#hero-intro-box p{
color: white;
margin: 15px;
padding: 3%;
padding-top: 2%;
}

#hero-intro-box #book-now-button{
height: 50px;
width: 150px;
background: rgba(255,255,255,0.5);
text-align: center;
border-radius: 15px;
border: 2px solid darkgrey;
margin: auto;
}

@media (max-width: 400px) {
#heroarea{
height: 600px;
}

#hero-intro-box{
height: auto;
margin-bottom: 30px;
position: relative;
top: 0px;
bottom: 0px;
}

}

@media (min-width: 550px) {
#heroarea{
height: calc(100vh - 100px);
}
}
    <link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet" />

<div id="header">
<div class="container">
</div>
</div>

<div id="heroarea">
<div class="container">
<div id="hero-intro-box" class="ten columns">
<h1>Enjoy world-class Cuisines</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<div id="book-now-button">Book us now!!</div>
</div>
</div>
</div>

最佳答案

对齐元素最可靠的方法是使用 CSS3 表格布局。更多信息请阅读 this article .

在下面的代码中我使用了这种技术。查看它如何在全屏模式下和窗口调整大小时运行。我还修复了您的背景以适合整个页面大小,将文本居中放置在“立即预订我们!!”按钮并使文本对齐(更美观)。

body {
margin: 0;
}

#header{
width: 100%;
height: 100px;
background: rgba(0,0,0,0.7);
}

#heroarea{
width: 100%;
height: calc(100vh - 100px);
background-color:red;
}

.container {
display: table;
width: 100%;
height: 100%;
}

.container > span {
display: table-cell;
text-align: center;
vertical-align: middle;
}

#hero-intro-box{
background: rgba(0,0,0,0.8);
width: 100%;
max-width: 800px;
height: auto;
border: 1px solid white;
border-radius: 15px;
padding: 10px 10px 30px 10px;
}

#hero-intro-box h1{
color: white;
text-align: center;
padding-top: 3%;
}

#hero-intro-box p{
color: white;
margin: 15px;
padding: 3%;
padding-top: 2%;
text-align: justify;
}

#hero-intro-box #book-now-button{
display: table;
height: 50px;
width: 150px;
background: rgba(255,255,255,0.5);
text-align: center;
border-radius: 15px;
border: 2px solid darkgrey;
margin: 0 auto;
}

#book-now-button > span{
display: table-cell;
text-align: center;
vertical-align: middle;
}

@media (max-width: 400px) {
#heroarea{
height: 600px;
}

#hero-intro-box{
height: auto;
margin-bottom: 30px;
position: relative;
top: 0px;
bottom: 0px;
}

}

@media (min-width: 550px) {
#heroarea{
height: calc(100vh - 100px);
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" rel="stylesheet" />

<div id="header">
<div class="container">
</div>
</div>

<div id="heroarea">
<div class="container">
<span>
<div id="hero-intro-box" class="ten columns">
<h1>Enjoy world-class Cuisines</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
<div id="book-now-button"><span>Book us now!!</span></div>
</div>
</span>
</div>
</div>

关于html - 如何使黑框垂直和水平居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28001779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com