border-box with percents and margins - border-box

how do you use border box with percentage and margins?
example follows.
<style>
.half{
width: 50%;
float: left;
background: red;
margin: 5px;
box-sizing: border-box;
box-shadow: 0px 0px 3px black;
}
</style>
<div class="half">half</div>
<div class="half">half</div>
i want the div(.half) to take up 50% of the screen - a 5px margin all around the div is this posable every time i try it makes it wider than 50% and puts the second box on the next row i would like to avoid % based margins if posable.

margins are never computed as part of the width, even using box-sizing: border-box;
So try replacing margin with border: 5px solid transparent
Or, if you can't override borders, depending on the effect you want to achieve try with :after/:before pseudoelements, e.g.
.half {
width: 50%;
float: left;
background: red;
box-sizing: border-box;
box-shadow: 0px 0px 3px black;
}
.half:after, .half:before {
content: "";
width: 5px; /* or more if you need more space */
display: inline-block;
}
Example: http://jsbin.com/imiqak/1/edit
Or you may use some nested elements, like so:
.half {
width: 50%;
float: left;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 5px;
}
.half p {
background: red;
box-shadow: 0px 0px 3px black;
}
Example: http://jsbin.com/imiqak/3/edit

margin are considered as outside the box (it's spaces around your box, not in the box). Margins size are not counted as part of the container width.
Indeed, when you type box-sizing: border-box;, you means the size of the box includes the border size, and if you look at this image below, you'll see that the margin are after the border, so it's ignored.

Try this:
CSS:
<style type="text/css">
.half{
width: 49%;
float: left;
background: red;
box-sizing: border-box;
}
.half:last-child{
margin-left: 1%;
}
</style>
HTML:
<div class="half">half</div>
<div class="half">half</div>

Related

Centering image and text within a div

In my asp.net mvc4 view I have some nested divs to show an image and under it a text like below:
<div id="Outer1" class="elementStatus">
<div class="image"> <!-- THIS IS THE IMAGE -->
<img id="MyImg1" src="#Url.Content("~/images/Image.gif")">
</div>
<div class="text"> <!-- THIS SHOWN THE TEXT UNDER IMAGE-->
Statuts Text
</div>
</div>
and the css:
.elementStatus
{
visibility: hidden;
}
.image{
float: left;
margin-top: 2px;
padding-top: 1px;
padding-left: 10px;
width: 35px;
}
.text{
float: left;
margin-top: 1px;
padding-left: 14px;
padding-bottom: 2px;
width: 35%;
font-weight: bold;
font-size: 1em;
}
I want that the image in the first div to be centered horizontally and vertically in the div where it is placed. The same for the second div, I want the text to be centered horizontally and vertically within its div. So how to do it?
Please, do not downvote!
Recommend a background image:
.image {
background-image: url('~/images/Image.gif');
background-repeat: no-repeat;
background-position: center;
background-size: 35px 35px;
}
For the text:
.text {
text-align: center;
}
For vertical text alignment:
.text {
line-height: 35px;
height: 35px;
}
This is a pretty broad question but it's easily solved using flexbox (assuming you're supporting newer browsers). Going to remove vendor prefixes for the sake of clarity but you'll want something like this:
#Outer1 {
display: box;
box-align: center;
box-orient: vertical;
box-pack: center;
}
http://jsfiddle.net/VqwLH/
Since you have updated saying you need support for older browsers, I think you can use display: table-cell for the parent container:
#Outer1 {
display: table-cell;
height: 500px;
width: 500px;
border: 1px solid red;
vertical-align: middle;
text-align: center;
}
http://jsfiddle.net/VqwLH/1/

Overflow scroll with padding Webkit v. Firefox

I'm trying to figure out why padding is included in this scrolling content for Chrome (Mac Version 27.0.1453.110) but not in Firefox (Mac 21.0). I'm using the box-sizing: border-box; When I hit the bottom the scrolling content the padding is there in Chrome, but in Firefox the last element is the bottom, there isn't any padding below. Should I be using margin instead? Is this a bug for 1 of the browsers?
I have an example on codepen
CSS:
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }
body {
background-color: #222;
}
p {
padding-bottom: 20px;
}
.boxy {
background-color: #111;
border: 4px solid #FFFFFF;
height: 442px;
overflow: auto;
position: relative;
margin: 0 auto;
overflow: hidden;
position: relative;
width: 722px;
}
.inside-boxy {
background-color: #ff6000;
height: 160px;
padding: 12px 50px 50px 28px;
position: absolute;
overflow-y: scroll;
}
.in-links {
background-color: #fff;
height: 132px;
margin-top: 22px;
width: 600px;
}
HTML:
<html>
<head>
<title>Paul Irish Box Model FTW</title>
<meta charset="utf-8">
</head>
<body>
<div class="boxy">
<div class="inside-boxy">
<p>...</p>
<p>...</p>
<div class="in-links">links!</div>
</div>
</div>
</body>
</html>
.inside-boxy has a fixed height. I would not issue the padding through that container. Instead, I would issue the padding and margin properties of the components inherent to .inside=boxy. I re-wrote your CSS below. Let me know if you have any issues.
p {
padding:20px;
}
.boxy {
background-color: #111;
border: 4px solid #FFFFFF;
height: 442px;
overflow: auto;
position: relative;
margin: 0 auto;
overflow: hidden;
position: relative;
width: 722px;
}
.inside-boxy {
background-color: #ff6000;
height: 160px;
position: absolute;
overflow-y: scroll;
width:100%;
}
.in-links {
background-color: #fff;
height: 132px;
margin:20px 0 20px 0;
width: 100%;
}

Can i remove bottom scroller on overflow:scroll?

So i have a popup window with a scroller. For scroller i used basic css element overflow: scroll. But the problem is scroller appears on the side and on the bottom. Now i want to know if there is anyway to remove the bottom scroller, because even though its locked its useless to me and it would look better without it. Ive googled it and havent found anything so if you have a solution please share it. If you need any of the code tell me and i will post it.
This is "my" css for popup (i got the code from http://www.zurb.com/playground/reveal-modal-plugin):
.reveal-modal-bg {
position: fixed;
height: 100%;
width: 100%;
background: #000;
background: rgba(0,0,0,.8);
z-index: 100;
display: none;
top: 0;
left: 0;
}
.reveal-modal {
visibility: hidden;
top: 100px;
left: 50%;
margin-left: -300px;
width: 520px;
height: 400px;
background: #eee url(modal-gloss.png) no-repeat -200px -80px;
position: absolute;
z-index: 101;
padding: 30px 40px 34px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
-moz-box-shadow: 0 0 10px rgba(0,0,0,.4);
-webkit-box-shadow: 0 0 10px rgba(0,0,0,.4);
-box-shadow: 0 0 10px rgba(0,0,0,.4);
overflow:scroll;
}
.reveal-modal h1{
color: green;
font-size: 40px;
}
.reveal-modal strong{
font-style: inherit;
}
.reveal-modal.small { width: 200px; margin-left: -140px;}
.reveal-modal.medium { width: 400px; margin-left: -240px;}
.reveal-modal.large { width: 600px; margin-left: -340px;}
.reveal-modal.xlarge { width: 800px; margin-left: -440px;}
.reveal-modal .close-reveal-modal {
font-size: 22px;
line-height: .5;
position: absolute;
top: 8px;
right: 11px;
color: #aaa;
text-shadow: 0 -1px 1px rbga(0,0,0,.6);
font-weight: bold;
cursor: pointer;
}
a better way of doing it would be
overflow-x:hidden;
overflow-y:auto;
That way it means that if a page gets bigger with jquery and then you need to scroll the view won't get smaller and affect your measurements
This should work:
overflow-x:hidden;
overflow-y:auto;

href link not working in ie 8

I just can't figure this out. The links below work absolutely fine in every browser except IE8. If you click on the elements in IE8, the button disappears. If you click again where it was it reappears. But no amount of clicking will actually lead you to the href location. Can anybody explain to me why that might be? I've exhausted all of my thoughts and it still doesn't work. Many thanks for all your thoughts!
The html...
<div style="padding:0px 0px 30px 0px; clear: both;">
<div style="width: 50%; display: block; float: left;">
Learn More
</div>
<div style="width: 50%; display: block; float: left;">
Get Started
</div>
</div>
...and the stylesheet...
.big_button {
-moz-box-shadow: inset 0px 1px 0px 0px #bbdaf7;
-webkit-box-shadow: inset 0px 1px 0px 0px #bbdaf7;
box-shadow: inset 0px 1px 0px 0px #bbdaf7;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #79bbff
), color-stop(1, #378de5) );
background: -moz-linear-gradient(center top, #79bbff 5%, #378de5 100%);
filter: progid : DXImageTransform.Microsoft.gradient (
startColorstr = '#79bbff', endColorstr = '#378de5' );
background-color: #79bbff;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border: 1px solid #84bbf3;
display: inline-block;
color: #ffffff;
font-family: Arial;
font-size: 18px;
font-weight: bold;
padding: 15px 45px;
text-decoration: none;
margin-left: 130px;
}
.big_button:hover {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #378de5), color-stop(1, #79bbff) );
background: -moz-linear-gradient(center top, #378de5 5%, #79bbff 100%);
filter: progid : DXImageTransform.Microsoft.gradient (startColorstr = '#378de5', endColorstr = '#79bbff' );
background-color: #378de5;
}
.big_button:active {
position: relative;
top: 1px;
}
Its because of the 'filter:' part in your css, remove it, then it will work...
I just figured it out. IE8 seems to have a problem with :active in css. So the following section of the stylesheet was causing issues:
.big_button:active {
position: relative;
top: 1px;
}
Current fix is to exclude the ':active' part of the css for the button as it's not essential. If anybody has any further information on why IE8 has issues with :active, I'd be interested to know about it.

css does not work in chrome (linux and windows)

i have this code:
<style type="text/css">
div {
margin: 100px auto;
width: 0px;
height: 0px;
border-right: 30px solid transparent;
border-top: 30px solid red;
border-left: 30px solid red;
border-bottom: 30px solid transparent;
border-top-left-radius: 30px;
border-top-right-radius: 30px;
border-bottom-left-radius: 30px;
border-bottom-right-radius: 30px;
position: relative;
}
</style>
<div></div>
that produce:
in firefox,
but in chrome (linux, and windows - didnt try in mac) i see nothing why?
I believe it is some sort of bug, actually. It works for me if you change the height and width to 1px. This leaves a little white dot, unfortunately, but that can be fixed by changing the background to red and the background-clip to content.
JSFiddle example.
Because you gave 0px width and 0px height to the div so you see nothing.
width: 0px;
height: 0px;
Change this, hope it'll be visible.

Resources