标题在CSS中不会跨越屏幕

时间:2016-06-27 04:00:11

标签: html css css3 flexbox navbar

我有最疯狂的时间试图在屏幕上伸展标题。我的宽度设置为100%,但左边有一个巨大的差距。

它似乎与屏幕中间的文字联系在一起,当我确实碰到完全拉伸标题的东西时(比如将我的&#34; bg&#34;类从<html>移到<body>),突然,下面的文字被猛烈撞击在左边。

我只需要在屏幕上100%标题,并将其下方的文字置于中心位置。救命啊!

&#13;
&#13;
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0b1 | 201101 
   NOTE: WORK IN PROGRESS
   USE WITH CAUTION AND TEST WITH ABANDON */

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	outline: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}

ins {
	text-decoration: none;
}
del {
	text-decoration: line-through;
}

table {
	border-collapse: collapse;
	border-spacing: 0;
}

/* MY CODE BEGINS BELOW */

.header {
	display: flex;
	width: 100%;
	height: 150px;
	background-color: black;
	opacity: 0.8;
	position: absolute;
}

.bg {
  display: flex;
  display: -webkit-flex;
  background: black url(https://images.unsplash.com/photo-1465205568425-23fdd3805e49?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=7e476dbc9096ec1c869bd2cb97d82c70) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  margin: auto;
  padding: auto;
  width: 100%;
  justify-content: center;
}

.headline {
  display: flex;
  width: 100%;
  justify-content: center;
  margin: auto;
  -ms-box-orient: horizontal;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
}

.headline p {
  margin-top: 200px;
  background-color: transparent;
  color: white;
  font-family: 'Heebo', sans-serif;
  font-size: 50px;
  width: 100%;
}
&#13;
<!DOCTYPE html>

<html class="bg" lang="en">
<head>
  <meta charset="utf-8">

  <title>Hi</title>
  <meta name="description" content="Testing">
  <meta name="author" content="Nate">

  <link rel="stylesheet" href="style.css">
  <link href="https://fonts.googleapis.com/css?family=Heebo" rel="stylesheet">

  <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>

<body>

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

<div class="headline">
	<p>Centered Text</p>
</div>

 <script src="js/scripts.js"></script>
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

由于您的标题位于绝对位置,因此您需要为left position添加规则并将其设置为0。请参阅下面的更新代码;

&#13;
&#13;
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0b1 | 201101 
   NOTE: WORK IN PROGRESS
   USE WITH CAUTION AND TEST WITH ABANDON */

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	outline: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}

ins {
	text-decoration: none;
}
del {
	text-decoration: line-through;
}

table {
	border-collapse: collapse;
	border-spacing: 0;
}





/* MY CODE BEGINS BELOW */




.header {
	display: flex;
	width: 100%;
	height: 150px;
	background-color: black;
	opacity: 0.8;
	position: absolute;
    left : 0;
}

.bg {
  display: flex;
  display: -webkit-flex;
  background: black url(https://images.unsplash.com/photo-1465205568425-23fdd3805e49?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=7e476dbc9096ec1c869bd2cb97d82c70) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  margin: auto;
  padding: auto;
  width: 100%;
  justify-content: center;
}

.headline {
  display: flex;
  width: 100%;
  justify-content: center;
  margin: auto;
  -ms-box-orient: horizontal;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -moz-flex;
  display: -webkit-flex;
}

.headline p {
  margin-top: 200px;
  background-color: transparent;
  color: white;
  font-family: 'Heebo', sans-serif;
  font-size: 50px;
  width: 100%;
}
&#13;
<!DOCTYPE html>

<html class="bg" lang="en">
<head>
  <meta charset="utf-8">

  <title>Hi</title>
  <meta name="description" content="Testing">
  <meta name="author" content="Nate">

  <link rel="stylesheet" href="style.css">
  <link href="https://fonts.googleapis.com/css?family=Heebo" rel="stylesheet">

  <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
</head>

<body>



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


<div class="headline">
	<p>Centered Text</p>
</div>

 <script src="js/scripts.js"></script>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试将left:0;添加到您的.header