Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set CSS width to 100% + right border is missing?

Tags:

html

css

border

I set a div's width to 100% of the window. When I apply a border to this div, the right border is cut off. Do I have to perform a box model hack to this?

#textBoxContainer {    width:100%;    height:30%;    position:fixed;    z-index:99;    bottom:0px;    left:0px;    background-color:#999;    border: 4px solid #000;  }
<div id="textBoxContainer"></div>
like image 640
worked Avatar asked Feb 26 '11 16:02

worked


People also ask

How do I change the width to 100% in CSS?

It seems like this should be one of the easiest things to understand 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.

Why are my borders not showing in CSS?

CSS Border Not Showing If you've set the shorthand border property in CSS and the border is not showing, the most likely issue is that you did not define the border style. While the border-width and border-color property values can be omitted, the border-style property must be defined. Otherwise, it will not render.

How do I fix the width in CSS?

To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.


2 Answers

Already has been answered, but I like this solution better. Add this to textBoxContainer:

-webkit-box-sizing: border-box; -moz-box-sizing: border-box; -ms-box-sizing: border-box; box-sizing: border-box; 

It will put the border on the inside of the box. More info: http://css-tricks.com/box-sizing/

Edit - Doesn't work on IE7, but not much does. Here's more on that: box-sizing support in IE7

like image 200
d_rail Avatar answered Sep 21 '22 12:09

d_rail


The easiest fix in your case is this:

#textBoxContainer {      height: 30%;      position: fixed;      z-index: 99;      bottom: 0;      left: 0;      right: 0;      background-color: #999;      border: 4px solid #000;  }
<div id="textBoxContainer"></div>

Live Demo

  • Remove width: 100%.
  • To make the div fill the screen, instead add right: 0.

It's perfectly viable to give an element both a left and a right (or a top and a bottom), like we're doing here.

like image 25
thirtydot Avatar answered Sep 25 '22 12:09

thirtydot