[英]How can I set margin to one element of two elements which are next to each other?
Here is my code:这是我的代码:
.fold1_content .logo{ border: 1px solid; display: inline-block; height: 80px; width: 80px; text-align: center; } .fold1_content .title{ display: inline-block; margin-right: 5px; font-size: 60px; }
<div class="fold1_content"> <div class="logo">logo</div> <div class="title"> title </div> </div>
All I'm trying to do is set .title{margin-top: 30px;}
.我要做的就是设置
.title{margin-top: 30px;}
。 I mean I want to bring that title down a little bit.我的意思是我想把这个标题降低一点。 How can I do that?
我怎样才能做到这一点?
Currently neither padding
nor margin
will not affect only on one element.目前
padding
和margin
都不会只影响一个元素。
you have to use vertical-align property.你必须使用vertical-align属性。 For more infovertical-align property
有关更多信息垂直对齐属性
.fold1_content .logo{ border: 1px solid; display: inline-block; height: 80px; width: 80px; text-align: center; } .fold1_content .title{ display: inline-block; margin-right: 5px; font-size: 60px; margin-top: 30px; vertical-align: top; }
<div class="fold1_content"> <div class="logo">logo</div> <div class="title"> title </div> </div>
You just need to use + css selector it would select just next item of your current class.您只需要使用 + css 选择器,它就会选择当前类的下一个项目。
I made the changes in your code.我对您的代码进行了更改。 I think it might helpful.
我认为它可能会有所帮助。
.fold1_content .logo{ border: 1px solid; display: inline-block; height: 80px; width: 80px; text-align: center; } .fold1_content .title{ display: inline-block; margin-right: 5px; font-size: 60px; } .fold1_content .logo + .title { margin-left: 50px; }
<div class="fold1_content"> <div class="logo">logo</div> <div class="title"> title </div> </div>
You could also try positioning your divs, if that doesn't bother:你也可以尝试定位你的 div,如果这不打扰:
.fold1_content { position: relative; } .fold1_content .logo{ border: 1px solid; display: inline-block; height: 80px; width: 80px; text-align: center; } .fold1_content .title{ position: relative; display: inline-block; margin-right: 5px; font-size: 60px; top: 30px; }
<div class="fold1_content"> <div class="logo">logo</div> <div class="title"> title </div> </div>
.fold1_content { display:flex; flex-direction:row; align-items: center; justify-content: space-evenly; //only if you need both div to equally space } .fold1_content .logo{ border: 1px solid; display: inline-block; height: 80px; width: 80px; text-align: center; } .fold1_content .title{ display: inline-block; margin-right: 5px; font-size: 60px; }
<div class="fold1_content"> <div class="logo">logo</div> <div class="title"> title </div> </div>
Try Flexbox it gives better output试试 Flexbox 它可以提供更好的输出
Add float:left;添加浮动:左; or float:right;
或浮动:右; to both of your elements then your margin should work as expected.
到您的两个元素,那么您的边距应该按预期工作。 Best of luck!
祝你好运!
<style>
.fold1_content .logo{
border: 1px solid;
display: inline-block;
height: 80px;
width: 80px;
text-align: center;
float:left;
}
.fold1_content .title{
display: inline-block;
margin-right: 5px;
font-size: 60px;
float:left;
margin-top:30px;
}
</style>
<div class="fold1_content">
<div class="logo">logo</div>
<div class="title">
title
</div>
</div>
Here yo go with one more solution给你一个解决方案
.fold1_content .logo{ border: 1px solid; display: inline-block; height: 80px; width: 80px; text-align: center; } .fold1_content .title{ display: inline-block; margin-right: 5px; font-size: 60px; vertical-align: text-top; }
<div class="fold1_content"> <div class="logo">logo</div> <div class="title"> title </div> </div>
Hope this will help you.希望这会帮助你。
.fold1_content{ display: -webkit-flex; display: flex; width: 400px; height: 250px; } .fold1_content .logo{ border: 1px solid; height: 80px; width: 80px; text-align: center; } .fold1_content .title{ height: 80px; width: 80px; margin-right: 5px; font-size: 60px; }
<div class="fold1_content"> <div class="logo">logo</div> <div class="title"> title </div> </div> Try flex property, it's awesome.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.