Image Center CSS3

Posted on 2015-01-19
Last Modified: 2015-01-24
I have images defined through CSS to float right or left depending on the context of the page where they are used. However, in some cases I need to also center those images vertically (top to bottom). Is there a way to do this through CSS or do I have to deal with each image through HTML?

Here is the CSS style currently driving this and a snippet of code from one page where I am using this feature.

.content_post .img_left { float:left; margin-left:20px; }

    <div id="content_area">
	<div class="page_wrapper">
		<div class="content_post content_inner">
			<img src="images/legal.png" alt="legal" class="img_right" />
			<p><span class="green">Our</span> <span class="blue">Healthy Florida</span>works with a variety of people and partners, to achieve its mission. These partners and people provide services, guidance and solutions for complex business challenges within healthcare.</p>
			<p>The Healthy Florida Platform provides a powerful application development framework that serves as the foundation for this innovation and collaboration. The platform enables Target-ed Patient Engagement within the healthcare ecosystem.</p>
		<div class="content_post content_inner">
			<img src="images/cosairus.png" alt="cosairus" class="img_left" />
			<p>Cosairus is a software development firm located in Atlanta, GA USA specializing in web site design, da-tabase construction and tuning, and mobile application development. Our philosophy is to develop efficient and adaptable software for our clients using industry-proven practices.</p>
		<div class="content_post content_inner">
			<img src="images/cosairus.png" alt="cosairus" class="img_left" />
			<h2>Florida Community Health Center</h2>
			<p>Since 1976, Florida Community Health Centers Inc. (FCHC) has been considered a leader in Florida’s primary and preventive health care services. Comprehensive health care services are provided through a network of health centers surrounding Lake Okeechobee in South-Central Florida.</p>
		<div class="content_post content_last content_inner">
			<img src="images/cosairus.png" alt="cosairus" class="img_left" />
			<h2>Hendry Regional Medical Center</h2>
			<p>Hendry Regional Medical Center is a 25-bed critical access hospital that incorporates modern technology with state-of-the-art medical staff to care for its patients. At HRMC, we understand that caring for your health is a team effort. You'll find our staff to be courteous, respectful, and enthusiastic in serving your healthcare needs.</p>

Open in new window

In this scenario I would like the three images using class="img_right" to be centered vertically as well as floating left.
Question by:gacto
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 38

Expert Comment

by:Tom Beck
ID: 40558047
It's impossible to know from what you have posted exactly what vertically centered is relative to. Is it vertically centered in the class="content_post" div? If so, what happens to the other content. If there were no other content and the content_post div had a height that was greater than the image height then something like this would center the image vertically in that div.

.content_post .img_right {
  position: relative;
  top: 50%;
  transform: translateY(-50%)

Author Comment

ID: 40558050
Does this help to see the rest of the CSS for that entire section?

#content_area { padding-bottom:109px; }
#content_area .content_post { padding-top:60px;  border-bottom:1px solid #99d5db; padding-bottom:60px; display:table; }
#content_area .content_last { border:none; }
.content_post .img_left { float:left; margin-right:20px; }
.content_post .img_right { float:right; margin-left:20px; }
.content_post .img_right_fixed { position:absolute; right:0px; width:992px; margin-left:20px; }
.content_post h2 { font-size:30px; color:#494a4b; margin:0; }
.content_post h5 { font-size:30px; color:#494a4b; margin:0; }
.content_post p { font-size:17px; color:#494a4b; line-height:1.5; }
.content_post .green { color:#47a447; font-weight:bold; }
.content_post .blue { color:#428bca; font-weight:bold; }
LVL 53

Expert Comment

ID: 40558123
You can put the image in a div with display:table-cell as a property and that will let you position vertically is you are looking to do the centering within a container such as I do in the articled with a vertical positioning solution

Or if you are trying to center in the viewport you can use CSS calc() and vh units of measure as outlined in the article about calc() and viewport relative units

If you are looking to center relative to the full page then you will need to do the calculation and positioning with script.

LVL 38

Accepted Solution

Tom Beck earned 500 total points
ID: 40558157
The additional css does help, thanks. I'm guessing then that you want the right side images to be vertically centered in the height of the content_post div. I'm also assuming that moving the right side image tags to a different place in the markup is undesirable, that you would rather have all image tags as the first child of the content_post divs. If this is all correct than you could give the content_post divs a position:relative.

.content_post { position:relative }

Then use this for your right side images.

.content_post .img_right { position: absolute; top: 50%; right:20px; transform: translateY(-50%) }

Adjust the distance from the right edge as needed.

The next problem is what to do about the paragraph content being obscured by the image. You would need another class added to the content_post divs that included right side images to deal with this.

Author Comment

ID: 40568143
Tom - this will work. I will have to deal with the paragraph content but at this point that will be a non-issue.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question