Vertical line centered above div - html

I am trying to get this done in HTML and CSS. I am able to get the box done using the border and padding. But how do I get the line above?
Here is what I have so far:
.november {
padding: 1%;
border: 2px solid #000;
}
<div class="november">November 2014</div>

Pseudo element goodness
The HTML
It's a one liner:
<div>November 2014</div>
The CSS
The vertical line is created with a :before pseudo element:
The :before pseudo element is given position: absolute
left: 50% shifts the line to the middle and bottom: 100% pops the line above the div
The line is created by the 2px width
margin-left: -2px shifts the line 2px to the left to correctly offset its position (this is equal to the width)
The div is made position: relative and the position: absolute :before will position itself in relation to it. Space above the div is created with the top margin.
Complete Example
In this example, display: inline-block allows the div to expand and retract with its contents.
div {
padding: 10px;
border: solid 2px #000;
display: inline-block;
position: relative;
margin-top: 50px;
}
div:before {
content: '';
width: 2px;
height: 50px;
background: #000;
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -2px;
}
<div>November 2014</div>

I tried this and got it right:
body {
background: #EEE;
}
.november {
margin: 0;
padding: 1%;
border: 2px solid white;
clear: both;
}
<div class="col-sm-2">
<hr style="width: 2px; border-top: 50px solid white; padding: 0; text-align: center; margin: auto;" />
<div class="november">November 2014</div>
</div>

Related

Display block property is not working wants div below other div

I have two divs, div1 and div2. I wanted div2 below div1 because I have used position absolute property in div1. Div2 is going above div1
I wanted to use position absolute because I wants to position div1 at bottom left corner
.home {
position: absolute;
border: 2px solid green;
top: 50%;
left: 8px;
padding: 33px 23px;
line-height: 60px;
display: block;
}
.about {
display: block;
border: 2px solid red;
}
<div class="home">div1</div>
<div class="about">div2</div>
If you want to have the div1 in lower left corner, and want div2 below the div1, then:
you can enclose the 2 divs in another div, and have the position: absolute; for that div
and also, instead of top: 50%;, you can have bottom: 0.
This will make sure that, the parent div is placed exactly at the bottom left corner (and not on left edge).
And you can remove the position: absolute; from div1.
This way, you can be sure that div2 will appear below div1, as both these divs will not have position set as absolute, and they will appear relative to each other.
.home {
border: 2px solid green;
padding: 33px 23px;
line-height: 60px;
display: block;
}
.about {
display: block;
border: 2px solid red;
}
.bottomLeft {
position: absolute;
bottom: 0;
}
<div class="bottomLeft">
<div class="home">div1</div>
<div class="about">div2</div>
</div>
Edit 1: Modified top: 50%; to bottom: 0;, and improved formatting.
To immediately solve your problem, I would suggest wrapping both of these divs in another element, then positioning that new outer element absolutely at the bottom left:
.container {
/* position in the bottom left*/
position: absolute;
bottom: 0;
left: 0;
}
.home {
border: 2px solid green;
padding: 33px 23px;
line-height: 60px;
}
.about {
border: 2px solid red;
}
<div class="container">
<div class="home">div1</div>
<div class="about">div2</div>
</div>
There are many ways to solve the same problem in CSS however, and which is the best largely depends on the context. There's not really enough information in your question to give a definitive answer, or even to be sure the above will work as expected in your case.

How to add border-bottom with a triangle on a div which is inside a row

Ok. I have an Angular2 application. Im using an angular component called flex-layout (that let me work with flexbox through directives, thats all). Then, i have a div with class="row" and a dynamic amount of divs inside it. Each dynamic div have an image inside of it.
I need to mark one of those divs as selected, and then add an specific class to it. That class has to put a border-bottom and a background color (already do that), but i need to add a little triangle at the middle of the border-botom on selected div.
Work already done
Fail when selecting another div
As you see on the above images, i managed to put that triangle on the middle of all row (no matter what div i selected)
But, when i change the selected div, triangle doesnt move at all. It always stays at center of the row, and i need the triangle be at the center of selected div instead.
changeSelectedBrand(brandId: number) {
this.selectedBrand = brandId;
}
div.image-row {
height: 90px;
max-height: 90px;
width: 100%;
max-width: 100%;
margin: 0px;
padding: 20px;
background-color: #EEEEEE;
}
.div-image-row-selected {
background-color: #DDDDDD !important;
border-bottom: 3px solid mat-color($primary,400);
}
.div-image-row-selected:after {
content: '';
position: absolute;
top: 100%;
left: 0;
right: 0;
width: 0;
height: 0;
border-top: solid 10px mat-color($primary,400);
border-left: solid 10px transparent;
border-right: solid 10px transparent;
}
div.image-row > img {
height: 65px;
}
<div fxLayout="row">
<div fxFlex="100%" fxLayoutAlign="center center" class="image-row" *ngFor="let brand of brands" [ngClass]="{'div-image-row-selected': brand.id === selectedBrand}" (click)="changeSelectedBrand(brand.id)">
<img src="{{brand.url}}" />
</div>
</div>
The snippet is not functional, i know... is just to show you how things are done right now.
So, repeat the question: How can i make that triangle to move to the center of the bottom border of a selected div?
Thank you
put a position relative wrapper for div.image-row and then update the triangle style(.div-image-row-selected:after) with required left.
div.image-row {
height: 90px;
max-height: 90px;
width: 100px;
max-width: 100%;
margin: 0px;
padding: 20px;
background-color: #EEEEEE;
position: relative;
}
.div-image-row-selected:after {
content: '';
position: absolute;
top: 100%;
left: calc(50% - 10px);
right: 0;
width: 0;
height: 0;
border-top: solid 10px #ff0000;
border-left: solid 10px transparent;
border-right: solid 10px transparent;
}

how to create a half-box in html/css

I'm trying to create a div that has a left and top border with text in top line. what I am trying to achieve is the following...
html half box
I am able to get the top with the text using the following css or alternately a table but can't get it with the left border also. any 'outside the box' thinkers?
.hr-sect {
display: flex;
align-items: center;
color: blue;
margin: 8px 0px;
}
.hr-sect::before
{
content: "";
width: 20px;
background: #000;
height: 1px;
font-size: 0px;
line-height: 0px;
margin: 0px 8px;
}
.hr-sect::after {
content: "";
width:100%;
background: #000;
height: 1px;
font-size: 0px;
line-height: 0px;
margin: 0px 8px;
}
CATEGORY
CATEGORY
You can simulate that interrupted border line by using an absolutely placed div that has a non-transparent background, just make sure it matches the actual background color.
.half-box {
border-left: 1px solid black;
border-top: 1px solid black;
position: relative;
padding: 30px;
}
.half-box > .title {
background-color: white;
padding: 0 10px;
position: absolute;
top: 0;
left: 30px;
font-size: 20px;
transform: translateY(-50%);
}
<div style="height: 100px">
</div>
<div class="half-box">
some content
<div class="title">
CONTENT
</div>
</div>
Set a positioning context on the outer box with position: relative;
For the border, use a pseudo ::before element with content: " "; and give it a position: absolute; to take it out of the flow. Give it a top and left border.
For the heading, also use position: absolute; and move it up with top: -20px or whatever. Set the same background color as the outer box to mask the border.
Adjust your margins and paddings as needed.
See this codepen: https://codepen.io/matthewsmith_io/pen/RVYQqy

How to pixel-perfect mockup this border?

I'm trying to mockup this design:
But, I can't render the red border correctly. I tried with the obvious solution:
border: 1px solid #939393;
border-left: 4px solid red;
But It's affected by the top and bottom borders, leaving the red stripe with diagonal corners, as you can see in this fiddle:
http://jsfiddle.net/anp0e03k/
Is there any way correct way to fix this?
The only thing that I can think is to add a div inside with red background and negative margins on top and bottom, but it seems to be an overkill and would love to find something that doesn't ruins the html semantic.
Apply the left border to a :before pseudo element of the div and remove the divs left border.
Compatibility: All modern browsers and IE8 +
Give the :before
height: 100% to span the entire height of your div
margin-top: -1px to overlap the top border
padding-bottom: 2px to overlap the bottom border
Then use either
position: absolute on the :before with position: relative on the div like this example:
body {
background-color: #c2c2c2;
}
div {
margin: 50px;
background-color: #FFF;
border: 1px solid #939393;
height: 50px;
width: 200px;
border-left: none;
position: relative;
}
div:before {
content: '';
display: block;
border-left: 4px solid red;
height: 100%;
margin-top: -1px;
padding-bottom: 2px;
position: absolute;
top: 0;
left: 0;
}
<div>
</div>
or
display: inline-block for the :before like this example:
Note: You will probably want to use vertical-align: top / middle / bottom for the :before. This example uses the value top.
body {
background-color: #c2c2c2;
}
div {
margin: 50px;
background-color: #FFF;
border: 1px solid #939393;
height: 50px;
width: 200px;
border-left: none;
}
div:before {
content: '';
display: inline-block;
border-left: 4px solid red;
height: 100%;
margin-top: -1px;
padding-bottom: 2px;
vertical-align: top;
}
<div>
There is text in this
</div>
Final result

Link not appearing in div class, appearing outside of it

I'm trying to re-create a few elements I've seen online and I've been using Element Inspector but can't seem to figure out why this a href element is loading outside of my modalHeader class.
Here's some HTML:
<div id="modalContainer">
<div class="fakeModal">
<div class="modalHeader">
<h2>Fake Modal Heading</h2>
x
</div> <!-- end modalHeader -->
</div> <!-- End fakeModal -->
And corresponding CSS (using Less)
#modalContainer {
width: 700px;
height: 250px;
background: gray;
padding: 1px; }
.fakeModal {
width: 500px;
height: 150px;
margin: 50px auto 50px auto;
border-radius: 3px;
//border: 3px solid black;
background: white;
}
.modalHeader {
h2 {
background: #dullGray;
border-bottom: solid 1px #EEE; //This makes so much of a difference!!!!
border-radius: 3px 3px 0 0;
display: block;
-webkit-margin-before: 1em;
-webkit-margin-after: 1em;
-webkit-margin-start: 0px;
-webkit-margin-end: 0px;
padding: 9px 15px;
}
a.close{
position: absolute;
top: 10px;
right: 10px;
color: gray;
text-decoration: none;
font-size: 18px;
}
a.close:hover {
text-decoration: underline;
color: gray;
}
}
Can anyone figure out why the x isn't rendering in the horizontal box I've defined in modalHeader?
#zack; you give position: absolute; to your a tag so, give position: relative; to your parent div modalHeader that's work for you .
CSS:
.modalHeader {position: relative;}
for more read this article http://css-tricks.com/791-absolute-positioning-inside-relative-positioning/
You've set the link to be position absolute, not relative to it's parent container. Remove the position, and change the top and right to margins.
An absolute position always refers to the element above which is positioned relative or absolute. If there isn't one, it refers to the body. Try to change position: absolute; to position: relative; or define the modalHeader as position: relative;.