Achieving perfect alignment for CSS heading and floated controls in the center of the page

I seem to encounter situations where I need inline-block elements on opposite ends of the same "line" while also needing them to be vertically aligned.

Take a look at this example: http://jsfiddle.net/96DJv/4/ (focus on aligning the buttons with the heading)

This is the code snippet:

<div class="people">
    <div class="head">
        <div class="controls">
            <button>Delete</button>
            <select>
                <option>Some Option</option>
            </select>
            <!-- other inline-block elements -->
        </div>
        <h1 class="title">Title</h1>        
    </div>
    <table class="list">
        <tr>
            <th>Name</th><th>Age</th><th>Score</th>
        </tr>
        <tr>
            <td>John</td><td>14</td><td>200</td>            
        </tr>
        <tr>
            <td>Jack</td><td>23</td><td>2100</td>
        </tr>
    </table>
</div>

The styling:

.people {
    width: 400px;
}

.list {
    width: 100%;
}

.list th {
    text-align: left;
}

.title {
    overflow:hidden;
}

.controls {
    float: right;    
}

.head {
    background-color: #F1F1F1;
}

I've heard that floating an element removes the ability to adjust its vertical alignment. Is using position: absolute or tables my only options? Is there another way to achieve this?

I've been searching for a solution tirelessly but haven't found one yet.

Answer №1

Hey there,

I've devised a solution that involves minimal alterations to the HTML code and some tweaks to the CSS styling.

HTML

<div class="users">
<div class="header">
    <h2 class="heading">Heading</h2> <-- Moved H2 tag above the settings div
    <div class="settings">
        <button>Remove</button>
        <select>
            <option>Another Option</option>
        </select>
    </div>                  
</div>
<table class="data">
    <tr>
        <th>Name</th><th>Age</th><th>Grade</td>
    </tr>
    <tr>
        <td>Alice</td><td>22</td><td>A+</td>            
    </tr>
    <tr>
        <td>Bob</td><td>31</td><td>B-</td>
    </tr>
</table>

CSS

 .heading {   
      display: table-cell;
    }

.settings {
  display: table-cell;    
  vertical-align: middle;
  text-align: right;
}

.header {
    display: table;
    width: 100%;
    padding: 10px;
    background-color: #EFEFEF;
}

Check out this snippet http://jsfiddle.net/4yPtK/

UPDATE 1: For those who prefer not using table display in the markup, here's an alternative approach:

To center the controls, ensure its line-height matches the font size + line height of the title.

.heading {
    overflow:hidden;
    font-size: 1.8em;
    line-height: 1.8em;
}

.settings {
    float: right; 
    line-height: 3.6em;
    vertical-align: middle;    
}

Answer №2

To vertically center the content of your head class, simply apply display: flex and add align-items: center.

Here is a link to an example on how to achieve this: http://jsfiddle.net/96DJv/5/

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

What is the best way to include default text in my HTML input text field?

Is it possible to include uneditable default text within an HTML input field? https://i.stack.imgur.com/eklro.png Below is the current snippet of my HTML code: <input type="text" class="form-control input-sm" name="guardian_officeno" placeholder="Off ...

Simple HTML files on a local server are having trouble loading images and JavaScript, yet the CSS is loading

Having worked in web design for quite some time, I recently began working on a basic Angular app that is meant to be very simple. For this project, I am only using the angular files and a single html file as the foundation. As I started setting up the bas ...

Create a border that does not inherit the opacity of the object

Check out this jsBin for reference: http://jsbin.com/uyonux/1 The hover state is working fine, but the focus state is not. I want the blue color to remain solid #13A3F7 without inheriting the opacity of .4. Is there a way to achieve this without the elem ...

None of the angular directives are functioning properly in this code. The function attached to the submit button is not executing as expected

I've experimented with various Angular directives in this code, but none seem to be functioning properly. I'm wondering if a library file is missing or if there's some issue within the code itself, potentially related to the jQuery file. The ...

Tips for customizing the appearance of date circles and text in Vue V-Calendar.io

On v-calendar.io, there is a demo showcasing the correct display where the date "2018-01-01" has a red background and dark text: However, my display does not match that. I have included Vue.js and v-calendar through CDN, and the page is formatted in HTML ...

When the LI element is clicked, it triggers the display of another LI element without affecting the rest of the

New to the web development scene and trying to figure out how to create a collapsible text feature using HTML, JavaScript, and JQuery. I've got the styling down but struggling with the scripting part. Here's an example of what I'm trying to ...

Error in Layout of Navigation Panel and Tabbed Pages

Working on a school project, I encountered a challenge. I found two useful solutions from the W3 website - a sticky navigation bar and the ability to include tabs on a single page for a cleaner presentation of information. However, when trying to implement ...

Could the problem with inset box-shadow and large border-radius on one side be related to Chrome?

Have you noticed a discrepancy in the inset box-shadow behavior with large border-radius on one side, particularly in Chrome? I'm curious about which browser is showing accurate results. Here's how it appears in Chrome: https://i.stack.imgur. ...

Safari enables seamless text flow into the footer without triggering scrolling as long as the length is manageable

Encountering a unique issue solely in Safari 6.0.5 on OS X 10.8.4 within certain conditions, which doesn't exist when tested on Firefox or Chrome under various scenarios in OS X. The column layout with floats is the crux of the problem. When the text ...

Modifying the Color of Individual Items within the Pagination Component in MUI

I am attempting to modify the background color of every item in my Pagination component by utilizing makeStyles. import { Pagination } from "@material-ui/lab"; import { makeStyles } from "@material-ui/core"; const pagination = makeStyl ...

CSS margin-left causing issues in Chrome Extension

My current situation is incredibly puzzling, as I am at a loss for what is happening. I developed a Firefox add-on using jQuery and CSS to revamp a website. When attempting to transfer the add-on to Chrome (which was relatively straightforward due to the s ...

Using a personalized font in CSS isn't functioning correctly and is displaying Times New Roman instead

I've been struggling to include a custom font in my project, but it's not working as expected. Here's how I added it to the .css file: @font-face { font-family: 'Open Sans'; src: url('/open-sans.eot'); src: u ...

Encountering obstacles as a novice trying to master CSS styling techniques

I'm having trouble aligning buttons at the center top of the screen. Despite my efforts, I can't seem to get them perfectly aligned https://i.stack.imgur.com/j7Bea.png button { width: 13%; height: 50px; display: inline-block; fo ...

Is it possible to dynamically render css using Express.js?

I have a need to dynamically generate CSS with each request. For example, in my Express.js application, I've set up a route like this: /clients/:clientId/style.css When a matching request is received, I want to retrieve the Client from the reposito ...

Prevent elements from overlapping within a flexbox container

I've encountered an issue with resizing the window causing the logos to overlap the portrait image. Is there a way to fix the logos relative to the portrait? Below is the code: /* Defaults for Main Part of Pages */ body, div, h1, p { margin: 0; ...

Showing a div element with the power of JavaScript

I want to enhance the accessibility of my website for users who do not have JavaScript enabled. Content that will be visible if the user has JavaScript enabled. Content visible when JavaScript is disabled. By default, DisableJS is set to Display:none; ...

Issues with the parallax zooming out effect

Delving into the world of parallax effects, I'm on a quest to achieve a captivating zoom-out effect while scrolling. However, I'm encountering an issue where the image shrinks beyond the browser width and the div's height. In the example pr ...

Make HTML design responsive to any screen size

After completing the design of a mobile app interface with dimensions 750w by 1334H, I encountered an issue where it appears too large on the app testing screen. I am seeking a way to automatically adjust the design to fit all screens without compromising ...

Is it possible to create a mouse-following effect with lighting using Javascript

Recently, I've been honing my Javascript skills and decided to create a follow-mouse function. After successfully implementing it, I started brainstorming a new concept that I'm unsure is achievable. Is there a way to develop an "orb of vision" ...

Tips for keeping images stationary while expanding on hover

Currently, I am in the process of developing my first website. My main goal is to create an image gallery that displays thumbnails of images and expands them when you hover over each one. Initially, I adjusted the sizes of the images to 100px*100px using H ...