简体   繁体   English

如何将边距设置为相邻的两个元素中的一个元素?

[英]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.目前paddingmargin都不会只影响一个元素。

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.

相关问题 多行上彼此相邻的元素没有边距 - No margin on elements next to each other on multiple lines 如何使用javascript或angular将一个数组的每个元素添加到其他数组中的对应元素 - How can I add each element of one array to corresponding elements in other arrays with javascript or angular 如果将其他元素动态添加到页面,是否可以将两个div相邻修复? - Can I fix two divs next to each other if other elements are added dynamically to the page? 如果我的javascript数组的值包含两个元素,如何使用一个元素而不使用另一个元素? - If my javascript array has values that have two elements how can I use one element without the other? 如何根据Angular中没有jQuery的其他元素的高度设置页脚页边距? - How can i set the margin of footer based on height of other elements, in Angular and no jQuery? 如何将两个或多个元素并排放置并溢出? - How to put two or more Elements sit next to each other with overflow? CSS将两个元素彼此对齐 - CSS align two elements next to each other 如何激活两个元素,一个一个地选择每个元素? - How do I make two elements active, selecting each element one by one? 如何使用 JavaScript 设置一个元素的边距等于另一个元素的高度? - How to set margin of one element equal to height of other element using JavaScript? 如何将两个复选框在表格行中彼此相邻? - how can i bring the two checkboxes next to each other in a table row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM