What is the best way to center this div vertically?

My code is structured like this: http://jsfiddle.net/LtzhN/

I want to center the blue button vertically within the container, but the container does not have a fixed height.

Is there a way to achieve this using only CSS?

I am aware that I could use jQuery, something like:

var halfOuterHeight = $('.jbe-result').height()/2,
    halfButtonHeight = $('.jbe-run').height()/2;

$('.jbe-run').css('margin-top', halfOuterHeight - halfButtonHeight)

However, I prefer to avoid using jQuery for this task!

Answer №1

The special container is not necessary for the button, just use the CSS (the top margin should be half the height plus the vertical padding of the button).

Check out the jsFiddle Demo

.jbe-run {
    margin-top: -11px;
    padding:4px 10px;
    width:70px;
    color:#ffffff;
    font-size:12px;
    border-radius:4px;
    -webkit-border-radius:4px;
    text-align: center;
    position: absolute;
    top: 50%;
    right: 10px;
}

Answer №2

I have made some updates to your fiddle:

Check out the updated JSFiddle Demo here

.jbe-result{
    ...
    display:table;
}
.jbe-run-container{
    display:table-cell;
    vertical-align:middle;
    border-left:1px solid silver;
    width: 30%;
}

Avoiding the use of float can prevent any unintended consequences in your layout.

Answer №3

There is some work to be done on this.

To improve the structure, I added a surrounding container for text and heading.

<div class="jbe-result">
    <div class="jbe-whatever">
    <h3>text goes here which may be lengthy, increasing the height of the div</h3>
    <p>Date saved: 13-09-2013, 12:14 am</p>
        </div>
    <div class="jbe-run-container">
        <div class="jbe-run">Run</div>
    </div>


</div>

In the CSS code, I set both jbe-whatever and jbe-run-container to display as table-cell:

.jbe-whatever {
 display: table-cell;
}

.jbe-run-container{
    width:32%;
    margin-left:2%;
    padding-left:2%;
    border-left:1px solid #c9cfdd;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

The button should have an inline-block display and unnecessary properties removed:

.jbe-run{
    padding:4px 10px;
    width:70px;
    color:#ffffff;
    font-size:12px;
    border-radius:4px;
    -webkit-border-radius:4px;
    display: inline-block;
}

UPDATE: http://jsfiddle.net/LtzhN/5/ Set the jbe-result display property to table instead of table-row.

Cleaned up version: http://jsfiddle.net/LtzhN/8/

Answer №4

To resolve this issue, some adjustments need to be made. One solution is to enclose the text within a div container named jbe-content-container on the same level as the jbe-run-container. The next step involves removing the float: right property from the jbe-run class, rearranging the structure so that the 'jbe-content-container' precedes the jbe-run container, and applying display: inline-block with vertical-align: middle. Additionally, margin and padding may need to be adjusted for proper alignment.

HTML

<div class="jbe-result">
    <div class="jbe-content-container">        
        <h3>small amount of text</h3>
        <p>Date saved: 13-09-2013, 12:14 am</p>
    </div>
    <div class="jbe-run-container">
        <div class="jbe-run">Run</div>
    </div>
</div>

<div class="jbe-result">
    <div class="jbe-content-container">
        <h3>text goes here which could be quite long winded which will push the height of the div higher</h3>
        <p>Date saved: 13-09-2013, 12:14 am</p>
    </div>
    <div class="jbe-run-container">
        <div class="jbe-run">Run</div>
    </div>
</div>

CSS

.jbe-result{
    float: left;
    width: 96%;
    padding: 5px 2%;
    border-radius: 5px;
    border: 1px solid #c9cfdd;
    background-color: #ffffff;
    margin-bottom: 6px;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    -webkit-transition-duration: .25s;
    transition-duration: .25s;
    opacity:1;
    height:auto;
    position: relative;
    display: table-row;
}

.jbe-content-container {
    width: 64%;
    border-right:1px solid #c9cfdd;
    padding-right:2%;
    margin-right:1%;
}

.jbe-content-container,
.jbe-run-container {
    display: inline-block;
    vertical-align: middle;
    zoom: 1;
}

.jbe-result h3{
    font-size: 14px;
    color: #003777;
    margin: 0 0 2px 0;
}

.jbe-result p{
    color: #888888;
    font-size: 12px;
    margin: 0;
}

.jbe-run{
    margin:0;
    float:right;
    padding:4px 10px;
    width:70px;
    color:#ffffff;
    font-size:12px;
    border-radius:4px;
    -webkit-border-radius:4px;
    text-align: center;
}

.jbe-run-container{
    width:32%;
}

.jbe-run, .jbe-edit, .add-div{
    background-image: linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -o-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -moz-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -webkit-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -ms-linear-gradient(bottom, #003777 52%, #005DA4 85%);
    background-image: -webkit-gradient(
        linear,
        left bottom,
        left top,
        color-stop(0.52, #003777),
        color-stop(0.85, #005DA4)
    );
}

Explore this jsFiddle for visualization

Answer №5

.custom-jbe-run{
    margin-top: -15px;
    padding:6px 12px;
    width:75px;
    color:#ffffff;
    font-size:14px;
    border-radius:5px;
    -webkit-border-radius:5px;
    text-align: center;
    top: 50%;
    float: right;
    position: relative;
}

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

A PHP script or regular expression that swaps the positions of characters within a string

(Update1: per suggestion by George Cummins) I need assistance in creating a regex search and replace or a php function to switch positions of characters within a string. (since I couldn't figure it out on my own) For example, if I have the following C ...

The phenomenon of an invisible Absolute or relative position leading to grid components overlapping in Next.js

I've been struggling with this issue for over 48 hours now. I've attempted to troubleshoot by commenting out everything except the affected components and even swapping entire components around, but the problem persists. Oddly enough, rearranging ...

Strategies for creating a grid-inspired layout: tips and tricks

Recently, I've been tasked with creating a catalogue section for our e-commerce site. My designer came up with this layout which I find really appealing, but I'm having trouble figuring out how to implement it. I attempted using tables, but it di ...

Tips for keeping several buttons in the spotlight: HTML/CSS/JS

Looking for a solution to keep multiple buttons toggled on? I'm not very familiar with JavaScript, but I know that adding a class on click won't work if there's already a class set. Maybe giving the element an ID could be a possible solution ...

Can you demonstrate how to assign a CSS class to a datalist?

I have run into an issue where I can't seem to control the styling of the items on my datalist. The external styling does not provide any vertical spacing between each item. Is there a way to add this spacing? <div id="cssmenu"> <ul> ...

Ensure that the responsive SVG image remains centered even when resizing

I have been attempting to center crop the responsive SVG image while resizing. Even though I used preserveAspectRatio="xMidYMax slice", the image width does not stay consistent with my screen size. body{ margin:0;padding:0; } .wrapper { position: re ...

Is it possible to alter the css twice through the action of clicking on two individual buttons

One feature I have in my interface allows users to click on the edit button to bring up borders and change the background color of other divs. Once the edit button is pressed, it is replaced by cancel and save buttons. The goal is for the borders and backg ...

The top row of a Bootstrap table generated with JavaScript does not display the background color as intended

I am currently using JavaScript to generate some HTML that displays a Bootstrap table. Below is the code for creating this: function displayTableData(d){ let html = ''; html += '<table id="my-table" class="table&q ...

Is it possible to override the body width setting?

Is it possible to override the width of the body element? For example, consider the following: <body> <table id="table1"> <tr><td></td></tr> </table> <table id="table2"> <tr><td></td>& ...

"Keep scrolling through the div's content and then proceed with the

My layout consists of a div container that occupies 80% of the screen, with fixed sidebars on both the left and right taking up 10% each. The content within the div is quite large, so I've applied overflow: hidden to it in order to hide excess content ...

Bootstrap flex: on a single row, one column spans the entire height while the other contains elements that are vertically aligned in the center

I'm having trouble with Bootstrap 4 as I try to vertically align the elements of the right column and create a full-height column on the left with just a background color. I've come across many posts discussing similar issues, but none specific t ...

Is there a way to select and style the nth-child element within a Bootstrap .card body?

I am experimenting with changing the background-color of the card-body in bootstrap 4, but I am unsure about how to access the card-body element since each body has a different parent (I believe my understanding is correct). Can this be achieved using the ...

The functionality of jQuery is not properly integrating with Materialize for hiding select options

For a project I'm working on, I have implemented two Select elements in the HTML code. The second select should only be enabled when the first select meets certain conditions. You can check out the JSfiddle link for reference. $(document).ready(f ...

Please include text beneath the input group addon class

Hey everyone, I'm facing some issues with my layout. Whenever the input fields are empty or null, I encounter errors. I want to ensure that the input text and input group addon have equal heights. Also, upon clicking the submit button, I use jquery/aj ...

Tips for entering the lowest and highest values into the input field

I am looking to track the daily minimum and maximum prices of shares for various companies. Currently, I'm attempting to utilize this tag: <input type="number" name="number" placeholder="minprice"> <input type="number" name="number" place ...

How to create vertical spacing between <a> elements within the same div using HTML and CSS

Currently, the layout in picture 1 shows how my content is displayed. I am looking to create spacing (bottom margin?) between the images like the example in picture 2. The two side-by-side blocks are separate DIVs, and the images within each line belong to ...

Phonegap - Retaining text data in a checklist app beyond app shutdown

This is my first time developing an app with Phonegap. I am looking to create a checklist feature where users can input items into an input field. However, I am struggling with figuring out how to save these items so that they remain in the checklist even ...

Issue with Flex property not being supported in Safari browser

.rq-search-container { position: relative; width: 100%; background: white; display: flex; flex-direction: row; align-items: flex-start; border-radius: 2px; z-index: 30; -webkit-flex: 1 0 15em; } While this code functi ...

Issue with Flat-UI: Navigation bar is not collapsing correctly. Need help to resolve this problem

I am currently utilizing the most recent Twitter Bootstrap along with Flat UI. I have been trying to create a basic navbar that collapses when the screen size is reduced. How can I resolve this issue? This is how it currently appears: My navigation items ...

Having trouble customizing the active state of a react router navlink using css modules in React?

Objective I am attempting to add styles to the active route in a sidebar using css modules while still maintaining the base styles by assigning 2 classes. This is the code I have tried: <NavLink to={path} className={` ${classes.nav_ ...