Center the div vertically

How can I vertically center a div next to a tall image using CSS? The left column (#watch) is an image and the right column (#watch-column) is a div that needs to be centered. I've tried different methods but nothing seems to work with my somewhat fluid website design. Any suggestions?

<section id="time-keeper-overview-id">
   <div class="row" id="watch-parent">
      <img id="watch" src="img/watch.png" class="img-responsive" alt="Responsive image">
      <div id="watch-column">
         <div id="time-keeper-description">
            <p>Insert paragraph of text here</p>
         </div>
         <img id="bus-stop-waves" src="img/bus-stop-waves.png" class="img-responsive" alt="Responsive image">
      </div>
   </div>
</section>

#watch {
  float: left;
  max-height: 100% !important;
  padding-right: 4%;
}

#watch-column {
  vertical-align: middle !important;
  overflow: hidden;
}

See the full site at

Answer №1

To achieve the desired layout, you will need to specify a height for the inner container. For your specific situation, it's best to set a height for #time-keeper-description and adjust other styles on the outer container. Consider adding the following CSS:

#time-keeper-description {
    height:500px; /* adjust as needed */
}

#watch-column {
    position: absolute;
    top: 50%;
    margin-top: -250px; /* half of #time-keeper-description height */
}

If setting a fixed height is not possible, consider using the following CSS on your div:

display: table-cell;
vertical-align: middle;

I hope this solution works well for your design! :)

Answer №2

When utilizing css properties like display: table-cell; for table displays using divs or containers, I have observed that it allows for effective vertical alignment.

display: table-cell;
vertical-align: middle;

Additionally, setting the css line-height property with a specific value enables vertical-align: middle; to function properly.

For further information, you can refer to this resource: http://css-tricks.com/centering-in-the-unknown/

Although Flex Box also supports this type of alignment, it may not be compatible with older browsers that do not automatically update, also referred to as "Ever Green" browsers -

For guidance on Flex Box Vertical Alignment, visit:

Another approach is to utilize position attributes in css such as relative and absolute, then specify top: 50%; However, the best method depends on the design and requirements that align with your project.

Answer №3

After receiving insights from various sources, I have come up with a solution for my own question. I appreciate all the input!

In this particular scenario, utilizing Flexbox proved to be the most effective approach. This was especially useful when dealing with an image that needed to fill the viewport height while maintaining a fluid design. By using flex: 0 0 auto; in conjunction with max-height: 100%;, I was able to achieve the desired outcome.

#view-container {
   display: -webkit-flex;
   display: -moz-box;
   display: -ms-flexbox;
   display: flex;

   -webkit-align-items: center;
   -moz-box-align: center;
   -ms-align-items: center;
   align-items: center;
}

#image {
   max-height: 100%;
   -webkit-flex: 0 0 auto;
   -moz-box-flex: 0 0 auto;
   -ms-flex: 0 0 auto;
   flex: 0 0 auto;
   padding-right: 4%;
}

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

Align audio and video elements in HTML5 using JavaScript

I am facing a situation where I have two files - one is a video file without volume and the other is an audio file. I am trying to play both of these files using <audio> and <video> tags. My goal is to make sure that both files are ready to pla ...

Can you please elaborate on how the CSS properties: -webkit-border-radius and border-radius are utilized?

https://i.sstatic.net/ILDTb.png What is the reason for having different values of 25px for -webkit-border-radius and border-radius in various positions? table.cal{ color: #064525c; border-spacing: 0; border: 3px solid black; -webkit-bor ...

JQueryMobile 1.3.2 encounters issues with popups following the installation of Chrome version 43.0.2357.65 m update

Is anyone else experiencing issues with the latest version of Chrome "43.0.2357.65 m" causing problems with JQueryMobile 1.3.2? When I try to click on a popup, it suddenly jumps to the top of the page and the scroll bar disappears. This was not an issue in ...

I have configured my CSS styles in the module.css file of my Next.js application, but unfortunately, they are not being applied

I recently developed a nextjs with electron template application: Here is the link to my code on CodeSandbox: https://codesandbox.io/s/sx6qfm In my code, I have defined CSS classes in VscodeLayout.module.css: .container { width: '100%'; he ...

Subsequent line in hta/html

I have been attempting to achieve the following output: Line 1 Line 2 Line 3 My attempts so far have included: <p>Line 1 Line 2 Line 3</p> Unfortunately, this results in: Line 1 Line 2 Line 3 I also tried the following method: <p>L ...

Executing a function upon loading a page triggered by a submitted form

Recently on my index.php page, I implemented a form that posts data into a third-party newsletter system. After the form submission, the page reloads to index.php?mail&. Is there a way to detect when the page is loaded and determine if the form has bee ...

Animating range tick movements as the range thumb moves

My custom range input includes a span that displays the range's value and a div with multiple p elements acting as ticks. To create these ticks, I had to use a custom div because using "appearance: none" on the range hides the ticks by default. The ti ...

Adding animation to the initial loading process of a Vue application

I want to include an animation when the application's modules are loading for the first time. I have attempted to use an image, but I am unable to add an animation to it. Adding a CSS file did not always work as expected. I then tried utilizi ...

Use the controller to set the body's background image

Seeking help to update the image URL within the CSS code snippet provided. I would like to accomplish this from the controller and can work with either LESS or SCSS. Is there a solution for this? .background{ height: 100%; width: 100%; backg ...

Unique Soundcloud visualization using webglonDeletegist

After going through the SoundCloud documentation, I have been trying to figure out how to create a visualizer for SoundCloud tracks. The challenge is that in order to achieve this effectively, I need access to the source waveform. I suspect that direct acc ...

Transitioning the <mat-icon> from one <mat-form-field> to another may not always result in the icon being updated consistently

Being new to Angular, I have been working on creating a form for blog posting and I am trying to include selectable category icons next to the title. In my current setup, I have made a form with selectable font awesome icons. However, I am facing an issue ...

Clicking on the background does not alter the onclick function

Can anyone help me troubleshoot my code? My function load() isn't changing the background of .firstDiv for some reason. I've checked multiple times but I can't seem to spot any errors. function load() { document.getElementsBy ...

Preventing links from functioning on dynamically generated web pages

I have implemented the following code to deactivate all links on a preview page: var disableLink = function(){ return false;}; $('a').bind('click', disableLink); While this successfully disables all static links, any anchor tags loade ...

What is the best way to dynamically assign a random file from a folder as the background image using CSS property in JavaScript?

Algorithm in Pseudo Code: if currentCondition equals snow { aFunction() } else { // execute something different } function aFunction { search for images inside the snow directory at /images/condition/snow select a random jpeg/jpg from th ...

Node.js causing issues with executing jQuery front-end code properly

I created an automatic image scroller div consisting of a ul with small images. The aim is to have a basic scroller that starts running once the page loads. It functions perfectly when I test the code (HTML, CSS, and a JS file with jQuery) locally in a bro ...

Adding additional text within the paragraph will result in the images shifting backwards when utilizing flexbox

It seems like there might be something obvious that I'm missing here, but essentially my goal is to create a footer with 2 columns and 2 rows. In each column, there will be an icon (32x32) and 2 lines of text next to it. The issue I'm facing is ...

Tips for resolving the issue of concealing content underneath a preceding element via CSS

I have a unique extension that allows me to modify the HTML of websites. One feature I am working on is adding a span tag containing an SVG tag. While it typically works well, there is an occasional issue where the SVG element is hidden underneath the prec ...

Getting rid of the upper boundaries and outlines of the Bootstrap table

React code: <div style={{paddingLeft: '8px', paddingRight: '8px'}}> <div className="-subtitle"> <div className="pull-right" style={{marginBottom:'5px' ...

The horizontal scrolling feature will not activate

I meticulously followed the instructions in a YouTube tutorial to implement this code. What's puzzling is that while the code works perfectly for the person in the video, allowing the grid items to scroll to the right, it doesn't have the same ef ...

Slick.js integrated with 3D flip is automatically flipping after the initial rotation

I'm encountering an issue with my CSS3 carousel and 3D flipping. Whenever I navigate through the carousel and flip to the next slide, the first slide seems to automatically flip/flop after completing the rotation. You can see a visual demonstration o ...