Centering modal in twitter-bootstrap - css

I can't center my modal in twitter-bootstrap with various sizes. You can see live example here and here. (just click on the picture or "Report this image"). You'll see that modal is there working like charm, but it isn't horizontally centered. I tried everything: margins, float, text-align and even <center>
.modal:
.modal {
position: fixed;
top: 10%;
z-index: 1050;
width: auto;
background-color: #ffffff;
border: 1px solid #999;
border: 1px solid rgba(0, 0, 0, 0.3);
*border: 1px solid #999;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
outline: none;
-webkit-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-webkit-background-clip: padding-box;
-moz-background-clip: padding-box;
background-clip: padding-box;
}
.modal-body:
.modal-body {
margin: 0 auto 0 auto;
position: relative;
padding: 15px;
overflow-y: auto;
}

I know it's a little late, but I found the solution to all the problem with centering the Bootstrap Modal with different heights than the standard's (one).
$("#yourModal").modal('show').css({
'margin-top': function () { //vertical centering
return -($(this).height() / 2);
},
'margin-left': function () { //Horizontal centering
return -($(this).width() / 2);
}
});

A solution that works regardless of the child element size (in this case the modal). Can also be used to center vertically.
.centered {
left: 50%;
transform: translateX(-50%);
}
Essentially what we are doing here is pushing the element right by half of the parent container's width. In the case of the modal the parent would (should) be the body. The transform property is the pulling the element left by half of its own width.
Edit: To center vertically
.centered {
top: 50%;
transform: translateY(-50%);
}
Note: I think the horizontal centering only works if the height/width of the parent are the same, as the % comes from the parent width. If they are not the same then just use the parent height.

It's not the modal body that needs centering, it's the overall modal. Since that has fixed positioning, you could do it with CSS and jQuery (since jQuery is already being used):
CSS:
.modal { left: 50%; }
jQuery:
$('.modal').each(function(){
var modalWidth = $(this).width(),
modalMargin = '-' + (modalWidth/2) + 'px!important';
$(this).css('margin-left',modalMargin);
});
Alternatively it is possible with just CSS:
.modal {
width: 100%;
left: 0;
background-color: transparent; }
.modal-body {
display: inline-block;
background-color: #FFF; }
.modal img { min-width: none!important; }

Alternative to #Coop's answer. As you have a fixed width text area there, you can set the width of the modal and use negative margins rather than jquery.
.modal {
left:50%;
width:444px;
margin-left:-222px;
}
In your current code, there is nothing that will allow the modal to center.

You can use jquery to reproduce this behaivor:
left: 50%;
width: 560px;
margin-left: -280px;
Calculating the width of the div and asign css
$(document).ready(function () {
var modalWidth = $('#myModal').width();
$('#myModal').css("left", "50%");
$('#myModal').css("width", modalWidth);
$('#myModal').css("margin", (modalWidth/2)*-1);
});

.modal-dialog
{
padding-top: 15%;
}

Related

Anti aliasing border radius Issue when using rgba values

I'm having an issue where when I use border radius in combination of rgba valued colors like let's say rgba(255,255,255,.8) and then use a box-shadow to somewhat make the box appear feathered I get the issue that the corners are not solid as can be seen in this image.
Detail of the top left corner:
As can be seen, the edges when using border radius in combination with the other CSS element it makes a weird transparent edge when border-radius is set in place.
I've tried quite a bit but without much success, here's a code attempt as I wanted to attempt this for another project but just simply replicated it here: https://jsfiddle.net/01u7gbxa/1/
The code itself can be applied on any object so it seems which resolves to the same results:
background:rgba(0,0,0,.8);
box-shadow:0 0 15px 30px rgba(0,0,0,.8);
border-radius:60px;
Does anyone know if this is possible to fix at all?
Thanks in advance for further information.
You can do the same using blur filter. Apply it to a pseudo element to not affect any potential content
body {
background: #f00;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
padding: 0;
margin: 0;
}
.box {
width: 500px;
height: 200px;
border-radius: 60px;
position: relative;
}
.box:before {
content: "";
position: absolute;
top: -10px;
left: -10px;
right: -10px;
bottom: -10px;
background: rgba(0, 0, 0, .8);
border-radius: inherit;
filter: blur(10px);
}
<div class="box"></div>
Change these :
background:rgba(0,0,0,.8);
box-shadow:0 0 15px 30px rgba(0,0,0,.8);
to these:
background-color: #000;
box-shadow:0 0 15px 30px #000;
opacity : 0.8;

centering absolute text over image with darkened background

Ok so I need to position text overtop of a slider and have a darkend background behind the text so that it's readable. Simply using text-align: center doesn't work in this instance as the darkened background takes up the entire page width
http://codepen.io/SeanPeterson/pen/WoERXb?editors=1100
.wrapper h4{
position: absolute;
top: 50%; /* Would love to get rid of this! */
color: red;
margin: auto;
width: 100%;
background: rgba(0, 0, 0, 0.5);
padding: 1rem;
}
.wrapper{
position: relative;
width: 100%;
text-align: center;
}
So the only solution to this that I could think of was to position it using the left and right css properties. So in this case I put left and right to 40% each.
http://codepen.io/SeanPeterson/pen/WoERXb?editors=1100
.wrapper h4{
position: absolute;
top: 50%;
color: red;
margin: auto;
left: 40%;
right: 40%;
background: rgba(0, 0, 0, 0.5);
padding: 1rem;
}
The problem though is that this has to be a dynamic page and work for all of the different screen sizes. Using this method I would have to code in the values for all the screen sizes I can come up with ... just seems like it's not an eloquent solution to me. Plus I'm also running into the issue now where the absolute text overlaps other elements if the user increases page zoom.
I'm still a noob so I'm hoping there's a better way to do this?
Thanks Guys!
Add the text inside your h4 in a span:
<h4><span class=text>SOME TEXT</span></h4>
And then:
.text{
background: rgba(0, 0, 0, 0.5);
padding: 1rem;
}
And you can remove the padding from h4 so h4 would be:
.wrapper h4{
position: absolute;
top: 50%; /* Would love to get rid of this! */
color: red;
margin: auto;
width: 100%;
background: rgba(0, 0, 0, 0.5);
}
Here is a fixed version of your pen

putting a inset box shadow on an image or image within a div

I have an image on my page which i want to put an inset box shadow on.
I have tried doing this with the image both in, and out, of a div.
Can anyone help me to get an inset box shadow to display?
HTML:
<body>
<div id="logo">
<img src="images/key.jpg" width="3%" height="3%"/>
</div>
Next
Back
<img src="images/scene1.jpg" width="650" height="650" class="backing"/>
</body>
</html>
CSS
.backing {
position:relative;
z-index:-10;
float:left;
margin-left:12%;
box-shadow: 0 0 -50px -50px #FFF;
-moz-box-shadow: 0 0 -50px -50px #FFF;
-webkit-box-shadow: 0 0 -50px -50px #FFF;
}
.next {
position:relative;
margin-left:8%;
z-index:200;
}
.back {
position:relative;
margin-left:2%;
z-index:220;
}
Box-shadow inset will not work on image, you need to create a div and give box-shadow to that div and put image inside that div.
You can also use a negative z-index on the img element, and use the box-shadow with inset value on the div element.
div {
position: relative; /* Not required now */
margin: 10px;
float: left;
box-shadow: inset 0 0 12px blue;
border-radius: 50%;
}
div img {
display: block;
height: 100px;
width: 100px;
border-radius: 50%;
position: relative;
z-index: -1;
}
Demo
Most of the solutions posted here have problems with the parent elements, a simple solution to this, is using pseudo elements:
.box-shadow
{
background-color: #fff;
height: 235px;
margin: 32px 24px;
text-align: center;
width: 500px;
position: relative;
border-radius: 50%;
overflow: hidden;
}
.box-shadow::after
{
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
-webkit-box-shadow: inset 0 0 10px 10px #000;
-moz-box-shadow: inset 0 0 10px 10px #000;
box-shadow: inset 0 0 10px 10px #000;
border-radius: 50%;
overflow: hidden;
}
<div class="box-shadow">
<img src="http://www.google.com/logos/2012/addams11-hp.jpg" />
</div>
The other answers that propose z-index have an issue if put into context, in my case the image disappeared behind the main div. Preventing this involves setting z-index: 1; (and non static position) to all of the ancestor elements, which is problematic, and may break a lot of existing layout.
I found a clean solution that doesn't require having to touch all ancestor elements.
I finally figured it out with the help of Understanding z-index - The Stacking Context
HTML
The markup stays like this:
<div class="box-shadow">
<img src="/images/graphic.jpg" />
</div>
The challenge is to put the wrapper div and the image into a single stacking context. For this you have to apply styles to the parent element.
Stacking Context CSS
According to the linked article, the following elements create a stacking context:
the root element (HTML),
positioned (absolutely or relatively) with a z-index value other than "auto",
a flex item with a z-index value other than "auto",
elements with an opacity value less than 1. (See the specification for opacity),
elements with a transform value other than "none",
elements with a mix-blend-mode value other than "normal",
elements with isolation set to "isolate",
on mobile WebKit and Chrome 22+, position: fixed always creates a new stacking context, even when z-index is "auto"
specifing any attribute above in will-change even you don't write themselves directly
If we focus on the options that make sense for this use case, we have these alternatives, assuming the parent element of the .box-shadow element is #parent:
1. Positioning and z-index:
This is what I would choose if possible:
#parent {
position: relative;
z-index: 0;
}
2. Opacity
If the parent element needs to have a different position attribute or adding z-index has unwanted side effects, you can use an opacity value that's almost 1, so that it has no visible effect but still creates a stacking context:
#parent {
opacity: 0.999;
}
Finally, the shadow CSS
Then you can apply the shadow on the div and move the img behind it with z-index:
.box-shadow {
box-shadow: 0 0 10px 6px white inset;
}
.box-shadow img {
display: block;
position: relative;
z-index: -1;
}
Here’s a clean, simple and modern approach of CSS pseudo-elements to place a box shadow “on top of an image”, since img tags themselves don’t support pseudo-elements.
HTML:
<div class="box-shadow">
<img src="http://i.stack.imgur.com/8LzBY.jpg" />
</div>
CSS:
.box-shadow {
position: relative;
text-align: center;
}
.box-shadow::after {
box-shadow: inset 0 0 10px 10px #000;
bottom: 0;
content: "";
display: block;
left: 0;
height: 100%;
position: absolute;
right: 0;
top: 0;
width: 100%;
}
.box-shadow img {
max-width: 100%;
width: auto;
}
View the accompanying JSFiddle.
.backing {
position:relative;
z-index:-10;
float:left;
margin-left:12%;
box-shadow: inset 0 0 50px 50px #FFF;
-moz-box-shadow: inset 0 0 50px 50px #FFF;
-webkit-box-shadow: inset 0 0 50px 50px #FFF;
}
.next {
position:relative;
margin-left:8%;
z-index:200;
}
.back {
position:relative;
margin-left:2%;
z-index:220;
}
<div id="logo">
<img src="//picsum.photos/100" width="3%" height="3%"/>
</div>
Next
Back
<img src="//picsum.photos/650" width="650" height="650" class="backing"/>

display box shadow above the text

Let's say I have a simple div with some paragraphs inside.
I want to achieve a fading effect on the bottom of the div.
I have tried to set up a white shadow
box-shadow: inset 0 -15px 10px white;
but obviously this didn't work, since the box-shadow always appears under the text.
How can I accomplish this in pure CSS?
Thanks.
What about using the "after" css selector ? it even work with IE8 : http://caniuse.com/#search=after
.container{
position: relative;
height: 100px;
overflow: hidden;
}
.container:after{
content: "";
position: absolute; top: 0; bottom: 0; left: -15px; right: -15px;
box-shadow: inset white 0 -15px 10px;
}
I'm using negative left and right to put the inside box outside the main box to avoid the shadow on the sides.
I was able to replicate the effect with CSS only, but it's a little hackish:
http://jsfiddle.net/EaXTB/1/
I put two divs as siblings:
<div class="container">
<div class="shadow"></div>
<div class="copy">
<p>Lorem ipsum dolor sit amet, ...</p>
</div>
</div>
And positioned them absolutely:
.container { position:relative; }
.shadow {
-webkit-box-shadow: inset 0px -15px 10px 0px rgba(255, 255, 255, 1);
-moz-box-shadow: inset 0px -15px 10px 0px rgba(255, 255, 255, 1);
box-shadow: inset 0px -15px 10px 0px rgba(255, 255, 255, 1);
height: 200px;
width: 300px;
z-index: 100;
position:absolute;
}
.copy {
max-height: 200px;
width: 300px;
overflow: hidden;
z-index: 0;
position: absolute;
}
The only trouble is that for this to work, you must specify the height for the .shadow class. If it will always be the same size, then you can use this. Otherwise using a png overlay might be more effective.
Something like:
.shadow {
background: url(overlay.png) repeat-x bottom;
}
div {
height:300px;
color:red;
overflow-y: scroll;
}
div:after {
content: "";
box-shadow: 0px -50px 50px -50px #000 inset;
position:fixed;
top: 250px;
left:0;
width:100%;
height:50px;
overflow: hidden;
}
create a div below all your other divs and set a negative bottom margin to that div and a top drop shadow. You can do that or you can do what I have done. Save a png image with that fade and set it to an absolutely positioned div that sits at the bottom of your relatively positioned div that contains the text.

CSS Border Overlapped By Child Element

See here:
http://jsfiddle.net/cnJ6q/
I can't just add the border to the child element, it needs to be added to the parent (.dc-slick) - Is there any way to fix this? Z-index does not seem to help.
Thanks.
The child div will inherit its parent's z-index no matter what you use.
The problem here is mismatching border-radiuses. Use the same border radius on each element, otherwise you will get this overlap.
border-bottom-left-radius: 30px 30px;
border-bottom-right-radius: 30px 30px;
Updated example.
There are 2 ways of doing it. 1) Child element takes the same size as not-transformed parent element. Then background you should cast on parent element
.dc-slick {
border: 3px solid red;
right: 0px;
left: 0px;
position: fixed;
border-bottom-left-radius: 30px 30px;
border-bottom-right-radius: 30px 30px;
z-index: 10001;
margin-top: 0px;
background: black;
}
.dc-slick-content {
color:white;
z-index:9999;
width: 100%;
height: 200px;
border-bottom-left-radius: 15px 15px;
border-bottom-right-radius: 15px 15px;
}
2) You should scale child approximately in same way as parent.
.dc-slick {
border: 3px solid red;
right: 0px;
left: 0px;
position: fixed;
border-bottom-left-radius: 30px 30px;
border-bottom-right-radius: 30px 30px;
z-index: 10001;
margin-top: 0px;
}
.dc-slick-content {
background: black;
color:white;
z-index:9999;
width: 100%;
height: 200px;
border-bottom-left-radius: 28px 28px;
border-bottom-right-radius: 28px 28px;
}
You should have the background color and the border on the same element, so move the background: black to .dc-slick
.dc-slick {
background: black;
}
.dc-slick-content {
/*background: black;*/
/*border-bottom-left-radius: 15px 15px;*/
/*border-bottom-right-radius: 15px 15px;*/
}
Updated JSFidle: http://jsfiddle.net/RxyRV/
Well, it is actually possible.
To bring the child element behind the parent border,
make the child relatively (position: relative) (or absolutely (position: absolute) , if this is already used) positioned and give a negative z-index.
This also works with border-images, where it is very useful if you want to achieve a partially obscuring border image effect:
.container,
.inner {
width: 200px;
height: 200px;
}
.container {
/* from #https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Backgrounds_and_Borders/Border-image_generator */
border-image: url("https://mdn.github.io/css-examples/tools/border-image-generator/border-image-1.png") 27 / 20px / 0px stretch;
border-style: solid;
}
.inner {
background: rgba(0, 0, 0, 0.75);
}
.inner-2 {
position: relative;
z-index: -1;
}
<div class="container">
<div class="inner inner-1">
Test 1, child element above parent border.
</div>
</div>
<div class="container">
<div class="inner inner-2">
Test 2, child element behind parent border.
</div>
</div>
The downside to this technique is that the child element with negative z-index doesn't get JavaScript mouse/touch events. This is something I have to find out as I use this technique for a slider where these events are used.

Resources