I want to center my text in a relative height div which contains an image. I use absolute position but when my text is on two lines, the text is not centered. I've already tried to use a table but it doesn't work due to the img.
HTML:
<div id="hubs">
<h3>Nos Hubs</h3>
<hr>
<a class="thumbnail vignette-hub" href="http://kkw.fr">
<img style="opacity: 0.6;filter: alpha(opacity=60);" alt="AĆ©roport de Nantes" src="http://kkw.fr/uploads/upload-center/nantes-vue-aerienne091501270208.png" width="100%" />
<p class="txt-hub-image">
Hub de</br>Nantes
</p>
</a>
</div>
CSS :
.txt-hub-image {
z-index: 100;
position: absolute;
left: 0px;
top: 50%;
width: 100%;
height: 100%;
text-align: center;
text-decoration: none;
color: white;
font-weight: bold;
font-size: 16px;
}
.vignette-hub {
position: relative;
width: 25%;
min-width: 135px;
}
.thumbnail {
display: block;
padding: 4px;
margin-bottom: 20px;
line-height: 1.42857143;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 4px;
-webkit-transition: border .2s ease-in-out;
-o-transition: border .2s ease-in-out;
transition: border .2s ease-in-out;
}
.thumbnail > img,
.thumbnail a > img {
margin-right: auto;
margin-left: auto;
}
a.thumbnail:hover,
a.thumbnail:focus,
a.thumbnail.active {
border-color: #337ab7;
}
.thumbnail .caption {
padding: 9px;
color: #333;
}
Do you have any ideas ?
There are a few changes required to your snippet to make it automatically work for all dimensions:
p tags by default have a margin-top. If you don't reset it, then absolutely positioning it at 50% would become 50% + margin-top. This needs to be reset.
When you absolutely position an element at top: 50%, the box gets positioned at 50% height of the container and text keeps getting added from that position on. So, to match the center of the text block with the center of the parent, you have to translate the box with the text up by 50% of its own size. This can be done by adding transform: translateY(-50%).
You don't need to add a height: 100% on the p tag and it can be removed.
Note: Using transform method for positioning needs CSS3 support but I assume this shouldn't be a problem because you are already using transition.
If you want to support non CSS3 compatible browsers, have a look at the other approaches mentioned here. I have added a different answer just to explain the first two points I had mentioned above.
.txt-hub-image {
z-index: 100;
position: absolute;
left: 0px;
top: 50%;
width: 100%;
text-align: center;
text-decoration: none;
color: white;
font-weight: bold;
font-size: 16px;
/* added to fix the vertical centering */
margin-top: 0px;
transform: translateY(-50%);
}
.vignette-hub {
position: relative;
width: 25%;
min-width: 135px;
}
.thumbnail {
display: block;
padding: 4px;
margin-bottom: 20px;
line-height: 1.42857143;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 4px;
-webkit-transition: border .2s ease-in-out;
-o-transition: border .2s ease-in-out;
transition: border .2s ease-in-out;
}
.thumbnail > img,
.thumbnail a > img {
margin-right: auto;
margin-left: auto;
}
a.thumbnail:hover,
a.thumbnail:focus,
a.thumbnail.active {
border-color: #337ab7;
}
.thumbnail .caption {
padding: 9px;
color: #333;
}
<div id="hubs">
<h3>Nos Hubs</h3>
<hr>
<a class="thumbnail vignette-hub" href="http://kkw.fr">
<img style="opacity: 0.6;filter: alpha(opacity=60);" alt="AĆ©roport de Nantes" src="http://kkw.fr/uploads/upload-center/nantes-vue-aerienne091501270208.png" width="100%" />
<p class="txt-hub-image">
Hub de</br>Nantes
</p>
</a>
</div>
Here is a demo fiddle as the snippets feature seems to be down.
Change your .txt-hub-image class, top value from 50% to 25%.
Related
This question already has an answer here:
left-right movement.. css only very generic
(1 answer)
Closed 3 years ago.
The code below draws a circle inside a div. On the div hover the idea is to move the circle to the other edge of the div. My code works but the circle in instantly moving to the other side. I am trying to find a way to animate it moving through the div until it reaches left and then go back.
Which properties can I use to achieve this ?
div {
border-radius: 26px;
border: 1px solid;
height: 45px;
text-align: right;
transition: all .4s ease;
}
div:hover {
text-align: left;
}
.circle {
height: 45px;
width: 45px;
background-color: #bbb;
border-radius: 26px;
display: inline-block;
}
<div class="div1"><span class="circle"></span></div>
You cannot transition the text-align property.
Here's an example using the left property:
div {
border-radius: 26px;
border: 1px solid;
height: 45px;
}
div:hover .circle{
left:calc(100% - 45px);
}
.circle {
height: 45px;
width: 45px;
background-color: #bbb;
border-radius: 26px;
display: inline-block;
position:relative;
left:0;
transition: left .4s ease;
}
<div class="div1"><span class="circle"></span></div>
You're on the right path, but a little off. First, you want to apply the transition to the element you want to animate-- in this case the .circle element. Second, you need to use a property that can be animated using transition. We will use transform as it is more performant than rendering a position property. This is perhaps not exactly what you are looking for, but should put you on the right path.
div {
border-radius: 26px;
border: 1px solid;
height: 45px;
position: relative;
width: 98vw;
}
.circle {
height: 45px;
width: 45px;
background-color: #bbb;
border-radius: 26px;
display: inline-block;
transition: all 0.4s ease;
position: absolute;
right: 0px;
transform: translateX(0vw);
}
div:hover .circle {
transform: translateX(calc(-98vw + 100%));
}
<div class="div1"><span class="circle"></span></div>
I'm required to have a rounded "growing" effect upon hovering over a button.
Please see this link for a reference of how I need the button to work.
http://demo1.wpopal.com/corpec/home-4/
Currently I have achieved the "Not this" effect upon hover; though my employer wants the effect to have that bit of rounding.
I used the following css on the "not this" button to achieve the growing effect, though i need the edges to be rounded.
.Custom-Button a {
position: relative;
display: inline-block;
padding: 15px 70px;
border: 1px solid #fdc900 !important;
color: white;
font-size: 30px;
font-family: arial;
background-image: linear-gradient(#fdc900, #fdc900);
background-position: 50% 50%;
background-repeat: no-repeat;
background-size: 0% 100%;
transition: background-size .5s, color .5s;
}
.Custom-Button a:hover {
background-size: 100% 100%;
color: black;
}
<div class="Custom-Button">
BUTTON
</div>
I'm only allowed to use CSS to achieve the following effect and have already spent a day trying to get this to work.
applying pseudo element for button solve it ! hope this help!
.Custom-Button{
position: relative;
display: inline-block;
padding: 15px 70px;
border: 1px solid #fdc900 !important;
color: white;
font-size: 30px;
font-family: arial;
border-radius:50px;
position:relative;
}
.Custom-Button a{
z-index:99;
text-decoration:none;
transition:color 1s;
}
.Custom-Button:hover:after{
width:100%;
}
.Custom-Button:hover a{
color:black;
}
.Custom-Button:after{
width:0%;
transition: width 1s;
height:100%;
z-index:-1;
content:"";
position:absolute;
border-radius:50px;
top:0;
left:50%;
transform:translateX(-50%);
background-image: linear-gradient(#fdc900, #fdc900);
}
<div class="Custom-Button">
BUTTON
</div>
You can achieve this effect, by combining z-index, and transitions of position and width of an underlying element:
When hovering, the child of the filler, will transition from
position: absolute; left: 50%;
to
position: absolute; left: 0;
while also resizing from width: 0; to width: 100%;
This is what will give you the desired effekt of "growing from the middle"
also you need to apply a border radius to your growing element
a {
position: relative;
display: inline-block;
padding: 15px 70px;
border: 1px solid #fdc900 !important;
color: black;
font-size: 30px;
font-family: arial;
border-radius: 32px;
}
.text {
position: relative;
z-index: 2000;
}
.filler {
position: absolute;
z-index: 1000;
top: 0;
left: 50%;
width: 0;
height: 100%;
border-radius: 32px;
/* half of <a> height */
background-color: #fdc900;
transition: width .5s, color .5s, left .5s;
}
a:hover .filler {
z-index: 500;
width: 100%;
left: 0;
color: black;
}
a:hover .text {
color: white;
}
<a href="#">
<div class="text">
BUTTON
</div>
<div class="filler">
</div>
</a>
So I have a border transition on hover and on active for a circular button so the border increases in size. However, the border expands downwards, pushing the button downward. Is there any way to make it so the border expands evenly outward? I've searched this site and others for solutions, and while there are similar questions, they don't answer this specifically.
Thanks!
HTML:
<center><a class="btn" href="#"></a></center
CSS:
.btn {
vertical-align: top;
transform: translateY(20px);
background-color: black;
display: inline-block;
height: 300px;
width: 300px;
border-radius: 50%;
border: 0px solid red;
transition: border-width 0.1s ease-in;
margin: 0.5em;
}
.btn:hover {
border: 20px solid red;
}
.btn:focus {
border: 75px solid red;
}
Instead of using border, you can generate a border effect by placing a pseudoelement behind the button, and transforming its scale on hover and focus as needed.
*also note that <center> is deprecated in HTML5. You can center content with CSS instead.
.btn {
display: block;
margin: 5rem auto;
position: relative;
background-color: black;
height: 300px;
width: 300px;
border-radius: 50%;
transition: border-width 0.1s ease-in;
}
.btn:before {
content: '';
display: block;
position: absolute;
background: red;
border-radius: 50%;
width: 300px;
height: 300px;
z-index: -1;
transition: all .1s ease;
}
.btn:hover:before {
transform: scale(1.1);
}
.btn:focus:before {
transform: scale(1.25);
}
<a class="btn" href="#"></a>
I've built these circles that expand a border when there is a mouseover. The only problem I'm getting now is some times the circle will jitter/shake. And it becomes more apparent when I set the transition: all .1s ease-in-out; to more than .2s.
Is there a work around to this problem or is that just the way it is?
Here's the code in JsFiddle
Thanks for any and all help!
EDIT: I am transitioning the dimensions (width and height) of the circles to maintain centering. I realize this is causing the jittering during the transition. Is there a work around?
I got rid of the percent values for top/left positioning, cleaned up the margins and aligned the border-width of the outer circle:
Here is a DEMO
.box {
position: relative;
width: 220px;
height: 220px;
float: left;
margin-right: 50px;
}
.clearcircle {
position: absolute;
top:15px;
left:15px;
width: 190px;
height:190px;
border-radius: 100%;
background-color: transparent;
border: 5px solid #c0392b;
transition: all .2s ease-in-out;
}
.clearcircle:hover {
width:220px;
height: 220px;
top: 0px;
left: 0px;
border: 5px solid #c0392b;
}
.circle {
position: absolute;
top:50%;
margin-top: -100px;
left: 50%;
margin-left:-100px;
width: 200px;
height:200px;
border-radius: 100%;
background-color: #e74c3c;
overflow: hidden;
transition: all .2s ease-in-out;
}
.circle p {
position:relative;
text-align: center;
top: 50%;
margin-top: -55px;
color: white;
transition: all .3s;
}
.circle:hover{
background-color: #e97468;
}
Don't transition the width and the height. Keep the same width and height and just transition the border of your outer circle.
For your inner circle (.circle), set a white border 12px solid #ffffff. Now it is always in the same place relative to the outer circle, and now it will not have to change size. Also the title can not jump around because it is always in the same position.
For the outer circle, when it is not hovered, make sure it has the same size and border as when it is, but make the border white, 5px solid #ffffff.
I think you can then also do away with a lot of your extra positioning.
Here is a modified jsFiddle so you can take a look, and here is the CSS modified:
.box {
position: relative;
width: 220px;
height: 220px;
float: left;
margin-right: 50px;
text-align: center;
}
.clearcircle {
width: 225px;
height:225px;
border-radius: 100%;
background-color: transparent;
border: 5px solid #ffffff;
transition: all .2s ease-in-out;
}
.clearcircle:hover {
border: 5px solid #c0392b;
}
.circle {
width: 200px;
height:200px;
border: 12px solid #ffffff;
border-radius: 100%;
background-color: #e74c3c;
overflow: hidden;
transition: all .2s ease-in-out;
}
.circle p {
position:relative;
text-align: center;
color: white;
transition: all .3s;
}
.circle:hover{
background-color: #e97468;
}
Incidentally, putting a div or a p in your a tag breaks the tag for validated XHTML. You may want to use a div instead, with an "on click" action added that causes it to behave as a link.
Debounce jitter by margin: 0 -12%; if adding padding padding: 0 12%;
menu li a:hover {
margin: 0 -12%;
padding: 0 12%;
color: #fff;
background: #ff5a5f;
display: inline-block;
}
My codepen
http://codepen.io/leongaban/pen/sBvfL
So having a strange issue, I'm trying to get the background of the #portfolio div to stretch to contain the thumbnails below which are in an ul list inside of the portfolio div.
However 100% or auto doesn't affect the height. I have to set a static height, like 1000px. To get the background to cover the thumbs. However I'm trying to not set a static height since the thumbnails will get longer.
Perhaps I've been coding this too long, how would you code this?
HTML
<div id="portfolio">
<div class="portfolio-nav">
<h1>Portfolio</h1>
</div>
<div id="showcase-holder">
<div id="showcase-div">
<ul id="portfolio-thumbs">
<li>
<a href="/portfolio/chipestimate">
<img class="role-thumb" src="http://leongaban.com/images/thumb_chipestimate.jpg" alt="ChipEstimate"/>
</a><p>ChipEstimate</p>
</li>
<li>
<a href="/portfolio/shabang" title="Shabang">
<img class="role-thumb" src="http://leongaban.com/images/thumb_shabang.jpg" alt="Shabang"/>
</a><p>Shabang</p>
</li>
</ul>
</div>
</div>
CSS
body {
background: brown;
}
#portfolio {
position: relative;
width: 100%;
height: 50%;
background: #fff;
border-top: 2px solid #ccc;
z-index: 1;
}
#portfolio ul { list-style: none; }
.portfolio-nav { margin: 0 0 20px 0; }
.portfolio-nav h1 {
padding: 30px 0 10px 0;
text-align: center;
font-size: 2.5em;
font-weight: 700;
color: #d74927;
text-shadow: 1px 1px #ccc;
}
#showcase-holder {
position: relative;
width: 80%;
height: 100%;
margin: 0 auto;
border-bottom: 2px solid #ccc;
}
#portfolio-thumbs {
position: relative;
float: left;
list-style-type: none;
margin: 0 0 40px 0;
padding: 0 0 0 5%;
width: 100%;
height: 100%;
}
#portfolio-thumbs li {
position: relative;
float: left;
width: 20%;
margin: 1%;
text-align: center;
padding: 5px 5px 15px 5px;
background-size: 100% auto;
overflow: hidden;
background: white;
-webkit-transition: background .3s;
-moz-transition: background .3s;
-ms-transition: background .3s;
transition: background .3s;
}
#portfolio-thumbs li:hover {
color: #fff;
background: #d74927;
-webkit-transition: background .5s;
-moz-transition: background .5s;
-ms-transition: background .5s;
transition: background .5s;
}
#portfolio-thumbs li a {
color: #333;
text-decoration: none;
}
#portfolio-thumbs li p {
padding: 10px 0 0 0;
}
#portfolio-thumbs li img.role-thumb {
width: 95%;
min-width: 170px;
padding-top: 5px;
}
Parents will normally expand to the height of their children, though won't in case the children are relative.
You can remove positions and floats to accomplish expanding.
In order to expand a parentdiv based on positioned children try overflow: auto; on #portfolio. This will make #portfolio expand to the height of its children. As seen on this fork of your example.
overflow: auto; will actually let your browser decide, which normally renders this to overflow: hidden;. Though I tend to use overflow: auto; to prevent issues with scrollbars as the page possibly expands later on.
You have added float to your li elements, which means that the parent will not expand to contain these elements.
You can work around this by adding a clearing div.
<div style="clear:both;"></div>
after the showcase-holder div.
Adding overflow: hidden to both #showcase-div and #portfolio-thumbs should do it for you.
http://jsfiddle.net/5SFFP/