How to design "overflowing" border lines of css <div>? - html

Just getting into css and, though trying different approaches, I don't manage to design a content box with the borders I have in mind. It should look something like this:
In words: The borders should cross each other and continue for some maybe 30px, maybe we can call it overflow. Resulting in crosses at all four edges.
I have tried to design small cubic boxes each at every edge, and it kinda works. But I find it very hard to include them in my concept of responsiveness, as they don't shrink at the same rate that the actual box (lets call it <box>) does. The <box> has side margins in percent, so when the page is being scaled down, the small boxes <sbox> are in my way and preventing the margins of <box> from reaching out all the way to the frames borders.
Any ideas on how to make that one more elegant?

You can do this using the help of before and after pseudo classes.
* { box-sizing:border-box; }
.box { padding:20px; width:100px; height:100px; position:relative; border-left:2px solid #000; border-right:2px solid #000; }
.box::after { position:absolute; top:5px; left:-7px; background:#000; width:110px; height:2px; content:"";}
.box::before { position:absolute; bottom:5px; left:-7px; background:#000; width:110px; height:2px; content:"";}
<div class="box">
Content
</div>
Demo

An example without pseudo classes
.outer{
height: 1em;
margin: 0 1em 0 1em;
}
.content{
border: 1px solid #000;
border-left: none;
border-right: none;
}
.innerContent{
margin: 0 1em 0 1em;
}
.borderLeftRight{
border-left: 1px solid #000;
border-right: 1px solid #000;
}
<div class="outer borderLeftRight"></div>
<div class="content">
<div class="innerContent borderLeftRight">
Content
</div>
</div>
<div class="outer borderLeftRight"></div>

Somebody already did something similar. I think the most elegant way is with pseudo selectors :before & :after. I feel you should do it in this way and not with wrappers. Most important things are setting your element's position to relative and then before and after selectors position to absolute. Then fiddle with border and top, bottom, left, right properties.
.box {
display: inline-block;
position: relative;
padding: 2em;
}
.box:after,
.box:before {
position: absolute;
content: "";
}
.box:after {
border-top: 1px solid #f00;
border-bottom: 1px solid #f00;
left: 0;
right: 0;
top: 1em;
bottom: 1em;
}
.box:before {
border-left: 1px solid #f00;
border-right: 1px solid #f00;
left: 1em;
right: 1em;
top: 0;
bottom: 0;
}
<div class="box">
text inside
</div>

Just going to put this up here to show you can do this using a single pseudo element.
Fixed width
You will have to set the width and height for it, can get around this using calc but its support isn't amazing yet.
* {
box-sizing: border-box;
}
div {
width: 300px;
height: 200px;
border-top: 1px solid;
border-bottom: 1px solid;
margin: 100px;
position: relative;
padding: 10px 25px;
}
div:after {
content: "";
display: block;
position: absolute;
top: -20px;
left: 20px;
width: 260px;
height: 240px;
border-left: 1px solid;
border-right: 1px solid;
}
<div>Testing</div>
Auto width
Example using calc, this will work with any size of text.
* {
box-sizing: border-box;
}
div {
width: 300px;
height: 200px;
border-top: 1px solid;
border-bottom: 1px solid;
margin: 100px;
position: relative;
padding: 10px 25px;
}
div:after {
content: "";
display: block;
position: absolute;
top: -20px;
left: 20px;
width: calc(100% - 40px);
height: calc(100% + 40px);
border-left: 1px solid;
border-right: 1px solid;
}
<div>Hello</div>

Related

How to cut a corner with CSS when background image is necessary?

The above is an image of a project I'm working on. This is how far I got:
Creating the box was fairly simple; however, now I have NO IDEA how to create this cut corner on the bottom left. I've tried a bunch of things already and most things work if the background isn't transparent but a block of color. Since the background needs to be this image, I can't make the cut corner work without having one side show a certain color. This is my code:
<div class="profile">
// HTML content
</div>
<style>
profile {
border: 2px solid #fff;
color: #fff;
height: 100%;
padding: 10px;
width: 250px;
</style>
I've tried multiple things already, such as this here (not the exact code I used, but I followed this example):
.cut {
border: none;
position: relative;
}
.cut:before {
content: "";
position: absolute;
bottom: 0;
right: 0;
border-bottom: 20px solid lightgrey;
border-left: 20px solid #e67e22;
width: 0;
}
This creates a cut corner, but with a block of a solid color and I need the image to be shown, not the color.
Does anyone have a clue how to do this? Suggestions are much appreciated. Thanks!
You may use before/after element to make the bottom part like this :
.profile {
position:relative;
display:inline-block;
margin:50px;
border:1px solid #000;
border-bottom:none;
width:100px;
height:200px;
background:#ccc;
}
.profile:after {
content:" ";
position:absolute;
border:1px solid #000;
height:20px;
width:80px;
bottom:-20px;
right:-1px;
border-top:0;
border-left:0;
background:#ccc;
}
.profile:before {
content:" ";
position:absolute;
border-bottom:1px solid #000;
height:29px;
width:29px;
transform:rotate(45deg);
bottom:-15px;
left:6px;
background:#ccc;
}
<div class="profile"></div>
the bottom is split into tow part : a rectangle with only two border + a square with one border rotated with 45°
Hope it helps
NB : Becarefull when changing the dimensions
.profile {
position: relative;
display: inline-block;
width: 200px;
height: 200px;
border-top: 2px solid #000;
border-left: 2px solid #000;
border-right: 2px solid #000;
padding: 20px;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
.profile h2 {
margin: 0 0 10px;
}
.profile p {
font-size: 14px;
}
.profile .bottom {
position: absolute;
bottom: -30px;
right: -2px;
width: 180px;
height: 30px;
border-bottom: 2px solid #000;
box-sizing: border-box;
border-right: 2px solid #000;
}
.profile .bottom::before {
content: '';
position: absolute;
left: -10px;
bottom: -4px;
width: 2px;
height: 35px;
background-color: #000;
transform: rotate(-35deg);
}
<div class="profile">
<h2>Name</h2>
<p>Description</p>
<div class="bottom"></div>
</div>
I think you're trying to cut the corner of an image instead of div, so you can do something like this:
body {
background: url('https://www.lunapic.com/editor/premade/o-paint-bucket.gif');
}
.container {
display: inline-block;
width: 200px;
height: 300px;
overflow: hidden;
}
.container .image_container {
width: 320px;
height: 550px;
overflow: hidden;
display: block;
position: relative;
transform: rotate(45deg);
margin-left: calc(260px - 360px);
margin-top: -40px;
}
.container .image_container .image {
transform: rotate(-45deg);
margin-top: 10px;
margin-left: -10px;
}
<div class="container">
<div class="image_container">
<div class="image">
<img src="https://www.w3schools.com/css/img_fjords.jpg" />
</div>
</div>
</div>

CSS border collapse different sizes in a div

Please take a look here:
http://jsfiddle.net/ztu267zp/1/
border:3px solid grey;
border-bottom: 8px solid red;
At the bottom corners you can see, that both the grey and the red borders intersect diagonally.
Can I cut the grey border to end at the bottom of the DIV and the red border having 100% width over the full distance?
Thank you very much,
Doing it right now with box-shadows, but also here, there is no clean edge in Chrome and FF:
http://imgur.com/mf7ABEO
Thanks
matt
its not possible but you can use something like this
<div id="bord">
<div class="line-cover">
</div>
css
#bord{
height:200px;
width:200px;
border:3px solid grey;
border-bottom: 8px solid white;
}
.line-cover{
position: relative;
border-bottom: 8px solid red;
width: 100%;
top: 200px;
padding: 0 3px;
left: -3px;
}
Fiddle here
What about st. like that, using pseudoelement after?
#bord{
height:200px;
width:200px;
border:3px solid grey;
border-bottom: 0;
/*border-bottom: 8px solid red;*/
position: relative;
}
#bord:after {
display: block;
background: red;
height: 8px;
width: 100%;
content: '';
position: absolute;
bottom: -8px;
left: 0;
margin: 0 -3px;
padding: 0 3px;
}
http://jsfiddle.net/ztu267zp/4/

What is causing this oblique border issue?

I am trying to remove oblique border issue, best to show it in a picture:
Here is the css applied to the div:
.blog_post {background: #fff}
.blog_post .post {
border-right-color: #F1F1F1;
border-top-color: #FF0000;
}
.blog_post .post, .blog_post .sidebar {
background: none repeat scroll 0 0 #FFFFFF;
border-color: #FFFFFF;
border-width: 10px;
}
.blog_post .post {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background: none repeat scroll 0 0 #9A9570;
border-color: #8F8960 #8F8960 -moz-use-text-color;
border-image: none;
border-style: solid;
border-width: 10px;
float: left;
margin: -560px 0 0 -12px;
padding: 28px 30px;
position: relative;
width: 528px;
z-index: 9;
}
Any help would be greatly appreciated.
Easy way: Another container
You can't do this with traditional HTML borders as they work at shown above (that's how CSS triangles work!). The easiest way to get this effect is to wrap the element in another container.
Demo
HTML
<div class="container">
<div class="inner-container">
...
</div>
</div>
CSS
.container {
border-top:10px solid red;
border-bottom:10px solid red;
}
.inner-container {
border-left:10px solid blue;
border-right:10px solid blue;
}
Hard way: :before and :after
This method is a little more tricky but you can manage to pull it off with only one wrapping element.
Demo
HTML
<div class="container">
...
</div>
CSS
.container {
border-top:10px solid red;
border-bottom:10px solid red;
position:relative;
/* pad out the left and right to allow room for the border */
padding:0 10px;
}
.container:before,
.container:after {
position:absolute;
top:0;
bottom:0;
width:10px;
background-color:blue;
display:block;
content:"";
}
.container:before {
left:0;
}
.container:after {
right:0;
}
You can always use inset box shadows. They are pretty easy to use, and they don't require much CSS, nor do you have to change the HTML.
Check it out. jsFiddle here
div {
box-shadow: inset 0px 10px 0px red;
border: 10px solid blue;
border-top: 0px;
}
Using pseudo-classes :before and :after
.border-fixed {
width: 300px;
height: 300px;
background: #EEE;
margin: 60px auto 0;
border: solid 10px #DDD;
border-top-color: #BBB;
position: relative;
}
.border-fixed:before,
.border-fixed:after {
content: "";
top: -10px;
left: -10px;
position: absolute;
width: 10px;
height: 10px;
background: #BBB;
}
.border-fixed:before {
right: -10px;
left: auto;
}

How do I simplify this header with lines on the side, so that it doesn't need css3 background properties?

I have a css class for centering a heading, and adding vertically centered lines on either side. Problem is, it uses css3 background properties, and not every browser supports those. So I'd like to simplify this for cross browser compatibility, but am not sure how to do that.
Is there a simpler way to achieve this, without the css3 background (and without adding any extra elements or static heights/widths)?
demo here
.section-heading {
display: table;
white-space: nowrap;
}
.section-heading:before {
background: linear-gradient(to bottom, black, black) no-repeat left center / 95% 1px;
content: "";
display: table-cell;
width: 50%;
}
.section-heading:after {
background: linear-gradient(to bottom, black, black) no-repeat right center / 95% 1px;
content: "";
display: table-cell;
width: 50%;
}
<h2 class="section-heading">Example</h2>
You can use fieldset and legend, it's not very beautiful code but you don't need CSS3
http://jsfiddle.net/dASCv/9/
fieldset {
text-align: center;
border: none;
border-top: 1px solid black;
}
legend {
padding: 20px;
}
<fieldset>
<legend>
<h2>Example</h2>
</legend>
</fieldset>
OR this other method whit :after and :before
http://jsfiddle.net/dASCv/10/
div {
text-align: center;
}
h2:before,
h2:after {
border-top: 1px solid black;
display: block;
height: 1px;
content: " ";
width: 40%;
position: absolute;
left: 0;
top: 1.4em;
}
h2:after {
right: 0;
left: auto;
}
<div>
<h2>text TEXT</h2>
</div>
There is my best try.
I have a little isue that I have corrected in Chrome; but I really don't know even why it works.
The CCS is
.test {
display: table-row;
white-space: nowrap;
line-height: 0px;
}
.test:before,
.test:after {
border-color: transparent;
border-top: solid black 1px;
border-left: solid transparent 1px;
border-bottom: solid rgba(0,0,0,0.01) 11px;
border-right: solid transparent 1px;
content: "";
display: table-cell;
width: 50%;
}
.test:before {
border-right: solid 30px transparent;
}
.test:after {
border-left: solid 30px transparent;
}
I am using the border to display the black line, and to have it positioned in place I have reduced the height of the table to 0.
fiddle
In the fiddle, I have kept your original demo, so that you can compare side by side.
And now, the weird part. change rgba(0,0,0,0.01) in the border bottom to rgba(0,0,0,0.001), and it will break (at least in Chrome).
I really would like to understand that ...
new answer
All the above was asuming that the requirement was to have a transparent style (that is , that it was posible to set a background that could be seen thru the h1. If this is not the case, there is another posible solution, using box-shadow instead of gradient barckground:
.test {
display: table-row;
white-space: nowrap;
}
.test:before,
.test:after {
border: solid white 10px;
content: "";
display: table-cell;
width: 50%;
height: 10px;
line-height: 10px;
box-shadow: inset 0px 5px white, inset 0px 6px black;
}
.test:before {
border-right-width: 10px;
border-left-width: 1px;
}
.test:after {
border-left-width: 10px;
border-right-width: 1px;
}
new demo
1-element solution
FIDDLE
div {
display: inline-block;
background: #fff;
padding: 0 10px;
}
div:before {
content: '';
display: block;
position: absolute;
left: 0;
width: 100%;
height: 20px;
border-bottom: 1px solid #c2c2c2;
margin-top: -9px;
z-index: -1;
}
<div>A header</div>
(NB: for this solution to work, you need to set text-align:center on its parent element)
2-element solution (works over a background image)
FIDDLE
.splitHR {
text-align: center;
overflow: hidden;
}
.splitHRText {
display: inline-block;
position: relative;
padding: 0 10px;
}
.splitHRText:before,
.splitHRText:after {
content: '';
display: block;
width: 1000px;
position: absolute;
top: 0.73em;
border-top: 1px solid #c2c2c2;
}
.splitHRText:before {
right: 100%;
}
.splitHRText:after {
left: 100%;
}
<div class="splitHR">
<span class="splitHRText">A header</span>
</div>
Please add Prefix for the CSS
For Webkit browswers
-webkit-gradient
Firefox
-moz-linear-gradient
IE
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000');
More Details Here http://webdesignerwall.com/tutorials/cross-browser-css-gradient
Using this way also you can achieve the answer, please check this
<p style="display:block; width:100%; text-align:center;
border-bottom:1px #ccc solid; line-height:3px">
<span style="background-color:red; ">Examples</span></p>
http://jsfiddle.net/dASCv/11/
css
h2 {
border-bottom: 1px solid black;
text-align: center;
margin: 0;
padding: 0;
}
h2 span {
display: inline-block;
position: relative;
padding: 0 20px;
bottom: -15px;
background-color: white;
}
markup
<h2><span>text Textsssssssssssssssss</span></h2>
You could set the bottom for span in percentage if you set the height for h2.
you can use this code:
JsFiddle DEMO
HTML:
<h2><span>Title is here</span></h2>
CSS:
h2{
display:block;
text-align:center;
line-height:30px;
}
h2 span{
background:#fff;
padding:0 10px;
}
h2:after{
content:"";
display:block;
border-top:1px solid black;
margin-top:-15px;
}
Update:
you can use this code too: (single element)
JsFiddle
HTML:
<h2>Title is here</h2>
CSS:
h2{
display: table;
margin: 0 auto;
text-align: center;
line-height: 30px;
padding: 0 10px;
background: #FFF;
}
h2:after{
content:"";
display:block;
border-top:1px solid;
margin-top:-15px;
position:absolute;
width:100%;
left:0;
z-index:-1;
}

any border blank space trick ? for CSS

I need some trick to insert border blank space by using CSS like this..
I using CSS box-shadow like this
box-shadow:
-1px 0px 0px 0px #000,
0px -1px 0px 0px #000,
0px 1px 0px 0px #000,
1px 1px 0px 0px #000
I have no idea how to make border / shadow look like the picture.
I will use only one html element.. <div></div>
Any trick ?
Playground : http://jsfiddle.net/ES66k/
with one div only: http://jsfiddle.net/ES66k/1/ (tested on Fx18 and chrome)
div {
width:300px;
height:170px;
margin:100px;
border-top: 1px black solid;
border-bottom: 1px black solid;
position: relative;
}
div:after, div:before {
content: "";
position: absolute;
top: -1px;
width: 20px;
height: 172px;
border-top: 40px white solid;
border-bottom: 40px white solid;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
div:before { border-left: 1px black solid; left: 0; }
div:after { border-right: 1px black solid; right: 0; }
It's bit hacky, anyway, since it's relying on a fixed height and on a solid color as background (white) but maybe could be useful for your purpose.
You can create 4 <div>'s with classes .top-left, .top-right, .bottom-left and .bottom-right. Make them absolute and the container relative. Size them, make them the color of the containers bg-color and get them to the corners with top, right, bottom and left properties. Their value must be minus the border width.
Here is example of element with 3px border:
HTML:
<div class="box">
<div class="corner top-left"></div>
<div class="corner top-right"></div>
<div class="corner bottom-left"></div>
<div class="corner bottom-right"></div>
</div>
CSS:
.box{
width: 300px;
height: 300px;
border: 3px solid #666;
position:relative;
}
.corner{
width: 10px;
height: 10px;
background-color: #fff;
position: absolute;
}
.top-left{
top: -3px;
left: -3px;
}
.top-right {
top: -3px;
right: -3px;
}
.bottom-left{
bottom: -3px;
left: -3px;
}
.bottom-right{
bottom: -3px;
right: -3px;
}
Try to use the CSS3 attribute border-image:
Here's a demo you can have a look and try out yourself: CSS3 border-image
div {
width:300px;
height:170px;
margin:100px;
position:relative;
background:#ccc;
}
div:before, div:after {
content:"";
position:absolute;
top:0;
left:0;
}
div:before {
width:280px; /*****-20px*****/
height:168px; /*****-2px*****/
margin-left:10px;
border-top:1px solid #f00;
border-bottom:1px solid #f00;
}
div:after {
width:298px; /*****-2px*****/
height:150px; /*****-20px*****/
margin-top:10px;
border-left:1px solid #f00;
border-right:1px solid #f00;
}
Demo : http://jsfiddle.net/ES66k/4/
Done now, Don't need to set background-color :D
But thanks #Fabrizio Calderan anyway :D