cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="content">
  <div class="header">Assorted News</div>
  <div class="intro">It's easy to get stuck in an echo chamber, especially if your friends tend to believe the same things you do, or if you get your news from only one or two places. This can make it hard to see other people's points of view and understand their decisions. Assorted News collects an assortment of the latest news stories from a varied range of sources to help burst your filter bubble. Try to read something you wouldn't normally choose. Click 'Get News' to refresh the feed.</div>
  
  <button id ="getnews">Get News</button>

  <div class="allfeeds"> 
    
  </div>
  <div class="footer">Thanks to <a href="https://newsapi.org">News API</a></div>
</div>
            
          
!
            
              
body {
    word-wrap: break-word;

  background-color: black;
  text-align: center;
  font-family: sans-serif;
}

.header{
  text-align: center;
  background-color: aquamarine;
  font-size: 150%;
  padding: 10px;
  font-weight: bold;
  
}

.intro {
  color:white;
  padding: 10px;
  max-width: 70%;
  margin: 0 auto;
  text-align:left;
  font-size: 90%;
}
#getnews {
  margin: 20px auto;
  display:block;
  background-color: lightgreen;
  font-weight: bold;
  border: 2px solid lightgreen;
  font-size: 100%;
  
}

#getnews:hover {
  border: 2px solid black;
}

#getnews:focus {
  border: 2px solid lightgreen;
  outline:none;
}

.link {
  text-align: right;
}

.feed {
  margin: 10px;
  padding: 20px;
  background-color: white;
  text-align: center;
  display: inline-block;
  max-width: 300px;
  vertical-align: top;
  horizontal-align: left;
  
  
}

.feedimg {
  max-width: 80%;
  padding: 10px;
  display: inline-block;
}

.feedheadline {
  font-size: 150%;
}

.feedbody, .feedheadline {
  text-align: left;
  display: inline-block;
  margin: 10px;
  max-width: 80%;
}

a {
  color: black;
  text-decoration:none;
  
}

.footer {
  margin:20px 0px 0px 0px;
  color: white;
  padding: 10px;
}

.footer a {
  color: white;
  text-decoration: underline;
}
            
          
!
            
              $(document).ready(function() {
  var template =
    '<div class="feed"><div class="feedheader"></div><a target="_blank" href ="" class="sourcelink"><img class="feedimg"></img><div class="feedheadline"></div><div class="feedbody"></div><br><i>Read More...</i></a></div>';

  var numOfSources = 18;

  for (var s = 0; s < numOfSources; s++) {
    $(".allfeeds").append(template);
  }

  var sourcesLink = "https://newsapi.org/v1/sources?language=en";
  var sourceLink = document.getElementsByClassName("sourcelink");
  var feedHeader = document.getElementsByClassName("feedheader");
  var feedHeadline = document.getElementsByClassName("feedheadline");
  var feedBody = document.getElementsByClassName("feedbody");
  var feedImg = document.getElementsByClassName("feedimg");

  var getArticle = function(articleLink, num) {
    jQuery.getJSON(articleLink, function(data) {
      sourceLink[num].href = data.articles[0].url;

      feedHeadline[num].innerHTML = data.articles[0].title;

      feedBody[num].innerHTML = data.articles[0].description;

      feedImg[num].src = data.articles[0].urlToImage;
    });
  };

  var getNews = function() {
    jQuery.getJSON(sourcesLink, function(data) {
      var randomArray = [];

      for (var j = 0; j < data.sources.length; j++) {
        var num = Math.floor(Math.random() * data.sources.length);
        if ($.inArray(num, randomArray) == -1) {
          randomArray.push(num);
        } else {
          j--;
        }
      }

      var k = 0;
      for (var i = 0; k < numOfSources; i++) {
        if (
          data.sources[randomArray[i]].hasOwnProperty("name") &&
          data.sources[randomArray[i]].category == "general"
        ) {
          feedHeader[k].innerHTML = data.sources[randomArray[i]].name;

          getArticle(
            "https://newsapi.org/v1/articles?source=" +
              data.sources[randomArray[i]].id +
              "&sortBy=top&apiKey=8bc565f985194462a16368169177e93d",
            k
          );

          k++;
        }
      }
    });
  };

  getNews();

  $("#getnews").click(getNews);
});

            
          
!
999px
Loading ..................

Console