I have an image that I want displayed over another image inside of a div. The div has padding and margins. I want to align the image to the top and left of the div without the padding of the div showing. The div is relative position and image is absolute position. This works correctly in all browsers on desktop but not on iPhone.
I have checked that all divs have a position assigned as relative.
<div class="aaa">
<div class="bbb ccc">
<img src="common/banner.png" width="365" height="200" class="ddd"/>
<img src="images/picture.jpg" width="350" height="233" />
<h3>Text</h3>
</div>
</div>
<!-- ---CSS FOLLOWS, EXTRA CSS REMOVED--- -->
.aaa {
position: relative;
width:100%;
margin:auto;
padding:15px 0;
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: top;
flex-direction: row;
}
.ccc {
position: relative;
}
.bbb {
max-width:350px;
position:relative;
color:#FFF;
flex-grow: 1;
padding: 15px 15px 50px 15px;
margin:15px;
text-align:left;
overflow:hidden;
}
#media (max-width: 410px) {
.bbb {
position:relative;
width:90%;
margin:15px 0;
height:auto;
overflow:auto;
}
.bbb img{
position:relative;
width:100%;
height:auto;
}
.bbb a,
.bbb a:hover,
.bbb a:focus,
.bbb a:visited {
position:relative;
margin-top:30p
}
}
.bbb a,
.bbb a:hover,
.bbb a:focus,
.bbb a:visited {
display: inline-block;
padding: 5px 15px;
position: absolute;
bottom:10px;
left: 50%;
transform: translate(-50%, 0);
transition: background 0.2s linear, color 0.2s linear;
}
Image should be flush with top and left of div.
Your markup doesn't contain any link (<a>), yet, the only element in your CSS you applied position:absolute to is .bbb a (with various modifiers), but that doesn't apply to anything.
Let's go over the basics: in order to display 2 elements one on top of the other (which, admittedly, is what you want achieve), you need:
a parent with position:relative
one child without position or with position:relative (this will constitute the document flow: will fill and determine the size of the parent and, indirectly, will also size the other element).
another child with position:absolute; + top, left, width and height (alternatively you can replace width:100%;height:100% with bottom:0;right:0), which will thus map itself to the dimensions of the parent, which, in turn, takes its dimensions from the normal flow (which only contains the other child). This element, being absolutely positioned, is not part of normal flow.
relative-parent {
position: relative;
font-size: 3rem;
}
normal-child {
height: 180px;
background-color: red;
color: white;
}
absolute-child {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: white;
color: red;
/* hide absolute child */
opacity: 0;
border: 1px solid red;
transition: opacity .3s;
}
relative-parent:hover absolute-child {
/* show absolute child on parent hover */
opacity: 1;
}
relative-parent,
normal-child,
absolute-child {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
/* all indented rules are irrelevant
I basically added them to style the example */
<relative-parent>
<normal-child>Normal child</normal-child>
<absolute-child>Absolute child</absolute-child>
</relative-parent>
The above is the general principle. All the styling (unimportant) rules have been indented, so the ones I talked above remained prominent.
The entire construct takes its size from the size of the flow, which is the normally positioned child (in the example: height: 180px; width: 100%. If you change that element, you also change the parent and the other child).
It doesn't really matter what elements you use (they can be even custom elements, as I made them, provided you give them a block or flexbox level value for display). If you use <div> as parent and <img>s as children, you should give the one in parent flow display:block.
If you apply the above principle without any validation errors, it will work cross-browser/cross-device. It's CSS level 1.
I'm not sure to understand, why 2 pictures on a phone? I see the tags "a" in your CSS so I send you this.
<a href="#" id="name">
<img title="Hello" src="common/banner.png" onmouseover="this.src='images/picture.jpg'" onmouseout="this.src='common/banner.png'" />
</a>
Related
.btngo:hover{
bottom:3px;
}
btngo goes up for 3px when pointer is over, but if pointer is just on the edge of btngo it starts flickering, i.e. goes up and down very fast.
Is there a way to prevent this?
This effect should not start before pointer is 3px inside of btngo.
This is because once the hover takes effect and the element moves, you are no longer hovering and so the hover no longer applies...and it loops.
A solution is to maintain the hover by giving the pointer something to hover over while the pointer is apparently no longer over the element.
This can be achieved by a pseudo-element positioned at the bottom of the element (since this jitter is only an issue when hovering from below)...and expand the height of the pseudo-element on parent hover.
div {
width:100px;
height:100px;
position: relative;
border:1px solid red;
margin:2em auto;
}
div::before {
content:"";
position: absolute;
width:100%;
height:3px; /* your proposed bottom position value change */
top:100%;
background:transparent;
}
div:hover {
bottom:3px;
}
div:hover::before {
height:6px; /* position value plus height */
}
<div></div>
No additional HTML, pure CSS solution.
A solution is to create a container on where you apply the hover effect and you avoid the flicker as this container will not move.
.container {
position: relative;
display: inline-block;
margin: 10px;
}
.btngo {
width: 200px;
height: 100px;
border: 1px solid;
position: relative;
}
.container:hover .btngo {
bottom: 3px;
}
<div class="container">
<div class="btngo">
text
</div>
</div>
I have a div defined inside a div, the outer div has certain opacity, this leads to the inner-element whose z-index is higher than it's container appear dim.IS there way to not make the inner div appear dim even though the outer div is div.
Here's the code
<body>
<style>
#cont{ background-color:yellow; width:900px; height:900px; margin:auto; padding:40px; position:relative; }
#top{ position:relative; background-color:orange; width:100%; padding:10px; }
#cont1{ background-color:black; width:800px; padding:20px; box-sizing:border-box; position:relative; z-index:3; opacity:0.4; }
#cont1_text{color:white; z-index:4; opacity:10; padding:20px; top:10px; }
#cont2{ background-color:blue; width:800px; padding:20px; box-sizing:border-box; position:relative; z-index:3; }
#butt{ position:relative; clear:both; }
</style>
<div id="cont">
<div id="cont1">
<div id="cont1_text">
The Last line of above code still shows the length of the array is 4, even though a element is deleted.HOW??
Well, the delete method just deletes the value from the defined position but the position still remains.It’s just like drinking coke, the liquid is gone after drinking(deleting) but the bottle remains. This creates a hole or leaves an empty space in the array.
</div>
</div>
<div id="cont2">
</div>
</div>
</body>
The one way I know of achieving the desired result is by not placing the inner div inside the outer div. Then the div containing text is placed above container div by maintaining position, top, left etc.But the problem here is that the container's height will not increase according to the length of text as div containing text is not inside the container'd div.
The output and edit can be made here https://jsfiddle.net/sum1/av6r0aug/
whenever you don't want to apply the opacity to inner child use instead rgba on background-color.
why?
because in opacity according to MDN
The value applies to the element as a whole, including its contents,
even though the value is not inherited by child elements. Thus, an
element and its contained children all have the same opacity relative
to the element's background, even if the element and its children have
different opacities relative to one another.
So, see snippet below:
#cont {
background-color: yellow;
width: 900px;
height: 900px;
margin: auto;
padding: 40px;
position: relative;
}
#top {
position: relative;
background-color: orange;
width: 100%;
padding: 10px;
}
#cont1 {
background-color: rgba(0, 0, 0, 0.4);
width: 800px;
padding: 20px;
box-sizing: border-box;
position: relative;
z-index: 3;
}
#cont1_text {
color: white;
z-index: 4;
opacity: 10;
padding: 20px;
top: 10px;
}
#cont2 {
background-color: blue;
width: 800px;
padding: 20px;
box-sizing: border-box;
position: relative;
z-index: 3;
}
#butt {
position: relative;
clear: both;
}
<div id="cont">
<div id="cont1">
<div id="cont1_text">The Last line of above code still shows the length of the array is 4, even though a element is deleted.HOW?? Well, the delete method just deletes the value from the defined position but the position still remains.It’s just like drinking coke, the
liquid is gone after drinking(deleting) but the bottle remains. This creates a hole or leaves an empty space in the array.</div>
</div>
<div id="butt">
<div id="cont2"></div>
<div id="cont2_text"></div>
</div>
</div>
One for the CSS gurus - is it possible for a div to 'escape' the constrained in the boundaries of a div with fixed dimensions and overflow:hidden?
Ive recreated the example here: http://jsfiddle.net/Wt3q4/1/
Ive tried setting z-indexes on all the elements, and assigning the div with class b position:absolute with no joy.
Since .b is nested with an element that's position:relative;, setting .b to absolute won't do anything. That I know of, with the element structure you have defined, there isn't going to be a CSS work around.
Without knowing more about your layout and what you're trying to accomplish, it's difficult to advise. You could try setting up a "double container" if that makes sense, and use a jQuery function to move the element out of the overflow:hidden; element when you want to show it.
http://jsfiddle.net/Wt3q4/3/
HTML
<div class="a">
<div class="b">
<div class="c">
</div>
</div>
</div>
<div id="show" class="button">Show!</div>
<div id="hide" class="button">Hide!</div>
CSS
.a{
position:relative;
height:200px;
width:200px;
border:3px solid #f00;
background:#ccc;
}
.b{
position:relative;
height:200px;
width:200px;
background:#ccc;
overflow: hidden;
}
.c{
width:50px;
height:300px;
border:3px solid #00f;
background:#dad;
margin:30px;
position:absolute;
z-index:333;
}
.hidden{
display: none;
}
.button {
width: 50px;
padding: 5px;
text-align: center;
border: 3px solid #aaa;
background: #ddd;
margin: 20px;
float: right;
}
jQuery
$('#show').on('click', function(){
$('.c').prependTo('.a');
$('.b').addClass('hidden');
});
$('#hide').on('click', function(){
$('.c').prependTo('.b');
$('.b').removeClass('hidden');
});
Based on my understanding of CSS's block formatting context, your div.b is a child of div.a, which means that div.a sets the block formatting context for div.b. Once you set overflow: hidden on the parent element, any child content that flows out of the parent content box will not be visible.
This is more apparent if you set outline: 1px solid black on the parent container so that you can see the extend of the content box, both with overflow hidden and visible.
Your question does touch on the essentials of CSS's visual formatting model.
How about something like:
.menu > li > ul {
position: absolute; /* you still need this here */
background-color: #9F26B4;
width: 10000000000000000px;
margin-left: -100000px;
padding-left: 100000px;
list-style: none;
z-index: 1000;
top: 0;
left: 0;
}
This, for example, overflows the entire page from left to right (assuming that the body overflow-x is set to hidden) and then set element width to enormous width, margin it to negative left to fill any left content, and padding to the left to move object inside the element to desirable X position. What you think?
<div class="titelcontent">
<div class="name">Name</div>
<div class="hzline"></div>
</div>
I want name div and hzline div to auto fit 100% in titelcontent.
The label (for example, Name) will vary in length and I want the red underline to span the remainding space of the titlecontent div.
How do I achieve the following? It is easy to do this using tables but I can't figure out how to do this via span or div.
You can use div like a table by using table-cell.
.titlecontent {
display: table;
}
.name {
display: table-cell;
white-space: nowrap;
}
.hzline {
display: table-cell;
border-bottom: 3px solid red;
width: 100%;
}
See DEMO.
Updated to allow background images to show through
You can make the mark-up a bit tighter by using a pseudo-element as follows:
<div class="wrapper">
<div class="inner">Photoshop</div>
</div>
and use the following CSS styling:
div.wrapper {
color:#82439a;
font-size: 16px;
font-weight: bold;
font-family: tahoma;
line-height: 180%;
background: red url(http://placekitten.com/1000/500) no-repeat left top;
overflow: hidden;
}
div.inner {
position: relative;
display: inner;
color: yellow;
padding-right: 0.50em;
border: 1px dotted yellow;
}
div.inner:after {
content: "\A0";
position: absolute;
bottom: 0;
left: 100%;
border-bottom: 5px solid #d71d00;
width: 1000%;
}
Demo fiddle: http://jsfiddle.net/audetwebdesign/wE8bC/
How It Works
The parent element div.wrapper may contain a background image or be transparent and show the background of some ancestor element. You need to set overflow: hidden.
For the label (<div.inner>), set position: relative and then generate a 100% width pseudo-element with a bottom border to serve as an underline. Use absolute positioning to place div.inner:after to the right of <div.inner> (left: 100%) and make the width relatively large. The pseudo-element will trigger an overflow condition but this is taken care of by hiding the overflow in the parent element. You can control left/right spacing using padding.
You can use set the display property to either inline or inline-block. If you use display: inline, it will work in IE7; adjust the line height as needed for styling.
Note that the generated content is a non-breaking space, hex code "\A0".
Support for IE7
If you need to support IE7, you will need a hack if you use inline-block as discussed in a previous question: IE7 does not understand display: inline-block
IE7 also does not support table-cell so some of the other posted solutions will face the same limitation.
Or an alternative to using display: table:
.name {
float: left;
}
.line-wrapper {
display: block;
overflow: hidden;
padding-top: 6px;
}
.hzline {
border-bottom: 3px solid red;
width: 100%;
}
See example.
I've guessed you are looking something like this. Please find my solution based on my understanding about the image you posted.
HTML
<div>
<span>Photoshop</span>
</div>
<div>
<span>Adobe Illustrator</span>
</div>
<div>
<span>3D Max</span>
</div>
<div>
<span>Maya</span>
</div>
<div>
<span>Windows 8 Pro</span>
</div>
CSS
div {
line-height: 150%;
border-bottom: 5px solid #d71d00;
}
div span{
position:relative;
bottom: -10px;
background:#fff;
padding: 0 5px;
color:#82439a;
font-size: 16px;
font-weight: bold;
font-family: tahoma;
}
Please do let me know your feedback. Thanks
I have made a fiddle for reference: http://jsfiddle.net/kLFn9/
The overflow:hidden in question is highlighted.
Basically, i'm using :hover:after to show a tool tip. but the parent element has overflow: hidden on it. How can i force the element hovered to escape the parent element?
Relevant CSS:
div {
width:500px;
height:200px;
background:red;
margin: 50px;
overflow: hidden; /* this rule */
}
span:hover:after {
content: attr(data-name);
color: black;
position: absolute;
top: -150px;;
left: 0;
}
Unfortunately, there's no (easy) way to allow a child tag to override the effects of the overflow:hidden declaration on the parent div. See: Allow specific tag to override overflow:hidden
Your only possible recourse would be with javascript: first grab the span's offset relative to the document, then move it to another location in the DOM (i.e. direct child to the body), set its position to absolute, and use the offsets you grabbed to set its left and top properties, that would locate it at the same position within the document, but now it's not contained by the div, and so no longer needs to obey overflow:hidden.
You can use margin-top and padding-top.
padding-top will extend your parent area, but a negative margin-top will keep it in the expected position.
It will look like you're escaping the overflow, but in fact you're not.
div {
width:500px;
height:200px;
background:red;
margin: 50px;
overflow: hidden; /* this rule */
background-clip: content-box; /*use this to constrain the background color within the content-box and do not paint the padding */
padding-top: 200px; /* space required to display the tooltip */
margin-top: -150px; /*200px - 50px of the original margin*/
}
span {
background: blue;
color: white;
position: relative;
top:100px;
display:block;
width: 100px;
margin: auto;
}
span:hover:after {
content: attr(data-name);
color: black;
position: absolute;
top: -150px;;
left: 0;
}
<div>
<span data-name="here">hover</span>
</div>
This may introduce pointer events problems, but you can fix them using pointer-events then.
I am using simple z-index for force the element hovered to escape the parent element. Please check
div {
width:500px;
height:200px;
background:red;
margin: 50px;
overflow: hidden; /* this rule */
}
span {
background: blue;
color: white;
position: relative;
top:100px;
display:block;
width: 100px;
margin: auto;
}
span:hover:after {
content: attr(data-name);
color: black;
position: fixed; /* Here I replaced position abosolute to fixed */
top: 10px; /* Here I replaced top -150px to 10px */
left: 250px; /* Here I replaced positionleft 0 to 250px */
z-index:99999;} /* Here I added new z-index property to 99999 */
<div>
<span data-name="here">hover</span>
</div>
There is no way using plain CSS to overflow a parent elements borders with a child, if it was set to overflow:hidden;. On possible CSS option is to use a sibling element to that one which has overflow:hidden; set and show that as popup.
I'm not sure what your trying to get at, but I recreated a tooltip framework for you to view. It's basically smoke and mirrors where I call :hover and the .class associated with it.
http://jsfiddle.net/WE8Dw/
Hope this helps.
In some cases you can escape with div{position: absolute;}
You can set child's position to fixed.