Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

width: inherit minus (or plus)

Is there any method to inherit the width from other class but with minus (or plus) px size ?

This is not proper css but get the idea:

width: inherit - 20px ;

Since I'm using inherit width and 10px padding it goes outside the box for + 20px;

Live example:

.cb {
    width: 340px;
    height: 331px;
    border: 1px solid #ccc;
    padding: 0;
    background: #ccc;
    color: #fff;
    position: fixed;
    bottom: 0;
    right: 41px;
}

.cb .x {
    width: inherit;
    height: 100px;
    border: 0;
    padding: 10px;
    background: #781111;
    overflow-y: auto;
}
<div class="cb">
        
    <div class="cb x">
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>
    text<br>                
   </div>
        
</div>
like image 501
masteringtaiwan Avatar asked Jan 06 '14 20:01

masteringtaiwan


People also ask

Is width inherited?

width:inherit inherits width that defined by parent. This makes child width 25%, but if I redefine it with width:100% it will define width of child 50%.

Is width inheritable CSS?

CSS properties such as height , width , border , margin , padding , etc. are not inherited.

What does inherit mean in CSS?

The inherit CSS keyword causes the element to take the computed value of the property from its parent element. It can be applied to any CSS property, including the CSS shorthand property all . For inherited properties, this reinforces the default behavior, and is only needed to override another rule.


2 Answers

You can also use calc (see this jsfiddle), but I think that the box-sizing approach by kei might be better for your specific case.

The important parts for calc are:

position:relative; width: calc(100% - 18px); 

Check support for calc here.

like image 93
acarlon Avatar answered Oct 07 '22 17:10

acarlon


Add box-sizing:border-box to .cb .x

.cb {     width: 340px;     height: 331px;     border: 1px solid #ccc;     padding: 0;     background: #ccc;     color: #fff;     position: fixed;     bottom: 0;     right: 41px; }  .cb .x {     box-sizing:border-box;     width: inherit;     height: 100px;     border: 0;     padding: 10px;     background: #781111;     overflow-y: auto; }
<div class="cb">              <div class="cb x">     text<br>     text<br>     text<br>     text<br>     text<br>     text<br>     text<br>     text<br>                    </div>          </div>
like image 31
kei Avatar answered Oct 07 '22 19:10

kei