Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css to achieve {Width:100% -150px}

Tags:

css

height

I've got a site with a side nav and I want the rest of the screen to be a filled with an image, the side nav is off the left and is 150px wide.

Ideally id like to set the full screen image at width:100% -150px; but I'm pretty sure this is not proper CSS.

Is there way to achieve the same effect ? I can't use width:90%; as on wider screens 150px would be more like 5% of the width rather than 10%.

like image 649
sam Avatar asked Jun 19 '12 14:06

sam


People also ask

How do you write 100 Width in CSS?

If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.

How do I make my div 100% wide?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

What is width 100% means in CSS?

if you specify width:100%, the element's total width will be 100% of its containing block plus any horizontal margin, padding and border.

How do I make 100 Width in HTML?

A block level element (display:block;) will automatically take up 100% of the width of the parent element. You can resize its width using percentages or pixels.


1 Answers

In fact - it is! (with css3)

width: calc(100% -150px);
width: -moz-calc(100% - 150px);
width: -webkit-calc(100% - 150px);

works, but only for the most modern browsers... caniuse

If you dont want to use this, you can use margin to create an offset: margin-left:150px;. This however needs a parent-element with 100% width and your image to be a block-level element (display:block).

Another option is to use box-sizing. This lets you choose another box-model which doesn't calculate margins and paddings into the element-width. This helps in some typical "i need 100% width - Xpx border" cases too.

In response to @BerkerYüceer's comment - you can also use dynamic values within the calc like following:

/* declare css variable */
--leftmargin:150px;

/* use the variable like following */
calc(100% - var(--leftmargin));
like image 182
Christoph Avatar answered Sep 29 '22 14:09

Christoph