Solved

Image Center CSS3

Posted on 2015-01-19
5
203 Views
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" />
			<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.
0
Comment
Question by:gacto
  • 2
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
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%)
}
0
 

Author Comment

by:gacto
Comment Utility
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; }
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
Comment Utility
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&
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
Comment Utility
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.
0
 

Author Comment

by:gacto
Comment Utility
Tom - this will work. I will have to deal with the paragraph content but at this point that will be a non-issue.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
As a result of several questions about how to use Bootstrap I thought it would be a good idea to write down the development aspect of creating a Bootstrapped website in as little time as possible. Part 1 of this article will only concentrate on g…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now