Impact of values of left and right on width - css

Why there a difference between setting a value in left and right for a div positioned as absolute.
When i set a value for right it's okey the text fill right in the pseudo class FIDDLE1.
But not with left FIDDLE2.
I have to set a width FIDDLE3.
So how can i use left without setting a width and get result as FIDDLE1 ?
CSS:
div {
width: 100px;
height: 100px;
position: relative;
}
div:after {
content: 'ce champs est obligatoire';
position: absolute;
left: 100px;
}

The issue is that the div which is set to relative has a fixed width of 100px, if you were to make the width of that div wider, such as 300px, so that the content after were to fit, it would not wrap the text.
Give the div after white-space:nowrap; like this: http://jsfiddle.net/jme11/6avYE/ to avoid the problem.
OR,
Another option would be to wrap the div in another div and set the wrapper div to have the position: relative instead of the inner div like this: http://jsfiddle.net/jme11/PhVLj/
div:not(div>div) {
width: 500px;
height: 100px;
position: relative;
}
div>div {
width: 100px;
height: 100px;
background-color: aliceblue;
border: 1px gray solid;
}
div>div:after {
content: 'ce champs est obligatoire';
position: absolute;
left: 100px;
/* right: -167px;
width: 157px;*/
background-color: rgb(230, 122, 38);
color: white;
padding: 8px 10px;
border-radius: 5px;
font-family: 'lucida grande',tahoma,verdana,arial,sans-serif;
font-size: 13px;
}

Related

How do I extract the width of .main in CSS

I want to create an element in css and keep it at a constant right spacing from the main content.
for example:-
.main
{
padding: 0px 12px;
margin: 12px 8px 8px 8px;
min-height: 420px;
width: 924px;
height: 580px;
}
now I am creating an image that needs to be at a constant distance from the main content, and on its right hand side.
ie. say 100px from main content at all times, no matter the size of window:-
.NewElement {
width: 78px;
height: 70px;
position: fixed;
bottom: 550px;
right: .main.width() + 100px; <--- how do I represent this??
display: none;
text-indent: -9999px;
background: url('../xxx.png') no-repeat;
background-color: #000;
}
right: .main.width() + 100px; <--- how do I represent this correctly??
Place 'NewElement' within the 'main' DIV (assuming these are DIVs) and set the margin-left:100px, so it will always be relative to that main DIV.
<div class="main">
<div class="NewElement"></div>
</div>
Here's a fiddle.
You would have to use javascript or jquery to do this:
var width = $('.main').css('width');
$('.NewElement').css('width',width+100);

CSS: fix the height of a section within a variable height element

Related to this question.
Here's a fiddle: http://jsfiddle.net/DRbRS/
Notice how the red-outlined list div does not align at the bottom of the green container div.
The problem is that there is no way of knowing ahead of time what the resulting height of the list ought to be, even if the height of the header is known.
Is there any way to deal with this without resorting to javascript?
What we need is a style like height: fill;
Using position: absolute and setting top, left, right, and bottom: http://jsfiddle.net/QARC9/
This article describes why it works.
http://www.alistapart.com/articles/conflictingabsolutepositions/
Replace your CSS with this
#container {
left: 50px;
width: 200px;
position: fixed;
height: 90%;
border: 2px dashed green;
}
#header {
height: 30px;
line-height: 30px;
text-align: center;
border: 2px dashed blue;
margin-left:-2px;
margin-top:-2px;
width:200px
}
#list {
border: 2px dashed red;
overflow: auto;
height: 91%;
width:200px;
margin-left:-2px;
margin-top:-2px;
}​
or see the demo here http://jsfiddle.net/enve/DRbRS/3/

CSS3 "Tooltip" with :hover:after positioning and size

I know it is possible to create a custom "tooltip" with the :hover:after selectors and to align this tooltip relative to the original element by marking the original element as position:relative and the tooltip as absolute.
HTML:
test
<span custom-tooltip="testing a custom tooltip" class="tooltip">
test
</span>
test
CSS:
.tooltip {
position: relative;
}
.tooltip:hover:after {
content: attr(custom-tooltip);
position: absolute;
background: black;
color: white;
}
However, I must use absolute values to position or size this :after element
top: 30px;
left: -30px;
width: 300px;
What if I want to make the element as wide as it needs to be (Percentages are relative to the parent element creating a large vertical box so I can't tell it to go width: 100%)
And centered under the parent (left: -50% results in it being moved 50% of the parent to the left, not centered)
Is this possible without javascript? (If not, are there some magic selectors or functions to get the width of this or that and calc() the correct values?
You can force the tooltip onto a single line by using white-space:nowrap. I don't know of any way to center the tooltip without forcing a specific width on both the tooltip and the item the tooltip applies to. Here's a general-purpose example (without centering):
<p>Lorem <span tooltip="Lorem ipsum">ipsum</span> dolor sit amet.</p>
And the CSS:
*[tooltip] {
position: relative;
border-bottom: dotted 1px #000;
}
*[tooltip]:hover:before {
content: attr(tooltip);
background-color: #000;
color: #fff;
top: 1em;
position: absolute;
white-space: nowrap;
}
Note that I'm using :before instead of :after. If you want to center the tooltip and are able to define a fixed width, you can use this CSS instead:
*[tooltip] {
position: relative;
display: inline-block;
text-align: center;
width: 200px;
margin: 0 -75px;
}
*[tooltip]:hover:before {
content: attr(tooltip);
background-color: #000;
color: #fff;
top: 1em;
position: absolute;
white-space: nowrap;
width: 200px;
}
Here, the item is given a fixed width equal to the width of the tooltip then negative left/right margins to collapse it back down to the desired size. Note the addition of display:inline-block and text-align:center.
This technique isn't practical for inline tooltips, but works well for buttons and "call to action" links.
.tooltip
{
display: inline;
position: relative;
}
.tooltip:hover:after
{
background: #333;
background: rgba(0,0,0,.8);
border-radius: 5px;
bottom: 26px;
color: #fff;
content: attr(title);
left: 20%;
padding: 5px 15px;
position: absolute;
z-index: 98;
width: 220px;
}
code from TalkersCode complete code here Create CSS3 Tooltip

Nested div vertical align problem

I am trying to vertically center one div (containing a search bar) inside another (a top banner). I was under the impression that to do so you did the following:
#banner {
height: 35px;
width: 100%;
}
#searchbar {
height: 15px;
position: relative;
top: 50%;
margin-top: -7.5px; /* half of the height */
}
This works fine until you add the margin-top at which point it is applied to the #banner as well.
Is there an alternative way to do this, or am I just doing it wrong?
Here's a jsFiddle of my actual code.
I use line-height with the value being the same as height of parent div.
As seen here: http://jsfiddle.net/vkJ78/24/
CSS:
#banner {
background-color: #770E17;
height: 35px;
width: 100%;
border-bottom: 1px solid #333;
}
#src {
width: 300px;
height: 15px;
border: 1px solid #333;
padding: 3px;
}
#srcdiv {
width: 308px;
margin: 0px auto;
position: relative;
line-height: 35px;
}
EDIT: Per recommendation from NGLN, this will also fix horizontal centering, #srcdiv and #src having equal widths.
You have to add overflow: hidden to #banner. To clear the float, I guess.
Then, modify the negative margin to margin-top: -11px in #srcdiv (you have to sum the div height, the border, and the padding for the total height)
http://jsfiddle.net/vkJ78/1/
Give margin:0px and padding:0px and remove margin-top
body {
margin:0px;
padding:0px;
}

How can I center a box in CSS?

I would like to know how can i center this box?
HTML Code:
<div id="box"></div>
CSS Code:
#box
{
width : 30%;
height : auto;
overflow : auto ;
border : 1px solid #C5C5C5;
background : #F8F8F8;
position : absolute;
left : 33.6%;
border-top : none;
text-align : left;
display : none;
}
Try the following CSS:
#box
{
margin: 0 auto;
width: 100px; /* Or some other width */
}
Since #box is absolutely positioned, you would center it like so:
#box {
left: 50%; /* centers #box in its containing element */
margin-left: -15%; /* half the element's width (30%) */
}
Those properties are in addition to the ones you've set already.
The idea is to position #box's left edge in the center of its containing element (left: 50%), then move #box left by half its own width by giving it a negative margin (margin-left: -15%).
This works for me:
.Box {
background-color: lightgrey;
width: 400px;
border: 25px solid grey;
padding: 25px;
margin: 25px;
align-content:center;
position: fixed;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -100px;
}

Resources