代码之家  ›  专栏  ›  技术社区  ›  user7055375

如何将CSS按钮完全保留在其父DIV中?

  •  0
  • user7055375  · 技术社区  · 8 年前

    #createBtn {
      margin-bottom: 20px;
      width: 100%;
      padding: 15px;
      border-radius: 5px;
      border: 1px solid #7ac9b7;
      background-color: #4180C5;
      color: aliceblue;
      text-decoration: none;
      font-size: 15px;
      cursor: pointer;
      white-space: nowrap;
    }
    <div id="userNotificationsWrapper">
    
      <p>You have no notifications.</p>
    
      <div><a id="createBtn" data-method="get" href="/user_notifications/new">Create Notification</a></div>
    
    </div>

    但是链接/按钮一直在我的内容区域的底部边界(id为=“userNotificationsWrapper”的DIV)。我希望内容区域完全捕获链接/按钮,我希望它在我的消息后的下一行排队。

    https://jsfiddle.net/b3hxko01/ .

    2 回复  |  直到 8 年前
        1
  •  1
  •   tao    8 年前

    你有两个问题。首先是 width: calc(100% - 750px); 在…上 #userNotificationsWrapper . 你可能想用一个 max-width: 220px

    第二个是 width: 100%; 在…上 #createBtn . 我将替换为:

    #createBtn {
      display: inline-block;
      box-sizing: border-box;
      margin-right: 10px;
      width: calc(100% - 20px);
    }
    

    updated fiddle . 我的CSS添加在末尾。还没碰过你的。请随意搜索每个道具,看看它们做了什么。

        2
  •  -1
  •   Sampath1504    8 年前

    Demo

    这是css

    #userNotificationsWrapper {
     margin: 0 auto;
    background-color: #ffffff;
    border: 1px solid #C7CDD1;
    width: 75%;
    font-family: Acme;
    font-size: 18px;
    height:100px;
    padding-left: 10px;