简体   繁体   English

使用HTML / CSS在DIV中将元素居中

[英]Centering an element inside of a DIV using HTML/CSS

I am currently working on a periodic table, and although I got the mass and the number to be placed where I want them to, the element symbol just won't budge. 我目前正在处理元素周期表,尽管我得到了要放置在它们想要的位置的质量和数量,但是元素符号不会动摇。 I tried everything: converting it from <p> to <span> to <div> , using display: block and setting margin: 0 auto , using text-align: center , but it still won't budge. 我尝试了所有操作:使用display: block将其从<p>转换为<span><div> ,并设置margin: 0 auto ,使用text-align: center ,但是它仍然不会出错。

Here's the HTML: 这是HTML:

        <div id = "a1" class="element alkalimetal">
            <p>
                <span id = "anumber">118</span>
                <span id = "amass">9.008</span>
            </p>
            <br>
            <div id = "symbol">H</div>
        </div>
        <div id = "a2" class = "element noblegas">

        </div>

Here's the CSS: 这是CSS:

#anumber{
    font-family:Arvo;
    top: 0.1em;
    position:relative;
    vertical-align:super;
    font-size:1.5em;
    float:left;
    padding-left:0.2em;
}

#symbol{
    width: 5em;
    font-family: AvenirCondensed;
    font-size: 5em;
    text-align:center;
    margin: 0 auto;
}

#amass{
    font-family:AvenirNext;
    top: 0.1em;
    position:relative;
    vertical-align:super;
    font-size:1.5em;
    float:right;
    padding-right:0.2em
}
.element{
    border: 1px solid black;
    border-radius: 3px;
    height: 8em;
    width: 8em;
    float: left;
}

PS: This table is designed for mobile so I want to avoid using position:absolute or fixed margin widths. PS:此表是为移动设备设计的,因此我要避免使用position:absolute或固定的边距宽度。

Is this what you are trying to accomplish? 这是您要完成的工作吗?

http://jsfiddle.net/doitlikejustin/jAE7y/ http://jsfiddle.net/doitlikejustin/jAE7y/

#symbol{
    width: 5em;
    font-family: AvenirCondensed;
    font-size: 5em;
    text-align:center;
    margin: 0 auto;
    display:table-cell;
    vertical-align:center;
}

Here is an updated Fiddle with multiple elements http://jsfiddle.net/doitlikejustin/jAE7y/1/ 这是一个更新的提琴,其中包含多个元素http://jsfiddle.net/doitlikejustin/jAE7y/1/

Updated with alignment and fixed classes http://jsfiddle.net/doitlikejustin/jAE7y/3/ 更新了对齐方式和固定的类http://jsfiddle.net/doitlikejustin/jAE7y/3/

Change your #symbol rule to the following: 将您的#symbol规则更改为以下内容:

#symbol{
    /* width: 5em; REMOVE THIS */
    font-family: AvenirCondensed;
    font-size: 5em;
    text-align:center;
    /* margin: 0 auto; REMOVE THIS */
}

Your width was causing the symbol to spill over the div it was sitting in, breaking the flow. 您的宽度导致该符号溢出到它所在的div上,从而中断了流程。 The margin: 0 auto was no longer needed after that. margin: 0 auto此后不再需要margin: 0 auto

Also, as a side note, you should be using more Classes instead of ID's. 另外,请注意,您应该使用更多的类而不是ID。 ID's are intended for elements that are only used once per page. ID用于每个页面仅使用一次的元素。 You can easily set #symbol to a class .symbol as well as .amass and anumber . 您可以轻松地设置#symbol一类.symbol以及.amassanumber This will allow you to set the CSS rules once and use them for each element in the table. 这将允许您设置一次CSS规则,并将其用于表中的每个元素。

Here's the solution for readers that have a problem with margin: 0 auto not working like I did, or with a similar code. 这是针对页边距有问题的读者的解决方案:0 auto无法像我一样工作或使用类似的代码。 Key #1 was to set the inner div to float:none because it inherited this property from .element. 关键#1是将内部div设置为float:none,因为它从.element继承了此属性。 Key #2 was to set the inner div's width to width:inherit so that text-align: center would position the text properly. 关键#2是将内部div的宽度设置为width:inherit,以便text-align:center可以正确放置文本。 Code snippet attached 附带的代码段

.symbol{ max-height: 8em;
    width: inherit;
    display: inline-block;
    font-family: AvenirCondensed;
    font-size: 4.5em;
    float: none;
    text-align: center;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM