Tips on centering elements vertically using Zurb Foundation

I'm trying to create a login form with the username and password fields centered in the middle of the screen using the Zurb Foundation package. Unfortunately, I'm struggling to get things aligned vertically.

This is the code I have:

<!DOCTYPE html>

<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->

    <head>
      <meta charset="utf-8" />

      <!-- Set the viewport width to device width for mobile -->
      <meta name="viewport" content="width=device-width" />
      <title>
            Registration
        </title>

         <!-- Included CSS Files (Compressed) -->
          <link rel="stylesheet" href="stylesheets/foundation.min.css">
          <link rel="stylesheet" href="stylesheets/app.css">
          <link rel="stylesheet" href="stylesheets/main.css">

          <script src="javascripts/modernizr.foundation.js"></script>

          <!-- IE Fix for HTML5 Tags -->
          <!--[if lt IE 9]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
          <![endif]-->
    </head>
        <nav class="top-bar">
         <ul>
            <li class="name"><h1><a href="#">Title</a></h1></li>
            <li class="toggle-topbar"><a href="#"></a></li>
        </ul>
        <section>
            <ul class="left">
                <li><a href="#">Link</a></li>
            </ul>
            <ul class="right">
                <li><a href="#">Link</a></li>
            </ul>
        </section>
        </nav>
    <body>
        <div class="row" id="parent">
            <div class="six columns" id="child">
                <form id = "register">
                    <input type="text">

                </form>
            </div>
        </div>



        <script src="javascripts/jquery.foundation.topbar.js"></script>
        <!-- Included JS Files (Compressed) -->
        <script src="javascripts/jquery.js"></script>
        <script src="javascripts/foundation.min.js"></script>
        <!-- Initialize JS Plugins -->
        <script src="javascripts/app.js"></script>
    </body>
</html>

Here is some CSS that I've been experimenting with but haven't been able to get working yet:

body {
    /*background-image: url('../images/gplaypattern.png');*/
    background: red;
}

#register {
    background-color:  black;
    width:80%;
}

#parent {
    position: absolute; 
    left: 50%; 
    top: 50%; 
    height: 80px;
}

#child {
    height: 75px;
    position:absolute;
    top: 50%;
    background: green;
}

Strangely, when I try to make the height of an element dynamic (e.g. height: 30%;), it doesn't seem to work. Does anyone know why?

Answer №1

UPDATE

This feature will be included as a key element (xy-center) in the upcoming release of Foundation. For more information, visit Github.

If you are using an older version of Foundation, you can utilize the CSS Tricks centering method to center the sign-in form both vertically and horizontally with minimal CSS/HTML markup.

HTML

<div class="row" >
    <div class="small-12 medium-6 columns" id="login">
        <form >
            <div class="row collapse">
                <div class="small-12 medium-5 columns">
                    <input type="text" name="username" placeholder="Username" />
                </div>
                <div class="small-12 medium-5 columns">
                    <input type="password" name="password" placeholder="Password" />
                </div>
                <div class="small-12 medium-2 small-centered medium-uncentered columns">
                  <a href="#" class="button postfix">Go</a>
                </div>
            </div>
        </form>
    </div>
</div> 

CSS

#login {
    position: absolute;
    left: 50%;
    top: 50%;

    /*
    *  Centering with CSS Tricks method
    */
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
}

Result

The sign-in form will be perfectly centered both vertically and horizontally on any screen size. See a screenshot of the outcome below.

View the functioning jsFiddle

Answer №2

When it comes to vertical alignment, Foundation doesn't quite cut it. You can try to achieve it with vertical padding, but there's no perfect solution for dynamic content.

Check out this jsFiddle (not my creation) demonstrating how it should work using the following CSS:

div {
display: table-cell;
vertical-align: middle;
height: 50px;
border: 1px solid red;
}

Here's the link to the jsFiddle: http://jsfiddle.net/53ALd/6/ - just keep in mind that it may not integrate seamlessly with the rest of the Foundation CSS.

Answer №3

One issue that many encounter is the lack of efficient vertical centering support in HTML/CSS. Zurb's Foundation won't have a significant impact on this particular issue.

If you're seeking additional guidance on this topic, check out a related question here: Centering HTML div vertically and horizontally

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

Ways to incorporate style links in Angular v2 components?

Currently, I am working through the Angular tutorial available on their website. In my quest to create various components, templates, and styles, I have hit a roadblock. The issue lies in incorporating my styles into the components using the 'styleUR ...

Stop the selection of text within rt tags (furigana)

I love incorporating ruby annotation to include furigana above Japanese characters: <ruby><rb>漢</rb><rt>かん</rt></ruby><ruby><rb>字</rb><rt>じ</rt></ruby> However, when attemp ...

How can I modify the HTML code for rooms in the HTML 5 client of the bigbluebutton application?

Is it possible to customize the HTML code of rooms within the BigBlueButton source code? Additionally, how can I modify the CSS styles of elements in room sessions? For instance, I am interested in hiding the logo of BigBlueButton by setting 'display: ...

Tips for creating a margin on an HTML button's background color

My bootstrap button has dimensions of 46 x 96px and a border radius of 22px. When hovered, I would like the background color to only extend up to 40 x 90px, without filling the entire button. I understand this can be achieved using a box-shadow, but since ...

Bulma: Tips for creating equally sized buttons?

I am working with Bulma CSS and trying to ensure that all my buttons are the same size. Currently, each button appears to have a different size based on the text content. I have looked into using the "is-fullwidth" option, but it makes the buttons too la ...

What is the best way to share CSS styles between React and React Native?

Is it recommended to use inline CSS styles in a React / Next.js app to maintain code consistency across projects? For example, like this: import {StyleSheet, Dimensions} from 'react-native'; const vw = Dimensions.get('window').width / ...

Modify the color of the background for a flex-wrap list

I am facing a situation where I have a list with an unknown number of items, but I need to control the height of its container. To achieve this, I am using flex-flow: wrap column to split the list. The list starts off hidden and is displayed using jQuery&a ...

How to centrally align a component in React (both horizontally and vertically)

I am currently implementing React with Bootstrap in my project. Here is the render function I have been working on, with the goal of center aligning the form. render() { return ( <div class="row"> <div className="col-md-3 ...

The Fullpage.js embedded in an iframe is experiencing difficulty scrolling on iOS mobile devices

Trying to use Fullpage.js for a website with unique sections on different domains using full-screen iframes. However, encountering issues with scrolling on IOS mobile devices. The first solution rendered the page completely unscrollable: <iframe src=" ...

Leveraging the power of CSS calc() in combination with TailwindCSS styles and JavaScript

Can you explain why applying styles with TailwindCSS v3 to calculations using JS variables does not work? I'm working on a project using VueJS, and I've noticed that styling works fine when using calc as a string like this: ... <div class=&qu ...

Creating a responsive card layout in Bootstrap 4 that utilizes the vertical space of the viewport with a scrollbar

We are working on a page that has specific requirements: The page should not have a scroll bar. The card must expand vertically to fill the remaining space, even if there is no content in the card-body. We need a scroll bar for the card-body only when the ...

showing sections that collapse next to each other

I am currently designing a portfolio website using HTML, CSS, and vanilla JavaScript. I have implemented collapsing sections that expand when clicked on. However, the buttons for these sections are stacked vertically and I want to place them side by side. ...

Tips for maintaining the height of the outer div consistent with that of the inner div

I am encountering an issue with an inner div that is used for overlay and set to a min-height property. Despite changes in the overlay's height, the height of the outer div remains unaffected. #outer { width: 70px; height: 70px; background-co ...

CSS animation for horizontal scrolling with sticky positioning is not functioning as intended

I've designed a creative way to achieve infinite horizontal scrolling using CSS animation, and my goal is to make the first element in the list stick in place. Everything works smoothly when I utilize the left property within the @keyframes. However, ...

Concealing a block from top to bottom

I currently have a filter that is hidden when clicked, with a transition effect that moves it from the bottom to the top. I have recorded my screen to demonstrate this behavior: . However, I now need the filter to hide from top to bottom instead, essenti ...

When selecting a new tab in HTML, the current page position remains unchanged. However, I would like the page to automatically scroll to the

In the design of my website, I have incorporated buttons that are linked to various pages using navigation tabs. However, when these buttons are clicked, the view maintains its position on the new page (e.g., halfway scrolled through the page). Instead o ...

Tips for applying horizontal padding to the container-fluid class in Bootstrap 5

I'm struggling with the CSS code I wrote: .container-fluid{padding 3% 15%;} It's only adjusting the vertical padding to 3%, and there's no change in horizontal padding. #title { background-color: #ff4c68; } .container-fluid { pad ...

An oversized image creates additional room

I currently have a board that consists of 3 blocks, with each block containing a grid of 25 squares. These squares are 40x40px in size with a margin around them, resulting in an overall size of 220px x 220px. Each square also has some space above and below ...

When I try to call jQuery's getScript function, the callback does not execute as expected within

When I invoke the script in a function at runtime function CheckOutMyFace() { $.getScript("/scripts/jtimers.js", function (data) { }); }; I notice in Firebug that the script is being called every time the function is invoked, but for some reason the ...

CSS Grid: Dividing items equally based on the number of items present

Currently, I am delving into grid layouts in CSS and experimenting with code to divide a row. Here is the snippet I am playing around with: .grid-sample { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; row-gap: 4rem; padding: 0 5rem ...