I have some text, and beneath it a button. No matter what I change the top margin to, the button still sticks to the bottom of the text.
I am trying to leave a small gap between the text and the button.
Here is a Jsfiddle: http://jsfiddle.net/24bk2t78/
#import url(http://fonts.googleapis.com/css?family=Nunito:300);
.homethree a { text-decoration: none; }
sup { font-size: 36px; font-weight: 100; line-height: 55px; }
.button
{
margin-top:30%!important;
text-transform: uppercase;
letter-spacing: 2px;
text-align: center;
color: #0C5;
font-size: 24px;
font-family: "Nunito", sans-serif;
font-weight: 300;
position:relative;
padding: 20px 0;
width: 220px;
height:30px;
background: #0D6;
border: 1px solid #0D6;
color: #FFF;
overflow: hidden;
transition: all 0.5s;
}
.button:hover, .button:active
{
text-decoration: none;
color: #0C5;
border-color: #0C5;
background: #FFF;
}
.button span
{
padding-right: 0;
transition: padding-right 0.5s;
}
.button span:after
{
content: ' ';
right: -18px;
opacity: 0;
width: 10px;
height: 10px;
background: rgba(0, 0, 0, 0);
border: 3px solid #FFF;
border-top: none;
border-right: none;
transition: opacity 0.5s, top 0.5s, right 0.5s;
transform: rotate(-45deg);
}
.button:hover span, .button:active span
{
padding-right: 30px;
}
.button:hover span:after, .button:active span:after
{
transition: opacity 0.5s, top 0.5s, right 0.5s;
opacity: 1;
border-color: #0C5;
right: 0;
top: 50%;
}
<div class="row text-center homethree">
<div class="col-md-4">
<h4 class="service-heading">A Range of Classes</h4>
<p class="text-muted">WE TEACH CHILDRENS CLASSES, FAMILY GROUPS & ADULTS.</p>
<span>page 1</span>
</div>
<div class="col-md-4">
<h4 class="service-heading">Passionate Instructors</h4>
<p class="text-muted">ALL OUR CLASSES ARE TAUGHT BY PASSIONATE, MOTIVATIONAL & INSPIRING INSTRUCTORS.</p>
<span>page 2</span>
</div>
<div class="col-md-4">
<h4 class="service-heading">Friendly Team</h4>
<p class="text-muted">NEW MEMBERS ARE ALWAYS WELCOME. TWO FREE LESSONS FOR ALL.</p>
<span>page 3</span>
</div>
</div>
The problem is that <a> tags are inline elements. Changing them to an inline-block or block style element will make your margins properly apply.
.button {
display: inline-block;
}
More info: The Difference Between “Block” and “Inline”
On a sidenote, why not use <button> tags instead of <a>? They would be more appropriate.
try this demo
Fiddle
.button{
float:left;
margin-top:0;
}
.col-md-4
{
float:left;
width:100%
}
Related
This question already has an answer here:
CSS Divs Jumping when Border Added
(1 answer)
Closed 7 months ago.
I have a link button in a div:
<div class="text-box">
<h1 class="main-title">
<span class="main-title--primary">outdoors</span>
<span class="main-title--secondary">is where life happens</span>
</h1>
discover
</div>
And css property for it:
.main-cta-btn:link,
.main-cta-btn:visited {
display: inline-block;
background: white;
color: #28b485;
border: none;
border-radius: 50px;
padding: 10px 40px;
font-weight: bold;
font-size: 20px;
text-transform: uppercase;
animation: moveBottom 2s ease-in;
text-decoration: none;
}
When I try add hover affect to it with this code:
.main-cta-btn:hover,
.main-cta-btn:active {
background: transparent;
color: white;
border: 2px solid white;
cursor: pointer;
}
The link push up other div elements in the text-box div. How I can fix this push effect and I must say I have absolute positioning on main-title for centering it in the page.
main-title css property:
.text-box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
It's this border: 2px solid white; added on hover who's causing the issue, by increasing the width and hight of the button. The trick is to have that border from the beginning but with a transparent colour, and just change the colour on hover.
.main-cta-btn:link,
.main-cta-btn:visited {
display: inline-block;
background: white;
color: #28b485;
border: none;
border-radius: 50px;
padding: 10px 40px;
font-weight: bold;
font-size: 20px;
text-transform: uppercase;
animation: moveBottom 2s ease-in;
text-decoration: none;
/* Line I added */
border: 2px solid transparent;
}
.main-cta-btn:hover,
.main-cta-btn:active {
background: transparent;
color: red;
/* Line I added */
border-color: red;
cursor: pointer;
}
.text-box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
<div class="text-box">
<h1 class="main-title">
<span class="main-title--primary">outdoors</span>
<span class="main-title--secondary">is where life happens</span>
</h1>
discover
</div>
Im making an online game and was making the play button, but had the following issue:
What i want to achieve:
What is happening:
For some reason the background of the text is getting transparent...
HTML:
.wrapper {
display: flex;
justify-content: center;
}
.cta {
display: flex;
padding: 10px 45px;
text-decoration: none;
font-family: "Ceviche One", sans-serif;
font-size: 55px;
color: white;
background: #6225e6;
transition: 1s;
box-shadow: 6px 6px 0 black;
transform: skewX(-15deg);
}
.cta:focus {
outline: none;
}
.cta:hover {
transition: 0.5s;
box-shadow: 10px 10px 0 #fbc638;
}
.cta span:nth-child(2) {
transition: 0.5s;
margin-right: 0px;
}
.cta:hover span:nth-child(2) {
transition: 0.5s;
margin-right: 45px;
}
span {
transform: skewX(15deg);
}
span:nth-child(2) {
width: 20px;
margin-left: 30px;
position: relative;
top: 12%;
}
<div class="wrapper">
<a class="cta" href="#">
<span>JUGAR</span>
</a>
</div>
Any idea how to fix this? It works fine in codepen but not in my project.
Because you are applying the color on class cta which is parent for the text JUGAR. You need to set the color to the span not a tag
I was accidentaly applying a background-color to everything with *{}
Just hover on 'a headline' in the snippet below and you will see how elements are moving. Why?
There's no margin .. And they're only moving when I add border to the inline-block element. Try to add more border width in section.twelve a like:
section.twelve a {
border-bottom: 10px solid #FFFAFF;
}
But if you remove the border everything's fine.. Why is this behavior ? and is it only for border?
I just want to add any styles to the element without effecting the others.
section{
position: relative;
height: 300px;
padding: 15px 80px;
z-index: 1;
}
section h1{
font-size:3em;
font-weight: 100;
line-height: 1.3;
}
section a {
position: relative;
display: inline-block;
text-decoration: none;
transition: all 0.3s ease-in-out;
vertical-align: bottom;
}
section.twelve {
background: #121A5A;
color: #FFFAFF;
}
section.twelve a {
color:#D8315B;
font-weight: 700;
overflow: hidden;
padding: 0px 5px;
transition all 0.2s ease;
border-bottom: 5px solid #FFFAFF;
}
.twelve a:before{
content: "";
top:0; left: 0;
position: absolute;
width:100%; height: 100%;
background: #FFFAFF;
z-index: -1;
transition: all 0.2s ease;
transform: translateX(100%);
}
.twelve a:hover::before {
transform: translateX(-95%);
background: #D8315B;
}
.twelve a:hover{
color: #FFFAFF;
transform: translateX(5px);
border-bottom: 1px solid #FFFAFF;
}
<section class="twelve">
<h1>Write a headline that makes people do kind of a double take whenthey read it.</h1>
</section>
When you add, or change the width, of a border, that changes the size of the element. Hence, by adding the border on hover, the box grows to occupy more space, which naturally shifts the position of surrounding text / elements.
One method to resolve this issue is to always have the border present, so the size of the box is fixed. When the border shouldn't be visible, it's transparent.
Here's an example:
section {
position: relative;
height: 300px;
padding: 15px 80px;
z-index: 1;
}
section h1 {
font-size: 3em;
font-weight: 100;
line-height: 1.3;
}
section a {
position: relative;
display: inline-block;
text-decoration: none;
transition: all 0.3s ease-in-out;
vertical-align: bottom;
}
section.twelve {
background: #121A5A;
color: #FFFAFF;
}
section.twelve a {
color: #D8315B;
font-weight: 700;
overflow: hidden;
padding: 0px 5px;
transition all 0.2s ease;
border-bottom: 5px solid transparent; /* ADJUSTMENT */
}
.twelve a:before {
content: "";
top: 0;
left: 0;
position: absolute;
width: 100%;
height: 100%;
background: #FFFAFF;
z-index: -1;
transition: all 0.2s ease;
transform: translateX(100%);
}
.twelve a:hover::before {
transform: translateX(-95%);
background: #D8315B;
}
.twelve a:hover {
color: #FFFAFF;
transform: translateX(5px);
border-bottom: 5px solid white; /* ADJUSED */
}
<section class="twelve">
<h1>Write a headline that makes people do kind of a double take whenthey read it.</h1>
</section>
Yes, on hover you are changing element's border, so, element's total height also changes
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%.
Ive tried using z-index to over rule the absolute and relative positioning on the box hovers but it just doesn't work. Any body got any suggestions as to why? The links are there but to the side of the images rather than directly above.
HTML
<div id="container1">
<img src="images/blindsided1.jpg">
<div class="textbox">
<p id="title1">First Coding <br> Attempt</br></p>
<p id="text1"> This brief was my first attempt at designing a website.</p>
</div>
</div>
CSS
a:hover {
z-index: 99;
}
#container1 {
position:absolute;
float:left;
}
.textbox:hover {
opacity:1;
}
#title1 {
padding-top: 20px;
text-align:center;
color:#FFF;
font-family:"bebas";
font-size:32px;
word-spacing:2px;
letter-spacing:1px;
text-decoration:underline;
}
#text1 {
padding-top:40px;
text-align:center;
color:#FFF;
font-family:"bebas";
font-size:16px;
word-spacing:2px;
letter-spacing:1px;
}
.textbox {
width:361px;
height:362px;
position:absolute;
top:0;
left:0;
opacity:0;
border-radius:300px;
background-color: rgba(0, 0, 0, 0.75);
-webkit-transition: all 0.7s ease;
transition: all 0.7s ease;
}
You can wrap the whole hoverable div in the anchor tag, that way you can ensure that the hoverable div is clickable and that it points to the link that the image is referencing.
HTML
<div id="container1">
<a href="firstcoding.html">
<img src="http://zoarchurch.co.uk/content/pages/uploaded_images/91.png">
<div class="textbox">
<p id="title1">First Coding
<br>Attempt</br>
</p>
<p id="text1">
This brief was my first attempt at designing a website.
</p>
</div>
</a>
</div>
CSS
a:hover {
z-index: 99;
}
.textbox:hover {
opacity:1;
}
#title1 {
padding-top: 20px;
text-align:center;
color:#FFF;
font-family:"bebas";
font-size:32px;
word-spacing:2px;
letter-spacing:1px;
text-decoration:underline;
}
#text1 {
padding-top:40px;
text-align:center;
color:#FFF;
font-family:"bebas";
font-size:16px;
word-spacing:2px;
letter-spacing:1px;
}
.textbox {
width:361px;
height:362px;
position:absolute;
top:0;
left:0;
opacity:0;
border-radius:300px;
background-color: rgba(0, 0, 0, 0.75);
-webkit-transition: all 0.7s ease;
transition: all 0.7s ease;
}
The absolutely positioned child is sitting on top of the anchor link so any pointer interaction would fail.
You could use pointer-events:none on that child which would allow events to pass through but browser support it not great.
* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
a,
img {
display: block;
}
#container1 {
position: relative;
border: 1px solid red;
float: left;
}
#container1:hover .textbox {
opacity: 1;
}
#title1 {
padding-top: 20px;
text-align: center;
color: #FFF;
font-family: "bebas";
font-size: 32px;
word-spacing: 2px;
letter-spacing: 1px;
text-decoration: underline;
}
#text1 {
padding-top: 40px;
text-align: center;
color: #FFF;
font-family: "bebas";
font-size: 16px;
word-spacing: 2px;
letter-spacing: 1px;
}
.textbox {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
opacity: 0;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.75);
-webkit-transition: all 0.7s ease;
transition: all 0.7s ease;
pointer-events: none;
}
<div id="container1">
<a href="firstcoding.html">
<img src="http://lorempixel.com/output/cats-h-c-361-362-10.jpg" />
</a>
<div class="textbox">
<p id="title1">First Coding
<br/>Attempt
</p>
<p id="text1">This brief was my first attempt at designing a website.</p>
</div>
You would do better to wrap the div inside the link and re-factor your code as suggested in the comments.