Before / After selectors excluding padding - html

I'm writing to ask if there is any simpler method to do exactly the same thing like in this code:
jsfiddle.net/t9euvoe9/5/
I'm trying to do friends list. I noticed that simple using of built borders isn't enough here. I found some way which in EVERY friend is divided by custom single line (botttom).
I want lines to dissapear when I'm hovering friend (BOTTOM line of PREVIOUS friend, and BOTTOM line of actually HOVERING friend).
Also, these paddings are not working properly right now (when hovering).
edit:
I want to create friends list. Actually I have <div> which contains all another <div class="friend">. I want to separate every <div class="friend"> with a single line, which will be hidden after hoovering friend element
The way I'm actually doing this:
Position of <div class="friend"> element is set to relative. I'm creative custom border line using:before and :after pseudo-selectors.
When some <div class="friend"> is hovered, I'm hiding this border using display:none. This hides bottom border. To hide top border I'm moving <div class="friend"> little bit higher by setting margin-top: 1% and padding-top: 6% to overcome this move.
Example:
.friend {
position: relative;
width: 100px;
padding-top: 5%;
padding-bottom: 5%;
}
.friend:hover {
margin-top: -1%;
padding-top: 6%;
height: calc(100% + 1px);
background-color: red;
}
.friend:hover:after {
display:none;
}
.friend img {
vertical-align: middle;
}
.friend:first-child:before {
content: '';
display: block;
width: 100%;
left: 10%;
height: 1px;
position: absolute;
background-color: #ccc;
top: 0;
}
.friend:after {
content: '';
display: block;
width: 100%;
left: 10%;
height: 1px;
position: absolute;
background-color: #ccc;
bottom: 0;
}
<div class="friend">
<img class="img-circle" src="http://www.zerohedge.com/sites/default/files/pictures/picture-197359.gif" alt="" width="40" height="40">
<span class="name">Ania</span>
</div>
<div class="friend">
<img class="img-circle" src="http://www.zerohedge.com/sites/default/files/pictures/picture-197359.gif" alt="" width="40" height="40">
<span class="name">Ania</span>
</div>

Although this question could definetely be better (see the comment BSMP left), I gave a shot at answering.
http://jsfiddle.net/tw32r0L6/
What I did was remove a bunch of extra stuff, and simplify a little. You should really be using the CSS border property, because it is the built in way of doing something like this. Using pseudo-elements isn't required for this basic of a task.
So after removing the CSS you had for pseudo elements, you can see that I added this:
.friend:not(:first-of-type){
border-top: 2px #000 solid;
}
.friend:hover + .friend, .friend:hover{
border-top-color: transparent;
}
The first part selects the .friend divs, that are not the first one, as you can see by :not(:first-of-type). You can read more about CSS pseudo-classes here. You can see that the only style I added was a top border.
Then the second selector hides the top border from all .friend divs that are hovered, and the first .friend div following one that was hovered. This is done using the + selector which selects the next sibling of an element.
The last thing I did was remove some stuff you had in the other .friend:hover part, because that wasn't needed after these fixes.

Related

How to display element over its container in HTML?

I would like to make a custom tooltip for the element, but I have it contained in the box, which doesn't have enough space for the tooltip, so it just crops (well, technically I can scroll to reveal it because overflow is set to auto, but I would like it to be visible without doing that). Is there a way to make it pop over the edge? I have tried using z-index to no result.
Here is what I am talking about:
.box {
width: 100px;
height: 100px;
overflow: auto;
border-style: solid;
border-color: red;
}
.tooltip {
padding-top: 20px;
position: relative;
display: inline-block;
}
.tooltip .tooltiptext {
display: none;
max-width: 60vw;
min-width: 15vw;
background-color: white;
border-style: solid;
border-color: #1a7bd9;
position: absolute;
z-index: 1000000;
}
.tooltip:hover .tooltiptext {
display: block;
}
<div class='box'>
<div class='tooltip'> Hover for tooltip
<div class='tooltiptext'>
Wow, this is amazing, such an epic tooltip text
</div>
</div>
</div>
Edit: It is important that hover works on the element, not the box that it is in.
Lot of ways to go about it, but if you're just looking for the tooltip to be visible outside the container, you don't need z-index or overflow. You just need to move your tooltip so it comes next in the positioning context inside of a relative container.
Per your comment, since you want the tooltip to appear only when hovering over the text, I'd recommend having your relative container wrap precisely around just the content you want to hover. To illustrate this, I added a border on the outer box versus where you decide to use the relative container.
Then, simply change box:hover to relative-container:hover to target the the appropriate element.
I attempted to organize the HTML and classes to be a bit more semantic and succinct for illustration. Hope that helps!
Example
.box {
padding: 30px;
border: blue solid;
width: 300px;
display: flex;
align-items: center;
}
.relative-container {
position: relative;
}
.box-content {
border-style: solid;
border-color: red;
padding: 10px;
}
.tooltip {
position: absolute;
left: 0;
top: 0;
max-width: 60vw;
min-width: 15vw;
background-color: white;
border: #1a7bd9 solid;
display: none;
}
.relative-container:hover .tooltip {
display: block;
cursor: pointer;
}
<div class='box'>
<div class='relative-container'>
<div class='box-content'>
Hover for tooltip. I have a little padding to give the text some room.
</div>
<div class='tooltip'>
Wow, this is amazing, such an epic tooltip text. I'm aligned with the top of the box content containing text and take up the full width. I only appear when hovering over the box content (red outline), not the surrounding container (blue outline).
</div>
</div>
</div>
Is there a way to make the tooltip above the text?
Example with Tooltip Position Above Text
Sure, just read a bit about position: absolute; - you can position it with respect to the relative container however you like. Using this in combination with how you decide to position your actual content inside the container gives you many options, but you have to keep in mind the dynamic size of the tooltip based on content length, screen/browser dimensions, and location of hover target element and tooltip! :) JS can be handy here.
.box {
padding: 30px;
border: blue solid;
width: 300px;
display: flex;
align-items: center;
}
.relative-container {
position: relative;
}
.box-content {
border-style: solid;
border-color: red;
}
.tooltip {
position: absolute;
left: 0;
/* minus surrounding container padding and border*/
top: -34px;
border: #1a7bd9 solid;
min-width: 15vw;
/* full width accounting for subtracting left/right margins from body in snippet (8x2 = 16) minus border widths and left padding of surrounding container (30 + 8 = 38)* - content is fluid though so up to you how you deal with it if the tooltip content grows bigger, can't just keep it on top without accounting for fact content is dynamic :) */
width:calc(100vw - 54px);
background-color: white;
display: none;
}
.relative-container:hover .tooltip {
display: inline;
cursor: pointer;
}
<div class='box'>
<div class='relative-container'>
<div class='box-content'>
Hover for tooltip. I'm just an element with some text, so only hovering on the text brings up the tooltip.
</div>
<div class='tooltip'>
I'm aligned with the top left of the box content, and I'm given some additional width to overflow it. I'm mostly on top of the text - that is if the content isn't too long and screen size not too narrow! :)
</div>
</div>
</div>
In practice, you may find that placing the tooltip directly over the hovered content is undesirable (if it covers it up, user can't easily reference what they just hovered over while looking at tooltip). Also, as mentioned above, content is fluid and needs space to run, so it will overflow somewhere depending on its length and other factors.
Positioning Context
The takeaway is just to think of your relative container as the reference point for the absolutely positioned tooltip. You generally don’t need or want to style it much in most cases, just give it a position of relative and then let the child element(s) dictate size/positioning of content inside.
More on positioning context here.
Showing/Hiding the Tooltip
The other consideration is whether to remove the tooltip from the document flow (ex. display property) and/or change the visibility or opacity. I've just used display to remove and add the tooltip, which is the simple approach. This ensures that when hovering over the area taken up by the tooltip itself (which may extend outside the original hover text bordered in red) but before hovering on the actual text, the tooltip doesn’t unintentionally show. I also set the tooltip cursor to pointer during hover.
Other approaches probably fall outside the scope of what you're asking, but thought it was worth mentioning there are considerations here.
One way to implement this is to make a ::before pseudo-element that positioned above and next to the text being hovered.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.box {
width: 100px;
height: 100px;
padding-top: 20px;
border-style: solid;
border-color: red;
}
.tooltip {
position: relative;
display: inline-block;
}
.tooltip::before {
content: "Wow, this is amazing, such an epic tooltip text";
position: absolute;
left: 25%;
bottom: -75%;
display: none;
width: 500px;
background-color: white;
border: 2px solid pink;
}
.tooltip:hover::before {
display: flex;
}
<div class='box'>
<div class='tooltip'>
Hover for tooltip
</div>
</div>
You can change overflow to be visible and it will pop over the edge.
.box {
overflow: visible;
}

How to show the border css over the div element?

Image attached in this link
In number 9 cell border is over the background color
How do I show the border over the div or background Color and it should be responsive?
<div className="borderOverDiv"><div>
<div className="backgroundClr"></div>
.borderOverDiv{
position: absolute;
border: 1px solid red;
width: calc(100% - 94%);
height: 30px;
border-radius: 10px;
}
.backgroundClr{
background: blue
}
this code as I tried, seems not working
I am assuming that you are new to css so I will try to explain what is going on with this code the best that i can.
The fun part is in .element.active:after
There is a few thing
position: absolute this will allow us to set this element absolutly to container. But witch container? First that has position set to a different value than static or its initial value. That is why .element has position: relative which doesn't do anything on its own.
top, right, bottom, left which tell that this element will be exceeding parent element on every side by 5px.
z-index Simply the higher the value the "closer" this element is to user. initial value is 0 so 1 is placing this element above every other element.
content is required in pseudo-element :after in order to show them. This property just needs to be set and doesn't have to have any value specified.
The reis is just to make it look nicer.
And thats it.
You can use other element inside .element if you feel like it.
For example
<div class="element">
<div class="overlay"></div>
</div>
and it will work just fine if you will follow point form 1 to 3 (point 4 is required, as I said earlier, only in pseudo-element) but it will be less responsive. For example what will you have to do when other element needs this overlay? You will have to use javascript to append .overlay element to .element and with pseudo-element you just need to append class. or just show this overlay on hover. Other advantage is that it look prettier and doesn't bloat you html.
.container {
padding: 5px;
display: flex;
}
.element {
position: relative;
background-color: #0000ff;
padding: 10px 20px;
display: inline-block;
color: #ffffff;
}
.element.active:after {
content: '';
position: absolute;
top: -5px;
right: -5px;
bottom: -5px;
left: -5px;
border-radius: 40px;
background-color: rgba(200, 200, 200, .4);
border: 1px solid rgba(200, 200, 200 ,.8);
z-index: 1;
}
<div class="container">
<div class="element">7</div>
<div class="element active">8</div>
<div class="element">9</div>
</div>
replacing className with class should do the trick

Using CSS to make arrow-headed div

I am trying to implement a arrow-headed div. Below is the part of the code that is relevant to the post/question. I have been trying to figure out how to get this done for a while now but no success.
I have a grandparent div, a parent div with a child as follows
<div className="main-segment-container">
<div className="panel panel-default segment-select-box">
<div className="panel-header segment-select-box-header">MAIN SEGMENT</div>
<div className="panel-body segment-select-box-body">
<div className=has-subsegments'>
<input type="checkbox" className="form-check-input" value={checkedSegment.category_id} onChange={this.segmentChecked} />{' '}
<label className="form-check-label">{checkedSegment.name}</label>
</div>
</div>
</div>
</div>
Here is what I am trying to achieve (notice the arrowhead):
I am able to achieve this with this css:
.main-segment-container{
width: 100%
}
.has-subsegments{
background-color: #215C64;
width: 100%;
color: #fff;
position: absolute;
height: 30px;
}
.segment-select-box {
border-radius: 3px;
width: 100%;
/* max-height: 400px; */
/* overflow: scroll; */
position: relative;
}
.segment-select-box-body{
width: 100%;
max-height: 400px;
overflow: scroll;
padding: 0px;
display: inline-block;
}
.has-subsegments::after{
content: "";
margin-top: -15px;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
position: absolute;
border-left: 21px solid #215C64;
width: 0;
height: 0px;
right: -20px;
top: 50%;
}
Problem:
When I use the css above, the .has-subsegments element seems to be at a fixed position when I scroll. Like this:
Question
How do I implement scroll without removing the element from the normal position?
Note:
When i remove scroll from .segment-select-box-body class, everything works perfect but the children list becomes very long, therefore a scroll is needed.
adding position: relative; to .segment-select-box-body class makes the :after pseudo-element invisible.
EDIT
See JSFIDDLE here : https://jsfiddle.net/uuwhndgu/16/
EDIT
Thanks for posting the jsfiddle. I don't think, what you're trying to achieve is possible the way you are trying to do it.
I updated the fiddle with a suggested workaround/fix: https://jsfiddle.net/uuwhndgu/34/
what I did, is giving the wrapping col a little more width (you probably would have to either increase the col to .col-md-3 or decrease the width of .segment-select-box a little. You probably need to do the latter anyway), a max-heightof 200px and a overflow-y: scroll;. I set the width of .segment-select-box to 90% and changed position: absolute;of .has-subsegments to position: relative;. I don't know if this helps you but I BELIEVE, that there aren't many ways to achieve what you are trying to achieve.
Original answer
I am not quite sure how you intend this thing to behave. But if the highlighted entry (the one with the arrow) just ought to stay where it was, I think you can simply replace position: absolute; with position: relative; in your .has-subsegments class. Now, I wasn't able to recreate this anything close to perfectly, because it's a react app, but still, you should get the idea:
with position: absolute; on .has-subsegments
with position: relative; on .has-subsegments

how position a div ...px from another div

I was having problems with positioning my language option at the top of my blog. It was always on a different position on a Windows and a Mac. So I thought about making an inner and outer div. But I'm having troubles with the position of my inner div.
This is the result I want:
This is what I have now
This is the code I have in my CSS on wordpress:
EDITED
.outer {
margin: auto;
overflow: visible;
background: white;
margin-left: 925px;
margin-top: -8px;
margin-bottom:-30px;
font-size: 11pt;
color: #E1BDC3;
border: 1px dotted #999999;
border-radius:8px;
width: 255px;
height: 48px;
padding-top: 15px;
padding-left: 10px;
z-index: 1000;
}
.inner {
position: relative;
left: 160px;
top: -336px;
background: transparent;
width: 150px;
z-index: 10001;
}
The block with the border just has <div class="outer"...
And the inner div, the dropdown, is a widget that I'm trying to position at the top of the page, I gave the widget "inner" class.
QUESTION --> If I put marging-right: 4px, it starts counting from the right of the screen, how do I position (for example 4px) from the right of the Outer div?
So that it starts counting from the dotted border on the right (or the left, doesn't matter)
(I'm a beginner in HTML so if you know how to help me, could you please tell me what code I need, and where?
You should use % to refer to positions on the screen. But your problem can be fix using position: relative to refer to the poition inside the parent object, and moving it with top and left
.outer {
margin: auto;
overflow: visible;
background: white;
margin-left: 925px;
margin-top: -8px;
margin-bottom:-30px;
font-size: 11pt;
color: #E1BDC3;
border: 1px dotted #999999;
border-radius:8px;
width: 255px;
height: 48px;
padding-top: 15px;
padding-left: 10px;
z-index: 1000;
}
.inner {
position: relative;
left: 159px;
top: -17px;
background: transparent;
width: 100px;
}
<div class="outer">
OUTER
<div class="inner"><select><option>INNER</option></select></div>
</div>
To answer your question "how do I position (for example 4px) from the right of the Outer div?" you would do that by first determining how the elements relate to each other. This is determined by the position and display CSS properties. Depending on which position and display values your two elements have, the answer will vary.
The HTML markup you provide for your "outer" element shows that it is a div. Your CSS does not define a position for this element, so the browser default is static, i.e. position:static.
Your "inner" element is a mystery to us. If it is another div then it is another static position which we can help with. If it is a ul then it is an inline element, which will require a different answer.
Your markup is important here.
EDIT
First thing, your 'outer' div is not actually containing your inner div. This is the outer div markup:
<div class="hide_on_mobile outer">Choose language</div>
You'll see it doesn't contain the element in question that we want to position. Therefore, like my first sentence states, we need to understand how our element in question relates to those around it.
In your situation, you are not in a good spot because the element you want to position is contained by elements that don't relate to your target element. Therefore the only way to position them in the same spot on all screen sizes is to position them absolutely and use percentages.
Or the easy way, if you want to stick to one screen width:
.inner {
position: relative; //override by .widget_polylang element
left: 27px;
top: -17px; //override by .widget_polylang element
background: transparent;
width: 100px; //override by .widget_polylang element
}
You'll see some of your key/value parameters are being outclassed by the .widget_polylang element. The only way to change those is to edit the styles of .widget_polylang or add increased CSS specificity to .inner.

making two divs line up side by side without gap

I have seen people ask questions about how to get two divs to line up side by side. I can get mine to do that just fine.
My problem is that they will not smash up against each other. There always seems to be a gap.
For example, I have a wrapper div with a width of 500px. Inside that div I have two other divs with widths of 250px.
They will not line up next to each other because there is not enough space for each other.
When I set the width to 248px they do line up but with a 4px gap between each other.
I have an example of this code located here:
https://c9.io/riotgear66/day1/workspace/sams/html/index.html
Please feel free to take a look at it and try adjusting it with your browser's element inspector.
The layout problem is the result of applying display: inline-block to the div elements.
Any white space between those div elements are taken into account when laying out the content.
You could remove the white space (linefeed or carriage return) between the div's if you don't mind how your source code looks.
Since your parent container has specific dimensions (500px x 300px), I would use absolute positioning to place the child elements. This would make it easier to position your logo motif over the other images.
You could also use floats as stated in other responses, not any easier or harder.
In this application, the layout is fixed so there are no design considerations for a responsive or flexible design, so either approach is valid.
Demo
You can see how this might work in the following demo: http://jsfiddle.net/audetwebdesign/hZ5dB/
The HTML:
<div class="container">
<div class="panel ul"></div>
<div class="panel ur"></div>
<div class="panel ll"></div>
<div class="panel lr"></div>
<div class="overlay"><span>Cats</span></div>
</div>
and the CSS:
.container {
border: 1px dotted blue;
width: 500px;
height: 300px;
position: relative;
margin: 0 auto;
}
.panel {
width: 250px;
height: 150px;
position: absolute;
}
.ul {
background: red url("http://placekitten.com/400/400") -50px -20px no-repeat;
top: 0; left: 0;
}
.ur {
background: red url("http://placekitten.com/300/300") 0px -30px no-repeat;
top: 0; right: 0;
}
.ll {
background: red url("http://placekitten.com/350/250") -20px -20px no-repeat;
bottom: 0; left: 0;
}
.lr {
background: red url("http://placekitten.com/300/200") 0px -30px no-repeat;
bottom: 0; right: 0;
}
.overlay {
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
width: 100px;
height: 100px;
background-color: red;
border-radius: 50%;
}
.overlay span {
display: block;
background-color: gray;
border-radius: 50%;
text-align: center;
vertical-align: middle;
width: 80%;
height: 80%;
margin: 10%;
line-height: 80px;
}
I also show how you can create the circular motif without having to modify the original background images, saves a bit of work with PhotoShop or similar.
You shouldn't be using
display: inline-block;
Make them:
float: left;
Here is a jsfiddle sample of how it should be.
http://jsfiddle.net/Tqdqa/
The problem lies in the white space in your HTML. When using display: inline-block, white space after elements is taken into account like Marc Audet said.
To fix it without changing your current method, you must remove that white space. The easiest way I've found to do so while still maintaining readability of the HTML is by commenting it out, or using <!-- after each element and --> before the next element. This prevents having to change the whole structure and you can make each one 250px again as well
You could also move the closing > to the next line, move everything after the opening <div> to the next line, or use margin-left:-4px; for each element after the first. Or use a method described by others here, floating it or using FlexBox
Here is the CSS Tricks page that references this situation and provides more detail