Link to home
Start Free TrialLog in
Avatar of gacto
gacto

asked on

Image Center CSS3

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" />
			<h2>Partners</h2>
			<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>
		<div class="content_post content_inner">
			<img src="images/cosairus.png" alt="cosairus" class="img_left" />
			<h2>Cosairus</h2>
			<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>
		<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>
		<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>
		</div>
	</div>
    </div>

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.
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

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%)
}
Avatar of gacto
gacto

ASKER

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; }
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.

Cd&
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gacto

ASKER

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