对齐项目和居中线

时间:2018-03-01 01:33:17

标签: html css

我试图让它看起来像这样。这是从psd文件:

enter image description here

.drinks-section {
    position: relative;
    margin-left: 2rem;
    padding-left: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

  .item-drinks {
    margin-top: 1rem;
    display: flex;
    justify-content: space-between;
  }

  .item-drinks ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .item-drinks ul:first-child {
    font-weight: 700;
  }

  .item-drinks ul:last-child {
    font-weight: 100;
  }

  .item-drinks ul li {
    padding-bottom: 8px;
  }

.borderlines span {
  display: inline-block;
  position: relative;
}

.borderlines span:after {
  content: "";
  position: absolute;
  height: 10px;
  border-bottom: 5px solid #333;
  top: 8px;
  width: 5rem;
  bottom: 5px;
  left: 115%;
  margin-left: 15px;
}

.header-drinks {
  position: relative;
}

.header-drinks:after {
  content: "";
  position: absolute;
  height: 10px;
  border-bottom: 5px solid #333;
  width: 5rem;
  bottom: 0px;
}
<section class="section-five">
    <h1 class="borders"><span>Drinks</span></h1>
    <div class="drinks-section">
        <div class="drinks-content">
          <h4 class="header-drinks">Coffee</h4>
            <div class="item-drinks">
              <ul>
                <li class="borderlines"><span>Cafe Machiato</span></li>
                <li class="borderlines"><span>Cafe Americano</span></li>
                <li class="borderlines"><span>Cafe Lattte</span></li>
                <li class="borderlines"><span>Cappucino</span></li>
              </ul>
              <ul class="prices">
                   <li>$0.99</li>
                   <li>$0.99</li>
                   <li>$0.99</li>
                   <li>$0.99</li>
              </ul>
            </div>
          </div>
        </div>

但事实证明是这样的。我试图把线放在“咖啡”的底部,以及将菜单标题和价格分隔在中心的线条均匀分布。我不知道是否应该使用flexbox来对齐中心的.borderlines。

2 个答案:

答案 0 :(得分:1)

试试这个

   <style>
    .drinks-section {
    position: relative;
    margin-left: 2rem;
    padding-left: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
.drinks-content{min-width: 600px}

  .item-drinks {
    margin-top: 1rem;
    display: table;
    justify-content: space-between;
  }

  .item-drinks ul {
    margin: 0;
    padding: 0;
    display: table-cell;
    list-style: none;
  }

  .item-drinks ul:first-child {
    font-weight: 700;
  }

  .item-drinks ul:last-child {
    font-weight: 100;width: 1%;white-space: nowrap;
  }

  .item-drinks ul li {
    padding-bottom: 8px;position: relative
  }
  .borders{text-align: center}

.borderlines span {
  display: inline-block;
  position: relative;
}

.borderlines:after {
  content: "";
  position: absolute;
  height: 10px;
  border-bottom: 5px solid #333;
  top: 8px;
  width: 22rem;
  bottom: 5px;
  right: 10%;
  margin-left: 15px;
}

.header-drinks {
  position: relative;
}

.header-drinks:after {
  content: "";
  position: absolute;
  height: 5px;
  background: #333;
  width: 5rem;
  bottom: -10px;
  left: 0
}
</style>
<section class="section-five">
    <h1 class="borders"><span>Drinks</span></h1>
    <div class="drinks-section">
        <div class="drinks-content">
          <h4 class="header-drinks">Coffee</h4>
            <div class="item-drinks">
              <ul>
                <li class="borderlines"><span>Cafe Machiato</span></li>
                <li class="borderlines"><span>Cafe Americano</span></li>
                <li class="borderlines"><span>Cafe Lattte</span></li>
                <li class="borderlines"><span>Cappucino</span></li>
              </ul>
              <ul class="prices">
                   <li>$0.99</li>
                   <li>$0.99</li>
                   <li>$0.99</li>
                   <li>$0.99</li>
              </ul>
            </div>
          </div>
        </div>
</section>

演示https://www.w3schools.com/code/tryit.asp?filename=FOX40YH7L772

答案 1 :(得分:1)

您可以使用display: grid创建此布局。您可以简化HTML和CSS。以下示例中的一些解释。

您可以了解网格herehere,您可以看到当前的浏览器支持here

body {
  font-family: sans-serif;
  background: #eee;
  line-height: 1.8;
}

h1 {
  font-family: cursive;
  text-align: center;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

li {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-gap: 5%;
}

.line {
  height: 3px;
  background: black;
  /* vertically center the line */
  margin: auto 0;
}


/* create the line under "coffee" */
li:first-of-type span:first-of-type:before {
  content: '';
  position: relative;
  top: -1em;
  display: block;
  background: black;
  height: 2px;
}
<section>
  <h1>Drinks</h1>

  <h4 class="header-drinks">Coffee</h4>
  <ul>
    <li>
      <span>Cafe Machiato</span>
      <span class="line"></span>
      <span>$0.99</span>
    </li>
    <li>
      <span>Cafe Americano</span>
      <span class="line"></span>
      <span>$0.99</span>
    </li>
    <li>
      <span>Cafe Latte</span>
      <span class="line"></span>
      <span>$0.99</span>
    </li>
    <li>
      <span>Cappucino</span>
      <span class="line"></span>
      <span>$0.99</span>
    </li>
  </ul>
</section>