Placing play button in the middle

What am I doing wrong?

I was originally using absolute, but I just found it messes everything up after I placed it on blogger.
https://testpage34567.blogspot.com/

How else can I get this to work properly on blogger?

Using Absolute:

Absolute Off:
https://jsfiddle.net/7p3u3nj6/

<div style="visibility: hidden; position: absolute">
 <img style="visibility: hidden; position: absolute" src="https://i.imgur.com/DrNBAbe.png" width="266" height="250" alt="" />
 <img style="visibility: hidden; position: absolute" src="https://i.imgur.com/9JZdJz0.png" width="44" height="43" alt="" />
 <img style="visibility: hidden; position: absolute" src="https://i.imgur.com/IvGhkoU.png" width="44" height="43" alt="" />
</div>

<div id="playButton4" style="width: 266px; height: 250px; cursor: pointer;background-color: #000000 ;background-image: linear-gradient( to right,transparent 86px,#0059dd 86px, #0059dd 89px, transparent 89px, transparent  177px, #0059dd 177px, #0059dd 180px, transparent 180px ), url('https://i.imgur.com/DrNBAbe.png');box-Shadow:inset 0 0 0 3px #0059dd;background-repeat: no-repeat;" onclick=" 
var button = document.getElementById('playButton4');
var player = document.getElementById('player4');
 document.querySelector('#playButton4 .play').style.display='none';
 document.querySelector('#playButton4 .pause').style.display='none';
player.volume=1.0; if (player.paused) {


document.querySelector('#playButton4 .pause').style.display='inline-block';
player.play();
} else {
document.querySelector('#playButton4 .play').style.display='inline-block';
player.pause();
}">



<div class="play" style="width: 44px; height:43px;margin:97px 110px;background-color:rgba(0, 0, 0, 0);background-image: url('https://i.imgur.com/9JZdJz0.png');background-size: 16px 20px;background-repeat: no-repeat;background-position: 58% 50%;border-radius: 500px;border: 6px solid pink;"> </div>

<div class="pause" style="display: none;width: 44px; height:43px;margin:97px 110px;background-color:rgba(0, 0, 0, 0);background-image: url('https://i.imgur.com/IvGhkoU.png');background-repeat: no-repeat;background-size: 16px 20px;background-position: 50%;border-radius: 500px; border: 6px solid pink;"> </div>


</div>

<audio id="player4" style="display:none;">
  <source src='http://hi5.1980s.fm/;' type='audio/mpeg'></source>
</audio>

Absolute elements are removed from the flow and do not take up space in the document flow. That’s why they overlap other elements unless you have accounted for the space they occupy.!

If you want to place absolute elements in relation to a parent then that parent needs to be position:relative so that it forms a context for the absolutely placed children.

You should set your big playButtons to position:relative and then you can absolutely place those smaller buttons in the middle where you want them. The larger parent holds open the space of the document so that content underneath doesn’t overlap and then the smaller buttons are placed in the middle of the parent and have no effect on the flow of the page.

e.g.

#playButton5{position:relative!important;}
.play{position:absolute;}

The !important is because you like to use inline styles so I only reproduce here the extra rules you need whether applied inline or not. Note that although I only added position:absolute to .play it is the margins that you have applied to that element that then make it go central (you could of course use co-ordinates instead now that it is absolutely placed but either will work).

1 Like

I did this and the playbutton isn’t showing.

It only appears after I click on it.

Am I supposed to do something else?

I added this in:

<style>
#playButton4{position:relative!important;}
.play{position:absolute;}
</style>

https://testpage34567.blogspot.com/




<div style="visibility: hidden; position: absolute">
 <img style="visibility: hidden; position: absolute" src="https://i.imgur.com/DrNBAbe.png" width="266" height="250" alt="" />
 <img style="visibility: hidden; position: absolute" src="https://i.imgur.com/9JZdJz0.png" width="44" height="43" alt="" />
 <img style="visibility: hidden; position: absolute" src="https://i.imgur.com/IvGhkoU.png" width="44" height="43" alt="" />
</div>

<style>
#playButton5{position:relative!important;}
.play{position:absolute;}
</style>



<div id="playButton4" style="width: 266px; height: 250px; cursor: pointer;background-color: #000000 ;background-image: linear-gradient( to right,transparent 86px,#0059dd 86px, #0059dd 89px, transparent 89px, transparent  177px, #0059dd 177px, #0059dd 180px, transparent 180px ), url('https://i.imgur.com/DrNBAbe.png');box-Shadow:inset 0 0 0 3px #0059dd;background-repeat: no-repeat;" onclick=" 
var button = document.getElementById('playButton4');
var player = document.getElementById('player4');
 document.querySelector('#playButton4 .play').style.display='none';
 document.querySelector('#playButton4 .pause').style.display='none';
player.volume=1.0; if (player.paused) {
document.querySelector('#playButton4 .pause').style.display='inline-block';
player.play();
} else {
document.querySelector('#playButton4 .play').style.display='inline-block';
player.pause();
}">

<div class="play" style="width: 44px; height:43px;margin:97px 110px;background-color:rgba(0, 0, 0, 0);background-image: url('https://i.imgur.com/9JZdJz0.png');background-size: 16px 20px;background-repeat: no-repeat;background-position: 58% 50%;border-radius: 500px;border: 6px solid pink;"> </div>

<div class="pause" style="display: none;width: 44px; height:43px;margin:97px 110px;background-color:rgba(0, 0, 0, 0);background-image: url('https://i.imgur.com/IvGhkoU.png');background-repeat: no-repeat;background-size: 16px 20px;background-position: 50%;border-radius: 500px; border: 6px solid pink;"> </div>


</div>

<audio id="player4" style="display:none;">
  <source src='http://hi5.1980s.fm/;' type='audio/mpeg'></source>
</audio>

What do you mean by coordinates, what are you referring to?

Off Topic

@Paul_Wilkins explained to you a week ago that <source> should not have a closing tag.

See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source for more information.

1 Like

That’s because you set ,play to display:none in your css! How did you think it would display if you set it to not display?

Turn it back on with this:

#playButton4 .play{display:block}

Remember to engage your brain when you fiddle with the code :slight_smile:

We’ve been over this a number of times and co-ordinates for positioned elements are when you use top,left, right or bottom.

e.g.

.test{
position:absolute;
left:40px;
top:40px;
}

As I said above margins are working ok in this instance but are not always the best choice for absolutely positioned elements.

2 Likes

I don’t agree with the use of “should” there, for that wrongly implies that it not recommended but is still allowed,

Instead, the [<source> specs] (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source) definitively says that it must not have an end tag, in the “Tag omission” section.

The use of should and must, are very specific when it comes to the specifications.

4 Likes

And I said Blogger adds it in by default, on its own, without me adding it.

How this gets resolved, I have no idea.

Maybe someone could write to Google and let them know about this.

I removed

<style>
#playButton5{position:relative!important;}
.play{position:absolute;}
</style>

And added it inside the div. And it seems like it works perfectly fine without the inclusion of position: Relative. Do I need to add that in, or can I leave it out?

<div class="play" style="display:block;position:absolute;

https://testpage34567.blogspot.com/



<div id="playButton4" style="width: 266px; height: 250px; cursor: pointer;background-color: #000000 ;background-image: linear-gradient( to right,transparent 86px,#0059dd 86px, #0059dd 89px, transparent 89px, transparent  177px, #0059dd 177px, #0059dd 180px, transparent 180px ), url('https://i.imgur.com/DrNBAbe.png');box-Shadow:inset 0 0 0 3px #0059dd;background-repeat: no-repeat;" onclick=" 
var button = document.getElementById('playButton4');
var player = document.getElementById('player4');
 document.querySelector('#playButton4 .play').style.display='none';
 document.querySelector('#playButton4 .pause').style.display='none';
player.volume=1.0; if (player.paused) {
document.querySelector('#playButton4 .pause').style.display='inline-block';
player.play();
} else {
document.querySelector('#playButton4 .play').style.display='inline-block';
player.pause();
}">

<div class="play" style="display:block;position:absolute;width: 44px; height:43px;margin:97px 110px;background-color:rgba(0, 0, 0, 0);background-image: url('https://i.imgur.com/9JZdJz0.png');background-size: 16px 20px;background-repeat: no-repeat;background-position: 58% 50%;border-radius: 500px;border: 6px solid pink;"> </div>

<div class="pause" style="display: none;width: 44px; height:43px;margin:97px 110px;background-color:rgba(0, 0, 0, 0);background-image: url('https://i.imgur.com/IvGhkoU.png');background-repeat: no-repeat;background-size: 16px 20px;background-position: 50%;border-radius: 500px; border: 6px solid pink;"> </div>

</div>

I was told to only use codes that need to be added. So, I assume if it works fine without position: relative, then I don’t need to add it. Unless I’m wrong in that thinking.

Relative is used together with absolute.

Anything that is positioned absolute, looks up at its parents for anything that is positioned relative. The absolute element then positions itself in reference to that relative.

If nothing is declared as being relative, then the absolutely positioned elements will default to being positioned relative to the <html> element.

So, if you don’t mind absolutely positioned elements all defaulting to positioning themselves to the <html> element, then keep on leaving out relative.

One of the reasons why relative is used though, is that it does help for debugging later to know what the absolute positioned elements are supposed to be relative to.

3 Likes

So why is it a benefit to use relative.

Imagine that you have a section that is 500 pixels away from the left of the screen.

When it comes to positioning an absolute element inside of that section, one solution is to position that element left: 500px but when you have lots of elements in there, you then have to update all of them when the section moves.

Another solution is to say that the section is the relative, and position each element left: 0px instead. That way each element will be positioned relative to the section. The section is still 500 px away from the left of the screen, and the elements are 0 away from the left of the section. rather than relative to the top left of the screen. That way when the section moves, all of the positioned elements inside of it will move with it too.

1 Like

I will use relative then.

1 Like

To be clear, I wasn’t looking at the code on Blogger; I was looking at the code you posted in this topic, and in JSFiddle.

@Paul_Wilkins explained to you a week ago that should not have a closing tag.

And I said to him “Blogger adds it in by default, on its own, without me adding it.”

Now we all know, Blogger adds it in by default no matter what you do to the code.

Indeed. But when you’re posting here, or on JSFiddle, then you should use the correct code.

If you wish to draw attention to the fact that Blogger adds an incorrect closing tag, then make a note to that effect in your post, but at least ensure that your code is correct to start with.

(I’d also recommend abandoning Blogger, if it doesn’t allow you to do what you want correctly.)

Maybe it’s time we dealt with the elephant in the room and found you an alternative platform; one which can be fully controlled by you, and where you can learn to use more modern coding techniques.

2 Likes

I’m guessing Blogger may be doing some kind of backwards compatibility “correction “ or is not doing an html5 doctype as it should be doing.

That is, when it comes across an unclosed tag it adds a closing tag.

For example, for an unpaired <tag> it adds a </tag>

Have you tried making it a self closing <tag/> ?

You mean like this, this doesn’t work, still does it. It also does it with other tags, where if I have one </image> tag, it’ll add another one in. I have no idea how to prevent it from doing any of that.

<audio id="player4" style="display:none;">
  <source src='http://hi5.1980s.fm/;' type='audio/mpeg'/>
</audio>

Does the page have the html5 doctype - <html> - or is it html4 or xhtml?