简体   繁体   中英

CSS - Add space between elements except the last one

On this jsfiddle you can see that I have 6 circles: 3 on the first row and 3 on the second row.

I'd like to add some space between them and was planning to use margin-right: 5px. The issue if I do this is that the last elements (circle 3 and circle 6) will also have this extra 5px to their right which I don't want (since there's no elements next to them). Is there a workaround to that?

What I need is:

(Circle 1) 5px space (Circle 2) 5px space (Circle 3)

Thanks

HTML:

<div class="circle circlebackground">
    <p>Circle 1</p>
    <div class="innercircle">
        <p>by Angela</p>
    </div>
</div>
<div class="circle circlebackground">
    <p>Circle 2</p>
    <div class="innercircle">
        <p>by Angela</p>
    </div>
</div>
<div class="circle circlebackground">
    <p>Circle 3</p>
    <div class="innercircle">
        <p>by Angela</p>
    </div>
</div>

<div class="circle circlebackground clear">
    <p>Circle 4</p>
    <div class="innercircle">
        <p>by Angela</p>
    </div>
</div>
<div class="circle circlebackground">
    <p>Circle 5</p>
    <div class="innercircle">
        <p>by Angela</p>
    </div>
</div>
<div class="circle circlebackground">
    <p>Circle 6</p>
    <div class="innercircle">
        <p>by Angela</p>
    </div>
</div>

CSS:

.circle {
    float: left;
    margin-bottom: 10px;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    position: relative;
    box-shadow: inset 0 0 0 16px rgba(255, 255, 255, 0.6), 0 1px 2px rgba(0, 0, 0, 0.1);
    -webkit-transition: all 0.4s ease-in-out;
    -moz-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
    -ms-transition: all 0.4s ease-in-out;
    transition: all 0.4s ease-in-out;
}
.circlebackground {
    background: #fff;
    border:1px solid #37629B;

}
.innercircle {
    position: absolute;
    background: red;
    width: inherit;
    height: inherit;
    border-radius: 50%;
    opacity: 0;
    -webkit-transition: all 0.4s ease-in-out;
    -moz-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
    -ms-transition: all 0.4s ease-in-out;
    transition: all 0.4s ease-in-out;
    -webkit-transform: scale(0);
    -moz-transform: scale(0);
    -o-transform: scale(0);
    -ms-transform: scale(0);
    transform: scale(0);
    -webkit-backface-visibility: hidden;
}
.circle p {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    margin: 0;
}
.innercircle p {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    margin: 0;
    opacity: 1;
    -webkit-transition: all 1s ease-in-out 0.4s;
    -moz-transition: all 1s ease-in-out 0.4s;
    -o-transition: all 1s ease-in-out 0.4s;
    -ms-transition: all 1s ease-in-out 0.4s;
    transition: all 1s ease-in-out 0.4s;
}
.circle:hover {
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.1), 0 1px 2px rgba(0, 0, 0, 0.1);
}
.circle:hover .innercircle {
    -webkit-transform: scale(1);
    -moz-transform: scale(1);
    -o-transform: scale(1);
    -ms-transform: scale(1);
    transform: scale(1);
    opacity: 1;
}
.circle:hover .innercircle p {
    opacity: 1;
}

.clear {
    clear: both;
}

You can use this type of css if you really want to give margin-right.

.circle {
    float: left;
    margin-bottom: 10px;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    margin-left:5px;   /*added*/
    position: relative;
    box-shadow: inset 0 0 0 16px rgba(255, 255, 255, 0.6), 0 1px 2px rgba(0, 0, 0, 0.1);
    -webkit-transition: all 0.4s ease-in-out;
    -moz-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
    -ms-transition: all 0.4s ease-in-out;
    transition: all 0.4s ease-in-out;
}
.circle:nth-of-type(3n+0) {
    margin-right:0px;
}

jsfiddle link

Make a class with margin-right: 5px and that add you in the circle where you want.

Here the JSfiddle

.circle_5px_marging {
     margin-right: 5px;
}

add another class for your last div and there mention margin-right:0;

MARK-UP::

<div class="all_circles">
</div>
<div class="all_circles">
</div>
<div class="all_circles last_circle">
</div>

CSS::

.all_circles{
  margin-right:5px;
}
.last_circle{
  margin-right:0;
}

now in this example .all_circles is aplied to every div which have margin-right:5px; and change it for the last div by adding an extra class where margin-right:0;

note:: in this case the additional style, ie .last_circle must be defined after defining .all_circles because here .last_circle will override the margin property of .all_circles

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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