0

I have 6 divs in two rows of 3 and I want to populate each one with a title that's centered, and image centered and then text under the image that's left aligned.

What's the neatest way to achieve this?

Is it possible to do .block h2 {...} and center that way?

Fiddle

Here is my code so far:

HTML:

<div class="products">

<div class="block"><h2>...</h2><img src="..."><p>...</p></div>

<div class="block"><h2>...</h2><img src="..."><p>...</p></div>

<div class="block"><h2>...</h2><img src="..."><p>...</p></div>

<div class="block"><h2>...</h2><img src="..."><p>...</p></div>

<div class="block"><h2>...</h2><img src="..."><p>...</p></div>

<div class="block"><h2>...</h2><img src="..."><p>...</p></div>

</div>

CSS:

.products {
    display:inline-block;
}

.block {
    position:relative;
    width:213px;
    height:250px;
    background-color:#EEE;
    margin:10px;
    float:left;
    left:-10px;
}

Also, in JSFiddle, why are my "block" </divs> red at the end? is it not closed properly?

Mehul Tandale
  • 331
  • 1
  • 5
  • 17
Dr Robotnik
  • 352
  • 1
  • 4
  • 14

6 Answers6

2

You need the below selectors with text-align property

Demo

.block {
    text-align: center; /* Center text in .block */
}

.block p {
    text-align: left; /* Override centering by using left as a value */
}
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
1
.block h2 {
    text-align: center;   
}

.block img {
    display: block;
    margin: 0 auto;
}

JSFiddle: http://jsfiddle.net/kGt54/6/

Ed T.
  • 1,039
  • 2
  • 11
  • 17
  • I like this approach for centering the image as it doesn't involve extra markup – grimmus Oct 30 '13 at 11:47
  • That's also the way W3C wants us to center images. http://www.w3.org/Style/Examples/007/center.en.html#block – Ed T. Oct 30 '13 at 11:52
0
.products {
    display:inline-block;
    text-align:center; /*This will center everything inside products div*/
}

.block {
    position:relative;
    width:213px;
    height:250px;
    background-color:#EEE;
    margin:10px;
    float:left;
    left:-10px;
}
.block p{
   text-align:left; /* This will override the align center 
                       and will align-left only the <p> that are inside block */
}
Ricky Stam
  • 2,116
  • 21
  • 25
0

Like this

demo

css

.products {
    display:inline-block;
}

.block {
    position:relative;
    width:213px;
    height:250px;
    background-color:#EEE;
    margin:10px;
    float:left;
    left:-10px;
    text-align:center;
}
.block p{
    text-align:left;
}
Falguni Panchal
  • 8,873
  • 3
  • 27
  • 33
0

You can use the CSS property: text-align

.block h2{
    text-align:center;
}

.block p{
    text-align:center;
}

To center the images, you can put the pictures inside a paragraph:

Check the result here: http://jsfiddle.net/833Yq/

MTG
  • 551
  • 3
  • 14
0

Yes, add .block h2 {text-align:center;} to your css

You need to close your image tags with a / to stop the div's being highlighted in red

I updated the fiddle -http://jsfiddle.net/kGt54/8/ . This includes an extra div for centering the image. There would be other ways to approach the image centering

grimmus
  • 493
  • 5
  • 24