Border length smaller than div width? - html

I have following code
div {
width: 200px;
border-bottom: 1px solid magenta;
height: 50px;
}
<div></div>
The div width is 200px so border-bottom is also 200px but what should I do if I want border-bottom only 100px without changing div width?

You can use pseudoelements. E.g.
div {
width : 200px;
height : 50px;
position: relative;
z-index : 1;
background: #eee;
}
div:before {
content : "";
position: absolute;
left : 0;
bottom : 0;
height : 1px;
width : 50%; /* or 100px */
border-bottom:1px solid magenta;
}
<div>Item 1</div>
<div>Item 2</div>
No need to use extra markup for presentational purpose. :after is also supported from IE8.
edit:
if you need a right-aligned border, just change left: 0 with right: 0
if you need a center-aligned border just simply set left: 50px;

Another way to do this (in modern browsers) is with a negative spread box-shadow. Check out this updated fiddle: http://jsfiddle.net/WuZat/290/
box-shadow: 0px 24px 3px -24px magenta;
I think the safest and most compatible way is the accepted answer above, though. Just thought I'd share another technique.

I added line under under h3 tag like this
<h3 class="home_title">Your title here</h3>
.home_title{
display:block;
}
.home_title::after {
display:block;
clear:both;
content : "";
position: relative;
left : 0;
bottom : 0;
max-width:250px;
height : 1px;
width : 50%; /* or 100px */
border-bottom:1px solid #e2000f;
margin:0 auto;
padding:4px 0px;
}

You can use a linear gradient:
div {
width:100px;
height:50px;
display:block;
background-image: linear-gradient(to right, #000 1px, rgba(255,255,255,0) 1px), linear-gradient(to left, #000 0.1rem, rgba(255,255,255,0) 1px);
background-position: bottom;
background-size: 100% 25px;
background-repeat: no-repeat;
border-bottom: 1px solid #000;
border-top: 1px solid red;
}
<div></div>

You cannot have a different sized border than the div itself.
the solution would be to just add another div under neath, centered or absolute positioned, with the desired 1pixel border and only 1pixel in height.
http://jsfiddle.net/WuZat/3/
I left the original border in so you can see the width, and have two examples -- one with 100 width, and the other with 100 width centered. Delete the one you dont wish to use.

Late to the party but for anyone who wants to make 2 borders (on the bottom and right in my case) you can use the technique in the accepted answer and add an :after psuedo-element for the second line then just change the properties like so:
http://jsfiddle.net/oeaL9fsm/
div
{
width:500px;
height:500px;
position: relative;
z-index : 1;
}
div:before {
content : "";
position: absolute;
left : 25%;
bottom : 0;
height : 1px;
width : 50%;
border-bottom:1px solid magenta;
}
div:after {
content : "";
position: absolute;
right : 0;
bottom : 25%;
height : 50%;
width : 1px;
border-right:1px solid magenta;
}

I did something like this in my project. I would like to share it here. You can add another div as a child and give it a border with small width and place it left, centre or right with usual CSS
HTML code:
<div>
content
<div class ="ac-brdr"></div>
</div>
CSS as below:
.active {
color: magneta;
}
.active .ac-brdr {
width: 20px;
margin: 0 auto;
border-bottom: 1px solid magneta;
}

This will help:
http://www.w3schools.com/tags/att_hr_width.asp
<hr width="50%">
This creates a horizontal line with a width of 50%, you would need to create/modify the class if you would like to edit the style.

I have case to have some bottom border between pictures in div container and the best one line code was - border-bottom-style: inset;

div{
font-size: 25px;
line-height: 27px;
display:inline-block;
width:200px;
text-align:center;
}
div::after {
background: #f1991b none repeat scroll 0 0;
content: "";
display: block;
height: 3px;
margin-top: 15px;
width: 100px;
margin:auto;
}

The border is given the whole html element. If you want half bottom border, you can wrap it with some other identifiable block like span.
HTML code:
<div> <span>content here </span></div>
CSS as below:
div{
width:200px;
height:50px;
}
span{
width:100px;
border-bottom:1px solid magenta;
}

I just accomplished the opposite of this using :after and ::after because I needed to make my bottom border exactly 1.3rem wider:
My element got super deformed when I used :before and :after at the same time because the elements are horizontally aligned with display: flex, flex-direction: row and align-items: center.
You could use this for making something wider or narrower, or probably any mathematical dimension mods:
a.nav_link-active {
color: $e1-red;
margin-top: 3.7rem;
}
a.nav_link-active:visited {
color: $e1-red;
}
a.nav_link-active:after {
content: '';
margin-top: 3.3rem; // margin and height should
height: 0.4rem; // add up to active link margin
background: $e1-red;
margin-left: -$nav-spacer-margin;
display: block;
}
a.nav_link-active::after {
content: '';
margin-top: 3.3rem; // margin and height should
height: 0.4rem; // add up to active link margin
background: $e1-red;
margin-right: -$nav-spacer-margin;
display: block;
}
Sorry, this is SCSS, just multiply the numbers by 10 and change the variables with some normal values.

Border right length smaller than parent div
with pseudo-elements
#import url(https://fonts.googleapis.com/css?family=Raleway);
body{
font-family: 'Raleway', sans-serif;
}
div {
width : 200px;
height : 50px;
position: relative;
z-index : 1;
background-color: #f5f5f5;
margin: 20px auto;
padding: 20px;
color:#726E97;
}
div:before {
content : "";
position: absolute;
right : 0;
top : 25%;
height : 50px;
width : 50%;
border-right:5px solid #726E97;
}
<div>BOX 1</div>

Related

Issue of small 1px, 2px spacing , while using border radius ( in all browsers )

Here is a screen for the question above http://prntscr.com/66o7rf .
When I use border-radius in parent div and overlap another background with same value of border-radius, there appears a small space ( in screen, white space appears ).
I tried using background in :before and :after , but i don't think is a good way to do it.
Can anybody help with this?
body{
background: grey;
}
div {
width: 200px;
height: 200px;
background: #000;
border-radius: 10px;
padding-top: 160px;
overflow: hidden;
}
.footer {
width: 200px;
height: 40px;
background: #f00;
}
<div>
<div class="footer"></div>
</div>
When you use border-radius in parent div, dont set overflow: hidden to it. And for overlapping div or child div of it add border-radius to it but 2px less than parent div.
Example:
div{
background: #fff;
border-radius: 10px;
}
.child{
background: blue;
border-radius: 8px;
}

CSS title underline without the text part

Maybe this has been answered before but I haven't found a proper solution for my question on SO.
Ok, here goes:
I need to underline an h3 tag with 100% EXCEPT for the part where the actual text is. like so:
My super title
____________________________________________________
This has to be 100% width, so something like
h3:after {
content:"\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0\a0";
}
with fixed number whitespaces wouldn't work.
Also the tag has to be pure. Meaning it is not surrounded with DIVs or span etc... (User submitted content from wysiwyg) It's just an h3 tag.
Is this possible in css3 at all? If not I'm willing to add some js to make it work.
UPDATE:
Here's a screenshot with expected result:
you could try in this way:
Markup
<h3>A 100% wide title</h3>
CSS
h3 {
display: block;
width: 100%;
background: #fff;
white-space: nowrap;
overflow: hidden;
}
h3:after {
content: "";
display: inline-block;
width: 100%;
margin-left: .5em;
border-bottom: 1px #767676 solid;
}
Example: http://cdpn.io/Awpef
Resulting effect
Like this? demo
h3:after {
content:"";
border-bottom: 1px solid black;
overflow: hidden;
width: 100%;
display: inline-block;
position: relative;
left: 150px;
top: -20px;
}
Try this:
CSS
h3 {
display:block;
width:100%;
overflow: hidden;
position: relative;
}
h3:after {
content:'';
border-bottom:1px solid black;
position: absolute;
width: 100%;
}
JSFIDDLE
Without using the :after pseudo element, you could do this:
<div id="wraptitle"><h3>A Second Title</h3></div>
CSS
#wraptitle {
border-bottom : 1px solid black;
position : relative;
}
#wraptitle h3 {
display : inline;
position : relative;
bottom : 0;
border-bottom : 1px solid white; /* white is actually the background color */
padding-right : 10px;
}
Make sure margin of the h3 element is set to 0, or its border wouldn't go over it.
A live example can be seen here: http://jsfiddle.net/JYNn5/

complete border right in css

I have this HTML
.container_1
{
width: 80%;
border: 5px solid black;
overflow: hidden;
}
.container_2
{
float: left;
border: 5px solid red;
width: 100%;
}
.container_1
{
width: 80%;
border: 5px solid black;
overflow: hidden;
}
.container_2
{
float: left;
border: 5px solid red;
width: 100%;
}
<div class="container_1">
<div class="container_2">
Content 1
</div>
<div class="container_2">
Content 2
</div>
</div>​
Fiddle http://jsfiddle.net/uZVnB/3/
The problem is that as you see in fiddle the border of .container_1 overlaps the border to the border of .container_2 , so is any form that show complete the border of both containers
Remove float & width from
.container_2. Write like this:
.container_2
{
border: 5px solid red;
}
Check this http://jsfiddle.net/uZVnB/4/
Change the width of .container_2 from 100% to 98%, and everything will be fine. When you set its width to 100%, naturally, it will expand to the maximum, and the borders will overlap.
Remove float: left and width: 100%, since block element fills the entire width of its container, it works fine.
If you have to use float: left style (although I don't think it is required since you have a width: 100% which makes float not behaving as it is defined), you could use box-sizing: border-box, but it only works for mordern browsers, lower version of IE does not support this property.
You may also use position: absolute; left: 0; right: 0; to absolutely position it, but it also conflicts with float: left style and IE6 does not support it.
You can achieve it by using CSS attribute box-sizing:border-box;
SEE DEMO
CSS:
.container_1 {
width:80%;
border:1em solid;
overflow: hidden;
}
.container_2 {
box-sizing:border-box;
-moz-box-sizing:border-box; /* Firefox */
-webkit-box-sizing:border-box; /* Safari */
width:100%;
border:1em solid red;
}
Considering IE in mind, here is my solution.
The border will always be extra than the 100% width.
Here is the solution http://jsfiddle.net/uZVnB/41/
Hope this helps

Looking to have a <hr> and <h3> together

Like the example above. I've found some helpful script with the a small img which I do like however I don't know how to get the padding about the title so the line doesn't go straight through.
h3.line {
background-attachment: scroll;
background-clip: border-box;
background-color: transparent;
background-image: url(../images/line.jpg);
background-origin: padding-box;
background-position: center;
background-repeat: repeat-x;
background-size: auto auto;
display: block;
margin-bottom: 20px;
}
Which shows this.
Any suggestion or ideas?
You can have a 1px dot image which you can place as a background on the H3. Then have a span element in between which have a background on.
CSS:
h3 {
background: url(images/dot.png) left center repeat-x;
padding: 10px;
text-align: center;
}
h3 span { background: #fff; display: inline-block; padding: 10px 15px; }
HTML:
<h3><span>About</span></h3>
You can put a <span> for example in your <h3> and make it have the same background as your <h3> but without the line so the <span> effectively overlaps the <h3>.
You can say this to your span:
span {
display: block;
margin-left: auto;
margin-right: auto;
}
to make it center. You can add width and height to it too. line-height helps place your text to the middle vertically.
If you want to spare images than you can use text-decoration: line-through; to draw a line through your text.
Here is a solution using the CSS border property instead of an image.
the html:
<h2>
<span>This is a test</span>
<div></div>
</h2>
And here is the CSS:
h2 {
text-align:center;
background-color:#EFEFEF;
line-height:26px;
position:relative;
}
span {
background-color:#EFEFEF;
padding-right:5px;
padding-left:5px;
position:relative;
z-index:2;
}
h2 > div {
border-bottom:1px solid grey;
position:relative;
z-index:1;
top:-13px; /* half the line-height of the containing element */
}
A fiddle Demonstration
The <div> is placed inside the heading element, and positioned half-way up by settings its top position to one-half the height of the heading element, which is the headings line-height. z-index is used on the span and div so that the span gets a higher stack order than the div and obscures the (border) line where there is overlap.
I just stumbled upon another way of achieving this.
h1
{
position: relative;
padding: 0 26%;
}
h1:before,
h1:after
{
width: 25%;
background-color: rgba( 0, 0, 0, .5 );
content: '';
position: absolute;
top: 0;
bottom: 0;
}
Taken from: http://osvaldas.info/blog/background-independent-css-bars

Border Height on CSS

I have a table TD and on the right of it I want to add a 1 pixel border, so I've done this:
table td {
border-right:1px solid #000;
}
It works fine but the problem is that the border's height takes the total TD's height.
Is there a way to set the height of the border?
I have another possibility. This is of course a "newer" technique, but for my projects works sufficient.
It only works if you need one or two borders. I've never done it with 4 borders... and to be honest, I don't know the answer for that yet.
.your-item {
position: relative;
}
.your-item:after {
content: '';
height: 100%; //You can change this if you want smaller/bigger borders
width: 1px;
position: absolute;
right: 0;
top: 0; // If you want to set a smaller height and center it, change this value
background-color: #000000; // The color of your border
}
No, there isn't. The border will always be as tall as the element.
You can achieve the same effect by wrapping the contents of the cell in a <span>, and applying height/border styles to that. Or by drawing a short vertical line in an 1 pixel wide PNG which is the correct height, and applying it as a background to the cell:
background:url(line.png) bottom right no-repeat;
Yes, you can set the line height after defining the border like this:
border-right: 1px solid;
line-height: 10px;
For td elements line-height will successfully allow you to resize the border-height as SPrince mentioned.
For other elements such as list items, you can control the border height with line-height and the height of the actual element with margin-top and margin-bottom.
Here is a working example of both:
http://jsfiddle.net/byronj/gLcqu6mg/
An example with list items:
li {
list-style: none;
padding: 0 10px;
display: inline-block;
border-right: 1px solid #000;
line-height: 5px;
margin: 20px 0;
}
<ul>
<li>cats</li>
<li>dogs</li>
<li>birds</li>
<li>swine!</li>
</ul>
Building on top of #ReBa's answer above, this custom-border class is what worked for me.
Mods:
working with border instead of backaground-color since background-color is not consistent.
Setting height & top of the properties of :after in such a way that the total comes up to 100% where bottom's value is implicit.
ul {
list-style-type: none;
display: flex;
flex-direction: row;
}
li {
padding: 10px;
}
.custom-border {
position: relative;
}
.custom-border:after {
content: " ";
position: absolute;
border-left: 1px #6c757d solid;
top: 35%;
right: 0;
height: 30%;
margin-top: auto;
margin-bottom: auto;
}
<ul>
<li class="custom-border">
Hello
</li>
<li class="custom-border">
World
</li>
<li class="custom-border">
Foo
</li>
<li class="custom-border">Bar</li>
<li class="custom-border">Baz</li>
</ul>
Good Luck...
No, you cannot set the border height.
This will add a centered border to the left of the cell that is 80% the height of the cell. You can reference the full border-image documentation here.
table td {
border-image: linear-gradient(transparent 10%, blue 10% 90%, transparent 90%) 0 0 0 1 / 3px;
}
Just like everyone else said, you can't control border height.
But there are workarounds, here's what I do:
table {
position: relative;
}
table::before { /* ::after works too */
content: "";
position: absolute;
right: 0; /* Change direction for a different side*/
z-index: 100;
width: 3px; /* Thickness */
height: 10px;
background: #555; /* Color */
}
You can set height to inherit for the height of the table or calc(inherit - 2px) for a 2px smaller border.
Remember, inherit has no effect when the table height isn't set.
Use height: 50% for half a border.
Demo
table {
border-spacing: 10px 0px;
}
.rightborder {
border-right: 1px solid #fff;
}
Then with your code you can:
<td class="rightborder">whatever</td>
Hope that helps!
Currently, no, not without resorting to trickery. borders on elements are supposed to run the entire length of whatever side of the element box they apply to.
.main-box{
border: solid 10px;
}
.sub-box{
border-right: 1px solid;
}
//draws a line on right side of the box.
later add a margin-top and margin-bottom.
i.e.,
.sub-box{
border-right: 1px solid;
margin-top: 10px;;
margin-bottom: 10px;
}
This might help in drawing a line on the right-side of the box with a gap on top and bottom.
table td {
border-right:1px solid #000;
height: 100%;
}
Just you add height under the border property.