Center the elements in the header div, including the text and logo, in a vertical alignment

I struggle with CSS and often get stuck, even on simple layout questions. I'm currently reading a book but still can't figure out how to design a website with a header at the top, followed by a menu bar and content. However, I want the header to have text on the left and a logo on the right.

My approach so far:

<div id="headline">
<div id="headertext">Some title<br/>some more title text</div>
<div id="logo"><a href="http://www.example.com/~somelink"><img src="somelogo.png" /></a></div>
</div>

CSS code:

  #headline { overflow: hidden;
            height: 224px;
            text-align: left;
            padding: 0px 80px 0px 80px;
          }

  #headertext { font-family: "Trebuchet MS", Helvetica, sans-serif;
          font-size: 20pt;
          color: #000000;
          float: left;
          font-weight: bold;
        }
  #logo    { 
          float: right;
        }

I've successfully floated the text and logo to the left and right sides, respectively. Now, I want to vertically align both elements in the center of the parent <div> with a specific height.

Desired outcome (blue rectangle represents the logo):

I've tried using vertical-align: middle, but it hasn't worked. I also experimented with display:table-cell and display:inline, but they didn't yield the desired result. Should I add another wrapping <div> inside the headline element?

Edit: thanks for the fiddle suggestion; here's my attempt: http://jsfiddle.net/f5vpakdv/

Appreciate your assistance!

Answer №1

You can achieve a centered layout using display: table and display: table-cell, along with vertical-align: middle.

I have simplified your original CSS to highlight the key differences.

If you want your layout to adjust perfectly even after adding padding or margin, refer to this link: Box Sizing | CSS-Tricks.

<div id="headline">
    <div id="headertext">
        Some title<br/>some more title text
    </div>
    <div id="logo">
        <div id="fakeImg"></div>
    </div>
</div>

...

#headline {
    width: 100%;
    height: 224px;
    background: yellow;
    display: table;
}
#headertext {
    text-align: left;
}
#headertext,
#logo {
    display: table-cell;
    width: 50%;
    vertical-align: middle;

}
#fakeImg {
    width: 100px;
    height: 100px;    
    background: blue;
    float: right;
}

View Demo

Answer №2

To achieve this, you can utilize CSS along with vendor-specific transforms for better results.

.vertical-center {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

For a working example, I have created a fiddle and included an additional div wrapper.

http://jsfiddle.net/5o3xmfxn/

You can view the updated version of your fiddle here:

http://jsfiddle.net/f5vpakdv/1/

Answer №3

Your fiddle has been enhanced here. I included the property display:table; to the outer div and applied the styles

display:table-cell;
vertical-align:middle;
to both inner divs.

Additionally, I created another version using flexbox which can be found here.

To achieve this layout, I added the following CSS properties to the parent div:

display:flex;
align-items:center;
justify-content:space-between;

Answer №4

For an easier solution, consider wrapping the content you want to center in a wrapper and using a margin-top. Here is an example: http://jsfiddle.net/f5vpakdv/2/

<div id="headline">
    <div id="wrapper">
        <div id="headertext">Some title some
            <br/>more title text</div>
        <div id="logo"><a href="http://www.example.com/~somelink"><img src="somelogo.png" width="198px" height="120px" /></a>
        </div>
    </div>
</div>

CSS

 #wrapper {
      margin-top: 60px;
  }

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

Extracting Table(Array) Index into a Component in Angular

For my data on the HTML page, I created a 2-dimensional array. <tbody> <tr *ngFor="let i of myarray2"> <td *ngFor="let j of i"> {{j}} </td> </tr> </tbody> This is how it appears: https://i.sstatic.ne ...

Div Boxes at the Center

I've searched extensively for a solution to my unique problem, but haven't been able to find one that fits. I have 4 div boxes with a max width of 50% and a min width of 400px. When the site is viewed on a smaller screen, I want the boxes to alig ...

Combining four numbers to calculate a total with the click of a button

I am currently attempting to calculate the sum of 4 entries in 4 separate text fields, and everything appears to be working correctly except that when I click the button, the sum is not being set. For example, if I enter the number 1 in each text input, th ...

Display a DIV element containing a button using Jquery

Can anyone guide me on how to use Jquery to load a specific DIV onto the page only when called by a button or event? I've attempted several functions without success, making me question if it's achievable at all. Being new to Jquery, I believe my ...

Effective methods for increasing the height of a column div upon hovering, while also maintaining the position of surrounding columns

Struggling to make this work for 48 hours with no success. Here's the challenge: there is a standard row with regular columns that have an additional product class: <div class="row"> <div class="col-lg-4 product"> // Content ...

A guide to programmatically downloading a file with JavaScript on Internet Explorer

I'm currently facing a challenge with my web page. There is a button that, when clicked, should generate a CSV file (by converting from JSON) for the user to download. I've implemented the logic based on this jsfiddle, and it works perfectly in C ...

Selenium is having trouble reading the text from a dropdown that has the autocomplete feature disabled

It seems that Selenium is having trouble retrieving the text from the dropdown options. Here is a snippet of the code I am working on: WebElement dropdown=driver.findElement(By.id("selFromAccount")); List<WebElement> dropoptions=dropdown.findElemen ...

Is it possible for ColdFusion to transmit HTML5 Server-Sent Events using utf-8 encoding?

When using <cfcontent type="text/event-stream"> in ColdFusion, I encountered an issue where non-English characters were displayed as question marks: ???????123123???? ???. Interestingly, the rest of the page was able to display non-English characters ...

What is causing iframes to fail on some websites?

Recently, while I was experimenting with a jQuery tutorial, I encountered an issue where iframes were not working as expected. Surprisingly, only the iframes from w3schools seemed to work fine, leaving me confused about what could be causing the problem. & ...

The button text in Bootstrap 5 is black instead of white as shown in their demo

During the installation of Bootstrap 5, I encountered an issue where many of my buttons are displaying a black font instead of the expected white font as shown in the Bootstrap 5 Documentation For instance, the .btn-primary button on the official Bootstra ...

Issue encountered while trying to interpret Bootstrap 4 within Netbeans 8.2

Currently, I am working on an HTML5/JS application using Node.js in Netbeans 8.2. As I develop the welcome page, I intend to incorporate Bootstrap 4. However, when I attempt to add bootstrap.min.css to my stylesheets folder, I encounter an error as shown i ...

How to override an event in JavaScript when a specific value is entered

I am looking to implement a system with complex conditions. The goal is to have an alert appear when a value is inputted and the button is clicked. First, a confirmation message Have you input ? should be displayed, followed by clicked with input. If no va ...

How can I use CSS to place a div at the bottom of its container?

I am trying to figure out how to use CSS to position a div at the bottom of its container, but the size of the container is not fixed. Can anyone provide some guidance on this issue? Thank you in advance for helping me solve this problem. You can check o ...

Preventing a draggable item from being dropped into a sortable container when the maximum count has been reached

Check out the following code snippet: <link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.0/jquer ...

I'm curious about why I'm receiving the error "Unable to bind to 'ngFor' since it is not recognized as a property of 'li'. Can someone please explain why this is happening?

The issue is related to the *ngFor directive for nonvegfoodlist app.component.ts import { Component } from '@angular/core'; export class Menu { id : number; name :string; } const veg : Menu[] = [ { id:1 , name:'Rice'}, { id: ...

Generating an Array using Jquery

I am working with two functions. The first function takes the input from users in a text box, appends it to a div, and then displays it on the HTML page. The second function is supposed to change the styling of the word entered by the user, alternating the ...

the toggle feature is not functioning properly on smartphones and tablets

I have embarked on the journey of creating a responsive website. The @media tag is new territory for me, and I'm unsure if I've implemented it correctly. My goal is to have a slidetoggle menu for the nav when the window size is less than 550px. ...

Guide to configuring a bootstrap checkbox using the razor CheckBoxFor() method

Struggling to implement Bootstrap styled checkboxes in my ASP .NET MVC form. Despite hours of searching for a solution, I have yet to find one that works for me. Below is my HTML razor code, but the checkbox is not displaying. <div cl ...

Tap on the Dropdown Menu to access the Responsive design feature

Currently, I am working on perfecting a responsive navigation menu. My goal is to have the drop-down menu appear when the menu icon is clicked, as opposed to the hover effect. I am utilizing a Google jQuery script, but I am unsure if the drop-down functi ...

Ways to navigate through a webpage without encountering any overflow issues

My window is too small to scroll, but I still need the ability to do so. Is it possible to scroll even when the height of the container is not large enough to display the scrollbar? Below is the code I am using to achieve scrolling: setTimeout(function() ...