How do I align a javascript countdown box to the center of the page? The url is where the js is located is http://www.freelanceseohelp.com/offer/mobile/index.html
I tried aligning the following but I'm having problems:
.flip-clock-wrapper {
I tried adding the above to
<div class="clock"></div>
Any help fixing this will be appreciated. Thanks.
I'm pretty late to the party here but the following works for me at the time of writing (Version: 0.7.4 Beta). The idea is to wrap it in a container and override some of it's CSS.
HTML
<div class="container">
<div class="flip-clock"></div>
<!-- Flip clock is attached the this element, not .container -->
</div>
CSS
.container {
text-align: center;
}
.flip-clock {
display: inline-block;
width: auto;
}
By default .flip-clock has width: 100% and display: block. By setting width: auto and display: inline-block the item no longer spans the width of it's parent and is able to be centered by adding text-align: center to it's container.
.flip-clock-wrapper {
/* text-align: center; */
/* position: relative; */
/* width: 100%; */
margin: 0 auto;
max-width: 460px;
margin-top: 2em;
}
Remove the Commented CSS in the above CSS Class and add the rest 3 lines in your CSS class to get your Page like Below
through testing, i determined the width of the widget is 460px when considering margin. here is the css to make it happen.
.flip-clock-wrapper{
max-width: 460px;
margin: 3em auto 2em;
}
I also added top margin of 3em and bottom margin of 2em to align it correctly. that page has a ton of problems with the CSS.
Add new div element with specify display: inline-block; to archive this.
<div class="clock flip-clock-wrapper">
<div class="middle"> // Add div element (opening)
....
....
....
</div> // Add div element (closing)
</div>
CSS
.middle{
display:inline-block;
*display:inline;/* IE*/
*zoom:1;/* IE*/
}
Screenshot
Another way:
.flip-clock-wrapper {
display: table; //added
}
.flip-clock-divider {
float: left; // remove
}
.flip-clock-wrapper ul {
float: left; // remove
display: inline-block; // added
}
Add this is your css-
.flip-clock-wrapper{
width:460px;
margin:1em auto;
}
I hope I'll helps you.
Just decrease the width of your clock wrapper & set margin value.
.flip-clock-wrapper{
width: 480px;
margin: 30px auto;
}
Try like this
.flip-clock-wrapper{
max-width:480px;
margin:3em auto;
}
Using the .js file from a CDN I did not wanted to edit it.
I applied those lines to my website main CSS:
.flip-clock-wrapper {
width: inherit !important;
}
.flip-clock {
display: inline-block;
width: auto;
}
<table align="center">
<tr>
<td></td>
<td>
<div class="clock" style="margin:Inherited;"></div>
<div class="message"></div>
<script type="text/javascript">
code!!!
</script>
</td>
<td></td>
</tr>
</table>
Make the .flip-clock-wrapper contain width:auto and display:inline-block. That is:
.flip-clock-wrapper {
text-align: center !important;
position: relative;
width: auto;
margin: 1em;
display: inline-block;
}
Related
Is the property text-align: center; a good way to center an image using CSS?
img {
text-align: center;
}
That will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. See the W3C specification.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
<div style="border: 1px solid black;">
<img class="center" src ="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
I came across this post, and it worked for me:
img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
<div style="border: 1px solid black; position:relative; min-height: 200px">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
(Vertical and horizontal alignment)
Not recommendad:
Another way of doing it would be centering an enclosing paragraph:
<p style="text-align:center"><img src="https://via.placeholder.com/300"></p>
Update:
My answer above is correct if you want to start learning HTML/CSS, but it doesn't follow best practices
Actually, the only problem with your code is that the text-align attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span tag around the image and set its style to text-align: center, as so:
span.centerImage {
text-align: center;
}
<span class="centerImage"><img src="http://placehold.it/60/60" /></span>
The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span (or div).
You can do:
<center><img src="..." /></center>
There are three methods for centering an element that I can suggest:
Using the text-align property
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
Using the margin property
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
Using the position property
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
The first and second methods only work if the parent is at least as wide as the image. When the image is wider than its parent, the image will not stay centered!!!
But:
The third method is a good way for that!
Here's an example:
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>
On the container holding image you can use a CSS 3 Flexbox to perfectly center the image inside, both vertically and horizontally.
Let's assume you have <div class="container"> as the image holder:
Then as CSS you have to use:
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
And this will make all your content inside this div perfectly centered.
Only if you need to support ancient versions of Internet Explorer.
The modern approach is to do margin: 0 auto in your CSS.
Example here: http://jsfiddle.net/bKRMY/
HTML:
<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>
CSS:
p.pic {
width: 48px;
margin: 0 auto;
}
The only issue here is that the width of the paragraph must be the same as the width of the image. If you don't put a width on the paragraph, it will not work, because it will assume 100% and your image will be aligned left, unless of course you use text-align:center.
Try out the fiddle and experiment with it if you like.
img{
display: block;
margin-right: auto;
margin-left: auto;
}
If you are using a class with an image then the following will do
class {
display: block;
margin: 0 auto;
}
If it is only an image in a specific class that you want to center align then following will do:
class img {
display: block;
margin: 0 auto;
}
The simplest solution I found was to add this to my img-element:
style="display:block;margin:auto;"
It seems I don't need to add "0" before the "auto" as suggested by others. Maybe that is the proper way, but it works well enough for my purposes without the "0" as well. At least on latest Firefox, Chrome, and Edge.
Simply change parent align :)
Try this one on parent properties:
text-align:center
You can use text-align: center on the parent and change the img to display: inline-block → it therefore behaves like a text-element and is will be centered if the parent has a width!
img {
display: inline-block
}
To center a non background image depends on whether you want to display the image as an inline (default behavior) or a block element.
Case of inline
If you want to keep the default behavior of the image's display CSS property, you will need to wrap your image inside another block element to which you must set text-align: center;
Case of block
If you want to consider the image as a block element of its own, then text-align property does not make a sens, and you should do this instead:
IMG.display {
display: block;
margin-left: auto;
margin-right: auto;
}
The answer to your question:
Is the property text-align: center; a good way to center an image
using CSS?
Yes and no.
Yes, if the image is the only element inside its wrapper.
No, in case you have other elements inside the image's wrapper because all the children elements which are siblings of the image will inherit the text-align property: and may be you would not like this side effect.
References
List of inline elements
Centering things
.img-container {
display: flex;
}
img {
margin: auto;
}
this will make the image center in both vertically and horizontally
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
If you want to set the image as the background, I've got a solution:
.image {
background-image: url(yourimage.jpg);
background-position: center;
}
One more way to scale - display it:
img {
width: 60%; /* Or required size of image. */
margin-left: 20% /* Or scale it to move image. */
margin-right: 20% /* It doesn't matters much if using left and width */
}
Use this to your img CSS:
img {
margin-right: auto;
margin-left: auto;
}
Use Grids To Stack images. It is very easy here is the code
.grid {
display:grid;
}
.grid img {
display:block;
margin:0 auto;
}
If your img element is inside a div, which is itself inside another div whose display has been set as flexbox, as in my case here:
(HTML)
<nav class="header">
<div class="image">
<img
src=troll
alt="trollface"
></img>
</div>
<div class="title">
Meme Generator
</div>
<div class="subtitle">
React Course - Project 3
</div>
</nav>
(CSS)
.header{
display: flex;
}
.image{
width: 5%;
height: 100%;
}
.image > img{
width: 100%;
}
You could set your .image div to align itself vertically by doing this:
.image{
width: 5%;
height: 100%;
align-self: center;
}
display: block with margin: 0 didn't work for me, neither wrapping with a text-align: center element.
This is my solution:
img.center {
position: absolute;
transform: translateX(-50%);
left: 50%;
}
translateX is supported by most browsers
I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen:
https://codepen.io/artforlife/pen/MoBzrL?editors=1100
Sometimes we directly add the content and images on the WordPress administrator inside the pages. When we insert the images inside the content and want to align that center. Code is displayed as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
In that case you can add CSS content like this:
article p img{
margin: 0 auto;
display: block;
text-align: center;
float: none;
}
Use:
<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>
I think this is the way to center an image in the Laravel framework.
To center an image with CSS.
img{
display: block;
margin-left: auto;
margin-right: auto;
}
You can learn more here
If you want to center image to the center both vertically and horizontaly, regardless of screen size, you can try out this code
img{
display: flex;
justify-content:center;
align-items: center;
height: 100vh;
}
I need need to have each of my div's align to the edge of the main content div and stay on one line unless the dynamic content loaded is wider than a fixed width. Let's say 300px. I would like the main container to auto re-size according to content width with a margin of 20px on each side of the content. I want the content to go to automatically place it self on a new line if it exceeds the maximum width of the main container div. Here is my jsfiddle. I can't seem to get it to align correctly to the left or auto scale.
<div class='info_content'>
<div class='dealerName'><h3>{{dealerName}}</h3></div>
<div class='address'>{{address}}</div>
<div class='addressCont'>{{city}}, {{state}} {{zip}}</div>
<div class='telephone'><label for='phone'>Phone:</label>{{phone}}</div>
<div class='tags'><label for='Tags'>Tags:</label>{{tags}}</div>
<div class='dealerWebsite'><a href='{{href}}'>{{href}}</a></div>
</div>
CSS:
#DealerInfoContainer {
float: left;
margin-left: 50px;
max-width:400px;
height: auto;
border: 1px solid #000000;
}
#DealerInfo {
}
#DealerInfo p {
margin-top: -20px;
}
.telephone {
float: left;
}
.address {
float: left;
}
.addressCont {
float: left;
}
.tags {
float: left;
}
.dealerWebsite {
float: left;
}
.dealerName {
float: left;
}
.info_content {
width: 300px;
}
A different way, is to use display: inline-block on each div you want to float. like this this could be usefull if your content (as a can see) is variable
.inline{
display: inline-block;
}
If you want to use float:left, don't forget the clearfix at the end.
EDIT :
To make you code work, you must remove margin for the H3 and set a line-heigth with a vertical-align : jsfiddle
.clearfix{
clear:both;
}
.dealerName h3{
margin:0;
}
.align{
line-height :30px;
vertical-align: baseline;
}
For me the cleanest method is with display: inline-block.
Use this CSS:
div {float:left;display:-moz-inline-stack;display:inline-block;zoom:1;*display:inline;}
It's compatible with all major browsers and does everything you want.
In the following style from the website: http://6.470.scripts.mit.edu/css_exercises/exercise4.html
<style type="text/css">
#sponsors {
margin:auto;
margin-top:50px;
overflow: hidden;
width: auto;
display: inline-block;
}
div.image img {
margin: 3px;
border: 1px solid #ffffff;
}
div.image a:hover img {
border: 1px solid;
}
</style>
</head>
<body>
<h1>Sponsors of 6.470</h1>
<div id="sponsors">
<div class="image"><img src="images/appian.png" width="150" height="85"></div>
<div class="image"><img src="images/dropbox.png" width="150px" height="85px"></div>
<div class="image"><img src="images/facebook.png" width="150px" height="85px"></div>
<div class="image"><img src="images/nextjump.png" width="150px" height="85px"></div>
<div class="image"><img src="images/palantir.png" width="150px" height="85px"></div>
<div class="image"><img src="images/quora.png" width="150px" height="85px"></div>
<div class="image"><img src="images/tripadvisor.png" width="150px" height="85px"></div>
<div class="image"><img src="images/vecna.png" width="150px" height="85px"></div>
</div>
</body>
if the width: auto is removed from #sponsors then the div#sponsors is not center aligned even though margin: auto is used.
Similarly if instead of text-align: center is replaced by margin: auto in body style above, then the <h1> will not be center aligned which is preposterous;
because I have used margin: auto a lot of times and it was able to center the content without any issue. So hence help me and I will appreciate this a lot.
PS: I used firefox and besides use the doctype tag it is still not able to center with margin: auto.
Define width or margin on your #sponsors ID
as like this
#sponsors{
margin:0 auto; // left margin is auto, right margin is auto , top and bottom margin is 0 set
width:1000px; // define your width according to your design
}
More about margin auto
No need of using margin: 0 auto. Try the below code, it will work:
div#sponsors{
/* other css properties */
/* remove display:inline-block and margin: auto */
width:100%; /* instead of width: auto */
text-align: center;
}
div.img{
/*remove float:left */
/* other css properties */
display: inline-block;
}
Remove text-align: center from body tag and give to h1 tag instead.
For centering DIV you need to set css for below.
Example
#sponsors {
margin:0px auto;
}
Comment
You also need to set width for div.
DEMO
You must specify width to div and don't give margin twice
#sponsors {
margin:50px auto 0 auto;
margin-top:50px;
overflow: hidden;
width:160px;
background:aqua
}
DEMO
TO use margin:auto you should use position:relative, oh, and define a width
Imagine you as a browser, how do you center a "box" (like div) if you don't know what is the width of that? ;)
I hope to help you
correcting: as Christopher Marshall said you don't need position:relative but specify width.
If any div u want in center for margin auto always this div width is fix ......
#sponsors {
width:XXpx;
margin:50px auto 0;
overflow: hidden;
display: inline-block;
}
div{
position: relative;
border: 1px solid #ddd;
width:150px;
height:50px;
margin: auto;
/*position: absolute; top: 0; left: 0; bottom: 0; right: 0;*/
}
img.displayed {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
<html>
<div >
<a>
<img class="displayed" src="smiley.gif" >
</a>
</div>
</html>
demo added in jsfiddle
Take a look, maybe you have there a float property. In my case, setting float to none helps. Now div is properly aligned.
This worked for me!
.classofdiv{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
margin: auto doesn't put the element in center if width of element is 100%. So give some width to element and use margin: auto. It worked for me.
I have a regular layout that looks that this:
This layout is done using CSS floats.
When I switch to mobile, I want my layout to do this:
That is, I want my sidebar to be below the content. I can do this using absolute positioning, but I was wondering, is there a way to do this using floats so that if my content changes the sidebar will adjust for the height difference?
Here's how I would do it. The DIVs are floated on your desktop version, but displayed on top of eachother (default block display) on mobile.
CSS:
#sidebar {
float: left;
width: 30%;
}
#content {
float: right;
width: 70%;
}
.mobile #sidebar,
.mobile #content {
float: none;
display: block;
margin-bottom: 15px;
}
Standard HTML:
<body>
<div id="content">
...
</div>
<div id="sidebar">
...
</div>
</body>
Mobile HTML:
<body class="mobile">
<div id="content">
...
</div>
<div id="sidebar">
...
</div>
</body>
Media query, flex container and its order property should do the trick:
#media(max-width:767px) {
.container {
display: flex;
flex-wrap: wrap;
}
.content {
order: 1;
}
.sidebar {
order: 2;
}
}
Make sure to replace max-width value with your own mobile breakpoint.
Browser support for flex is also pretty decent now.
Assuming:
The two elements have a shared parent element
The content div appears BEFORE the sidebar in the source
You don't have to change the source order, you can achieve this with floats by default.
That is, in your desktop layout:
#content {
float: right;
width: 60%;
}
#sidebar {
float: left;
width: 40%;
}
Then, for mobile (using media queries or whatever other mechanism):
#content, #sidebar {
float: none;
clear: both;
}
Inside your mobile media queries set float:none.
Actually, I wanted to set layout like first layout so I had used:
.iconHome{
float: left;
border: 1px solid #73AD21;
width: 50%;
height: 100px;
background-color: aqua;
/*margin: 50px;*/
}
<div class="iconHome1">
</div>
<div class="iconHome1">
</div>
The result is the second layout!!!There fore, I think default "float:left" is not be set on mobile. You can use above way. Hope help you
Edit:
I tried some codes:
.iconHome1{
float: left;
border: 1px solid #73AD21;
width: 50%;/*185px*/
height: 200px;
background-color: aqua;
margin: 0;/*0 0 0 -7px*/
/*clear: left;*/
}
That means "width" & "margin" will effect to layout,although you have to set "float:left". Fix "width:49%", result:
i want a row with 2 cells, the row and the 2 cellsmust be in percent.
i had tried to do it like this:
#container
{
width: 100%;
display:inline-table;
}
#sidebar1
{
float: left;
width: 30%;
}
#mainContent
{
float: left;
width: 70%;
}
<div id="container">
<div id="sidebar1">
<telerik:RadFileExplorer ID="RadFileExplorer1" runat="server" EnableCreateNewFolder="False"
EnableOpenFile="False" Skin="WebBlue" Width="" ExplorerMode="FileTree" TreePaneWidth="">
<Configuration SearchPatterns="*.*" ViewPaths="/" />
</telerik:RadFileExplorer>
</div>
<div id="mainContent">
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" Skin="WebBlue" />
</div>
</div>
but it doesnt display correctl, when i resize, the right columns go down the first column.
Because of the way the CSS box model works, having 2 boxes next to each other whose combined width adds up to 100% (or the width of its container) will cause one box to be pushed down below the other in many cases.
You should make sure you have no margins, padding, or borders on your two column elements. This will be added to the width of your elements, in addition to the 70%/30% widths you have for your columns.
UPDATE: As ricebowl mentioned in the comments, many browsers have rounding errors that result in more than 100% width being rendered. This can be seen on PositionIsEverything's demo page. I've updated my answer to take this into consideration.
I've also updated my solution to include a fix for the column overflow problem you mentioned. Now, if the content is too large for the columns, a scrollbar will appear. If you don't want scrollbars, you can use overflow-x: hidden instead, however this will cut content off.
/* Reset all margin, padding, border to baseline */
#container, #sidebar1, #mainContent {
margin: 0px;
padding: 0px;
border: 0px;
}
/* Apply styles for column layout */
#container {
width: 100%;
overflow: auto
}
#sidebar1 {
width: 29%;
float: left;
overflow-x: auto
}
#mainContent {
width: 69%;
float: right;
overflow-x: auto
}
A live demo of this can be seen here: http://jsbin.com/eyise
I just tested this out in Firefox 3.5, IE7, IE8, & Google Chrome 3. It works identically in all three browsers. I would avoid using display: inline-table;. In my experience, I've never had very much luck using it consistently across all browsers.
If you need to add a border, do so using faux columns on the container element. And since you're obviously doing a liquid layout, you may also want to read this article on liquid faux columns.
<style type="text/css">
#main {
margin: auto;padding: 0px;width: 100%;}
#mainleft {
float: left; width: 100%; clear: none; background-color: #993300;
}
#mainright {
float: right;
width: 30%;
clear: none;
position: relative;
background-color: #0033FF;
}
</style>
</head>
<body>
<div id="main"><div id="mainleft"><div id="mainright"></div>
</div>
</div>
</body>
Make the mainContent 69%
And remove the container display.
I have just copied and paste this code from a project I am working on, should help
#main {
margin: auto;
padding: 0px;
width: 100%;
}
#mainleft {
float: left; width: 70%; clear: none;
}
#mainright {
float: right; width: 30%; clear: none; position: relative;
}
Just to extend the possibilities for others who get here: use display: table-cell;, works pretty well and has no rounding errors. Example:
HTML
<div class="table">
<div class="cell">textytext</div>
<div class="cell">textytext</div>
</div>
CSS
.table {
display: table;
width: 100%;
}
.table .cell {
display: table-cell;
text-align: left;
vertical-align: top;
}
jsfiddle
To add some spacing you can apply left-/right-padding to the cell, it won't interfere with the layout, plus you can easily make it a 3-column text by adding a cell. This only works for IE8, since the thread is about IE8 and 7, but I think it's a really nice solution.