简体   繁体   English

两个Div彼此并列

[英]Two Div's beside each other in fours

Preview of Design 设计预览

I'm trying to position 2 divs into 4 cols I tried several things, with float, position etc - nothing worked, I can't arrange it as I want it. 我正在尝试将2 divs定位为4列,我尝试了几种方法,包括浮动,位置等-没有任何效果,我无法按我的要求进行排列。 Any ideas how to make it work? 有什么想法使它起作用吗?

<section class="recent-clients cf">
   <div class="wrapper">
      <h4>Recent Projects</h4>
      <div class="recent-work-info">
         <div class="pull-left">
            <img src="images/recent-work.jpg">
         </div>
         <div class="text-recent">
            <h1>Virginia Travel For Kids</h1>
            <h6>Logo Design</h6>
            <div class="line"></div>
            <p>Nam vulputate tristique lorem at placerat. In eu sagittis justo. Vestibulum magna ante, faucibus quis risus sit amet, rutrum imperdiet nisi.</p>
         </div>
         <button class="view-button">View Project</button>
      </div>
      <div class="recent-work-info">
         <div class="pull-left">
            <img src="images/recent-work.jpg">
         </div>
         <div class="text-recent">
            <h1>Virginia Travel For Kids</h1>
            <h6>Logo Design</h6>
            <p>Nam vulputate tristique lorem at placerat. In eu sagittis justo. Vestibulum magna ante, faucibus quis risus sit amet, rutrum imperdiet nisi.</p>
         </div>
         <button class="view-button">View Project</button>
      </div>
      <div class="recent-work-info">
         <div class="pull-left">
            <img src="images/recent-work.jpg">
         </div>
         <div class="text-recent All artwork on this website falls under intellectual property law. No permission is given to copy, trace, reproduce or manipulate any work.">
            <h1>Virginia Travel For Kids</h1>
            <h6>Logo Design</h6>
            <p>Nam vulputate tristique lorem at placerat. In eu sagittis justo. Vestibulum magna ante, faucibus quis risus sit amet, rutrum imperdiet nisi.</p>
         </div>
         <button class="view-button">View Project</button>
      </div>
      <div class="recent-work-info">
         <div class="pull-left">
            <img src="images/recent-work.jpg">
         </div>
         <div class="text-recent">
            <h1>Virginia Travel For Kids</h1>
            <h6>Logo Design</h6>
            <p>Nam vulputate tristique lorem at placerat. In eu sagittis justo. Vestibulum magna ante, faucibus quis risus sit amet, rutrum imperdiet nisi.</p>
         </div>
         <button class="view-button">View Project</button>
      </div>
   </div>
</section>

CSS/SCSS CSS / SCSS

.recent-clients {
    margin: 14rem 0;

    h4 {
        background-color: $secoudmaincolor;
        color: $firstmaincolor;
        font-family: 'Lato', sans-serif;
        text-transform: uppercase;
        font-weight: bold;
        letter-spacing: 3px;
        font-size: 1rem;
        padding: 24px;
        width: 33%;
        margin: 0 auto;
        margin-bottom: 2em;
        text-align: center;
        -webkit-box-shadow: 0px 8px 18px rgba(38,40,44,0.2);
        -moz-box-shadow: 0px 8px 18px rgba(38,40,44,0.2);
        box-shadow: 0px 8px 18px rgba(38,40,44,0.2);
    }


    .recent-work-info {
        margin:10px; 
        float: left;


        h1 {
            padding-top: 20px;
            padding-bottom: 0px;
            font-family: 'Open Sans', sans-serif;
            color: #24292f;
            margin-bottom: 0px;
            font-weight: 700;
            font-size: 1.0873958442073999rem;
        }

        h6 {
            font-family: 'Open Sans', sans-serif;
            color: #9b9b9b;
            padding: 9px;
            padding-bottom: 0px;
            font-size: 0.920104175867799rem;
        }

        p {
            font-family: 'Open Sans', sans-serif;
            font-size: inherit;
            color: #9b9b9b;
            margin-top: 0px;
            padding-bottom: 8px;
        }

        img {
            border: 1px solid black;
            width: 224px;
            min-height: 199px;
            margin-right: 18px;
            background-size: cover;

        }

        .text-recent {
            background-color: $offwhitecolor;
            margin: 0px; 
            width: 61%;
            // min-height: 200px;

        }

        .view-button {
            top: -1em;
            width: 33%;
            text-align: center;
            z-index: 9999;
            /* position: relative; */
        }

    }

}

There are many different ways to achieve this, however, I'll just show you how you can do this with flexbox. 有多种方法可以实现此目的,但是,我仅向您展示如何使用flexbox做到这一点。


jsFiddle jsFiddle


 * { box-sizing: border-box; } h4 { text-align: center; } .wrapper { display: flex; flex-wrap: wrap; justify-content: space-between } .recent-work-info { flex-basis: 48%; display: flex; background-color: dodgerblue; margin-bottom: 1em; } .recent-work-info img, .recent-work-info .text-recent { flex: 1 0 50%; } .titles { padding: 1em; border-bottom: 1px solid #f5f5f5; } .text-recent p { padding: 1em; } .view-button { border: 0; background-color: yellow; width: 100%; height: 3em; } 
 <section class="recent-clients"> <h4>Recent Projects</h4> <div class="wrapper"> <div class="recent-work-info"> <img src="http://placehold.it/100x100"> <div class="text-recent"> <div class="titles"> <h1>Title</h1> <h6>Subtitle</h6> </div> <p>Nam vulputate tristique lorem at placerat.</p> <button class="view-button">View Project</button> </div> </div> <div class="recent-work-info"> <img src="http://placehold.it/100x100"> <div class="text-recent"> <div class="titles"> <h1>Title</h1> <h6>Subtitle</h6> </div> <p>Nam vulputate tristique lorem at placerat.</p> <button class="view-button">View Project</button> </div> </div> <div class="recent-work-info"> <img src="http://placehold.it/100x100"> <div class="text-recent"> <div class="titles"> <h1>Title</h1> <h6>Subtitle</h6> </div> <p>Nam vulputate tristique lorem at placerat.</p> <button class="view-button">View Project</button> </div> </div> <div class="recent-work-info"> <img src="http://placehold.it/100x100"> <div class="text-recent"> <div class="titles"> <h1>Title</h1> <h6>Subtitle</h6> </div> <p>Nam vulputate tristique lorem at placerat.</p> <button class="view-button">View Project</button> </div> </div> </div> </section> 


If you are not familiar with flexbox, you can learn and read about it in many sites, here's a list of some of them: 如果您不熟悉flexbox,可以在许多站点中学习和阅读它,以下是其中的一些列表:

Like harzzn said above, you should look into Bootstrap. 就像上面的harzzn所说,您应该研究Bootstrap。 It will make your life easier. 这将使您的生活更轻松。 However, here is the code that will do what you want to replicate the picture you have referenced: 但是,以下代码将执行您要复制所引用图片的操作:

First (you were on the good track), if you are floating your divs left to lay them one next to another, you need to declare the width because you can't position two divs next to another if they are 100% wide. 首先(在良好的轨道上),如果要向左浮动div以将其一个接一个地放置,则需要声明宽度,因为如果两个div的宽度为100%,则不能将两个div并排放置。 Which is default. 这是默认值。 So we will split the space 50 50 and position them one next to another. 因此,我们将分割空间50 50并将它们一个接一个地放置。

.recent-work-info {
    float: left;
    width: 50%;
}

Now for the rest you are doing the same thing. 现在,其余的您都在做同样的事情。 We will split your picture div and text div into 50 50: 我们将您的图片div和文本div分成50 50:

.pull-left {
    float: left;
    width: 50%;
}

.text-recent {
    width: 50%;
    float: left;
}

And finally we will position the button to the right and make it the same width as the div above: 最后,我们将按钮置于右侧,并使其宽度与上面的div相同:

button.view-button {
    float: right;
    width: 50%;
}

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

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