I wonder if there is way to set outerwidth of a div using css to ignore padding and borders.
When I set a div to be 50% width, padding and border will be added to the width. How can I solve that without javascript or jQuery.outerWidth() ?
Don't want to use an extra element
I'm wonder if there is way to set outerwidth of a div using css to ignore padding and borders.
You can use box-sizing: border-box to make padding and border be counted inside width:
div {
box-sizing: border-box;
}
See: http://jsfiddle.net/thirtydot/6xx3h/
Browser support: http://caniuse.com/css3-boxsizing
The spec: http://www.w3.org/TR/css3-ui/#box-sizing
Nest another div inside yours, and apply the paddings/borders to the nested one:
<div style="width:50%;"> <div style="padding:5px;"> .... </div> </div>
Unfortunately, there is no purely CSS way to achieve that (or at least I'm not aware of one).
I am assuming you don't want to add more elements and answer your question slightly differently.
The browser, not the style-sheet, determines the actual size of relative measurements such as em and %.
There is no common/standard mechanism to feed calculated data from the browser's internal layout engine back into a stylesheet (this could be a dangerous looping problem).
Truly, the only way to do this is to:
Use fixed width sizes and add pixel calculations into your style.
Use JavaScript or related framework to achieve the results.
I would simply add a div inside that div if possible.
<div id="outerwidth">
<div class="inner">
//Content
</div>
</div>
.outerwidth { width: 50%; }
.inner { padding: 20px; }
you could split the 50% value assigned to the width as this:
width: 46%;
margin: 0 1%; // 0 top/bottom and 1% each for left and right
padding: 0 1%; // same as above
you can recalculate the percentages to suit your needs, as long as the total is 50% you should be fine.
I would avoid using js to fix small cosmetic issues as this would not work with js off and would add extra workload to your client's browser - think of mobile and you will see why performance counts!
Related
I'm using centered imgs to act as backgrounds for some tiles. I'm trying to have these images scale with their parent div's height and if they are wider then their parent's for them to hide the overflow.
Example:
* I've got it working now. Answers are below, I'm updating this code to display all I needed to use to get it to work *
HTML
<div class="container">
<img class="derp" src="http://gridiculo.us/images/kitty02.jpg">
</div>
CSS:
.container {
height:250px;
width:50%;
}
.derp{
object-fit: cover;
height: 100%;
width: 100%;
}
Here's a near-example: http://codepen.io/chriscoyier/pen/myPMGB
The difference would be that I'm using s and not background-image, and that instead of the img filling the div completely it would fit to the height and hide the width overflow.
I'm trying to avoid using background-image since I'm using a lot of these tiles and making CSS rules for every one isn't going to work.
In order to scale it with the div's height, I'd change the height from px to % - this way, the larger's the div, the larger's the picture. In order to certain the image, i'd use margin in the image css. That'd look like so:
.derp{
height:80%;
width:80%;
margin:10%;
}
.container {
height:250px;
width:50%; /* needed */
/* inner img is centered horizontally */
vertical-align:top;
text-align:center;
overflow-x:hidden;
}
<div class="container" style="background-color:gray"> <!-- The background is there so you could see the image relative to the div -->
<img class="derp" src="http://gridiculo.us/images/kitty02.jpg">
</div>
The best way to keep the aspect ratio of the image is to set the width to auto (and it's the default behavior so you don't need to set explicitly). And with a simple overflow:hidden it works almost as you want it.
The hard part is centering horizontally. You can try this answer :css to center a image horizontally.
However if all your images aren't the same size, you will need to make one rule per image. And in this case putting the image as background-img would be better for semantic and accessibility (because your image doesn't have a sense in the page, it doesn't convey any information, it's decoration). An <img> would be read by a screen reader (the alt attribute), and in your case it wouldn't help a blind people.
Depending on how many browsers you need to support, I'd suggest you use object-fit! Support for it is okay if you can ignore IE, but in case your project qualifies, I see no problem with using it today. Also, there is always a polyfill.
You can find a nice summary on CSS-Tricks.com about the property. It basically works similarly to background-size, but for <img> tags. In your case, object-fit: cover; does the trick.
I made a little demo on CodePen that shows you how it works.
img {
height: 100%;
object-fit: fill;
width: 100%;
}
I am using Bootstrap for responsive design and my whole site is wrapped inside container. The problem is that on some pages I need to have <div> elements 100% wide on screen. The problem is their parent container is of course not 100% wide so the child also cannot be. What can I do if application.html.erb looks like this:
<body>
<div class="container">
<%= yield %>
</div>
</body>
So, all my generated sites are inside container. That was great for me until recently. Now I run into a problem that some elements inside generated sites need to be wider than container and I am not sure how to accomplish that. Any ideas? Thank you.
I will post a CSS solution thats works for IE9+, althought i believe its betters to use a different layout for that case with .container-fluid class
.fullscreen-row{
width: 100vw;
position: relative;
left: calc(-50vw + 50%);
}
.fullscreen-row:before,
.fullscreen-row:after {
display: table;
content: " ";
}
.fullscreen-row:after {
clear: both;
}
Use a different layout for the pages that require it?
Just create a new file in layouts layouts folder. And use it when rendering in the controller.
render action: :page, layout: 'new_layout'
http://guides.rubyonrails.org/layouts_and_rendering.html
Alternative solution I use quite often:
.element-we-want-to-jump-out {
position:relative;
width:110%;
/* (width - 100)/2 */
left:-5%;
}
.parent-of-container {
/* can be also a body element, only condition is that it should fill out full viewport width and you will not be able to scroll horizontally inside. */
overflow-x:hidden;
}
Note that Bootstrap container uses absolute paddings and the 10% used for overflowing might not be enough for very low viewport widths (< 300px). You can still use more and adjust your "jumping out element" (e.g. by changing background size if it is an image, or padding if it is a table etc...)
Hi this is a quick css 101,
I'm battling with aligning, maybe someone can help
on another note, i'd like to ask :
what is the correct way of thinking when
building html divs - restricting div's sizes upfront and overriden:hidden it's contect -OR- letting the inner divs push the parent and hence sizing it.
what measures are prefered for divs so it will fit best different resolutions?
rems/precents?
Thanks.
the wrapper should be a container for the content
<div id="wrapper">
<div id="top"></div>
<div id="content"><p>content here</p></div>
</div>
<div id="footerbg"></div>
I made the footer snap to the bottom outside of the wrapper, if you want to move the top outside that is also fine but remember you will have to adjust the bottom padding to compensate for its height too.
see jsFiddle here: http://jsfiddle.net/F577v/9/
Answer to your QNs:
dependent on the situation, I will always try allow for the content to size the div - but I make sure I have padding and margins to reflect design.
I have always used px count for sizing and use media types in css to compensate for different resolutions
You can make it with fixed position using css:
div {
position: fixed;
top: 400px;
bottom: 0;
left: 0;
right: 0;
}
I'm setting a height of 20px on a <div>, though when it renders in the browser, its only 14px high.
Any ideas?
<div style="display:inline; height:20px width: 70px">My Text Here</div>
You cannot set height and width for elements with display:inline;. Use display:inline-block; instead.
From the CSS2 spec:
10.6.1 Inline, non-replaced elements
The height property does not apply. The height of the content area should be based on the font, but this specification does not specify how. A UA may, e.g., use the em-box or the maximum ascender and descender of the font. (The latter would ensure that glyphs with parts above or below the em-box still fall within the content area, but leads to differently sized boxes for different fonts; the former would ensure authors can control background styling relative to the 'line-height', but leads to glyphs painting outside their content area.)
EDIT — You're also missing a ; terminator for the height property:
<div style="display:inline; height:20px width: 70px">My Text Here</div>
<!-- ^^ here -->
Working example: http://jsfiddle.net/FpqtJ/
This worked for me:
min-height: 14px;
height: 14px;
Also, make sure you add ";" to each style. Your excluding them from width and height and while it might not be causing your specific problem, it's important to close it.
<div style="height:20px; width: 70px;">My Text Here</div>
You're loosing your height attribute because you're changing the block element to inline (it's now going to act like a <p>). You're probably picking up that 14px height because of the text height inside your in-line div.
Inline-block may work for your needs, but you may have to implement a work around or two for cross-browser support.
IE supports inline-block, but only for elements that are natively inline.
Set positioning to absolute. That will solve the problem immediately, but might cause some problems in layout later. You can always figure out a way around them ;)
Example:
position:absolute;
Position absolute fixes it for me. I suggest also adding a semi-colon if you haven't already.
.container {
width: 22.5%;
size: 22.5% 22.5%;
margin-top: 0%;
border-radius: 10px;
background-color: floralwhite;
display:inline-block;
min-height: 20%;
position: absolute;
height: 50%;
}
You try to set the height property of an inline element, which is not possible. You can try to make it a block element, or perhaps you meant to alter the line-height property?
I'm told that it's bad practice to overuse it, but you can always add !important after your code to prioritize the css properties value.
.p{height:400px!important;}
use the min-height property. min-height:20px;
Here's a question that's been haunting me for a year now. The root question is how do I set the size of an element relative to its parent so that it is inset by N pixels from every edge? Setting the width would be nice, but you don't know the width of the parent, and you want the elements to resize with the window. (You don't want to use percents because you need a specific number of pixels.)
Edit
I also need to prevent the content (or lack of content) from stretching or shrinking both elements. First answer I got was to use padding on the parent, which would work great. I want the parent to be exactly 25% wide, and exactly the same height as the browser client area, without the child being able to push it and get a scroll bar.
/Edit
I tried solving this problem using {top:Npx;left:Npx;bottom:Npx;right:Npx;} but it only works in certain browsers.
I could potentially write some javascript with jquery to fix all elements with every page resize, but I'm not real happy with that solution. (What if I want the top offset by 10px but the bottom only 5px? It gets complicated.)
What I'd like to know is either how to solve this in a cross-browser way, or some list of browsers which allow the easy CSS solution. Maybe someone out there has a trick that makes this easy.
The The CSS Box model might provide insight for you, but my guess is that you're not going to achieve pixel-perfect layout with CSS alone.
If I understand correctly, you want the parent to be 25% wide and exactly the height of the browser display area. Then you want the child to be 25% - 2n pixels wide and 100%-2n pixels in height with n pixels surrounding the child. No current CSS specification includes support these types of calculations (although IE5, IE6, and IE7 have non-standard support for CSS expressions and IE8 is dropping support for CSS expressions in IE8-standards mode).
You can force the parent to 100% of the browser area and 25% wide, but you cannot stretch the child's height to pixel perfection with this...
<style type="text/css">
html { height: 100%; }
body { font: normal 11px verdana; height: 100%; }
#one { background-color:gray; float:left; height:100%; padding:5px; width:25%; }
#two { height: 100%; background-color:pink;}
</style>
</head>
<body>
<div id="one">
<div id="two">
<p>content ... content ... content</p>
</div>
</div>
...but a horizontal scrollbar will appear. Also, if the content is squeezed, the parent background will not extend past 100%. This is perhaps the padding example you presented in the question itself.
You can achieve the illusion that you're seeking through images and additional divs, but CSS alone, I don't believe, can achieve pixel perfection with that height requirement in place.
If you are only concerned with horizontal spacing, then you can make all child block elements within a parent block element "inset" by a certain amount by giving the parent element padding. You can make a single child block element within a parent block element "inset" by giving the element margins. If you use the latter approach, you may need to set a border or slight padding on the parent element to prevent margin collapsing.
If you are concerned with vertical spacing as well, then you need to use positioning. The parent element needs to be positioned; if you don't want to move it anywhere, then use position: relative and don't bother setting top or left; it will remain where it is. Then you use absolute positioning on the child element, and set top, right, bottom and left relative to the edges of the parent element.
For example:
#outer {
width: 10em;
height: 10em;
background: red;
position: relative;
}
#inner {
background: white;
position: absolute;
top: 1em;
left: 1em;
right: 1em;
bottom: 1em;
}
If you want to avoid content from expanding the width of an element, then you should use the overflow property, for example, overflow: auto.
Simply apply some padding to the parent element, and no width on the child element. Assuming they're both display:block, that should work fine.
Or go the other way around: set the margin of the child-element.
Floatutorial is a great resource for stuff like this.
Try this:
.parent {padding:Npx; display:block;}
.child {width:100%; display:block;}
It should have an Npx space on all sides, stretching to fill the parent element.
EDIT:
Of course, on the parent, you could also use
{padding-top:Mpx; padding-bottom:Npx; padding-right:Xpx; padding-left:Ypx;}