Jump to content

Adding a volume button with audio next to a word

Recommended Posts

  • Replies 1
  • Views 688
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Posted Images

Edit page where you want to place this > Add a Code Block > Paste this code

<div class="audio-block">
  <div class="text">
    text
  </div>
  
  <img class="icon" src="https://cdn.icon-icons.com/icons2/916/PNG/512/Audio_icon-icons.com_71845.png" alt="Audio Icon" onclick="playAudio()"> 
  
<audio id="audio" style="display: none;">
  <source src="https://www.w3schools.com/tags/horse.ogg?gidzl=Y8XlJO_tQ6w8bsfAXUXiRuMGQqYVxsaydC1f6SxlEJ-ObpWUbEjkFSB1QKcKlMvecCHj6sKqJZCIZVDfR0" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
</div>
<style>
   .audio-block {
    display: flex;
    align-items: center;
    padding: 10px;
  }

.text {
    font-family: Arial, sans-serif;
    font-size: 16px;
    font-weight: bold;
    color: #13538a;
    line-height: 1.4;
}

.icon {
    width: 30px;
    height: 30px;
    margin-left: 5px;
    cursor: pointer;
    transition: transform 0.3s ease-in-out, opacity 0.3s ease-in-out;
  }

 .icon:hover {
    transform: scale(1.1);
    opacity: 0.8;
  }
</style>
<script>
  function playAudio() {
    var audio = document.getElementById("audio");
    if (audio.paused) {
      audio.play();
    } else {
      audio.pause();
    }
  }
</script>

image.png.b058f1480c7c7490aa79d62655ac0c0b.png

If 2 text, audio, use this

<div class="audio-block">
  <div class="text">
    text 1
  </div>
  <span class="audio-icon" onclick="playAudio('audio1');"></span>
  <audio id="audio1" style="display: none;">
    <source src="https://www.w3schools.com/tags/horse.ogg?gidzl=pFSsEa5c1XNCw3CzIta26i3lDJ9GVW1AsRytOm5-K4dTw6PaMte7IuU-DpDRB0SUtRipOMM-e0GUGs875m" type="audio/mpeg">
    Your browser does not support the audio element.
  </audio>
</div>

<div class="audio-block">
  <div class="text">
    text 2
  </div>
  <span class="audio-icon" onclick="playAudio('audio2');"></span>
  <audio id="audio2" style="display: none;">
    <source src="https://tiengdong.com/wp-content/uploads/Tieng-dang-soan-tin-nhan-messenger-www_tiengdong_com.mp3?_=1" type="audio/mpeg">
    Your browser does not support the audio element.
  </audio>
</div>
<style>
  .audio-block {
    display: flex;
    align-items: center;
    padding: 10px;
/*     border: 1px solid #ccc;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); */
    margin-bottom: 10px;
  }

  .audio-icon {
    width: 30px;
    height: 30px;
    margin-left: 5px;
    cursor: pointer;
    background-image: url('https://cdn.icon-icons.com/icons2/916/PNG/512/Audio_icon-icons.com_71845.png');
    background-size: cover;
    background-repeat: no-repeat;
    transition: transform 0.3s ease-in-out, opacity 0.3s ease-in-out;
  }

  .audio-icon:hover {
    transform: scale(1.1);
    opacity: 0.8;
  }

  .text {
    font-family: Arial, sans-serif;
    font-size: 16px;
    font-weight: bold;
    color: #174688;
    line-height: 1.4;
  }
</style>
<script>
    function playAudio(audioId) {
    var audio = document.getElementById(audioId);
    if (audio.paused) {
      audio.play();
    } else {
      audio.pause();
    }
  }
</script>

image.png.68e1ad2730be7534d774b2fde7a5ade0.png

Email me if you have need any help (free, of course.). Answer within 24 hours. 
Or send to forum message

Contact Customer Care - Learn CSS - Buy me a coffee (thank you!)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.