Cannot vertical align absolute span - html

I'm trying to positioned the navigation in the middle position of the container, but I haven't can, yet. I'm using span for navigation.
I have positioned the parent containers as relative how is recommended and then assign position to span using top, right and left.
img{
max-width:600px;
}
.slider{
position: relative;
margin: 0 auto;
/*overflow: hidden;*/
width: 100%;
}
.slider ul{
/*white-space: nowrap;*/
list-style: none;
padding: 0;
position: relative;
}
.slider ul li{
margin-right: -4px;
width: 100%;
position: absolute;
display: none;
top: 0;
left: 0;
}
.slider ul li.active{
display: list-item;
}
/*Navegacion*/
.flechas-nav .anterior, .flechas-nav .siguiente{
position: absolute;
cursor: pointer;
}
.flechas-nav .anterior {
top: 50%;
}
.flechas-nav .siguiente{
right: 0;
top: 50%;
}
.flechas-nav span {
height: 100%;
width: 10%;
opacity: 0;
position: absolute;
z-index: 900;
}
.slider:hover .flechas-nav span {
opacity: 1;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="slider">
<ul class="slider-contenedor">
<li class="slide active">
<img class="img-responsive" src="http://i.imgur.com/Khgz4Qd.jpg" alt="">
</li>
<li class="slide">
<img class="img-responsive" src="http://i.imgur.com/Dc3XS7w.jpg" alt="">
</li>
<li class="slide">
<img class="img-responsive" src="http://i.imgur.com/aVzLCnM.jpg" alt="">
</li>
</ul>
<div class="flechas-nav">
<span class="anterior">Left</span>
<span class="siguiente">Right</span>
</div>
</div>
</div>
</div>
I put demo on jsfiddle.
How can I position vertical middle this span?
Demo

You have a few issues in your CSS. For one, when using position:absolute it removes the element out of the flow of the document. Both your image and text are being positioned absolute. so there is no height or width being registering in your document. That's why top: 50% isn't working. So start by removing the positioning from .slider ul li
.slider ul li{
margin-right: -4px;
width: 100%;
/*position: absolute;*/ //remove
display: none;
/*top: 0;*/ //not needed anymore
/*left: 0;*/ //not needed anymore
}
Now top:50% will work and the words "left" and "right" will seemingly be centered. There's a few more things:
Next, your div containers are display: block by default so they take the width of the document. Since your image is not width:100% the container is too large and the word "right" will disappear because the image isn't wide enough. (check out your demo and user4429928's answer - make the screen large and "right" will seem to disappear). You can set the container to display: inline-block so it wraps the image:
.container{
display: inline-block;
}
Now, I would remove the absolute positioning from .anterior and .siguiente and add the positioning to the parent instead and float the children:
.flechas-nav{
width: 100%;
position: absolute;
top: 50%;
color: #FFF;
transform: translateY(-50%); //read note below about this
}
.flechas-nav .anterior {
float: left;
}
.flechas-nav .siguiente{
float: right;
text-align: right;
}
Calling top:50% does not actually center an element, it positions it 50% from it's top. In this case becasue your text has very little height, it looks ok, but if you added height: 200px or something, you would see that it's not actually centered. To correct this you can use:
transform: translateY(-50%);
Now your content is centered and within its parent. And to fix the image rotating issue you can set the image thats fading out to absolute and the one that comes in to static:
FIDDLE

try the demo : http://jsfiddle.net/17ow14kf/2/
img{
max-width:600px;
}
.slider {
height: 210px;
margin: 0 auto;
position: relative;
width: 100%;
}
.slider ul{
/*white-space: nowrap;*/
list-style: none;
padding: 0;
position: relative;
}
.slider ul li{
margin-right: -4px;
width: 100%;
position: absolute;
display: none;
top: 0;
left: 0;
}
.slider ul li.active{
display: list-item;
}
/*Navegacion*/
.flechas-nav .anterior, .flechas-nav .siguiente{
position: absolute;
cursor: pointer;
}
.flechas-nav .anterior {
top: 50%;
}
.flechas-nav .siguiente{
right: 0;
top: 50%;
}
.flechas-nav span {
height: 100%;
width: 10%;
opacity: 0;
position: absolute;
z-index: 900;
}
.slider:hover .flechas-nav span {
opacity: 1;
}
.flechas-nav {
height: 50px;
margin-top: -25px;
position: absolute;
top: 50%;
width: 100%; color:#fff;
}
.flechas-nav .anterior {
left: 0;
top: 50%;
}

Related

How to make effect behind the picture?

How to make effect behind the picture? Like you can see in the picture. and of course image have to be responsive.
one line solution:
img {
padding:20px;
background:linear-gradient(transparent 40px,red 0 calc(100% - 40px),transparent 0);
}
<img src="https://picsum.photos/300/300">
Can you please check the below code? Hope it will work for you. In this code, we have given position relative to the img-block and gave background color in it's pseudo-element with position absolute.
.img-block {
padding: 30px;
position: relative;
display: inline-block;
}
.img-block:before {
content: "";
display: block;
position: absolute;
width: 100%;
height: 75%;
background: #F9CDF1;
top: 50%;
left: 0px;
z-index: 1;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
}
.img-block img {
position: relative;
z-index: 2;
}
<div class="img-block">
<img src="https://via.placeholder.com/490x590.png" alt="image" />
</div>
You can recreate an effect like this by utilising absolute positioning with a relative parent.
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.imageWrapper {
background: rebeccapurple;
width: 200px;
height: 200px;
position: relative;
margin: 16px;
}
.imageWrapper img {
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
<div class="imageWrapper">
<img src="https://unsplash.it/180/220" alt="">
</div>
Ignore the body styles, they are just to illustrate the point better in the preview. The .imageWrapper and .imageWrapper img are the things you want to pay attention to.

text-align and margin CSS properties are not responsive

.scss
div.playlist {
position: relative;
display: inline-block;
}
div.playlist {
span {
position: absolute;
text-align: center;
height: 100%;
width: 100%;
color: white;
font-size: 20px;
}
.span-icon {
padding-bottom: 50px !important;
}
}
div.playlist span:before {
display: inline-block;
vertical-align: middle;
height: 100%;
content: '';
}
.html
<div class="playlist">
<span class="span-icon"><ion-icon [name]="data.icon"></ion-icon></span>
<span>{{data.text}}</span>
<img [src]="data.imageUrl" [alt]="data.text" />
</div>
Out Put
Now I need as shown below.Please don't consider about the different icon type and the text.I just need this.I need a responsive top right and the bottom right appearance of the icon and text.I have tried with text-align: right and the margin properties.But you know that approach is not responsive on different view ports.So can you help me to solve this issue?
Position your both icons and span text as absolute and then if needed you could use CSS calc() function to align them at top-right and bottom-right above the image.
.playlist {
position: relative;
display: inline-block;
width: 240px;
height: 200px;
overflow: hidden;
}
.playlist img {
width: 100%;
height: 100%;
}
.playlist .span-icon {
position: absolute;
top: 5px;
right: calc(100% - 98%);
color: #fff;
}
.playlist .tm {
position: absolute;
bottom: 5px;
right: calc(100% - 98%);
color: #fff;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="playlist">
<span class="span-icon"><i class="fa fa-film"></i></span>
<span class="tm">2:10</span>
<img src="http://lorempixel.com/output/city-q-c-640-480-6.jpg">
</div>
<div class="playlist">
<span class="span-icon"><ion-icon [name]="data.icon"></ion-icon></span>
<span class="span-text">{{data.text}}</span>
<img [src]="data.imageUrl" [alt]="data.text" />
</div>
CSS
.span-icon {
position: absolute;
top: 0;
right: 0;
}
.span-icon {
position: absolute;
bottom: 0;
right: 0;
}
You simply set the icons absolute and position it with top, right. ...

Force child element visible outside an overflow:hidden parent

Only solution I found was to put a position: fixed on element I want to fully see. Any other options? (I dont want to 'cool-image' fixed). Help or hint would be awesome. Also, if anyone can explain solution - that would be even better
Fiddle: JSFiddle
HTML
<div class="img-cont">
<div id="slider">
<ul>
<li class="slide">
<img src="http://www.sportspearl.com/wp-content/uploads/2017/05/football-150x150.png" >
<div class="cool-image"></div>
</li>
</ul>
</div>
</div>
CSS
.img-cont{
height: 270px;
position: relative;
}
#slider {
position: relative;
background: green;
overflow: hidden;
width: 440px;
height: 200px;
}
#slider ul{
position: relative;
margin: 0;
padding: 0;
}
#slider ul li {
position: relative;
display: block;
float: left;
margin: 0;
padding: 0;
width: 440px;
height: 270px;
text-align: center;
line-height: 300px;
}
div.cool-image{
background-color: white;
position: absolute;
border: 5px solid #EEEEEE;
width: 650px;
height: 350px;
z-index: 1;
display: inline-block;
background-repeat: no-repeat;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Basketball.png/170px-Basketball.png);
margin-left: -40px; /* Just to product situation */
display: inline-block;
}
Unfortunately, you can't. The child element is only capable of changing within the parents region when the position is not set to fixed or absolute.
If you don't want to make the child fixed, you could try position:absolute; and set the parent to position: relative;
Like this...
.slide img {
position: absolute;
z-index: 2;
}
.slide {
position: relative;
}
Or you could try to only hide the overflow on 1 direction. Like overflow-y:hidden; Or overflow-x: hidden;

How do I change the position of the buttons in responsive design?

I am trying to change the positions of the buttons which will scale down to mobile relative to the image div. I tried position/absolute; but it didn't take effect. Neither did the buttons stay relative inside the image div. I am not sure if the media queries will do the job or not. Any solutions?
<div id="hero-wrapper" class="main_wrapper hero">
<img src="images/home-hero-image.jpg" alt="fruute">
<div class="herobuttons"><div id="hero-shop-cookies" class="hero-btn">shop cookies</div>
<div id="hero-shop-gifts" class="hero-btn">shop gifts</div></div>
</div>
CSS
#hero-shop-cookies {
top: 398px;
left: 550px;
}
#hero-shop-gifts {
top: 398px;
left: 715px;
}
.main_wrapper hero{
width: 100%;
}
#hero-wrapper{
position: relative;
}
.hero-wrapper > .hero-btn a{
position: absolute;
left: 0;
right: 15px;
bottom: 20px;
top: 0;
background-color: red;
}
#media screen and (min-width: 768px) {
#nav-menu{
display: none;
}
#navigation li, #navigation a{
float:left;
}
.main_wrapper hero, .hero-btn{
width: 100%;
}
.herobuttons{
position: relative;
left: 0;
top: 0;
}
#slideshow-area{
width: 300px;
}
}
Change min-width: 768px; to max-width: 768px;

Float a DIV toward bottom-right using CSS

I have three DIV and inside the DIV, I would like to float the "Learn More" to bottom right so it will be on top of the grey background.
CSS
/* the div for LEARN MORE */
#trt {
z-index: 9999999999999;
position: relative;
float: right;
bottom: 0; // not working
top: 12; //not working
}
/* the entire div */
.main .cols { padding-left: 2px; padding-right: 0px; padding-top: 10px; }
.main .cols .col { width: 315px; height: 108px; float: left; background: url(images/tempf.png) no-repeat 0 0; }
.main .cols .col:after { content:''; width: 100%; clear: both; }
.main .cols .col + .col { padding-left: 20px; }
.main .cols .col img.hid { float: left; width: 129px; height: 108px; }
.main .cols .col-cnt { width: 183px; float: right; }
.main .cols .col .more { font-weight: bold; color: #0206AA; }
HTML
<div class="main">
<section class="cols">
<div class="col">
<a href="link.aspx">
<img class="hid" src="css/images/orgNews.png" alt="" />
</a>
<div class="col-cnt">
<h3 style="color: #FFFFFF;">Organization News</h3>
<p style="color: #FFFFFF;">Interfaith Medical Center related news and updates</p>
<div id="trt">
<img src="css/images/arrow.png" width=11 height=11 align=absmiddle />
Learn More
</div>
</div>
</div>
</section>
</div>
CSS - After Edit
.trt {
z-index: 9999999999999;
position: absolute;
bottom: 3px;
right: 3px;
}
...
.main .cols .col-cnt { width: 183px; float: right; position: relative; }
...
This CSS worked:
.trt {
z-index: 9999999999999;
position: absolute;
top: 85px;
right: 3px;
}
set col-cnt div to position: relative set trt to position: absolute; bottom:3px; right:3px; that should get you where you need to be.. also, trt should be a class if it is being reused
First at all, you must set parent of #trt to relative.
#parent-of-trt {
position: relative;
}
And set #trt to absolute
#trt {
position: absolute;
left: 4px;
bottom: 5px;
}
Your float: right isn't working because of a width issue on the #trt div. Basically it's extending 100% of the width and so it can't technically go left or right. Instead of floating, just use...
#trt { text-align: right; }
???
As for getting it pushed down onto that grey line, add some margin-top to #trt to do that...
Other solution would be to use position: absolute; but would be less preferable.
Maybe use position: absolute; instead of relative
change position as fixed like following:
position:fixed;
it should work.