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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Find out what you should include to make the best professional email signature for your organization.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

738 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