如何使用CSS使特定部分完全对齐?

时间:2018-09-19 18:22:13

标签: html css mobile

我正尝试使用css,html和javascript从头开始重新创建此图形。 这是原始的: Recreate Image

这是我的版本 enter image description here

我遇到的问题是在移动尺寸和1024px尺寸下完美地调整关键规格。但是我没有运气。我正在使用flexbox,当时我正在考虑将方向用作一种方法,但我不知道容器div或什么位置。 我不想使用Bootstrap之类的框架。我需要从头开始。你能取悦我吗?非常感谢

    <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Convertibles</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
</head>

<body>

    <div class="container">
        <header>
            <h1>Convertibles</h1>
            <p class="subtext">2-in-1 laptop plus tablet</p>
        </header>


        <ul class="item-list">

            <!-- Item 1 Start-->
            <li class="item item1">
                <img src="images/chromebookpro.jpg">
                <div class="item-text">
                    <h2>Google Pixelbook</h2>
                    <p class="pricing">Starting at $999</p>
                    <p>The first high performance Chromebook with Google Assistent built in. Features a four-in-one design offering laptop, tablet, tent, and entertainment modes, an all day battery with fast charging, a sleek aluminum body, and an optional pen. </p>
                    <p class="item-specs"> Key Specs</p>
                    <div class="item-text-specs">
                        <div class="item-list">
                            <p class="article">12.3" Quad HD LCD</p>
                            <p class="article">Intel processor&reg;</p>
                        </div>
                        <div class="item-list">
                            <p class="article">128/256/512 GB</p>
                            <p class="article">8/16 GB memory</p>
                        </div>
                    </div>
                </div>
            </li>
            <!-- Item 1 End-->


            <!-- Item 2 Start-->
            <li class="item item2">
                <img src="images/chromebookpro.jpg">
                <div class="item-text">
                    <h2>Samsung Chromebook Pro</h2>
                    <p class="pricing">Starting at $549</p>
                    <p>The power of a Chromebook. The versatility of a tablet. A 360 degree rotating screen and an Intel&reg; Core&trade; M3 processor helps get things done. Write and create naturally with a built-in pen.</p>
                    <p class="item-specs"> Key Specs</p>
                    <div class="item-text-specs">
                        <div class="item-list">
                            <p class="article">12.3" Quad HD LCD</p>
                            <p class="article">Intel processor&reg;</p>
                        </div>
                        <div class="item-list">
                            <p class="article">32 GB eMMC</p>
                            <p class="article">1 x 4 GB memory</p>
                        </div>
                    </div>
                </div>
            </li>
            <!-- Item 2 End-->

            <!-- Item 3 Start-->
            <li class="item item3">
                <img src="images/chromebookpro.jpg">
                <div class="item-text">
                    <h2>ASUS Chromebook Flip C302</h2>
                    <p class="pricing">Starting at $499</p>
                    <p>Flip between a high-performance Chromebook, a handy backlit tablet, and anything in between with a 360 degree hinge.</p>
                    <p class="item-specs"> Key Specs</p>
                    <div class="item-text-specs">
                        <div class="item-list">
                            <p class="article">12.5" (16:9) LED</p>
                            <p class="article">Intel processor&reg;</p>
                        </div>
                        <div class="item-list">
                            <p class="article">0.53" thin</p>
                            <p class="article">4/8 GB memory</p>
                        </div>
                    </div>
                </div>
            </li>
            <!-- Item 3 End-->

        </ul>
    </div>
    <script src=""></script>
</body>

@import url('https://fonts.googleapis.com/css?family=Roboto');

* {box-sizing: border-box;}

body {background-color: #FFFFFF;}

.container {
    padding: 1rem 1rem;
    max-width: 1200px;
    min-width: 320px;
    margin: 0 auto;
}

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

}
li {
    padding: 1rem; 
    /*background: #F8F9FA;*/
    color: #202020;
    opacity: 0.9;
}

li:hover {
    box-shadow: 1px 2px 3px 0px rgba(25,25,25,0.6);
    opacity: 1;
    transition:all 0.5s ease;  
}


h1 {
    font-family: 'Roboto', sans-serif;
    font-weight: lighter;
    font-size: 2.5em;
    color: #202124;
    text-align: center;
    margin-top: -20px;
    height: 25px;
    width: 100%;
}
h2 {
    margin: 0;
    font-family: 'Roboto', sans-serif;
  font-weight: lighter;
}
.subtext {
    font-family: 'Roboto', sans-serif;
    font-weight: lighter;
    font-size: 0.9em;
    color: #202124;
    text-align: center;         
}

.pricing {
    font-family: 'Roboto', sans-serif;
    font-weight: lighter;
    font-size: 0.9em;
    margin-top: 0em;
    color: grey;
}
p {
    color: #555;
    line-height: 2em;
    font-family: 'Roboto', sans-serif;
    font-size: 0.9em;
    font-weight: bold;
}
img {
    width: 100%; margin-bottom:1.5rem; border-radius: 5px;
}

.item-specs {
    font-family: 'Roboto', sans-serif;
    font-weight: bolder;
    padding: 0.8em 0;
    border-top: 1px solid #e7e7e7;
    color: black;
    margin-top: 2em;
    margin-bottom: -1em;
}


/* Flexbox Styling */

.item-list {
  display: flex; 
  flex-wrap: wrap;
  }
.item {
  flex: 100%;
  margin: 0 0 1.25rem;
}

.article {
  flex: 0 0 50%;
  font-family: 'Roboto', sans-serif;
  font-size: 0.7em;
  font-weight: lighter;

}

@media (min-width: 36em) {

.item { 
  flex: 0 0 calc( 50% - 1.25rem / 2 );
  margin-left: 1.25rem;
}

 .item:nth-child(odd){ 
   margin-left: 0;
 }  

}

@media (min-width: 60em) {
.item {flex: 0 0 calc(100% / 3 - 0.875rem);
}

.item:nth-child(odd), item:nth-child(even) { 
    margin-left: 1.25rem;
}

 .item:nth-child(3n+1) { 
  margin-left: 0;} 
}

1 个答案:

答案 0 :(得分:1)

有几种方法可以实现这一目标。


静态

最简单但最脆弱的方法是将顶部包裹在delimiter中,确定最高盒子的高度,并将其设置为CSS中的<div>。但是,如果文本太长或在不同的屏幕尺寸下框会调整宽度,则会遇到可维护性问题。

HTML

min-height

CSS

<li class="item item1">
    <div class="item-intro">
        <img src="images/chromebookpro.jpg">
        <h2>Google Pixelbook</h2>
        <p class="pricing">Starting at $999</p>
        <p>The first high performance Chromebook with Google Assistent built in. Features a four-in-one design offering laptop, tablet, tent, and entertainment modes, an all day battery with fast charging, a sleek aluminum body, and an optional pen. </p>
    </div>
    <div class="item-text-specs">
        <p class="item-specs"> Key Specs</p>
        <div class="item-list">
            <p class="article">12.3" Quad HD LCD</p>
            <p class="article">Intel processor&reg;</p>
        </div>
        <div class="item-list">
            <p class="article">128/256/512 GB</p>
            <p class="article">8/16 GB memory</p>
        </div>
    </div>
</li>

动态-纯CSS

另一个选择是在顶部和底部项目说明中打两个框。然后使用.item-intro { min-height: 300px; } ,可以使高度自动匹配。但这对于移动响应而言变得复杂,并且将需要使用flexbox。您也可以使用order来达到类似的目的。

HTML

grid

CSS

<ul class="item-list">

    <!-- Item 1 Start-->
    <li class="item item1">
        <img src="images/chromebookpro.jpg">
        <div class="item-text">
            <h2>Google Pixelbook</h2>
            <p class="pricing">Starting at $999</p>
            <p>The first high performance Chromebook with Google Assistent built in. Features a four-in-one design offering laptop, tablet, tent, and entertainment modes, an all day battery with fast charging, a sleek aluminum body, and an optional pen. </p>
        </div>
    </li>

    <div class="item-text-specs item1">
        <p class="item-specs"> Key Specs</p>
        <div class="item-list">
            <p class="article">12.3" Quad HD LCD</p>
            <p class="article">Intel processor&reg;</p>
        </div>
        <div class="item-list">
            <p class="article">128/256/512 GB</p>
            <p class="article">8/16 GB memory</p>
        </div>
    </div>
    <!-- Item 1 End-->


    <!-- Item 2 Start-->
    <li class="item item2">
        <img src="images/chromebookpro.jpg">
        <div class="item-text">
            <h2>Samsung Chromebook Pro</h2>
            <p class="pricing">Starting at $549</p>
            <p>The power of a Chromebook. The versatility of a tablet. A 360 degree rotating screen and an Intel&reg; Core&trade; M3 processor helps get things done. Write and create naturally with a built-in pen.</p>
        </div>
    </li>
    <div class="item-text-specs item2">
        <p class="item-specs"> Key Specs</p>
        <div class="item-list">
            <p class="article">12.3" Quad HD LCD</p>
            <p class="article">Intel processor&reg;</p>
        </div>
        <div class="item-list">
            <p class="article">32 GB eMMC</p>
            <p class="article">1 x 4 GB memory</p>
        </div>
    </div>
    <!-- Item 2 End-->

    <!-- Item 3 Start-->
    <li class="item item3">
        <img src="images/chromebookpro.jpg">
        <div class="item-text">
            <h2>ASUS Chromebook Flip C302</h2>
            <p class="pricing">Starting at $499</p>
            <p>Flip between a high-performance Chromebook, a handy backlit tablet, and anything in between with a 360 degree hinge.</p>
        </div>
    </li>
    <div class="item-text-specs item3">
        <p class="item-specs"> Key Specs</p>
        <div class="item-list">
            <p class="article">12.5" (16:9) LED</p>
            <p class="article">Intel processor&reg;</p>
        </div>
        <div class="item-list">
            <p class="article">0.53" thin</p>
            <p class="article">4/8 GB memory</p>
        </div>
    </div>
    <!-- Item 3 End-->

</ul>

动态-JS

最终选择是编写一个JavaScript代码段,该代码段循环遍历顶部的框,找到最高的框,并将该高度应用于所有框。如 Static 示例中所述,您将必须修改HTML以分隔.item, .item-text-specs { flex: 0 1 100%; } /* Only needed for desktop */ @media (max-width: 768px) { .item-text-specs.item1 { order: 4; } .item-text-specs.item2 { order: 5; } .item-text-specs.item3 { order: 6; } } 中的顶部描述。我不会写Javascript,因为它很耗时,但是应该相对简单。如果框的宽度也发生变化,则最困难的功能将是在窗口大小发生变化时触发高度调整。因此,根据您对Javascript的熟悉程度,使用jQuery可能会更容易。