简体   繁体   English

保证金不居中

[英]Margin doesn't center

I am currently working on my portfolio website and so far. 到目前为止,我目前正在我的投资组合网站上工作。 But now I am having problem to center the "about me" page but i think i made a mistake somewhere because it doesn't do what i want. 但是现在我在将“关于我”页面居中时遇到了问题,但是我认为我在某个地方犯了一个错误,因为它不能满足我的要求。

Html: HTML:

<div class="pageAbout">

        <div id="icon">
            <img src="images/iconAbout.png" />
        </div>

        <div id="infoLeft">
            <h3>Name</h3>
                <p>Jeffrey van der Heijden</p>
            <h3>Birthday</h3>
                <p>-</p>
            <h3>Hobbies</h3>
                <p>hanging around with friends and family</p>
        </div>

        <div id="avatar">
            <img src="images/avatarMe.png" />
        </div>

        <div id="infoRight">
            <h3>Place of birth</h3>
                <p>Eindhoven</p>
            <h3>Phone</h3>
                <p>-</p>
            <h3>Email</h3>
                <p>-</p>
        </div>
</div>`

CSS: CSS:

.pageAbout{
width: 100%;
height: auto;
background-color: #e5e5e5;
padding-top: 1%;
} 

h3{
font-family: Aller_regular;
font-size: 16px;
} 

p{
font-family: Aller_regular;
font-size: 14px;
margin: 2% 0px;
}

#infoLeft{
width: 12%;
height: auto;
float: left;
margin-top: 2%;
text-align: right;
}

#avatar{
width: 18%;
height: auto;
float: left;
margin-top: 2%;
text-align: center;
}

#infoRight{
width: 12%;
height: auto;
float: left;
margin-top: 2%;
text-align: left;
}

so What I want is. 所以我想要的是

infoLeft needs to be on the left side of the avatar and infoRight on the right side of the avatar image and the image needs to be in the center of the page. infoLeft必须在left的侧面avatarinfoRightright的侧面avatar图像和图像必须是在页面的中心。

I hope i gave given enough and the right information. 我希望我给了足够的和正确的信息。

Thanks. 谢谢。

Okay, so these are the mistakes: 好的,这些是错误:

  1. You don't need any default styles like: width: 100%; 您不需要任何默认样式,例如: width: 100%; and height: auto; height: auto; .
  2. Give overflow: hidden; overflow: hidden; to clear the float s. 清除float
  3. Change the width s of the info-* and #image to sum up to 100% . 更改info-*#imagewidth s,总计为100%
  4. Align the #avatar img centered by using text-align: center; 通过使用text-align: center;对齐#avatar img居中text-align: center; .
  5. Create a faux margin for the #avatar to look like it is vertically centered. #avatar创建一个假边缘 ,使其看起来像垂直居中。

Snippet 片段

 .pageAbout { background-color: #e5e5e5; padding-top: 1%; overflow: hidden; } h3 { font-family: Aller_regular; font-size: 16px; } p{ font-family: Aller_regular; font-size: 14px; margin: 2% 0px; } #infoLeft{ width: 30%; height: auto; float: left; margin-top: 2%; text-align: right; } #avatar { width: 40%; height: auto; float: left; margin-top: 10%; text-align: center; } #avatar img { display: inline-block; } #infoRight{ width: 30%; height: auto; float: left; margin-top: 2%; text-align: left; } 
 <div class="pageAbout"> <div id="icon"> <img src="images/iconAbout.png" /> </div> <div id="infoLeft"> <h3>Name</h3> <p>Jeffrey van der Heijden</p> <h3>Birthday</h3> <p>-</p> <h3>Hobbies</h3> <p>hanging around with friends and family</p> </div> <div id="avatar"> <img src="images/avatarMe.png" /> </div> <div id="infoRight"> <h3>Place of birth</h3> <p>Eindhoven</p> <h3>Phone</h3> <p>-</p> <h3>Email</h3> <p>-</p> </div> </div> 

I advise you to improve your boxing and get into the CSS3 flexbox ! 我建议您改善拳击方式,并使用CSS3 flexbox

  1. Let the first icon div apart by creating another block .flex-center for centering the content. 通过创建另一个用于使内容居中的块.flex-center,使第一个图标div分开。
  2. The same inside for the avatar, just to center it vertically. 化身的内部相同,只是将其垂直居中。
  3. Then, move the width: 18% to the level of #avatar-container. 然后,将宽度:18%移动到#avatar-container的级别。
  4. Then add the correct flex properties to flex-center and avatar-container. 然后将正确的flex属性添加到flex-center和avatar-container中。
  5. Also, remove the auto height. 另外,删除自动高度。

     <div class="flex-center"> <div id="infoLeft" > <h3>Name</h3> <p>Jeffrey van der Heijden</p> <h3>Birthday</h3> <p>-</p> <h3>Hobbies</h3> <p>hanging around with friends and family</p> </div> <div id="avatar-container"> <div id="avatar"> <img src="images/avatarMe.png" /> </div> </div> <div id="infoRight" > <h3>Place of birth</h3> <p>Eindhoven</p> <h3>Phone</h3> <p>-</p> <h3>Email</h3> <p>-</p> </div> </div> 

and

.pageAbout{
    width: 100%;
    height: auto;
    background-color: #e5e5e5;
    padding-top: 1%;
} 

h3{
    font-family: Aller_regular;
    font-size: 16px;
} 

p{
    font-family: Aller_regular;
    font-size: 14px;
    margin: 2% 0px;
}

#infoLeft{
    width: 12%;
    /* height: auto; */
    /*float: left;*/
    margin-top: 2%;
    text-align: right;
}

#avatar{
    width: 18%;
    /* height: auto; */
    /*float: left;*/
    margin-top: 2%;
    text-align: center;
}

#infoRight{
    width: 12%;
    /* height: auto; */
    /*float: left;*/
    margin-top: 2%;
    text-align: left;
}

#avatar-container {
  width: 18%;
  display: flex;
  align-items: center;
  justify-content: center;

}

.flex-center {
  display: flex;
  justify-content: center;
}

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

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