简体   繁体   English

如何将该元素居中?

[英]How do I center this element?

My HTML: 我的HTML:

<div class="container">
    <div class="card login">
        <p id="Title">Plex</p>
            <div class="label">
        </div>
        Random text
    </div>

    <div class="card welcome">
        <div class="label">
            <p id="Title">Hi!</p>
        </div>
        Lorem ipsum
    </div>

    <div class="card extra">
        <div class="label">
            <p id="Title">Extra</p>
        </div>
        Lorem ipsum
    </div>
</div>

MY CSS: 我的CSS:

.container{
    display: flex;
    width: 100%;
    /*align-content: center;*/
}
.card {
    flex: 1 1 auto;
    border: 1px solid #f3f3f3;
    background-color: #f3f3f3;
    margin-left: 50px;
    margin-right: 50px;
    margin-top: 25px;
    height: 450px;
}
.label{
    background-color: #434342;
    width:auto;
    height: 70px;

}
.card:not(:first-child){
    margin-left: 20px;
}

#Title {
    float: left;
    font-family: Thinfont-Thin;
    font-size: 42px;
    color: #d2731d;
    text-align: center;
}

A JSFiddle: http://jsfiddle.net/cvYGW/ JSFiddle: http : //jsfiddle.net/cvYGW/

How do I make the PLEX HI EXTRA all align perfectly in the middle of their cards and how do I move them up or down? 如何使PLEX HI EXTRA在卡的中间完全对齐以及如何上下移动?

You have to reset margin to 0 for #title and put a line-height that equals to the height of the parent. 您必须将#title margin重置为0,并设置等于父级高度的行高。

#title { /* change this selector to .title */      
  line-height: 70px;
  margin: 0;
  font-family: Thinfont-Thin;
  font-size: 42px;
  color: #d2731d;
  text-align: center;
}

See: http://jsfiddle.net/cvYGW/9/ 参见: http : //jsfiddle.net/cvYGW/9/

By the way, remember that your code is not valid HTML , as you use the same ID three times (#title). 顺便说一句,请记住您的代码不是有效的HTML ,因为您使用相同的ID三次(#title)。 You should use class in this case. 在这种情况下,您应该使用class。 And you don't need any float property. 而且您不需要任何float属性。

You shouldn't have multiple instances of an ID, so firstly change #Title to a class 您不应该有一个ID的多个实例,因此首先将#Title更改为一个类

You can't use float:left; 您不能使用float:left; & text-align:center; text-align:center; , remove the margin from your title p, match the line height ,删除标题p的边距,匹配行高

.title {
  margin:0px;
  line-height:70px;
  font-family: Thinfont-Thin;
  font-size: 42px;
  color: #d2731d;
  text-align: center;
}

http://jsfiddle.net/cvYGW/6/ http://jsfiddle.net/cvYGW/6/

#Title {
font-family: Thinfont-Thin;
font-size: 42px;
color: #d2731d;
text-align: center;
margin:0 auto;
}

I have update the js fiddle you just have to add 我已经更新了js小提琴,您只需要添加

 #Title {
     line-height:70px;
     width:100%;
     margin:0;
 }

Check on fiddle 检查小提琴

Please refer below updated fiddle 请参考下面更新的小提琴

http://jsfiddle.net/cvYGW/10/ http://jsfiddle.net/cvYGW/10/

give some width for parent div and align the child elements in center of parent by using margin css property. 通过使用margin css属性为父div提供一些宽度,并在父中心对齐子元素。

margin: 0 auto;

Thanks, 谢谢,

Siva 西瓦

Check out http://jsfiddle.net/skyrbe/cvYGW/8/ 查看http://jsfiddle.net/skyrbe/cvYGW/8/

You were using same id for multiple p tags . 您对多个p标签使用了相同的ID。 Never do that. 绝对不要那样做。

.Title {
    float: left;
    font-family: Thinfont-Thin;
    font-size: 42px;
    color: #d2731d;
    text-align: center;
    width:100%;
    margin:0px auto;
    line-height:42px;
}

update css: 更新CSS:

#Title {
float: left;
font-family: Thinfont-Thin;
font-size: 20px;
color: #d2731d;
text-align: center;
width: 100%;

} }

jquery animation


src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
    <script> 
    $(document).ready(function(){
      $('#Title').click(function(){
        $('#Title').animate({
          bottom: '-=20'
        }, 1000);
      });
    }); 

updated fiddle 更新的小提琴

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

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