Replacement for TAG <center>

Asked

Viewed 632 times

5

I have the following form:

.table {
  max-width: 500px;
  border-collapse: collapse;
  font-size: 12;
  width: 320px;
  text-align: center;
  box-shadow: 2px 2px 2px 2px #aaa;
}

.table tr td{
  padding: 8px 8px 8px 8px;


}
.campo {
  border: 1px double black;
}

#teste {
  overflow: auto;
  max-height: 500px;
  max-width: 18ç0px;
  position:absolute;
  z-index:999; 
  left:0%; 
  top:0%;
  background-color:#fff;
  border:1px solid #000;
  padding:6px;
}

#teste2 {
  overflow: auto;
  max-height: 500px;
  max-width: 300px;
  position:absolute;
  z-index:999; 
  right:0%;
  top:0%;
  background-color:#fff;
  border:1px solid #000;
  padding:6px;
}
<div id="teste">
  <form id="form1" name="form1" method="post" action="#">
    <input type="button" onclick="ver()" value="x">
    <div id="div1" style="display: none;"></div>
  </form>
</div>

<center>
  <h1>x</h1>
</center>
<form id="form2" name="form2" method="post" action="#">
  <center>
    <table class="table">
      <tr>
        <td class="campo">
          <span id="inf1">*x</span><br/>
          <input type="text" id="txt" name="txt" size="2"/><br/>
          <span id="inf2">x?</span><br/>
          <input type="text" id="txt2" name="txt2" size="2"/><br/>
          x?
          <input type="checkbox" id="ckb" name="ckb" /><br/><br/>
          <input type="button" value="x" onclick="setaInf();">
        </td>
      </tr>

    </table>
    <br/>
    <span id="inf3"><small>*x</small></span>
  </center>
</form>

<div id="teste2">
  <form id="form3" name="form3" method="post" action="#">
    <input type="button" onclick="ver()" value="x">
    <div id="lol1" style="display: none;"></div>
  </form>
</div>

<br/><br/><br/>

<center>
  <div>
    <form id="form4" name="form4" method="post" action="#">
      <div id="lol" style="display: none;"></div>
    </form>
  </div>
</center>

I know the tag <center> was discontinued and according to references, the correct now would be to use the attribute css text-align:center; but even so I cannot reproduce the same effect, see the example below:

.table {
  max-width: 500px;
  border-collapse: collapse;
  font-size: 12;
  width: 320px;
  text-align: center;
  box-shadow: 2px 2px 2px 2px #aaa;
}

.table tr td{
  padding: 8px 8px 8px 8px;


}
.campo {
  border: 1px double black;
}

#teste {
  overflow: auto;
  max-height: 500px;
  max-width: 18ç0px;
  position:absolute;
  z-index:999; 
  left:0%; 
  top:0%;
  background-color:#fff;
  border:1px solid #000;
  padding:6px;
}

#teste2 {
  overflow: auto;
  max-height: 500px;
  max-width: 300px;
  position:absolute;
  z-index:999; 
  right:0%;
  top:0%;
  background-color:#fff;
  border:1px solid #000;
  padding:6px;
}
<div id="teste">
  <form id="form1" name="form1" method="post" action="#">
    <input type="button" onclick="ver()" value="x">
    <div id="div1" style="display: none;"></div>
  </form>
</div>

<div style='text-align:center;'>
  <h1>x</h1>
  <form id="form2" name="form2" method="post" action="#">
    <table class="table">
      <tr>
        <td class="campo">
          <span id="inf1">*x</span><br/>
          <input type="text" id="txt" name="txt" size="2"/><br/>
          <span id="inf2">x?</span><br/>
          <input type="text" id="txt2" name="txt2" size="2"/><br/>
          x?
          <input type="checkbox" id="ckb" name="ckb" /><br/><br/>
          <input type="button" value="x" onclick="setaInf();">
        </td>
      </tr>

    </table>
    <br/>
    <span id="inf3"><small>*x</small></span>
  </form>
</div>

<div id="teste2">
  <form id="form3" name="form3" method="post" action="#">
    <input type="button" onclick="ver()" value="x">
    <div id="lol1" style="display: none;"></div>
  </form>
</div>

<br/><br/><br/><br/><br/><br/>

<div style='text-align:center;'>
  <form id="form4" name="form4" method="post" action="#">
    <div id="lol" style="display: none;"></div>
  </form>
</div>

How do I center the two div´s of the first example without having to use the tag <center>?

  • Associated: http://answall.com/questions/32276/discontinues%C3%A7%C3%A3o-da-tag-center/32278#32278

  • remove the <center> tags and add the css in body, body{ text-align:center; }.. There are other ways to solve as well. It doesn’t mean that this is the only solution or the best solution.

3 answers

8


The text-align only serves to align elements inline or inline-block. The Divs by default are blocks, so are not affected by this property. You can center a div horizontally by defining its left and right margins as auto. For example:

div {
  width: 150px;
  height: 50px;
  margin: 0 auto;
  background: red;
  }
<div></div>

  • Only it wouldn’t affect all divs? In case I should apply to a style and play in divs that must be centralized, correct?

  • Well, your div #teste has position: absolute, and in that case the two things are incompatible. Or you define the position in the nail with position: absolute, or automatic centering with the technique I explained.

  • @bfavaretto I believe to use left: auto; e right: auto; Centralize, but I’m not sure.

  • @Oeslei No, I would need to use calc().

  • I tried to reproduce the problem in the fiddle, I have 4 Divs, two have to be absolute and two centered, one would have a div, and the other a table... like this: http://jsfiddle.net/vb873zo5/

  • @Marcelobonifazio I did not understand right in the fiddle what is wrong, and what needs to be centralized (note: position: float there is no).

  • In fact that style was not being used anywhere, it was just noise that I ended up taking the real case and I ended up bringing to the example, but I was able to solve the problem @bfavaretto, thankful ;]

  • 1

    @Marcelobonifazio Just add the margin: 0 auto in table. https://jsfiddle.net/vb873zo5/2/embedded/result/

Show 3 more comments

4

The style text-align: center; center the content within a block. In your case you didn’t center because you put the wrong style: text=align:center; where it should be text-align: center;.

To center the block (not its content), as is the example of your table, you should add another style: margin: 0 auto; or margin-left: auto; margin-right: auto;. Thus, the browser will calculate the right and left margin automatically to center the block.

  • Excuse me, when it came to creating the question I ended up not paying attention and I wrote wrong, already corrected the property text-align in the question.

0

I don’t know if I understand it very well but if you want to center Ivs on the screen you can add CSS margin:0 auto; in the div that will be aligned to the center.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.