3

I have the following code in my button's _postRender function:

myapp.Splash.btnSplashOK_postRender = function (element, contentItem) {
    $(element).find("ui-btn-inner").css({
        "padding-left": "0"
    });
    $(element).css({
        "padding-left": "0",
        "margin-left": "50%",
        "margin-right": "50%"
    });
};

And the result is this:

Rendered page

The padding-left, margin-left, and margin-right are being applied to the div just fine. But the find("ui-btn-inner") doesn't seem to be working at all. And the margin tags don't seem to be taking into account the width of the button in order to get it to the center of the page. What am I missing here?


Note: The accepted answer works perfectly well for this simple case. Though @rockmandew's Third Update provides a more powerful alternative for those wishing to do more complex placements.

embedded.kyle
  • 10,976
  • 5
  • 37
  • 56

5 Answers5

4

To align button add "transform":"translateX(-50%)"

$(element).css({
    "padding-left": "0",
    "margin-left": "50%",
    "transform":"translateX(-50%)"
});
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • I had seen the `text-align` advice elsewhere on here. That didn't work at all and the button remained all the way to the left. Your `transform` solution worked perfectly though. Need to read up a bit on that. – embedded.kyle Sep 25 '15 at 18:57
  • Any idea as to why the `find()` isn't working? It's only 11px and visually it looks centered enough. But for my own edification (and possible future use) I would like know why that's not working. – embedded.kyle Sep 25 '15 at 19:21
1

To answer to why it isn't working: you forgot the class selector ..

myapp.Splash.btnSplashOK_postRender = function (element, contentItem) {
    $(element).find(".ui-btn-inner").css({
        "padding-left": "0"
    });
    $(element).css({
        "padding-left": "0",
        "margin-left": "50%",
        "margin-right": "50%"
    });
};

If you want you can even chain these functions. (One of the reasons why I love jQuery so much)

myapp.Splash.btnSplashOK_postRender = function (element, contentItem) {
    $(element).css({
        "padding-left": "0",
        "margin-left": "50%",
        "margin-right": "50%"
    }).find(".ui-btn-inner").css({
        "padding-left": "0"
    });
};
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • The chaining is interesting and something I didn't know as I'm just learning jQuery. But unfortunately, using the class selector did not work. The `padding-left` on `ui-btn-inner` remained at 11px. – embedded.kyle Sep 25 '15 at 19:01
  • If you can figure out the _why_ I would be very interested to know. – embedded.kyle Sep 25 '15 at 19:21
  • @embedded.kyle - I assume its because the styles are being set since he said this was autogenerated code - I assume (and this is not best practice) that adding !important tags to the styles would make it work. **Edit**: sorry I thought I was responding to the person who posted this answer, not the OP :p – rockmandew Sep 25 '15 at 19:52
1

I would approach it the same as Lucas - Try this fiddle: http://jsfiddle.net/rockmandew/ddow1nd9/

Basically, I took the button and set its styles to the following:

.test {
  position: absolute;
  left: 50%;
  -moz-transform: translate(-50%, 0);
  -ms-transform: translate(-50%, 0);
  -webkit-transform: translate(-50%, 0);
  transform: translate(-50%, 0);
  height:25px;
}

However, this won't work alone - you need to wrap the button in a container (div) and set its position to relative. Which you can see in the following styles:

.button-contain {
  position:relative;
  height:25px;
}

The only issue with this solution is the fact that you have to set an explicit height on your button and set the same height on your button-container, this will allow the other content to respond to the height of the button. When you set the button Absolute, it removes it from the normal flow, thus if you don't set a height on the containing div, the div will have a height of 0 - hopefully, that makes sense to you. If you need further clarification, just ask!

First Update:

Okay, take a look at: https://jsfiddle.net/rockmandew/fhceb7dv/1/ (you will need to replace 'test' with your "$(element)" selector.)

I have adjusted the code for Jquery implementation - since the code is autogenerated, I have included a line of code which will wrap your target element with the "button-contain" I mentioned earlier. I then chained the CSS properties on top of that:

$('.test').wrap("<div class='button-contain'></div>").css({
  "position":"absolute",
  "left":"50%",
  /* My understanding is that the jQuery .css() will add browser prefixes automatically
  "-moz-transform":"translate(-50%, 0)",
  "-ms-transform":"translate(-50%, 0)",
  "-webkit-transform":"translate(-50%, 0)",
  */
  "transform":"translate(-50%, 0)",
  "height":"25px"
});

As you can see, the Jquery takes care of the browser prefixes. So this will wrap your $(element) in a div with the class of "button-contain", it will then set the CSS of the button according to the styles applied. Finally, you will need to add one class to your CSS (unless you want to handle the "button-contain" styles with Jquery as well). So your button-contain will have the same styles as mentioned before:

.button-contain {
  position:relative;
  height:25px;
}

Hopefully, this works for you. I'm not to sure what is going on with the padding though. Lets see where this gets you and go from there.

Second Update:

Try using a similar application of the following code:

$("#elem").css("cssText", "width: 100px !important;");

So instead of:

$(element).css({"padding-left": "0 !important"});

Try using it like so:

$('.test').wrap("<div class='button-contain'></div>").css({
  "position":"absolute",
  "left":"50%",
  /* My understanding is that the jQuery .css() will add browser prefixes automatically
  "-moz-transform":"translate(-50%, 0)",
  "-ms-transform":"translate(-50%, 0)",
  "-webkit-transform":"translate(-50%, 0)",
  */
  "transform":"translate(-50%, 0)",
  "height":"25px",
  "cssText", "padding-left:0 !important;"
});

I haven't tested this because I don't want to take the time to set up an example but I believe it will work. Give it a shot and let me know.

Third Update:

As the OP pointed out, the code in my second update has an error - the "cssText" should be followed with a ':' not a ','

Furthermore, in response to the OP additional comments, I have adjusted my solution to override the initial padding and apply all new (desired) styles. To do this, you will need to create a variable that contains all the styles you want to apply (this is because cssText will replace any existing 'style' tags):

var csstxt = $('.testTwo').css('cssText') + ';position:absolute;left:50%;transform:translate(-50%, 0);-moz-transform:translate(-50%, 0);-ms-transform:translate(-50%, 0);-webkit-transform:translate(-50%, 0);height:25px;padding-left:0 !important;';

As you can see, I had to re-include the Browser prefixes because we are no-longer handling the solution with .css() - Once you have the variable with your desired styles, you will want to run it through the following function:

$('.testTwo').css('cssText', csstxt);

This will now produce your desired results. Here is an updated Fiddle with everything I have touched on: https://jsfiddle.net/rockmandew/fhceb7dv/13/

rockmandew
  • 820
  • 13
  • 22
  • Unfortunately I'm using Lightswitch where the HTML and the majority of the CSS is auto-generated. What I'm looking for is minimal jQuery javascript code to make the necessary adjustments to the auto-code. Though I would like your opinion on the importance of the `moz`, `ms`, and `webkit` specific transforms. I did a quick test between IE11 and Chrome 45 and they looked fine to me. – embedded.kyle Sep 25 '15 at 19:28
  • You can do all of this through JS/Jquery code - give me a second and I can help you. I am bouncing around questions right now. – rockmandew Sep 25 '15 at 19:42
  • @embedded.kyle - Please let me know if this works for you, as I can not test that last update I made (i.e. the "cssText", "padding-left:0 !important;") – rockmandew Sep 25 '15 at 20:27
  • Your first update worked perfectly. For the second one, is the `,` supposed to be a `:`? I changed it to that and the button became smaller horizontally and was all the way to the left. The only thing in the `div`'s `style` attribute was `padding-left:0 !important;`. I'm not sure why you're using `cssText`. [This](http://www.w3schools.com/jsref/prop_style_csstext.asp) indicates that it is supposed to be used to _"Set the style for a

    element"_. But there are no paragraph tags. Just `

    – embedded.kyle Sep 25 '15 at 20:46
  • Okay, so you just read the description from the example, in reality, cssText does the following: "_The cssText property sets or returns the contents of a style declaration as a string._" I was using cssText because it allows you to set priority. However, looking into it, I would rewrite my answer, let me tinker my solution quick. I will post a third update. @embedded.kyle – rockmandew Sep 25 '15 at 20:50
  • @embedded.kyle - You were right about the ',' needing to be a ':' good catch. However, I was initially wrong as to what cssText did - I have refined my answer and Fiddle, I will edit my solution now. – rockmandew Sep 25 '15 at 20:58
  • That works. I like this. Though I'm going to leave @Pranav's solution as accepted since it's simpler. Lightswitch is designed for non-professional programmers so most people searching are going to want it quick and easy and not need the power of your solution. Hopefully your answer gets upvoted near the top though because this is good information for those that want to do more. – embedded.kyle Sep 25 '15 at 21:27
  • @embedded.kyle - If you aren't going to accept the chosen solution, at least leave a comment under the selected answer saying something like "For a more advanced/powerful solution, please see the solution provided by rockmandew below." That way people know there is a better answer to the question. – rockmandew Sep 25 '15 at 21:31
  • 1
    The StackExchange system is designed to push quality answers to the top. But alas, I only have one vote to give. Though you do have a fair point. I've added a note in the original question to give your answer some more visibility and help get you to the top. Cheers. – embedded.kyle Sep 25 '15 at 21:40
0

Try setting parent position to position: relative

And then set button style

left: 0;
right: 0;
position: absolute;
margin: 0 auto;
Lucas Souza
  • 371
  • 1
  • 6
0
$(element).parent().css({
    "display": "flex",
    "align-items": "center",
    "justify-content": "center"
});
4b0
  • 21,981
  • 30
  • 95
  • 142