【问题标题】:How do I keep my <h1> header to remain on top of my <h2> header?如何让我的 <h1> 标题保持在我的 <h2> 标题之上?
【发布时间】:2020-03-12 00:16:05
【问题描述】:

我是网页设计的初学者,刚刚开始设计自己的网站。我是一名平面设计师,设计了一个我希望网站看起来像的模型。我完成了大部分工作,除了这部分我卡住了,让我的 标题保持在我的 标题之上。这两个标题都在循环播放的视频背景之上。现在他们并肩而立。我还想在两个标题之间添加一条水平线——这是我设计的一部分。以下是我的 HTML 和 CSS 代码。 I also attached an image of my design vs what I have now。提前谢谢你。

另外,如果有人对我有任何建议或知道可以帮助我的最佳视频教程,我将不胜感激。谢谢!

H1 和 H2 位于“视频部分”下的 class=部分中。

CSS 代码和 HTML

@font-face{
	src: url(fonts/KeepCalm-Medium.ttf);
	font-family: keepcalm;}

@font-face{
	src: url(fonts/GOTHIC.TTF);
	font-family: centurygothic;}

body {
	margin: 0px;
	padding: 0px;}


/**Video Section**/
.section {
	position: relative;
	text-transform: uppercase;
	width: 100%;
	height: 534px;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;}

.section h1 {
	text-align: center;
	justify-content: center;	
	height: 108px;
	width:342px;
	font-family: keepcalm;
	font-size:36px;
	color: #ffffff;
	font-weight:500;
	line-height: 60px;
	transform: scale(1.189,1.189);
	letter-spacing: 15px;
	margin: 0px;
	padding: 0px;
	z-index: 1;	}

.section span {	font-size: 48px;}

hr {
	display: block;
	z-index: 1;}

.section h2{
	width: 181px;
	height: 22px;
	font-family: centurygothic;
	color: #ffffff;
	font-size:35px;
	font-weight: 400;
	letter-spacing: 9px;
	z-index:2;
}

.video-container {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;}

/*Topbar Section*/
.topbar {
	margin: 0px;
}
.topbar { 
	background-color: #e8e8e8;
	font-family: KeepCalm;
	font-size: 14px;
	align-items:flex-start;
	color: #000000;
	list-style: none;
	text-decoration: none;
	text-align: left;
	padding: 0px 0 0px 70px;}


.topbar > li {
	display: inline-block;
	padding: 0 20px 0 20px;}

.topbar > li > a {
	text-decoration: none;
	color: #000000;}

.phone {
	width:20px;
	padding: 4px 0 0px 4px }

.mail {
	height: 14px;
	}
/**Logo Section**/
.logo{
	float:left;
	padding-bottom: 10px;
	padding-left: 20px;}

/**Logo Text**/

.logotext {
	font-size: 14px;
	position: absolute;
	left: 140px;
	font-family: KeepCalm;
	float:left;
	padding-bottom: 10px;
	}

.logotext span {
	font-size: 14px;
	display: block;
	line-height: 14px;
	font-family: CenturyGothic;
	font-weight: 400;
}

.logotext span span {
	font-size: 14px;
	display: inline;
	line-height: 14px;
	font-family: CenturyGothic;
	font-weight: 400;
	background:  #e8e8e8;
}
/*Navigation Responsive*/
.selector-for-some-widget {
	box-sizing: content-box;}
	
.nav-item {
	background-color: #fff;
	font-family: KeepCalm;
	font-size: 14px;
	align-items:flex-start;
	color: #000000;
	list-style: none;
	text-align: right;
	padding: 20px 0 20px 0px;}

.nav-item  {
	display: inline-block;
	padding: 0 20px 0 20px;}

.nav-item {
	text-decoration: none;
	color: #000000;}

.navbar-light .navbar-nav .nav-item .nav-link:hover {
	color:  #e8e8e8;}

/*Topbar & Navigation Responsive*/

@media (max-width: 768px){
	.logo{
	width:5em;	
	padding-bottom: 1px;
	padding-left: 0px;
	}
	
	.logotext {
		font-size: 0.7em;
		line-height: 9pt;
		padding-bottom: 1px;
		padding-right: 100px;}
		
	.logotext span {
		line-height: 9pt;
		font-size: 0.8em;
		}
	.logotext span span {
		background: #FFFFFF;
		font-size: 1em;
		}
	.video-container {display: none;}
}

@media (max-width: 580px){
	.mail{width: 0px;}
	.hotmail{font-size: 0px;}
	.video-container {display: none;}
}

@media (max-width: 350px){
	.topbar {font-size: 0px;}
	.phone{ width:0px;}
	.mail {display: 0px;}
	.logotext {	font-size: 0px;}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="testtry.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>testtry</title>	
</head>
<body>

<!--Topbar-->

<div class="topbar">
	<ul class="topbar">
	<img src="Images/phone.png" alt="Call me at" class="phone">
	<li class="topbar topbartext">416-948-2164</li>
	<img src="mail1.png" alt="Email me at" class="mail">
	<li class="hotmail"><a href="">micaela-butron@hotmail.ca</a></li>
</ul>
</div>

<!--Navbar Responsive-->

<div class="Navcontainer">
	<nav class="navbar navbar-expand-lg navbar-light">
		  <a href="#"><img src="Logo1.png" alt="Micaela Butron Logo" class="logo"></a>
		  	<h1 class="logotext">
					I'm Micaela Butron.
					<span>an <span>Urban Designer</span>, + Graphic Designer.</span>
			</h1>
	
		  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
			<span class="navbar-toggler-icon"></span>
		  </button>
		  <div class="collapse navbar-collapse" id="navbarNav">
			<ul class="navbar-nav ml-lg-auto">
				  <li class="nav-item active"><a class="nav-link active" href="#">Home</a></li>
				  <li class="nav-item active"><a class="nav-link active" href="#">Resume</a></li>
				  <li class="nav-item active"><a class="nav-link active" href="#">Portfolio</a></li>
					<li class="nav-item active"><a class="nav-link active" href="#">Contact</a></li>
			</ul>
		  </div>
	</nav>
</div>


<!--VideoSection-->

<div class="section">
	<h1>Micaela <span>Butron</span></h1>
	<h2>Designer</h2>

	<div class="video-container">
		<video autoplay loop muted>
		<source src="Images/Videos/Video.mp4" type="video/mp4">
	</video>
	</div>
</div>

<!--Bootstrap Javascript Link-->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

</body>
</html>

【问题讨论】:

    标签: html css header position alignment


    【解决方案1】:
        <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="testtry.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title>testtry</title>  
    </head>
    <body>
    
    <!--Topbar-->
    
    <div class="topbar">
        <ul class="topbar">
        <img src="Images/phone.png" alt="Call me at" class="phone">
        <li class="topbar topbartext">416-948-2164</li>
        <img src="mail1.png" alt="Email me at" class="mail">
        <li class="hotmail"><a href="">micaela-butron@hotmail.ca</a></li>
    </ul>
    </div>
    
    <!--Navbar Responsive-->
    
    <div class="Navcontainer">
        <nav class="navbar navbar-expand-lg navbar-light">
              <a href="#"><img src="Logo1.png" alt="Micaela Butron Logo" class="logo"></a>
                <h1 class="logotext">
                        I'm Micaela Butron.
                        <span>an <span>Urban Designer</span>, + Graphic Designer.</span>
                </h1>
    
              <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
              </button>
              <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ml-lg-auto">
                      <li class="nav-item active"><a class="nav-link active" href="#">Home</a></li>
                      <li class="nav-item active"><a class="nav-link active" href="#">Resume</a></li>
                      <li class="nav-item active"><a class="nav-link active" href="#">Portfolio</a></li>
                        <li class="nav-item active"><a class="nav-link active" href="#">Contact</a></li>
                </ul>
              </div>
        </nav>
    </div>
    
    
    <!--VideoSection-->
    
    <div class="section">
      <div>
            <h1>Micaela <span>Butron</span></h1>
      </div>
      <hr>
      <span class="line"></span>
    
        <h2>Designer</h2>
    
    
        <div class="video-container">
            <video autoplay loop muted>
            <source src="Images/Videos/Video.mp4" type="video/mp4">
        </video>
        </div>
    </div>
    
    <!--Bootstrap Javascript Link-->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    
    </body>
    </html>
    

    CSS

         @font-face{
        src: url(fonts/KeepCalm-Medium.ttf);
        font-family: keepcalm;}
    
        @font-face{
            src: url(fonts/GOTHIC.TTF);
            font-family: centurygothic;}
    
        body {
            margin: 0px;
            padding: 0px;}
    
        hr{
          border-top-color: black;
        }
        /**Video Section**/
        .section {
            position: relative;
            text-transform: uppercase;
            width: 100%;
            height: 534px;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
          flex-direction: column;
        }
    
        .section h1 {
            text-align: center;
            justify-content: center;    
            height: 108px;
            width:342px;
            font-family: keepcalm;
            font-size:36px;
            color: #000;
            font-weight:500;
            line-height: 60px;
            transform: scale(1.189,1.189);
            letter-spacing: 15px;
            margin: 0px;
            padding: 0px;
            z-index: 1;
          display: block;
          padding-bottom: 150px;
        }
        hr{
          border-top: 1px solid red !important;  
        }
    
    
        .section span { font-size: 48px;}
    
        /* hr {
            display: block;
            z-index: 1;} */
    
        .section h2{
            width: 181px;
            height: 22px;
            font-family: centurygothic;
            color: #000;
            font-size:35px;
            font-weight: 400;
            letter-spacing: 9px;
            z-index:2;
          display: block;
        }
    
        .video-container {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;}
    
        /*Topbar Section*/
        .topbar {
            margin: 0px;
        }
        .topbar { 
            background-color: #e8e8e8;
            font-family: KeepCalm;
            font-size: 14px;
            align-items:flex-start;
            color: #000000;
            list-style: none;
            text-decoration: none;
            text-align: left;
            padding: 0px 0 0px 70px;}
    
    
        .topbar > li {
            display: inline-block;
            padding: 0 20px 0 20px;}
    
        .topbar > li > a {
            text-decoration: none;
            color: #000000;}
    
        .phone {
            width:20px;
            padding: 4px 0 0px 4px }
    
        .mail {
            height: 14px;
            }
        /**Logo Section**/
        .logo{
            float:left;
            padding-bottom: 10px;
            padding-left: 20px;}
    
        /**Logo Text**/
    
        .logotext {
            font-size: 14px;
            position: absolute;
            left: 140px;
            font-family: KeepCalm;
            float:left;
            padding-bottom: 10px;
            }
    
        .logotext span {
            font-size: 14px;
            display: block;
            line-height: 14px;
            font-family: CenturyGothic;
            font-weight: 400;
        }
    
        .logotext span span {
            font-size: 14px;
            display: inline;
            line-height: 14px;
            font-family: CenturyGothic;
            font-weight: 400;
            background:  #e8e8e8;
        }
        /*Navigation Responsive*/
        .selector-for-some-widget {
            box-sizing: content-box;}
    
        .nav-item {
            background-color: #fff;
            font-family: KeepCalm;
            font-size: 14px;
            align-items:flex-start;
            color: #000000;
            list-style: none;
            text-align: right;
            padding: 20px 0 20px 0px;}
    
        .nav-item  {
            display: inline-block;
            padding: 0 20px 0 20px;}
    
        .nav-item {
            text-decoration: none;
            color: #000000;}
    
        .navbar-light .navbar-nav .nav-item .nav-link:hover {
            color:  #e8e8e8;}
    
        /*Topbar & Navigation Responsive*/
    
        @media (max-width: 768px){
            .logo{
            width:5em;  
            padding-bottom: 1px;
            padding-left: 0px;
            }
    
            .logotext {
                font-size: 0.7em;
                line-height: 9pt;
                padding-bottom: 1px;
                padding-right: 100px;}
    
            .logotext span {
                line-height: 9pt;
                font-size: 0.8em;
                }
            .logotext span span {
                background: #FFFFFF;
                font-size: 1em;
                }
            .video-container {display: none;}
        }
    
        @media (max-width: 580px){
            .mail{width: 0px;}
            .hotmail{font-size: 0px;}
            .video-container {display: none;}
        }
    
        @media (max-width: 350px){``
            .topbar {font-size: 0px;}
            .phone{ width:0px;}
            .mail {display: 0px;}
            .logotext { font-size: 0px;}
        }
    

    【讨论】:

    • 谢谢!简直不敢相信他们之间的简单 和一些填充是什么。谢谢!
    【解决方案2】:

    尝试将此代码添加到您的 CSS 文件中。适用于 991px 及以下的屏幕宽度。

    @media (max-width: 991px){
        .section {
            flex-direction: column;
        }
        .section h1 {
            height: auto;
            margin-bottom: 30px;
            padding-bottom: 15px;
            position: relative;
        }
        .section h1:after {
            content: '';
            background-color: #FFF;
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            width: 150px;
            height: 10px;
            margin: 0 auto;
        }
        .section h2 {
            height: auto;
            width: auto;
        }
    }
    

    【讨论】:

      【解决方案3】:

      https://jsfiddle.net/kara2lucy112/4whn91b5/34/#&togetherjs=0INK0h4PoC

      
          .wrapper{
           text-align:center; 
          }
          <div class="section">
          <div class="wrapper">
            <h1>Michaela Burton</h1><h2> Designer</h2> </div>
          Section continues blah blah blah 
          </div>
      
      

      需要将 H1 和 H2 包裹在另一个 div 中,以应用居中样式。如上图

      【讨论】:

      • 我认为除非

        元素上有浮动,否则不会发生这种情况。在没有浮动设置的情况下,块级元素是否总是将其他块级元素向下推?

      • 我遵循了这些步骤,但它只是更改为:i.stack.imgur.com/yar5F.jpg 两个标题仍然彼此相邻,只是不再居中对齐。
      • 让我再看看这个。我快速浏览,我的错。
      • 好的,我知道现在问题出在哪里。您需要将标题包装在 div 中,然后对其应用组合样式。我知道这是沿着这些思路,给它们 100% 的宽度,然后将它们包装在一个 div 中,其中包含文本 align 和 justify 内容属性。很抱歉,这就是我跳枪的结果。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2023-02-25
      • 2013-06-02
      相关资源
      最近更新 更多