How can I do width = 100% - 100px in CSS?

前端 未结 18 836
借酒劲吻你
借酒劲吻你 2020-12-12 13:28

In CSS, how can I do something like this:

width: 100% - 100px;

I guess this is fairly simple but it is a bit hard to find examples showing

相关标签:
18条回答
  • 2020-12-12 13:47

    Working with bootstrap panels, I was seeking how to place "delete" link in header panel, which would not be obscured by long neighbour element. And here is the solution:

    html:

    <div class="with-right-link">
        <a class="left-link" href="#">Long link header Long link header</a>
        <a class="right-link" href="#">Delete</a>
    </div>
    

    css:

    .with-right-link { position: relative; width: 275px; }
    a.left-link { display: inline-block; margin-right: 100px; }
    a.right-link { position: absolute; top: 0; right: 0; }
    

    Of course you can modify "top" and "right" values, according to your indentations. Source

    0 讨论(0)
  • 2020-12-12 13:49

    It started to work for me only when I checked all the spaces. So, it didn't work like this: width: calc(100%- 20px) or calc(100%-20px) and perfectly worked with width: calc(100% - 20px).

    0 讨论(0)
  • 2020-12-12 13:51

    Are you using standards mode? This solution depends on it I think.

    If you're trying to make 2 columns you could do something like this:

    <div id="outer">
        <div id="left">
            sidebar
        </div>
        <div id="main">
            lorem ispsum etc... 
        </div>
    </div>
    

    Then use CSS to style it:

    div#outer
    {
        width:100%;
        height: 500px;
    }
    
    div#left
    {
        width: 100px;
        height: 100%;
        float:left;
        background: green;
    }
    
    div#main
    {
       width: auto;
       margin-left: 100px; /* same as div#left width */
       height: 100%;
       background:red;
    }
    

    If you don't want 2 columns you can probably remove <div id="left">

    0 讨论(0)
  • 2020-12-12 13:54

    This works:

    margin-right:100px;
    width:auto;
    
    0 讨论(0)
  • 2020-12-12 13:55

    You can look into using LESS, which is a JavaScript file that pre-processes your CSS so you can include logic and variables into your CSS. So for your example, in LESS you would write width: 100% - 100px; to achieve exactly what you wanted :)

    0 讨论(0)
  • 2020-12-12 13:58

    my code, and it works for IE6:

    <style>
    #container {margin:0 auto; width:100%;}
    #header { height:100px; background:#9c6; margin-bottom:5px;}
    #mainContent { height:500px; margin-bottom:5px;}
    #sidebar { float:left; width:100px; height:500px; background:#cf9;}
    #content { margin-left:100px; height:500px; background:#ffa;}
    
    </style>
    
    <div id="container">
      <div id="header">header</div>
      <div id="mainContent">
        <div id="sidebar">left</div>
        <div id="content">right 100% - 100px</div>
      <span style="display:none"></span></div>
    </div>
    

    enter image description here

    0 讨论(0)
提交回复
热议问题