simulate "inner border" in CSS? - css

I have the following 4 divs with the CSS below. The problem is, the border on the red span draws over the others. How can I avoid this? I tried adding margins to spanRed, even negative margins, neither of which worked.
http://jsfiddle.net/eh9rM/
Bonus points This doesn't work in IE (8,9 tested) at all... only the blue div shows up. :)
<div id="spanBlue"></div>
<div id="spanGreen"></div>
<div id="spanOrange"></div>
<div id="spanRed"></div>
#spanBlue {position: fixed;
top: 0px; left: 0px;
height: 100%;
width: 10%;
background-color: #4D9DB8;
border-right: 10px solid #045B6F;
z-index: 1;}
#spanGreen {position: fixed;
top: 0px; left: 0px;
height: 10%;
width: 100%;
background-color: #A4AC79;
border-bottom: 10px solid #34655F;
z-index: 1;}
#spanOrange {position: fixed;
top: 0px; left: 0px;
height: 10%;
width: 10%;
background-color: #FA9D26;
border-right: 10px solid #045B6F;
z-index: 2;}
#spanRed {position: fixed;
bottom: 0px; right: 0px;
height: 90%;
width: 90%;
background-color: WHITE;
margin-top: 20px;
margin-left: 20px;
border-top: 10px solid #B52024;
border-left: 10px solid #B52024;
z-index: 3;}

You have two options:
Add div { box-sizing: border-box }. This switches the elements to the 'traditional' model, where borders and paddings are included in the width (supported from IE8+)
Use the Flexible Box model (IE10+)
Add the borders as pseudo-elements (IE8+)
Using pseudo-elements (remove the border from #spanRed):
#spanRed:after {
content:' ';
display:block;
position:absolute;
left:0;
top:0;
right:0;
bottom:0;
border:4px solid red;
}
Bear in mind that using position:fixed as the basis for a layout is very fragile.
edit: if you need IE7 support, add the extra element via JS:
$('#spanRed').append('<span class="after" />')
Then reference it in the CSS. Be aware that you have to repeat the whole style, you can't use both selectors together otherwise IE7 ignores the rule.
Or, since these are all "useless" elements anyway, just add it to the HTML:
<div id="spanRed">
<span class="inner"></span>
</div>
Here's your code using that: http://jsfiddle.net/eh9rM/2/

Related

Overflow: hidden property not working for after and before pseudo classes

Basically I am trying to create a hexagonal shape, which would have a circle inside it and the extra parts of the circle should be hidden.
Demo: https://codepen.io/AskSaikatSinha/pen/jwXNPJ?editors=1100
My HTML:
<div class="container">
<div class="radius-rect"></div>
<div class="hex">
<div id="hexagon" >
<div class="semi-cir" ></div>
</div>
</div>
</div>
My CSS:
#hexagon {
width: 100px;
height: 55px;
background: #0088CD;
position: absolute;
border-top: 1px solid #0088CD;
border-bottom: 1px solid #0088CD;
border-radius: 2px;
}
#hexagon:before {
content: "";
position: absolute;
top: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 25px solid #0088CD;
border-radius: 2px;
}
#hexagon:after {
content: "";
position: absolute;
bottom: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 25px solid #0088CD;
border-radius: 2px;
}
.semi-cir{
position: relative;
left: 10px;
background-color:#00A9F1;
height:100px;
width:100px;
-webkit-border-radius:75px;
-moz-border-radius:75px;
z-index: 1;
overflow: hidden;
}
The overflow: hidden does not have any effects.
Try to give background color as same as it is given to 'semi-cir'.
same trick is applied on link provided by you : https://codepen.io/AskSaikatSinha/pen/jwXNPJ?editors=1100
#hexagon {
width: 100px;
height: 55px;
background: #0088CD;
position: absolute;
top:50px;
left:50px;
border-top: 1px solid #0088CD;
border-bottom: 1px solid #0088CD;
border-radius: 2px;
}
#hexagon:before {
content: "";
position: absolute;
top: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 25px solid #0088CD;
border-radius: 2px;
}
#hexagon:after {
content: "";
position: absolute;
bottom: -25px;
left: 0;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 25px solid #0088CD;
border-radius: 2px;
}
.semi-cir{
position: relative;
left: 10px;
background-color:#00A9F1;
height:100px;
width:100px;
-webkit-border-radius:75px;
-moz-border-radius:75px;
z-index: 1;
overflow: hidden;
}
.radius-rect{
height:200px;
background:#00a9f1;
}
<div class="container">
<div class="radius-rect"></div>
<div class="hex">
<div id="hexagon" >
<div class="semi-cir" ></div>
</div>
</div>
</div>
See the definition of the overflow property by MDN:
The overflow CSS property specifies whether to clip content, show scrollbars, or display overflowing content when it is too large for its block-level container.
(...)
hidden: Content is clipped if necessary to fit the content box. No scrollbars are provided.
The content of the element is clipped. Properties like background, border are part of the elements so are not clipped. You would have to apply overflow: hidden on the parent (#hexagon) to hide what is exceeding of the childreen (.semi-cir).
However, I do not know what you are thing to render precisely. If you simply want a "semi-circle" like the class name suggest, you can wrap your full circle in a parent with overflow just big enough to hide one of its half.
If you make like the circle is inside the hexagon with a non-linear separation, you can stack several of the "circles parts" described above.
But all of this is definitively over-engineered, and overflow is not the right property for that. You can take a look at the clip and clip-path properties that were made for this usecase.
The clip CSS property defines what portion of an element is visible. The clip property applies only to absolutely positioned elements, that is elements with position:absolute or position:fixed.
-- https://developer.mozilla.org/en/docs/Web/CSS/clip
The clip-path CSS property prevents a portion of an element from getting displayed by defining a clipping region to be displayed i.e, only a specific region of the element is displayed. The clipping region is a path specified as a URL referencing an inline or external SVG, or shape method such as circle(). The clip-path property replaces the now deprecated clip property.
-- https://developer.mozilla.org/en/docs/Web/CSS/clip-path
Here are some great article about it:
https://css-tricks.com/clipping-masking-css/
https://www.html5rocks.com/en/tutorials/masking/adobe/
However, be careful of the browser support. clip is deprecated and clip-path is not supported by IE and Edge.

How to vertical align element relative other element with pseudo-class? [duplicate]

This question already has answers here:
Vertically center two divs inside a wrapper (with dynamic content and content below the wrapper)
(4 answers)
Closed 7 years ago.
I have multiple boxes (items) with an item number and a description. I want to vertical align my item number regardless of the description height (using only HTML and CSS).
See this image for more info:
<div class="item">
<div class="item-number">1</div>
<div class="item-description">Text placeholder</div>
</div>
As you can see I have multiple boxes and the description text can have different lengths, so I can't absolute position my item number relative to the top.
Any one got any suggestions on how to achieve this?
you should position your number absolutely in order to achieve this. You could also minimalize markup by using a pseudo element, allowing you to do this with a single element.
I have also used a data-attr in order to allow you to dynamically alter the number within the div if you so wish.
Something like:
div {
width: 200px;
border: 5px solid lightgray;
box-shadow: 5px 5px 10px dimgray;
margin: 20px;
padding: 20px;
padding-left: 30px;
position: relative;
}
div:before {
content: attr(data-pointNum);
position: absolute;
top: 50%;
left: -5px;
box-sizing: border-box;
height: 40px;
width: 40px;
transform: translate(-50%, -50%);
text-align: center;
line-height: 30px;
border-radius: 50%;
border: 5px solid tomato;
background: white;
box-shadow: 5px 5px 10px dimgray;
}
<div data-pointNum="1">some text</div>
<div data-pointNum="2">some moretext
<br/>spanning multiple
<br/>lines</div>
Demo: http://jsfiddle.net/UI_Designer/xzmzpL4g/1/
.item{
border:1px solid #000;
padding:20px;
margin-left:20px;
position:relative;
margin-bottom:20px;
}
.item-number{
position: absolute;
left:-10px;
width:20px;
height:20px;
border:1px solid #ccc;
border-radius:50%;
text-align:center;
background:#FFF;
top: 40%;
transform: translate(-20%,0);
}
you can try this
$(document).ready(function() {
$('.item-number').css('top', ($('.item').height() / 2) + 'px');
});
.item-description {} .item {
border: 1px solid #000;
width: 150px;
padding-left: 20px;
}
.item-number {
position: absolute;
left: 5px;
width: 20px;
height: 20px;
border: 1px solid #ccc;
border-radius: 50%;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="item">
<div class="item-number">1</div>
<div class="item-description">Text placeholder Text placeholder Text placeholder Text placeholder</div>
</div>
and this is demo
https://jsfiddle.net/0xf5hvej/
of course you will need more styling to achieve what you need but this is the basic

CSS multiple borders aren't changed in dynamic width

I written multiple borders using pseudo elements way of CSS-tricks.
<span class="something">
label: <span id="count">20</span>
</span>
CSS style is like this:
.something {
background-color: #B3B3B3;
padding: 10px;
position: relative;
border: 2px solid #000;
}
.something:before {
content: " ";
display: block;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
border: 2px solid #FF6666;
}
It looks good. but if I changed count, it cause change width and inner border isn't changed like this:
You can see this demo at jsfiddle.
How can I fix it?
do you really need flexible width of element .something? if the element .something would have fixed width, the problem would be solved:
.something {
display: block;
width: 200px;
background-color: #B3B3B3;
padding: 10px;
position: relative;
border: 2px solid #000;
}
HOWEVER
if you need flexible width, you should redraw the .something:before element after you increase the .something width. i updated jsfiddle for that - check it out.
the <div id="container"> is added only because jsFiddle does not support $(document) modifications.

How can I keep the progress bar number value centered and on top?

I have a set of progress bars displaying different values in real time. My only problem is that I can't seem to figure out how to keep the number value in the center of the bar, as well as on top at all times. Right now it's being pushed 'ahead' of the blue bar, and disappears when it goes outside the right side of the bar.
Here's how it looks:
Markup:
<td class="gridTableCell">
<div style='position: relative' class='progress progress-info'>
<div class='bar' id='signalRdepthRangePercentage-#:ViewUnitContract.ConveyanceId #' style='width: #: DepthRangePercentage#%'>
</div>
<span class='gridSpan' id='signalRdepth-#:ViewUnitContract.ConveyanceId #'>#: ViewUnitContract.CurrentRun.LatestWellLogEntry.Depth#</span>
<span class='hidden' id='signalRMaxDepthRange-#:ViewUnitContract.ConveyanceId #'>#: MaxDepthRange#</span>
<span class='hidden' id='signalRMinDepthRange-#:ViewUnitContract.ConveyanceId #'>#: MinDepthRange#</span>
</div>
</td>
And my css 'gridSpan':
.gridSpan {
position: absolute;
top: 0px;
z-index: 2;
text-align: center;
color: #676767;
width: 100%
}
The first of the three spans is the one that displays the number value inside the bar.
Any suggestions how I can keep this centered at all times, and not pushed in front of the blue filler with a huge margin?
Do something like the following:
FIDDLE
The outer element has text-align:center
The gridSpan element has display:inline-block (not absolutely positioned)
The inner element (with the blue % progress) needs to be absolutely positioned, so as not to be effected by the text-align:center.
Markup:
<div class="outer">
<span class="inner"></span>
<span class="gridSpan">9048.343</span>
</div>
CSS
.outer
{
width: 70%;
margin:20px;
height: 30px;
border: 1px solid gray;
overflow: hidden;
border-radius: 15px;
position:relative;
text-align: center;
}
.inner
{
background: aqua;
display:inline-block;
position:absolute;
left:0;
width: 20%;
height: 30px;
}
.gridSpan {
display:inline-block;
margin-top: 5px;
color: #676767;
position: relative;
z-index:2;
}
Alternatively, if you knew the width of the value you could do this by adding display:block;left:0;right:0 and margin:0 auto to your class:
.gridSpan {
display:block;
position: absolute;
margin: 0 auto;
top: 0px;
left:0;
right:0;
z-index: 2;
color: #676767;
width: x px; /*(width of value)*/
}
Actually, I finally figured this out based on this fiddle:
http://jsbin.com/apufux/2/edit (Wonder why I've never seen this post before!?)
Seems that I was missing some style overrides to the .bar and .progress part:
.progress {
position: relative;
}
.bar {
z-index: 1;
position: absolute;
}
.progress span {
position: absolute;
top: 0px;
z-index: 2;
text-align: center;
color: #676767;
width: 100%
}
Anyways, thanks for your effort! :)

Floating container overlaps bottom div, but not top

Hey i'm a little unsure about this structure.
Essentially I want to a have 4 divs.
<div class="container">
<div class="top-border"></div>
<div class="box"></div>
<div class="bottom-border"></div>
</div>
The container holds the three smaller divs. My goal is to have the box div hold the content, and the border divs create a bracket around the box. Border-top will be floated to the left, and border-bottom will be floated to the right. The only issue is that the container overlaps the bottom bracket, but not the top. I don't want it to overlap either... Is there a way to fix this?
Here is a JsFiddle: http://jsfiddle.net/6ghzN/
On the bottom-border div, change
margin-top: -40px;
to
margin-bottom: -8px;
I would go a different way,
Just add .box:before and .box:after
This way, you don't have all those extra divs to be marked up!
.container{
background:#dedede;
width:80%;
height:auto;
float:left;
}
.box{
height:800px;
width:100%;
color:#cecece;
float:left;
position:relative;
}
.box:before{
content: "";
width: 40px;
height: 40px;
border-left: 8px solid gray;
border-top: 8px solid gray;
position: absolute;
left: -8px;
top: -8px;
}
.box:after{
content: "";
width: 40px;
height: 40px;
border-right: 8px solid gray;
border-bottom: 8px solid gray;
position: absolute;
right: -8px;
bottom: -8px;
}
http://jsfiddle.net/6ghzN/11/
I had success using this method:
1) Remove background color from .container and add it to .box.
.box{
...
background:#dedede;
}
2) Add a negative margin to the right of .top-border so that .box floats correctly:
.top-border{
...
margin-right:-40px;
}
http://jsfiddle.net/6ghzN/2/
Add margin-bottom: -10px; to bottom-border class.
jsfiddle

Resources