I'm using the 960.gs grid system for a design. What is the best way to add a thin separating vertical line between two boxes? The width and color should be adjustable.
My plan is to define a couple of div classes with absolute positions and background color, one for each possible position, and use JQuery to make sure that it has the same height as the surrounding boxes. That seems a bit complicated, though. Is there a better solution?
You can implement a border using the pseudo-selector :after and absolute positioning, like so:
.line:after {
border-right: 1px solid #000000;
content: "";
display: block;
margin: 1px;
position: absolute;
right: -11px;
top: 0;
bottom: 0;
}
.grid_1, .grid_2, .grid_3, .grid_4, .grid_5, .grid_6, .grid_7, .grid_8, .grid_9, .grid_10, .grid_11, .grid_12, .grid_13, .grid_14, .grid_15, .grid_16 {
position:relative;
}
Here is a demo http://jsfiddle.net/andresilich/ZTyf4/show/
Edit here http://jsfiddle.net/andresilich/ZTyf4/
If you don't want the separating line to change the position of the next row of DIVs, I think absolute positioning is your best bet. What you could do is use an absolutely-positioned :after selector to position something relative to the bottom of the box yet not affect the layout. This works for me to position a line between boxes without affecting layout, just change the values of the last four properties as needed:
#topbox:after {
content: "";
display: block;
position: absolute;
margin-top: 25px;
height: 5px;
width: 400px;
background-color: #999;
}
I think this is do-able without jQuery. The main issue is accounting for the variable height of the elements.
reference here: http://jsfiddle.net/uqZgt/1/
HTML:
<div class="container">
<div class="box-1">
This box has alot of content. This box has alot of content. This box has alot of content.
</div>
<div class="box-2">
This box has a little bit of content. This box has a little bit of content. This box has a little bit of content. This box has alot of content. This box has alot of content. This box has alot of content.
</div>
</div>
CSS:
.container {
width: 242px;
}
.container div {
width: 100px;
background: #ff0000;
float: left;
padding: 10px;
border-right: 2px solid #000
}
.box-1 + .box-2 {
border-right: none;
border-left: 2px solid #000
}
.box-1 ~ .box-2 {
margin-left: -2px
}
in this example, all divs in the .container div have a 2px solid black border-right. However, an element with class box-2 which directly proceeds an element with .box-1 will have a 2px solid black border-left, and no border-right. So far this creates a 3px border in between the two elements.
Now, .box-1 ~ .box-2 selects any .box-1 that directly preceeds a .box-2, and sets it's margin-left to -2px. This drags it's sibling two pixels to the left, which effectively makes the borders of both elements overlap.
The .container div has a width equal to the sum of the width of the two elements (200px), plus padding (10px right and left = 20px) plus the width of one of the borders (2px). 242px, so the two elements fit perfectly.
No matter which div has more content, the border will appear to span the height of the div with the most content.
I may not be understanding your problem. I would probably just use a right or left border on one of the columns and adjust padding to be sure it is centered between the 2.
Related
I would like to use the CSS :before selector to create a copy of my current div styles, and overlay it on top of my initial div. But I am unable to do this, without changing the :before position.
Why does this happen, and how can I assure that the position of these two is always the same?
.example {
margin: 100px;
height: 100px;
width: 100px;
background-color: transparent;
border: 5px solid red;
border-radius: 50%;
border-right-color: transparent;
display: block;
}
.example:before {
content: "";
display: block;
border: 5px solid yellow;
width: 100px;
height: 100px;
background-color: transparent;
border-right-color: transparent;
border-radius: 50%;
}
<div class='example'/>
Why does this happen
Because the ::before pseudo element is rendered as if it was the first child element of the div, so it has to be inside the border of the parent element. It's top left static position is offset by 5px from what you perceive as the dimensions of the parent, but which is actually the border outside of those. That borders are rounded to be circle-shaped might confuse one's perception of things here at first glance. (Thanks DaniP for clarification.)
And so you have stuck a 110px wide and high (5px borders on each side plus 100px width/height) child into an element that itself is just 100px by 100px, so it must overflow out of the parent. Add overflow: hidden to the div, or an outline, and you see where the pseudo child gets cut off.
overlay it on top of my initial div
For example margin:-5px on the pseudo child would do.
Depending on what you want to achieve in the end, it might not be the best approach. For example if the div is eventually going to contain actual (text) content, then the pseudo child would push that out of the way. As an alternative, you could position the div relative, and the pseudo child absolute, with top/left -5px each, that would achieve the same positioning of the child. If it needs to be behind content, add z-index.
I'm trying to get a gap created within a div's border to fit an image, similar to this:
Is there a way to do this in pure CSS? All I can see is:
.box {
background: url(img.png) bottom left;
border-top: 1px solid #eee;
border-left: 1px solid #eee;
}
But my problem is border-right: 1px solid #eee; creates a line on top of my image, which is of course not desired.
It needs to be responsive. This image is an example, but you get the general idea.
Something like this?
http://jsfiddle.net/6Ufb5/
div {
border: 1px solid #ccc;
position: relative;
}
img {
position: absolute;
top: 10px;
left: 10px;
}
Give the container position relative and the img absolute, shift it to left 10px and shift it down 10px from the top and you have what you desire.
For the responsive part, that's just giving the container and/or img a % width.
Like so:
http://jsfiddle.net/6Ufb5/2/
You can achieve this by using absolute positioning of the image element - and it has to be in a <img> element, not as the background image because it will never overlap the parent border (or even if it does by adjusting the background-position property, the border will lie on top of the background image... a behavior that is expected, by the way.
<div class="box">
Content goes here
<img src="http://placehold.it/300x200" />
</div>
And the CSS:
.box {
position: relative;
border: 1px solid #333;
}
.box img {
position: absolute;
bottom: -1px;
right: -1px;
}
If you want a dynamic and/or responsive solution, you might have to resort to JS to doing so - such as resizing the image depending on the box dimensions, and assigning a height to the box to take into account of the image height (since image is absolutely positioned, it is taken out of the document flow).
See fiddle here: http://jsfiddle.net/teddyrised/xH6UV/
This might work if you can alter your markup. For accessibility I think the image should be an image and not a background, and this method is responsive (though you may want to alter margins at small sizes with media queries).
http://jsfiddle.net/isherwood/79Js5
.box {
border: 1px solid #ccc;
display: inline-block;
padding: 10px 0 10px 10px;
width: 40%;
}
.box img {
margin-right: -10%;
margin-bottom: -10%;
width: 105%;
}
<div class="box">
<img src="http://placehold.it/200x100/f3f3f3" />
</div>
I wanted to give all my divs a border-radius of 5px, and I wanted certain divs to have additional border propertied according to their id's.
div {
background-color: #CCCCCC;
border-radius: 5px;
}
#toppanel {
height: 70px;
width: 90%;
margin: auto;
}
#leftpanel {
float: left;
width: 150px;
height: 500px;
border: 5px black solid;
}
my #toppanel div now has rounded corners, but my #leftpanel div does not. Does this mean that applying any border properties to a more specific set of elements excludes any border-related instructions more generally issued?
Do I need to remove the border-radius instruction from the div{...} instruction, and then add it to every id? Or is there a way to issue the border-radius instruction to all divs at once, while giving different divs different instructions regarding their borders using id's?
I'm currently using jsfiddle inside of a fully-updated Safari on a mid-2012 macbook air.
Maybe do
#leftpanel , #toppanel{
border-radius:inherit;
}
I've set two span elements side by side with a separating border being applied to one of the spans. The problem occurs when one span has more lines than the other. If the span with less content is the one which has the border applied to it, the border doesn't stretch to the bottom of the container.
I've tried adding height and min-height elements to the span, the containing div, the HTML and body tags in various combinations with no success.
Here is the sample HTML:
<div class="newspecs">
<div class="ns_row_type_2">
<span class="ns_field_name">Flash Exposure Compensation</span>
<span class="ns_field_value">+/- EV<br>more text<br>more text<br>more text<br>more text</span>
</div>
</div>
And the applicable CSS:
.newspecs div {
display: block;
clear: both;
}
.newspecs span {
display: inline-block;
vertical-align: top;
}
.ns_row_type_1,
.ns_row_type_2 {
border-bottom: 1px solid #fff;
}
.ns_row_type_1 {
background-color: #ccc;
}
.ns_field_name {
width: 100px;
padding: 3px;
border-right: 1px solid #fff;
}
.ns_field_value {
width: 280px;
padding: 3px;
}
The full CSS and HTML is at:
http://yazminmedia.com/clients/IR/test.htm
Anyone have an idea of what is going on?
Thanks!
Option 1: Floats, extra border, and 1px negative margin
http://jsfiddle.net/95uMq/
Option 2: CSS3
http://jsfiddle.net/95uMq/1/
Option 3: Table based Layout
http://jsfiddle.net/95uMq/2/
You should really be using a HTML TABLE here as this is a table of data that you are trying to display.
However as a nasty hack, change ns_field_value to...
.ns_field_value {width: 280px; padding: 3px; margin-left:-4px; border-left:1px solid #fff}
This just adds a border-left to the field value 'cell' and then does a -4px margin so that the borders overlap appearing to create a single border.
I want to be able to draw a border OUTSIDE of my Div! So if my div is say 20px by 20px, I want a 1px border outside of this (so in essence, I get a div 22x22px large).
I understand that I can just make the div 22x22 to start with, but for reasons I have, I need the borders to be on the outside.
CSS outline works, but I want only border-bottom or border-top thingy, so something like outline-bottom, which does not work, is what I want.
Is there a way to do this?
Thanks
I think you've got your understanding of the two properties off a little. Border affects the outside edge of the element, making the element different in size. Outline will not change the size or position of the element (takes up no space) and goes outside the border. From your description you want to use the border property.
Look at the simple example below in your browser:
<div style="height: 100px; width: 100px; background: black; color: white; outline: thick solid #00ff00">SOME TEXT HERE</div>
<div style="height: 100px; width: 100px; background: black; color: white; border-left: thick solid #00ff00">SOME TEXT HERE</div>
Notice how the border pushes the bottom div over, but the outline doesn't move the top div and the outline actually overlaps the bottom div.
You can read more about it here:
Border
Outline
Try the outline property CSS Outline
Outline will not interfere with widths and lenghts of the elements/divs!
Please click the link I provided at the bottom to see working demos of the the different ways you can make borders, and inner/inline borders, even ones that do not disrupt the dimensions of the element! No need to add extra divs every time, as mentioned in another answer!
You can also combine borders with outlines, and if you like, box-shadows (also shown via link)
<head>
<style type="text/css" ref="stylesheet">
div {
width:22px;
height:22px;
outline:1px solid black;
}
</style>
</head>
<div>
outlined
</div>
Usually by default, 'border:' puts the border on the outside of the width, measurement, adding to the overall dimensions, unless you use the 'inset' value:
div {border: inset solid 1px black};
But 'outline:' is an extra border outside of the border, and of course still adds extra width/length to the element.
Hope this helps
PS: I also was inspired to make this for you : Using borders, outlines, and box-shadows
IsisCode gives you a good solution. Another one is to position border div inside parent div. Check this example http://jsfiddle.net/A2tu9/
UPD: You can also use pseudo element :after (:before), in this case HTML will not be polluted with extra markup:
.my-div {
position: relative;
padding: 4px;
...
}
.my-div:after {
content: '';
position: absolute;
top: -3px;
left: -3px;
bottom: -3px;
right: -3px;
border: 1px #888 solid;
}
Demo: http://jsfiddle.net/A2tu9/191/
Why not simply using background-clip?
-webkit-background-clip: padding;
-moz-background-clip: padding;
background-clip: padding-box;
See:
http://caniuse.com/#search=background-clip
https://developer.mozilla.org/en-US/docs/Web/CSS/background-clip
https://css-tricks.com/almanac/properties/b/background-clip
I shared two solutions depending on your needs:
<style type="text/css" ref="stylesheet">
.border-inside-box {
border: 1px solid black;
}
.border-inside-box-v1 {
outline: 1px solid black; /* 'border-radius' not available */
}
.border-outside-box-v2 {
box-shadow: 0 0 0 1px black; /* 'border-style' not available (dashed, solid, etc) */
}
</style>
example: https://codepen.io/danieldd/pen/gObEYKj
Way late, but I just ran into a similar issue.
My solution was pseudo elements - no additional markup, and you get to draw the border without affecting the width.
Position the pseudo element absolutely (with the main positioned relatively) and whammo.
See below, JSFiddle here.
.hello {
position: relative;
/* Styling not important */
background: black;
color: white;
padding: 20px;
width: 200px;
height: 200px;
}
.hello::before {
content: "";
position: absolute;
display: block;
top: 0;
left: -5px;
right: -5px;
bottom: 0;
border-left: 5px solid red;
border-right: 5px solid red;
z-index: -1;
}
Put your div inside another div, apply the border to the outer div with n amount of padding/margin where n is the space you want between them.