Issue in the width of a fixed positioned div - html

How to make a fixed positioned div fit its content width?
I've made a demo here
Before clicking on the Load button, the div must only have enough width to contain the gif image and the button.
When more content is loaded, the width of the div must fit its width.
here is the CSS I've made
body{background: #eee; }
#divToCenter{
border-radius: 5px;
box-shadow: 0 0 0 10px rgba(0,0,0,0.2);
background: #fff;
padding: 20px;
position: fixed;
top: 20px;
right: 0;
left: 0;
margin-right: auto;
margin-left: auto;
max-width: 400px;
}

Since you are using JavaScript/jQuery to show/hide the loader, why don't you change the width of the box via JavaScript?
You could simply toggle a class called .wide for instance, to achieve the desired result as follows:
EXAMPLE HERE
CSS:
#divToCenter{
/* other declarations... */
text-align: center;
width: 50px;
}
#divToCenter.wide {
width: 400px;
text-align: left;
}
jQuery:
var $divToCenter = $("#divToCenter");
$("#show-more-data").click(function() {
// ...
$divToCenter.addClass("wide");
});
$("#show-less-data").click(function() {
// ...
$divToCenter.removeClass("wide");
});

Add display: table, position: relative and margin:0 auto to the following element:
#divToCenter{
border-radius: 5px;
box-shadow: 0 0 0 10px rgba(0,0,0,0.2);
background: #fff;
padding: 20px;
display: table;/*Add this*/
position: relative;/*Change fixed with relative*/
top: 20px;
margin:0 auto;/*Add this*/
max-width: 400px;
}
fiddle

Related

How can I set the text directly beneath an image?

I have created a container by <div> and I also pasted the pattern card and the image into the container. I am trying to put the text (under the name of class="under-name"beneath an image but the text is just sticking in the bottom without any effect.
I have been tried for several ways to move up the text but the text is just still locating in the same place.
The first thing I tried by setting with inline with moving the position by left or right. And I also tried to create the other new container with and it could not affect anything at all.
I want to move text beneath the image.
Please advise me the way to solve it.
enter image description here
.topcon {
background-color: #f6f5f5;
position: relative;
width: 250px;
height:250px;
border: 15px;
padding: 50px;
margin: 180px auto 150px auto;
border-radius: 20px;
}
.pattern-card
{
position: relative;
right: 50px;
border-radius: 20px 20px 0px 0px;
bottom: 50px;
}
.user-name{
position: relative;
width:40%;
left:0;
top:1000%
text-align: center;
margin: 0 auto 0 auto;
}
.victor{
position: relative;
background-color:#ffffff;
border:3px solid #ffffff;
border-radius: 50%;
display: inline-block;
margin-left: 50px;
margin-right: auto;
bottom: 110px;
width: 50%;
}
<div class="topcon">
<img class="pattern-card"src="images/bg-pattern-card.svg" alt="pattern card at the frame.">
<img class="victor"src="images/image-victor.jpg" alt="image for Victor">
<figcaption class="user-name">Victor Crest</figcaption>
26
London
80K
Followers
803K
Likes
1.4K
Photos
</div>
Try changing your 'user-name' class style to this
.user-name {
position: absolute;
width:40%;
left:20vh;
top: 20vh;
text-align: center;
margin: 0 auto 0 auto;
}
Add
bottom: 60px;
In .user-nameclass, adjusting the pixels until you have the position you are looking for.
It works as below. The most important is to you vh
This is my first time to use vh.
.user-name {
position: absolute;
width:40%;
left:20vh;
top: 20vh;
text-align: center;
margin: 0 auto 0 auto;
}

Aligning with Custom CSS & Gravity Form in Wordpress

I can't seem to make an element move in CSS. It's a form with a background and it's centered. I can't see what I'm doing wrong.
#skyformbox {
width: 50%;
margin: 0 auto;
margin-top: 0px;
clear: both;
border: 3px solid #000000;
padding-top: 20px;
background: #ccc url(http://www.ultraframehomeimprovements.co.uk/wp-
content/uploads/2018/07/Sky-box.png);
overflow: auto;
padding: 5;
left: 2000px;
}
<div align="left">
<div id="skyformbox">
[gravityform id="12" title="false" description="false"]
</div>
</div>
Why are you positioning 2000px left? As far as I know the "left" property will only work if the positioning is set to absolute...
Anyway try this:
#skyformbox {
width: 50%;
margin-left: 0px;
margin-top: 0px;
clear: both;
border: 3px solid #000000;
padding-top: 20px;
background: #ccc url(http://www.ultraframehomeimprovements.co.uk/wp-content/uploads/2018/07/Sky-box.png);
overflow: auto;
padding: 5;
left: 2000px;
}
Setting the margin-left to 0px did the trick for me (assuming that what you're trying to do here is to get the form to align to the left side of the page).

Height in CSS with Auto/Calc

I'm having a problem on my new website. But first I should give you some information.
I'm building a full responsive website with a portfolio. My portfolio images stands in a DIV and response to the screen size. On a screen with more then 1005px it's working perfect. Also the scaling works great. This is because the following CSS line:
#media only screen and (max-width: 1005px) {
The div with my image is newtextportfolio. The image itself doesn't use CSS except 100% width and height. When i give my div the follow definition (height: auto;) i get a white line under my images. And i don't want the white line :(
.newtextportfolio {
width: calc(95% + 10px);
height: auto;
margin-right: 25px;
}
I can make it disappear if I make the height for example 200px. But when I view my site on an iPad this makes the images stretch (because it's fixed and not responsive).
Does anyone know how i can make the white line disappear? I already tried some things with calc, percentages but this also makes the line, only fixed pixels doesn't.
.newcontainersmallleftprices {
width: 310px;
float: left;
margin-left: 14px;
}
.newtop1 {
width: 310px;
background-image: url("../images/tops/portfolio1.png");
text-align: center;
font-size: 23px;
color: #FFFFFF;
height: 50px;
-moz-box-shadow: 0 0 5px #888;
-webkit-box-shadow: 0 0 5px#888;
box-shadow: 0 0 5px #888;
margin: auto;
padding-top: 20px;
float: left;
position: relative;
z-index: 2;
}
.newtextportfolio {
width: 310px;
height: 200px;
text-align: center;
-moz-box-shadow: 0 0 5px #888;
-webkit-box-shadow: 0 0 5px #888;
box-shadow: 0 0 5px #888;
margin: auto;
background-color: #FFFFFF;
float: left;
position: relative;
z-index: 1;
font-size: 17px;
}
#media only screen and (max-width: 1005px) {
.newcontainerpricesmall {
width: 100%;
}
.newcontainersmallleftprices {
width: 95%;
margin-bottom: 25px;
margin-left: 4%;
}
.newtextportfolio {
width: calc(95% + 10px);
height: auto;
margin-right: 25px;
}
.newtop1 {
width: 95%;
padding-top: 20px;
padding-left: 5px;
padding-right: 5px;
margin-right: 25px;
}
}
<div class="newcontainer">
<div class="newcontainerpricesmall">
<div class="newcontainersmallleftprices">
<div class="newtop1">Broeckerhave</div>
<div class="newtextportfolio">
<a href="http://beta.gjwd.nl/images/portfolio/broeckerhave.png" data-lightbox="image-100" title="" class="portfolioimg"><img src="http://beta.gjwd.nl/images/portfolio/thumb/broeckerhave.png" width="100%" height="100%" /> </div>
</div>
Make the img element display:block.
https://jsfiddle.net/jmarikle/95gsk2tu/
An alternative approach is to give the image vertical-align: top;. This is being caused by the fact that images are inline elements with some block attributes. They retain line height, letter spacing, etc. Block level elements do not have those added calculations to their size, and aligning vertically collapses the attributes that cause the gap at the bottom.

Place a div in absolute position inside media page print area

I need to print a page with a fixed size, and an absolute position div inside:
.page
{
width: 21cm;
min-height: 29.7cm;
padding: 1.2cm;
margin: 1cm auto;
border: 1px #D3D3D3 solid;
border-radius: 5px;
background: white;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
position: relative;
}
Media classes to print:
#page
{
size: A4;
margin: 0;
}
#media print {
.page {
margin: 0;
border: initial;
border-radius: initial;
width: initial;
min-height: initial;
box-shadow: initial;
background: initial;
page-break-after: always;
position: relative;
}
}
The div:
<div id="proc_comp">Printed by Computer</div>
#proc_comp
{
position: absolute;
left: 190px;
bottom: 15px;
font-size: 0.65em;
}
But the div, when I try to Print in the Browser, it does not appear in the same place as the HTML layout, why?
Thanks.
The wrong positioning appears because you are using pixel-based placement inside your #proc_comp rule whereas the page is using A4 format (non-pixel based layout). You may try to use following alternative
#proc_comp {
position: absolute;
left: 50%;
top: 50px;
margin-left: -5em;
margin-top: -1em;
}
You may have to play around with the negative margins to fit the height and width of your displayed message to make it appear centered.

Center a CSS box?

HTML:
<div class="cont">
<center>
<div class="xmdiv">
<img class="xmenu" src="media/file1.png">
<img class="xmenu" src="media/file2.png">
</div>
</center>
<p>-snip-</p>
</div>
CSS
.cont {
position: relative;
margin-top: 3%;
background-color: transparent;
width: 65%;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 1px;
-moz-box-shadow: 0px 0px 11px #000000;
-webkit-box-shadow: 0px 0px 11px #000000;
box-shadow: 0px 0px 11px #000000;
font-family: 'Open Sans', sans-serif;
color: #070707;
font-size:15px;
font-weight:300;
text-align:justified;
line-height:1.5;
}
p {
margin-left: 8px;
margin-right: 8px;
}
What it looks like:
I want it to be centered, how do I do that? Tried looking it up online, and it didn't really work.
Is this what you want ? Link: http://jsfiddle.net/jtFUs/
CSS:
.cont
{
position: relative;
margin: 0 auto;
}
If you need to center a div vertically and horizontally on your page, use:
div {
width: 100px;
height: 100px;
background-color: #000000;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
http://jsfiddle.net/65adr/48/
In case you need to center it just horizontally, use:
center
{
width: 200px;
display: block;
margin-left: auto;
margin-right: auto;
}
Just define a width for the element, in this case i added 200px for example.
http://jsfiddle.net/65adr/50/
To center a div (and many other types of elements), use this CSS code:
.cont {
margin: 0 auto;
width: XXXpx;
}
JSFiddle.
Make sure to specify the width, or else the div won't be centered.
Edit
To center an element without specifying with, you could do something like this (not sure if this will work in all browsers, however):
body { text-align:center; }
.cont { display:inline-block; }
Side-note
Don't use the center tag, it is deprecated. You can read more about it here.