Text over top of border of div below - html

I'd like to have text appear on the top of a div that has a border that also has a white background so you don't see the border line going through the text. I've tried adding z-index to the text but I believe since position: relative it doesn't matter. I'm also open to other suggestions as to how to accomplish and would prefer not to use a fieldset and legend.
Fiddle
#large-div-text {
padding: 20px;
border: 1px solid rgba(64, 189, 233, 0.42);
position: relative;
margin-top: -10px;
}
#why {
background-color: #FFF;
text-align: center;
z-index: 1000;
}
<div id="why">
No line behind me, please!
</div>
<div id="large-div-text">
Large div text
</div>

You have the right idea about setting a z-index. However, note that in order for a z-index to apply, you need to specify a position property other than the default of static. That will have your #why element sit on top. From here, it's just a matter of giving it a fixed width (along with margin: 0 auto for alignment) so that the rest of the border gets shown.
This can be seen in the following:
#large-div-text {
padding: 20px;
border: 1px solid rgba(64, 189, 233, 0.42);
position: relative;
margin-top: -10px;
}
#why {
background-color: #FFF;
text-align: center;
position: relative;
z-index: 1000;
width: 250px;
margin: 0 auto;
}
<div id="why">
No line behind me, please!
</div>
<div id="large-div-text">
Large div text
</div>
Note that the width property denotes just how much of the border is shown - feel free to adapt to suit! If you want it to perfectly wrap around the text, I'd recommend using a <span> tag instead of a <div>.

Actually, you don't need to use absolute or z index something.
The div#why is block, you dont want to make it block, instead make it inline-block so it consume it's normal width, not 100%.
The problem now is how you can center the div#why, i used position:relative, and transformX.
Cheers!
#large-div-text {
padding: 20px;
border: 1px solid rgba(64,189,233,0.42);
position: relative;
margin-top: -10px;
}
#why {
background-color: #FFF;
text-align: center;
z-index: 1000;
display: inline-block;
transform: translateX(-50%);
left: 50%;
position: relative;
}
<div id="why">
No line behind me, please!
</div>
<div id="large-div-text">
Large div text
</div>

You must use a position attribute for z-index to work. And you can use a span element to set the background of the text. The span is an inline element ulike div, and will change its width depending on the content.
#large-div-text {
padding: 20px;
border: 1px solid rgba(64, 189, 233, 0.42);
position: relative;
margin-top: -10px;
z-index: 0;
}
#why {
text-align: center;
z-index: 1000;
position: relative;
}
.whitebg {
background-color: white;
padding: 0.5em;
}
<div id="why">
<span class=whitebg>No line behind me, please!</span>
</div>
<div id="large-div-text">
Large div text
</div>

You can simply add some margin
#large-div-text {
padding: 20px;
border: 1px solid rgba(64, 189, 233, 0.42);
position: relative;
margin-top: -10px;
}
#why {
background-color: #FFF;
text-align: center;
z-index: 1000;
margin: 10px;
}
<div id="why">
<span class=whitebg>No line behind me, please!</span>
</div>
<div id="large-div-text">
Large div text
</div>

Related

Responsive div height on div overlap

I am trying to do the following in the image: The top div will contain text of variable length. The bottom div is fixed in place and should respond by resizing its height to avoid the overlap, while overflowing the text if needed. Is there a way to do this in CSS? I have attached the jsfiddle.
https://jsfiddle.net/0nr3g7kq/123/
* {
font-family: Arial, Helvetica, sans-serif;
}
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
margin: 20px;
padding: 5px;
width: 300px;
height: 145px;
position: relative;
}
.content1,
.content2 {
border-style: dotted;
border-width: 2px;
border-color: blue;
margin: 1px;
width: 268px;
}
.content2 {
position: absolute;
bottom: 20px;
}
.title {
font-weight: bold;
}
.desc {}
<div class="card">
<div class="content1">
<span class="title">
This top div should compress the bottom div downwards without changing its position.
</span>
</div>
<div class="content2">
<span class="desc">
This bottom div should respond by being compressed and show 1 less visible line with text overflow but not change position
</span>
</div>
</div>
Just add min-width:268px; to .content1, .content2 it will solve current problem
put max-width: instead of width: in .content1, .content2 and remove .content2 { position: absolute; bottom: 20px; }

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 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

floating divs inside parent with no fixed width

I have a set of divs that vary in size depending on an image inside it. Inside each div I would like two more divs, one is floated left and the other is floated right, like so:
I sort of accomplished it this way ... html:
<div class="image-wrap">
<img src="{{ img }}">
<div class="lookbook-title"><h5 >{{ title }}</h5></div>
<div class="item-buy">{{ theme:partial src="_buynow" }}</div>
</div>
and css:
div.image-wrap {
max-height: 1000px;
max-width: 100%;
border: 0;
display: inline-block;
height: auto;
box-sizing: border-box;
}
.lookbook-title {
position: relative;
top: -36px;
float: left;
padding-left: 10px;
padding-right: 10px;
color: #f7f7f7;
}
.item-buy {
position: relative;
top: -56px;
float: right;
padding-right: 20px;
pointer-events: none;
-webkit-font-smoothing: antialiased;
fill: #f7f7f7;
}
The reason I say "sort of" is because it initially was working just fine, but now the floated divs are appearing on above and outside their parent divs. What is interesting is that if I inspect the problem with dev tools and uncheck and recheck the "float" on either div both go back to where I want them to go...
You need to clear your floats.
Here is a interesting article that explains it in detail: http://css-tricks.com/snippets/css/clear-fix/
Hope this helps.
You should use position: absolute; for your 'floating' elements instead of float.
You'll need to add position: relative; to the parent wrap element - this will tell the children to respect the bounds of this element instead of floating somewhere outside of it. Then you can add position: absolute; to each of the children that you want to float and use top, bottom, left, right to control where the box is positioned. Experiment with different values to get the hang of it.
div.image-wrap {
height: 300px;
width: 400px;
position: relative;
border: 1px solid red;
}
.lookbook-title,
.item-buy {
background: white;
position: absolute;
bottom: 10px;
height: 100px;
width: 100px;
}
.lookbook-title {
border: 1px solid lime;
left: 10px;
}
.item-buy {
border: 1px solid blue;
right: 10px;
}
<div class="image-wrap">
<img src="http://www.placehold.it/400x300.jpg">
<div class="lookbook-title"><h5>Div 1</h5></div>
<div class="item-buy">Div 2</div>
</div>

Vertical line centered above div

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>