Using CSS or JQuery (CSS preferred),
Is it possible to take a div (of 100% width), with its contents centered, and align any contents that wrap to the left?
Imagine a set of icons centered on your browser. If you reduce the width of your browser, instead of having wrapped icons displaying in the center of the second line, I would like them to align left.
I have tried searching here before posting, but couldn't find anything that exactly matches my question.
Update:
@Explosion Pills solved it, but here is an updated JSFiddle that addresses browsers other than IE and Chrome by adding word-wrap: pre-wrap;
to the outer div
: http://jsfiddle.net/etY4r/2/
You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
Use text-align: center for the container, display:inline-block(-moz-inline-box for Firefox 2) for the wrapper div, and display:inline for the content div to horizontally center content that has an undefined width across browsers.
Align the text left or right Select the text that you want to align. On the Home tab, in the Paragraph group, click Align Left or Align Right .
You can do this with just CSS (and quite simply). The outer div can be text-align: center
and the inner div (containing the icons) text-align: left
with display: inline-block
:
http://jsfiddle.net/ExplosionPIlls/etY4r/
The inline-block
elements are centered in the context of the entire div, but when you decrease the window size enough it will cause the elements to wrap inside of the inner element itself and they are aligned to the left.
$('.class').css('css property','alignment');
Example:
$(document).ready(function(){
$('.myClass').css('text-align','left');
$('.myClass2').css('text-align','center');
$('.myClass3').css('text-align','right');
});
.css() works with any css property in this format :D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With