Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS How to center elements in DIVs (four separate DIVs)?

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #39955
    sntshkmr60
    Member

    I have four divs in my page of equal length and width. I have one `

    ` in each div. I want to center the `

    ` in its div. My current code is something like this:

    ## CSS ##

    body, html { height: 100%; padding: 0; margin: 0; }
    div { width: 50%; height: 50%; float: left;}
    h1 { display: inline-block;}

    ## HTML ##

    Some header here

    #110574
    johnegraham2
    Member

    If the content of your h1 is just text, you should be able to just add the `text-align: center` to the h1 directly. Then you wouldn’t have to reset the rest of the div’s contents.

    An h1 is `display: block` by default, so it should fill the width of its container. And this will center the text.

    If your h1 is an image replacement, give it a fixed width with left and right margins `auto`.

    Either way you wouldn’t need to set the display as `inline-block`.

    #110575
    wolfcry911
    Participant

    I’m guessing he wants the h1 centered vertically as well as horizontally

    http://codepen.io/wolfcry911/pen/xKiDb
    or
    http://codepen.io/wolfcry911/pen/gFbyk

    #110579
    sntshkmr60
    Member

    @wolfcry911
    Changing the `display` to `table` and `table-cell` are messing the things up. I have set `cursor: pointer` on the `h1` and when I apply `table-cell` to h1 then pointer is (virtually)enabled to whole screen.

    #110580
    sntshkmr60
    Member

    @kgscott284
    That will center horizontally only. I want vertically too.

    #110581
    sntshkmr60
    Member

    @johnegraham2 Setting the h1 to `text-align: center` doesn’t do anything in my case. There is my own reason I have set `display: inline-block`, I don’t mind if h1 is by default `display: block`.

    #110582
    wolfcry911
    Participant

    So I needed to guess about the vertical alignment as well as the cursor: pointer?

    you could wrap the text in a span and place the pointer on the span instead of the h1…

    #110595
    Kitty Giraudel
    Participant

    On a side note: h1 tag should be present only once per page. Once, not four. If you want to deal with subtitles, there are 5 other levels to do this.

    #110596
    sntshkmr60
    Member

    @HugoGiraudel Its not a regular page *(you can say this an app)* and I’m not keeping SEO in my mind while doing this.

    Should I use normal `

    ` and increase *(+ bold)* the text size?

    #110578
    Kitty Giraudel
    Participant

    If those are subtitles, use h2. Or the appropriate subtitle level.

    If those are quotes, use blockquote.

    If it’s some simple content, use p.

    #110602
    Senff
    Participant

    In HTML5, having multiple H1’s is allowed and perfectly valid.

    #110603
    wolfcry911
    Participant

    Google themselves stated it was okay even before html5

    #110608
    Kitty Giraudel
    Participant

    Oh. So my bad. I learnt a while ago “one h1 per page”.

    #110635
    lamjk
    Member

    why your html code just only one??

    #110637
    sntshkmr60
    Member

    Can anybody tell me what is error in this line:

    Validator is giving errors.

Viewing 15 posts - 1 through 15 (of 16 total)
  • The forum ‘CSS’ is closed to new topics and replies.