How to create responsive floating elements with CSS?

CSSWeb DevelopmentFront End Technology

To create responsive floating elements with CSS, the code is as follows −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .floatRight, .floatLeft {
      color: white;
      font-size: 20px;
      padding: 10px;
      font-weight: 500;
   }
   .floatRight {
      background-color: rgb(0, 2, 95);
      float: right;
   }
   .floatLeft {
      background-color: rgb(85, 0, 74);
      float: left;
   }
   @media (max-width: 768px) {
      .floatRight {
         float: left;
      }
      .floatLeft {
         float: right;
      }
   }
</style>
</head>
<body>
<h1>Responsive Float Example</h1>
<h2>The floats will swap their position on smaller screens</h2>
<div class="clearfix">
<div class="floatRight">Float Right ></div>
<div class="floatLeft">Float Left <</div>
<br />
</div>
</body>
</html>

Output

The above code will produce the following output −

On resizing the screen the floats will swap their position as follows −

raja
Published on 08-May-2020 16:09:37
Advertisements