如何在一行中对齐类

时间:2017-05-16 04:02:51

标签: html css

我希望类名为user的div位于页面顶部,而不是位于当前所在的card div下方。

我将display:更改为absoluteinline-block,但这也无效。

.card {
  opacity: 0.8;
}
.card:hover{
  opacity: 1;
}
.card {
  height: 450px;
  width: 320px;
  box-shadow: 1px 1px 20px grey;
  word-wrap: break-word;
  background-color: #564f4c;
  border-radius: 10px;
  margin: 12px;
  text-align: center;
  display: inline-block;
}

img {
  width: 100%;
  height: 270px;
  border-radius: 10px;
}

.info .name {
  font-family: Tahoma, Geneva, sans-serif;
  text-align: center;
  margin-top: 9px;
  font-size: 35px;
  color: white;
}
.answer, .reject {
  text-align: center;
  width: 47%;
  height: 40px;
  color: white;
  line-height: 40px;
  font-size: 20;
  font-family: Verdana, Geneva, sans-serif;
  border-style: none;
}
.answer:hover, .reject:hover, .profile:hover {
  box-shadow: 1px 2px 3px grey;
  cursor: pointer;
}
.answer {
  background-color: #3add0d;
  display: inline-block;
}

.reject {
  display: inline-block;
  background-color: #ff2b2b;
}

.phone {
  text-align: center;
  width: 100%;
  margin-top: 15px;
  height: 40px;
  display: inline-block;
}
.profile {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background-color: #dd520d;
  color: white;
/*position: relative;*/
  margin-top: 10px;
  font-size: 20;
  font-family: Verdana, Geneva, sans-serif;
}
.user {
  height: 100px;
  width: 100px;
  background-color: blueviolet;
  display: inline-block;
}
<!DOCTYPE html>
<head>
<title>Card</title>
</head>
<body>
  <div class="card">
    <img src="http://video.blastingnews.com/p/4/2017/02/10/en_gomez090217.jpg" alt="Selena Gomez">
    <div class="info">
      <div class="name">Selena Gomez</div>
      <div class="phone">
        <div class="answer">Answer</div>
        <div class="reject">Reject</div>
      </div>
    </div>
    <div class="profile">View Profile</div>
  </div>
  <div class="user">it should be at the top not here</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

首先将<div class="user">移到HTML中的<div class="card">上方,然后从display: inline-block; CSS中移除.card

请参阅以下更新代码:

.card {
  opacity: 0.8;
}

.card:hover {
  opacity: 1;
}

.card {
  height: 450px;
  width: 320px;
  box-shadow: 1px 1px 20px grey;
  word-wrap: break-word;
  background-color: #564f4c;
  border-radius: 10px;
  margin: 12px;
  text-align: center;
}

img {
  width: 100%;
  height: 270px;
  border-radius: 10px;
}

.info .name {
  font-family: Tahoma, Geneva, sans-serif;
  text-align: center;
  margin-top: 9px;
  font-size: 35px;
  color: white;
}

.answer,
.reject {
  text-align: center;
  width: 47%;
  height: 40px;
  color: white;
  line-height: 40px;
  font-size: 20;
  font-family: Verdana, Geneva, sans-serif;
  border-style: none;
}

.answer:hover,
.reject:hover,
.profile:hover {
  box-shadow: 1px 2px 3px grey;
  cursor: pointer;
}

.answer {
  background-color: #3add0d;
  display: inline-block;
}

.reject {
  display: inline-block;
  background-color: #ff2b2b;
}

.phone {
  text-align: center;
  width: 100%;
  margin-top: 15px;
  height: 40px;
  display: inline-block;
}

.profile {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  background-color: #dd520d;
  color: white;
  /*				position: relative;*/
  margin-top: 10px;
  font-size: 20;
  font-family: Verdana, Geneva, sans-serif;
}

.user {
  height: 100px;
  width: 100px;
  background-color: blueviolet;
  display: inline-block;
}
<body>
  <div class="user">Look, I'm at the top now.</div>
  <div class="card">
    <img src="http://video.blastingnews.com/p/4/2017/02/10/en_gomez090217.jpg" alt="Selena Gomez">
    <div class="info">
      <div class="name">Selena Gomez</div>
      <div class="phone">
        <div class="answer">Answer</div>
        <div class="reject">Reject</div>
      </div>
    </div>
    <div class="profile">View Profile</div>
  </div>
</body>

如果您需要任何其他帮助,请与我们联系。

答案 1 :(得分:0)

现在在jsFiddle中设置你的代码后,我看到了你想要的东西。

float: left添加到.card和.user

在您拥有的整个位周围添加一个包装器,并将此类添加到它:

.clearfix::after {
    content: "";
    clear: both;
    display: table;
}

因此,将这些浮点数添加到CSS类中,您的HTML应该如下所示

<div class="clearfix">

  <div class="card">
            <img src="http://video.blastingnews.com/p/4/2017/02/10/en_gomez090217.jpg" alt="Selena Gomez">
            <div class="info">
                <div class="name">Selena Gomez</div>
                <div class="phone">
                    <div class="answer">Answer</div>
                    <div class="reject">Reject</div>
                </div>
            </div>
            <div class="profile">View Profile</div>
        </div>
        <div class="user">it should be at the top not here</div>
  </div>
  <div>
  <p>
  I'm a new div
  </p>
  </div>

clearfix div中的任何内容都会浮动,其外的任何内容都将作为普通div。

请参阅小提琴:https://jsfiddle.net/323x9oxm/