【问题标题】:How to add spacing in between <div> [duplicate]如何在 <div> 之间添加间距 [重复]
【发布时间】:2014-05-20 00:28:48
【问题描述】:

所以我在页面顶部有一个菜单,但我不知道如何在按钮之间添加间距。我只想在按钮之间留一点空间,这样它们就不会显得那么草率了。

这是我的菜单。代码在它下面。

HTML:

<div>
  <ul class="topbar">
    <a href="pages/index.html" class="fade">Home Page</a>
    <a href="pages/userprofile.html" class="fade">Profile</a>
    <a href="pages/createacc.html" class="fade">Create an Account</a>
    <a href="pages/settings.html" class="fade">User Settings</a>
    <a href="pages/contact.html" class="fade">Contact Us</a>
  </ul>
</div>

CSS

ul
{
    list-style-type: none;
    padding:0;
    margin:0;
}

/****************************/

.topbar {
    width: 700px ;
    Height:initial;
    background-color: #2b2b2b;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;

【问题讨论】:

  • 您的 HTML 无效,ul 中只允许使用 li。或者完全删除 ul 包装器。
  • 尝试了任何边距或填充?

标签: html css


【解决方案1】:

你的“按钮”应该放在列表项&lt;li&gt;

<div>
  <ul class="topbar">
    <li><a href="pages/index.html" class="fade">Home Page</a></li>
    <li><a href="pages/userprofile.html" class="fade">Profile</a></li>
    <li><a href="pages/createacc.html" class="fade">Create an Account</a></li>
    <li><a href="pages/settings.html" class="fade">User Settings</a></li>
    <li><a href="pages/contact.html" class="fade">Contact Us</a></li>
  </ul>
</div>

关于样式和间距...

ul.topbar {
    width: 700px ;
    background-color: #2b2b2b;
    margin:0;
    padding:0;
}

ul.topbar li
{
    list-style-type:none;
    display:inline-block;
    padding:0 10px;
}

ul.topbar a
{
    font-family:Arial;
    color:#950095;
    text-decoration:none;
}

ul.topbar a:hover
{
    color:#FFF;
    text-decoration:underline;
}

您可以查看here

如果出于某种奇怪的原因,您无法访问生成菜单的代码,您可以使用一些 jQuery 将 &lt;li&gt; 包裹在您的 &lt;a&gt; 周围:

$( "ul.topbar a" ).wrap( "<li></li>" );

【讨论】:

  • 谢谢!我设法将您的代码压缩为:“ul.topbar li { list-style-type:none; display:inline-block; }”并将 标签设置为
【解决方案2】:

下面应该这样做

.topbar a:not(:last-child){
  margin-right:10px;
}

Demo Fiddle

这会选择菜单中的所有 a 项目,代表菜单选项,并为除最后一个之外的所有项目添加右边距。

也就是说,您的 HTML 无效,您需要将您的 a 包装在 li 元素中,将您的 HTML 更改为:

<div class="topbar">
    <a href="pages/index.html" class="fade">Home Page</a>
    <a href="pages/userprofile.html" class="fade">Profile</a>
    <a href="pages/createacc.html" class="fade">Create an Account</a>
    <a href="pages/settings.html" class="fade">User Settings</a>
    <a href="pages/contact.html" class="fade">Contact Us</a>
</div>

【讨论】:

    【解决方案3】:

    广告到你的 &lt;a&gt; 标签 display:inline-block 然后像 div 一样对待它们.. 放置宽度高度边距和填充。 hope it works ;)

    .topbar a{
    display:inline-block;
    line-height:30px/*its standart but u can change it*/
    padding:0 5px; /*this will putt space from left and right u can edit it too*/
    /*keep in mid that the elemens with inline-block will fitt in the width of their parent element and make a new line if needed*/
    }
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签