CSS, horizontal distribution + horizontal+vertical centered text

aresando

I have a list of items, DIVs that I want to look like large squares, that I want to appear in line and centered on my page, distributed horizontally, and with the text inside each of the DIVs centered both vertically and horizontally.

In other words, something like this with three items:

|
|   [........]        [........]        [..Even..]   |
|   [..Text..]        [MoreText]        [..More..]   |   
|   [........]        [........]        [..Text..]   | <- page border   

    <--------------fixed-total-width------------->

Same with a fourth item inserted:

|
|   [........]  [........]  [........]  [..Even..]   |
|   [..Text..]  [MoreText]  [........]  [..More..]   |   
|   [........]  [........]  [........]  [..Text..]   | <- page border   

    <--------------fixed-total-width------------->

My constraints:

  • The overall width (DIVs + interspace) is fixed
  • The size of each DIV is fixed
  • The number of DIVs is dynamic
  • The text content is dynamic
  • No Javascript, only HTML and CSS

I have used many combinations of display, align and div structures... The items are either not distributed properly, or the text is not vertically centered, or other glitches appear...

All suggestions are welcome!

Thanks! RS

--

Current CSS:

.container { background-color:#FFFFFF; width:900px; margin:10px auto; }
.container .item { display:table-cell; text-align:center; width:160px; height:160px; }
.container .textitem { width:160px; height:160px; display:table-cell; vertical-align:middle;
                       text-align:center; background-color:#CCCCCC; }

Current HTML:

<div class="container">
 <div class="item"> <div class="textitem">Text</div> </div>
 <div class="item"> <div class="textitem">MoreText</div> </div>
 <div class="item"> <div class="textitem">Even more text</div> </div>
</div>
cantera

CSS flexbox layout is designed to solve the requirements you're describing:

http://jsfiddle.net/Sh2D2/4/

.container {
    background-color:#FFFFFF;
    width:450px;
    height: 450px;
    margin:10px auto;
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
}

.container .item {

}

.container .textitem {
    width:100px;
    height:100px;
    display:table-cell;
    vertical-align:middle;
    text-align:center;
    background-color:#CCCCCC;
}

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Vertical and Horizontal alignment of text with CSS

From Dev

wx.BoxSizer both VERTICAL and HORIZONTAL centered

From Dev

wx.BoxSizer both VERTICAL and HORIZONTAL centered

From Dev

How to make vertical text change into horizontal text in css?

From Dev

Search function for vertical or horizontal text

From Dev

Convert vertical text into horizontal in shell

From Dev

Horizontal listview with vertical text in Qml

From Dev

CSS - Vertical Border with Horizontal Gradients

From Dev

CSS Resize work for horizontal but not vertical?

From Dev

Vertical and horizontal CSS grid system

From Dev

Resizing vertical and horizontal images in CSS

From Dev

css vertical and horizontal alignment issues

From Dev

CSS issue aligning vertical and horizontal

From Dev

UITextField horizontal text alignment - text not centered

From Dev

UITextField horizontal text alignment - text not centered

From Dev

Horizontal list with evenly spaced and centered text

From Dev

CSS - Centered div fill remaining horizontal space

From Dev

CSS Horizontal Menu Not Centered Position Fixed

From Dev

Vertical tab control with horizontal text at design time

From Dev

center table and text in html page - vertical and horizontal

From Dev

Absolute vertical and horizontal center text in responsive div

From Dev

Linux tr to convert vertical text to horizontal

From Dev

Vertical and horizontal align image and text in container bootstrap

From Dev

HTML/CSS : Vertical border overlapping horizontal border

From Dev

horizontal and vertical alignment css on item that changes size

From Dev

Horizontal Menu with Vertical Submenu (HTML/CSS Only)

From Dev

CSS horizontal menu with hover vertical submenu

From Dev

CSS: How to limit horizontal/vertical scrolling?

From Dev

Creating CSS circles connected with lines vertical and horizontal

Related Related

  1. 1

    Vertical and Horizontal alignment of text with CSS

  2. 2

    wx.BoxSizer both VERTICAL and HORIZONTAL centered

  3. 3

    wx.BoxSizer both VERTICAL and HORIZONTAL centered

  4. 4

    How to make vertical text change into horizontal text in css?

  5. 5

    Search function for vertical or horizontal text

  6. 6

    Convert vertical text into horizontal in shell

  7. 7

    Horizontal listview with vertical text in Qml

  8. 8

    CSS - Vertical Border with Horizontal Gradients

  9. 9

    CSS Resize work for horizontal but not vertical?

  10. 10

    Vertical and horizontal CSS grid system

  11. 11

    Resizing vertical and horizontal images in CSS

  12. 12

    css vertical and horizontal alignment issues

  13. 13

    CSS issue aligning vertical and horizontal

  14. 14

    UITextField horizontal text alignment - text not centered

  15. 15

    UITextField horizontal text alignment - text not centered

  16. 16

    Horizontal list with evenly spaced and centered text

  17. 17

    CSS - Centered div fill remaining horizontal space

  18. 18

    CSS Horizontal Menu Not Centered Position Fixed

  19. 19

    Vertical tab control with horizontal text at design time

  20. 20

    center table and text in html page - vertical and horizontal

  21. 21

    Absolute vertical and horizontal center text in responsive div

  22. 22

    Linux tr to convert vertical text to horizontal

  23. 23

    Vertical and horizontal align image and text in container bootstrap

  24. 24

    HTML/CSS : Vertical border overlapping horizontal border

  25. 25

    horizontal and vertical alignment css on item that changes size

  26. 26

    Horizontal Menu with Vertical Submenu (HTML/CSS Only)

  27. 27

    CSS horizontal menu with hover vertical submenu

  28. 28

    CSS: How to limit horizontal/vertical scrolling?

  29. 29

    Creating CSS circles connected with lines vertical and horizontal

HotTag

Archive