Aligning text at the center of a hero div

Why isn't the text vertically centered in the div with this code? I specified the positions as 50%, so I don't understand. Also, is it possible to give a custom height for the image?

Ignore the blue line at the top of my picture, it's just the navbar.

<section class="bgimage-fooldal">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hero-text-fooldal">
        <h2 class="hero_title">Hello, world! Full width Hero-unit header</h2>
        <p class="hero_desc">This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique. You can gather online information about your customers and suppliers, thus staying informed.</p>
        <p><a href="#" class="btn btn-large hero_to_link">Request a Quote »</a></p>
      </div>
    </div>
  </div>
</section>


/*************************** Custom Hero CSS ****************************/
.bgimage-fooldal {
  width:100%;
  height:400px;
  background: url('../images/assets/teszt-1.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size:cover;
  background-attachment: fixed;
}
.hero-text-fooldal {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

Answer №1

To ensure the vertical centering of the bgimage-fooldal class, you must add position:relative to it according to the height of your div.

.bgimage-fooldal {
  width:100%;
  height:400px;
  background: url('https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png');
  background-repeat: no-repeat;
  background-position: center;
  background-size:cover;
  background-attachment: fixed;
  position:relative;
}
.hero-text-fooldal {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

}
<section class="bgimage-fooldal">
  <div class="container-fluid">
    <div class="row">
      <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 hero-text-fooldal">
        <h2 class="hero_title">Hello, world! Full width Hero-unit header</h2>
        <p class="hero_desc">This is a template for a simple marketing or informational website. It includes a large callout called the hero unit and three supporting pieces of content. Use it as a starting point to create something more unique. Vevőidről, szállítóidról online információt nyerhetsz, így értesülhetsz arról.</p>
        <p><a href="#" class="btn btn-large hero_to_link">Árajánlat kérése »</a></p>
      </div>
    </div>
  </div>
</section>

Answer №2

To achieve the desired layout, Flexbox can be utilized. Here is a sample implementation:

.hero-text-fooldal {
  width:100%;
  height:400px;
  background: url('../images/assets/teszt-1.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size:cover;
  background-attachment: fixed;
  display: flex;
  justify-content: center;
  align-items: center;
}

Kindly remove the styles associated with .hero-text-fooldal

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Disappearing CSS Box Shadows

I'm attempting to replicate effect number 2 from a website I found, but I'm running into an issue. Whenever I add more HTML content to the page, the effect disappears. Here is a sample of what I have so far: http://jsfiddle.net/aHrB7/ Here is a ...

The text/font-weight of the header button is mysteriously shifting without warning

While creating a header for my webpage, I encountered an issue where the text family was changing when the dropdown menu was launched in Safari 8. Curiously, this behavior did not occur when using Chrome to launch the jQuery function. Even after attempting ...

When should the preloader be placed within the DOMContentLoaded or load event?

I'm looking to implement a preloader on my website to ensure everything is fully loaded - images, JavaScript, fonts, etc. However, I'm unsure whether to use the following: window.addEventListener('DOMContentLoaded', () => { // code ...

Broadcasting real-time video from a webcam on an HTML website that is accessible locally through a Raspberry Pi device

My goal is to stream live video from a USB camera connected to my Raspberry Pi device onto a simple HTML site that is only visible on localhost. The site will be hosted on the Raspberry Pi itself and will only need to display the video streamed by the Ra ...

Design a cascading navigation menu using CSS for enhanced user experience

Looking to enhance the W3Schools menu with a multi-level dropdown? Check out the original W3Schools menu for reference here. I've attempted to create a multi-level dropdown as shown in these images: Multi Level Dropdown Photo 1 Multi Level Dropdown Ph ...

What is the best way to display HTML code that is stored as a string?

Hey there! I'm facing an issue with sending a string from MongoDB through res.render. When I check the sent string, it appears as just plain text instead of HTML, so I'm looking for a solution to fix this. router.get("/:id", async function (req, ...

What is the method for customizing the color of the drop-down arrow in

Is it possible to customize the color of the dropdown arrow while maintaining the same style? #cars{ width:150px; } <!DOCTYPE html> <html> <body> <select name="cars" id="cars"> <option value="volvo">Volvo</option& ...

Photographs housed within a pop-up window

My popover is causing me some trouble: I've got a popover that displays PHP-generated content without a fixed height, and I want to add an image inside it. The tooltip appears over my element like this: The problem is that if the image isn't pr ...

Aligning cards

Struggling to align these cards perfectly at center: Despite thorough research on both Google and Stack Overflow, the solution continues to elude me. I've experimented with various methods such as display: flex, justify-content: center, and align-ite ...

Looking to Move the Image Up?

I've been experimenting with creating a grid layout where the bottom image will move up until it is 10px away from the image directly above it. However, no matter what I attempt, the spacing seems to be based on the tallest image rather than the one a ...

"Can we have selection options or drop-down menus that are in line with today

Currently working on a website project for a client where I, as a designer learning to code, have integrated three select options. This is how it appears: The client's new requirement is that the arrival date should automatically display as the curr ...

The box shadow does not envelop the entire block uniformly

I have been struggling to evenly cover the shadows around all the blocks using CSS. Unfortunately, after working on it for 2 days, I still haven't been able to find a solution. The middle line seems to be misaligned and thicker compared to the others. ...

Tips for showcasing the output information in react framework

I'm currently developing a project that involves using rabbitMQ and react. After successfully connecting my rabbitMQ server to my react app, I was able to retrieve data from the server. Although I can output this data to the console using console.lo ...

Tips on making scrollable content within a static div element

I am currently working on designing a shopping cart layout for a website that allows users to order food from a restaurant. I want the shopping cart to remain fixed on the screen, but be scrollable when there is an overflow of items being added. This is a ...

Implementing the 'bootstrap tour' feature in a Ruby on Rails application

I have integrated bootstrap-tour.min.css and bootstrap-tour.min.js into my project. <script type="text/javascript" src='bootstrap-tour.min.js'></script> <link rel="stylesheet" type="text/css" href="bootstrap-tour.min.css"> Her ...

In-depth preg_match_all analysis

I'm encountering an issue with my detailed preg_match_all not working as expected. Instead of retrieving the desired data, I am getting a blank Array. Below is the code snippet I'm struggling with: <?php $remote_search = file_get_content ...

Searching on Google for the Twitter Bootstrap documentation using the Google search

Seeking advice on creating a basic index page with a Google-style centered search bar using Twitter Bootstrap. Struggling to achieve desired aesthetics. Need help adding a search icon button to the right side of the search field. Utilized typeahead for fu ...

JavaScript text spacing

I'm currently utilizing the JavaScript code snippet below to clear out the existing content within a div element in order to insert new elements. However, I've encountered an issue where once the div is cleared, its CSS styling is lost. I am atte ...

How can I retrieve the position of a div or an image within a div (specifically the top and left coordinates) and incorporate these values into CSS?

I'm currently working on a website that is dynamic and utilizes bootstrap. I am looking to incorporate an animation where the dynamic thumbnails in the col-md-4 div zoom to 100% when clicked, appearing at the center of the container. I am struggling ...

Safari experiencing a problem with the CSS mix-blend-mode: difference

I have been experimenting with a mouseover effect using mix-blend-mode. The effect is functioning as desired in Chrome and Firefox. .btn-anim { position: relative; display: inline-block; overflow: hidden; padding: 1.25rem; color: #fff; --x: 6 ...