I have a responsive website, on one of the pages I have 2 buttons, they are next to each other when the screen size is wide enough to fit both buttons, when the page is made smaller (i.e for a smartphone) then the second button (right) goes below the first. This works fine.
However when the buttons are one above the other they are positioned on the left, I would like to center them.
I have based it on this: http://jsfiddle.net/erenyener/puA72/2/
Here is a JS Fiddle: https://jsfiddle.net/kg2grant/
Here is the code:
HTML
<div class="button-wrapper">
<div class="fiftyLEFT" ><button type="submit" class="submit" value="submit" name="submit_button"><span>Search</span></button></div>
<div class="fiftyRIGHT"><button onclick="location.href='https://google.com';" class="submit" style="font-size: 22px; width: 500px" value="show" name="show_button">Google</button></div>
</div>
CSS
button.submit {
border: none;
background-image: linear-gradient(-180deg, #52A1DD 50%, #53A2DE 100%);
border-radius: 100px;
max-width: 90%;
margin-right: 5%;
padding: 8px 10px;
font-size: 26px;
color: #fff;
line-height: 30px;
cursor: pointer;
-webkit-transition: all .15s ease-in-out;
-moz-transition: all .15s ease-in-out;
-o-transition: all .15s ease-in-out;
transition: all .3s ease-in-out;
margin-top: 10px;
z-index: 1;
position: relative;
}
button.submit:hover {
color: #3193e9;
background: #fff;
}
button.submit {
width: calc(.5px + 50vw);
}
button.submit img: hover {
background-color: #C37500;
opacity:0.7 !important;
filter:alpha(opacity=70) !important;
}
.button-wrapper
{
width:100%;
padding-top:10px;
padding-bottom:10px;
display:inline-block;
border: 3px solid black
}
.button-wrapper > .fiftyLEFT
{
width:50%;
float:left;
min-width:50px
}
.button-wrapper > .fiftyRIGHT
{
min-width:400px;
width:50%;
float:left;
white-space: nowrap;
}
I have tried many things to like margin 0 auto and adding another container however had no luck! Thanks in advance for your help.
You can change the display to flex and achieve what you want by using #media query.
Forked jsFiddle with the behavior needed: https://jsfiddle.net/o1gpcLcr/
You will have to remove the float left property to do the margin auto.
.button-wrapper > .fiftyLEFT {
width: 50%;
/* float: left; */
min-width: 50px;
margin-left: auto;
margin-right: auto;
}
.button-wrapper > .fiftyRIGHT {
min-width: 400px;
width: 50%;
/* float: left; */
white-space: nowrap;
margin-left: auto;
margin-right: auto;
}
Then do the float left on larger screens with #media (min-width: 768px), or wherever you want it to break.
Using float makes margin auto behave strangely because the floated element have been removed from the regular flow of content. If you remove float: left from your last two CSS rules and declare margin: auto, it should work.
Alternatively, you can remove float: left, and then add
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
to your .button-wrapper rule. Using flexbox has the same effect as margin auto, but may make other changes and additions easier later in development.
To learn more about flexbox, check out https://flexbox.io for some great video tutorials.
You'll want to use CSS media queries, with more of a 'mobile-first' approach.
Make the buttons width: 100% by default and then use #media (min-width: 768px) {...} to change the width to 50%.
I've made some changes to your code:
button.submit {
border: none;
background-image: linear-gradient(-180deg, #52A1DD 50%, #53A2DE 100%);
border-radius: 100px;
padding: 8px 10px;
margin: 5px 0;
font-size: 26px;
color: #fff;
line-height: 30px;
cursor: pointer;
-webkit-transition: all .15s ease-in-out;
-moz-transition: all .15s ease-in-out;
-o-transition: all .15s ease-in-out;
transition: all .3s ease-in-out;
z-index: 1;
position: relative;
}
button.submit:hover {
color: #3193e9;
background: #fff;
}
button.submit img: hover {
background-color: #C37500;
opacity:0.7 !important;
filter:alpha(opacity=70) !important;
}
.button-wrapper {
width:100%;
padding: 10px 0;
display:inline-block;
border: 3px solid black
}
button {
width: 100%;
}
#media (min-width: 768px) {
button {
float: left;
width: 50%;
white-space: nowrap;
}
}
<div class="button-wrapper">
<div class="button" ><button type="submit" class="submit" value="submit" name="submit_button"><span>Search</span></button></div>
<div class="button"><button onclick="location.href='https://google.com';" class="submit" value="show" name="show_button">Google</button></div>
</div>
Related
I am having two different issues. The first is really irritating. I am attempting to align the text with the image inside of the first box, so that they are side-by-side an inline fashion. I am not sure what I am doing wrong and I do not want to use floats.
Secondly, I am attempting to get the image to transform: translate on the x-axis on hover. The thing is, I want the image to transform on the .extra-box:hover...not on the actual image, but I only want the image to move. I cannot figure this out.
What am I doing wrong?
#extra {
margin: 25px auto;
width: 460px;
height: auto;
}
.extra-box {
position: relative;
width: 40%;
padding: 8px;
border-radius: 3px;
/*border: 1px solid #739BAF;*/
border: 2px solid black;
display: inline-block;
background: none;
transition: 0.5s ease;
}
.extra-box:hover {
border: 2px solid red;
transition: 0.5s ease;
}
.extra-box:first-child {
margin-left: 0;
width: 40%;
margin-right: 10%;
}
.extra-box:last-child {
width: 40%;
}
.extra-box a {
text-decoration: none;
}
.extra-box-text {
color: black;
font-size: 1em;
display: inline-block;
width: auto;
}
.extra-box-icon img {
padding-left: 5px;
width: 15px;
height: auto;
display: inline-block;
-webkit-transition: all .5 ease-in-out;
transition: all .5 ease-in-out;
/*transform: translateX(30px);
-webkit-transform: translateX(30px);*/
}
<div id="extra"><div class="extra-box">
<a href="contact">
<div class="extra-box-text">Need help?<br>Contact Us Now</div><div class="extra-box-icon"><img src="icons/right-arrow.png"></div>
</a>
</div><div class="extra-box">
<a href="register">
<div class="extra-box-text">Need an account?<br>Register Now</div>
</a>
</div>
</div>
As other answers pointed out, you were missing a display: inline-block, but the problem was in .extra-box-icon
I have also added the transform for the image: (See the beginning of the CSS)
.extra-box-icon {
display: inline-block;
}
img {
transition: transform 1s;
}
.extra-box:hover img {
transform: translateX(-100px);
}
#extra {
margin: 25px auto;
width: 460px;
height: auto;
}
.extra-box {
position: relative;
width: 40%;
padding: 8px;
border-radius: 3px;
/*border: 1px solid #739BAF;*/
border: 2px solid black;
display: inline-block;
background: none;
transition: 0.5s ease;
}
.extra-box:hover {
border: 2px solid red;
transition: 0.5s ease;
}
.extra-box:first-child {
margin-left: 0;
width: 40%;
margin-right: 10%;
}
.extra-box:last-child {
width: 40%;
}
.extra-box a {
text-decoration: none;
}
.extra-box-text {
color: black;
font-size: 1em;
display: inline-block;
width: auto;
}
.extra-box-icon img {
padding-left: 5px;
width: 15px;
height: auto;
display: inline-block;
-webkit-transition: all .5 ease-in-out;
transition: all .5 ease-in-out;
/*transform: translateX(30px);
-webkit-transform: translateX(30px);*/
}
<div id="extra">
<div class="extra-box">
<a href="contact">
<div class="extra-box-text">Need help?
<br>Contact Us Now</div>
<div class="extra-box-icon">
<img src="icons/right-arrow.png">
</div>
</a>
</div>
<div class="extra-box">
<a href="register">
<div class="extra-box-text">Need an account?
<br>Register Now</div>
</a>
</div>
</div>
Add vertical-align:top; in .extra-box class to align it to same level.
to fix the alignment issue and if you don't want to use float, you can change the display property of .extra-box-text and extra-box-text img to inline-block
.extra-box-text, .extra-box-text img{
display:inline-block;
width: /*adjust as needed*/
}
to deal with the animation, i suggest you set the image itself as a background of .extra-box, you would then change the background-position on hover
Hope this helps
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%.
I have 5 divs aligned with float left and inline-block. I want move out one div of they and centered in the middle of the page.
I try to reset the float and set the position absolute, but the div doesn't centered (still in the right at page).
If reset the position (relative) the div is centered correctly, but the others divs move to the bottom of the page (i don't understand why).
* {
margin: 0;
padding: 0;
}
body {
background-color: #00ced1;
}
/* Box */
.box {
background-color: #ff0000;
border: .250rem solid #fff;
color: white;
cursor: pointer;
display: block;
float: left;
font-family: segoe ui;
margin: 1.5% 1%;
padding: 1rem;
text-align: center;
transition: position .35s ease-in,
margin .35s ease-in,
width .35s ease-in,
transform .35s ease-in;
}
.box:hover {
background-color: #20b2aa;
}
.box:nth-child(1) {
display: block;
float: none;
position: relative;
width: calc(100/4- 2*1rem - 2*1% - 2*.250rem);
margin: 200px auto;
}
/* Width of the boxes */
.width {
width: calc(100%/4 - 2*1rem - 2*1% - 2*.250rem);
}
Here is the example at jsFiddle: http://jsfiddle.net/035j1s90/1/
Thanks.
If I am not wrong, you want to center one <div> in the middle and place the other four <div>'s directly underneath it. The large gap between the centered div and the four div's is because of the margin: 200px auto property which you've specified inside the CSS for .box:nth-child(1). Simply change it to margin: 0px auto 0px auto and your layout will be fine then, like this:
* {
margin: 0;
padding: 0;
}
body {
background-color: #00ced1;
}
/* Box */
.box {
background-color: #ff0000;
border: .250rem solid #fff;
color: white;
cursor: pointer;
display: block;
float: left;
font-family: segoe ui;
margin: 1.5% 1%;
padding: 1rem;
text-align: center;
transition: position .35s ease-in,
margin .35s ease-in,
width .35s ease-in,
transform .35s ease-in;
}
.box:hover {
background-color: #20b2aa;
}
.box:nth-child(1) {
display: block;
float: none;
position: relative;
width: calc(100/4- 2*1rem - 2*1% - 2*.250rem);
margin: 0px auto 0px auto;
}
/* Width of the boxes */
.width {
width: calc(100%/4 - 2*1rem - 2*1% - 2*.250rem);
}
<div class="box width">Translate</div>
<div class="box width">Rotate</div>
<div class="box width">Scale</div>
<div class="box width">Skew</div>
<div class="box width">Matrix</div>
UPDATED:
I've modified your HTML structure just a little bit. <div class="box width">Translate</div> which you want to be centered has been moved in the end of your HTML and I've put it inside <div id="centered"><div>.
* {
margin: 0;
padding: 0;
}
body {
background-color: #00ced1;
}
/* Box */
.box {
background-color: #ff0000;
border: .250rem solid #fff;
color: white;
cursor: pointer;
display: block;
float: left;
font-family: segoe ui;
margin: 1.5% 1%;
padding: 1rem;
text-align: center;
transition: position .35s ease-in, margin .35s ease-in, width .35s ease-in, transform .35s ease-in;
}
.box:hover {
background-color: #20b2aa;
}
#centered {
text-align: center;
}
#centered .box {
display: inline-block;
float: none;
}
/* Width of the boxes */
.width {
width: calc(100%/4 - 2*1rem - 2*1% - 2*.250rem);
}
<div class="box width">Rotate</div>
<div class="box width">Scale</div>
<div class="box width">Skew</div>
<div class="box width">Matrix</div>
<div id="centered">
<div class="box width">Translate</div>
</div>
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/