简体   繁体   English

在图像之间添加空间?

[英]Adding space between images?

I have this div: 我有这个股利:

<div id="barraLinhas">
    <img onclick="linhaAzul()" src="Linha_Azul.png" value="Linha Azul" />
    <img onclick="linhaAmarela()" src="Linha_Amarela.png" value="Linha Amarela" />
    <img onclick="linhaVerde()" src="Linha_Verde.png" value="Linha Verde" />
    <img onclick="linhaVermelha()" src="Linha_Vermelha.png" value="Linha Vermelha" />
</div>

And this CSS: 而这个CSS:

#barraLinhas {
    width: 950px;
    height: 50px;
    margin-bottom: 10px;
}

And I would like to have those images with spaces between them but I am having trouble doing so (it is probably stupid easy.. I am just being dumb). 而且我想让这些图像之间留有间隔,但是这样做很麻烦(这可能很愚蠢。我只是傻瓜)。

EDIT: Or even have them spaced in those 950px 编辑:甚至将它们隔开950px

Since you wanted space between them, you can use + sibling selector to target image margin if only a image is present after the current image. 由于您希望在它们之间留出空间,因此如果在当前图像之后仅存在一个图像,则可以使用+同级选择器来定位图像空白。

 #barraLinhas { width: 950px; height: 50px; margin-bottom: 10px; } #barraLinhas img + img { margin-left: 15px; } 
 <div id="barraLinhas"> <img onclick="linhaAzul()" src="http://placehold.it/100x100" value="Linha Azul" /> <img onclick="linhaAmarela()" src="http://placehold.it/100x100" value="Linha Amarela" /> <img onclick="linhaVerde()" src="http://placehold.it/100x100" value="Linha Verde" /> <img onclick="linhaVermelha()" src="http://placehold.it/100x100" value="Linha Vermelha" /> </div> 

add this css 添加这个CSS

#barraLinhas img{
    float: left;
    margin-right: 10px;
}

this will put them under barraLinhas from left to right, with 10px space between them 这会将它们从左到右置于barraLinhas下,它们之间有10px的间距

You can use flexbox: 您可以使用flexbox:

#barraLinhas {
  display: flex;
  justify-content: space-between;
}

 #barraLinhas { width: 950px; height: 50px; margin-bottom: 10px; display: flex; justify-content: space-between; } 
 <div id="barraLinhas"> <img src="http://placehold.it/100x100" alt="Linha Azul" /> <img src="http://placehold.it/100x100" alt="Linha Amarela" /> <img src="http://placehold.it/100x100" alt="Linha Verde" /> <img src="http://placehold.it/100x100" alt="Linha Vermelha" /> </div> 

You can just use CSS like so: 您可以像这样使用CSS:

img{
    margin-right: 10px;
}

JSFiddle here JSFiddle在这里

try 尝试

#barraLinhas img:not(:first-child){
    margin-left: 5px;
}

or 要么

#barraLinhas img:nth-child(n+2){
    margin-left: 5px;
}

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

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