Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Image Center CSS3

Posted on 2015-01-19
Medium Priority
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
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 style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

618 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