简体   繁体   中英

Can't Get Text Responsive & Centered Over Background Image Section

Hi want my page to have a nav bar, and then below it have a header with a background image and then below that the main content. The issue I'm having is when I put my text over the background image in the header section, I cant configure it properly to be responsive. Fiddle

<style>
  header {
  background:
    linear-gradient(
      rgba(0, 0, 0, 0.5),
      rgba(0, 0, 0, 0.5)
    ),
    url('http://getwallpapers.com/wallpaper/full/6/3/b/920866-download-free-beautiful-background-pics-1920x1200-for-ios.jpg');
  background-size: cover;
  height: 75vh;
}

</style>

<header>
 <div class="topnav">
   <a class="active" href="#home">Home</a>
   <a href="#news">News</a>
   <a href="#contact">Contact</a>
   <a href="#about">About</a>
</div>

<div class='headertext'>
  <h1> Trying To Get This Text Responsive </h1> 
</div>

</header>

<main>
 Main Content Goes Here 
</main>

style.css

/* Add a black background color to the top navigation */
.topnav {
    background-color: #333;
    overflow: hidden;
}

/* Style the links inside the navigation bar */
.topnav a {
    float: left;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
    background-color: #ddd;
    color: black;
}

/* Add a color to the active/current link */
.topnav a.active {
    background-color: #4CAF50;
    color: white;
}

h1 aligns left by default. Try:

.headertext h1{
   text-align:center;
}

to make it centered

Have you tried using <center> tags? It's obsolete but may work. You may want to have a look. as for things being responsive, try using " onclick=func() " and making a script for it or " <a href=func()>Responsive text</a> ". Hope this helps, any questions or extra things you may need feel free to ask.

Adding this CSS should solve the problem:

header {
  position: relative;
}
.headertext {
  top: 46px;
  height: calc(100% - 46px);
  width: 100%;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
}

Fiddle: https://jsfiddle.net/1fk4pxv5/

(Also note the edited HTML in the fiddle code, around the .headertext DIV, it included one h1 too much.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM