Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="main">
	<ul class="cards">
		<li class="cards_item">
			<div class="card">
				<div class="card_image"><img src="https://assets.codepen.io/652/photo-1468777675496-5782faaea55b.jpeg" alt="mixed vegetable salad in a mason jar. " /></div>
				<div class="card_content">
					<h2 class="card_title">Farmstand Salad <span class="orange">&#x2022; $9</span></h2>
					<div class="card_text">
						<p>
							Dig into the freshest veggies of the season! This salad-in-a-jar features a mixture of leafy greens and seasonal vegetables, fresh from the
							farmer's market.
						</p>
						<p>
							Served with your choice of dressing on the side: housemade ranch, cherry balsamic vinaigrette, creamy chipotle, avocado green goddess, or honey
							mustard. Add your choice of protein for $2 more.
						</p>
					</div>
					<button class="card_btn orange">See more +</button>
				</div>
			</div>
		</li>

		<li class="cards_item">
			<div class="card">
				<div class="card_image"><img src="https://assets.codepen.io/652/photo-1520174691701-bc555a3404ca.jpeg" alt="a Reuben sandwich on wax paper. " /></div>
				<div class="card_content">
					<h2 class="card_title">Ultimate Reuben <span class="orange">&#x2022; $18</span></h2>
					<div class="card_text">
						<p>
							All great meals take time, but this one takes it to the next level! More than 650 hours of fermenting, brining, aging, and curing goes into each
							and every one of our legendary Reuben sandwiches.
						</p>
						<p>
							Every element of this extraordinary sandwich is handcrafted in our kitchens, from the rye bread baked from our secret recipe to the cave-aged
							Swiss cheese, right down to the pickle. The only thing we didn't make on the premises is the toothpick ( but we're looking into how to do that).
						</p>
						<p>
							This unforgettable sandwich has all of the classic Reuben elements: corned beef, rye bread, creamy Russian dressing, sauerkraut, plus a sweet
							gherkin pickle. No substitions please!
						</p>
						<p>Add a side of french fries or sweet potato fries for $2 more, or our housemade pub chips for $1.</p>
					</div>
					<button class="card_btn orange">See more +</button>
				</div>
			</div>
		</li>
		<li class="cards_item">
			<div class="card">
				<div class="card_image">
					<img src="https://assets.codepen.io/652/photo-1544510808-91bcbee1df55.jpeg" alt="A side view of a plate of figs and berries. " />
				</div>
				<div class="card_content">
					<h2 class="card_title">Fig &amp; Berry Plate <span class="orange">&#x2022; $16</span></h2>
					<span class="note-sm">Seasonal.</span>
					<div class="card_text">
						<p>A succulent sextet of fresh figs join with a selection of bodacious seasonal berries in this refreshing, shareable dessert.</p>
						<p>Choose your drizzle: cherry-balsamic vinegar, local honey, or housemade chocolate sauce.</p>
					</div>
					<button class="card_btn orange">See more +</button>
				</div>
			</div>
		</li>
	</ul>
</div>
              
            
!

CSS

              
                /* Mohave & Open Sans Fonts */
@import url("https://fonts.googleapis.com/css2?family=Mohave:wght@300;700&family=Open+Sans&display=swap");

/* Fonts & Colors */
:root {
	--title-font: "Mohave", sans-serif;
	--body-font: "Open Sans", sans-serif;
	--orange: hsl(0, 100%, 65%);
	--lighter-orange: hsl(0, 100%, 77%);
	--grey: hsl(197, 3%, 46%);
	--dark: hsl(0, 0%, 16%);
	--white: hsl(0, 0%, 100%);
}

/* General */
*,
*::before,
*::after {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}

::selection {
	background-color: var(--lighter-orange);
	color: var(--white);
}

body {
	background-image: url("https://github.com/Javieer57/CPC-Card-Text/blob/main/assets/wood.jpg?raw=true");
	color: var(--dark);
	font-family: var(--body-font);
}

p {
	margin: 0;
	&:not(:last-child) {
		margin-bottom: 14px;
	}
}

ul {
	list-style: none;
}

img {
	width: 100%;
}

/* Layout */
.main {
	max-width: 1200px;
	margin: 0 auto;
}

/* Typography */
.card_title {
	font-family: var(--title-font);
	font-size: 28px;
	line-height: 24px;
	text-transform: uppercase;
}

.card_text p {
	font-family: var(--body-font);
	font-size: 14px;
	line-height: 24px;
}

/* Utilities */
.orange {
	color: var(--orange);
}

.note-sm {
	color: var(--grey);
	font-family: var(--title-font);
	font-size: 14px;
}

/* Components */
.cards {
	align-items: start;
	display: flex;
	flex-wrap: wrap;
	margin: 50px 0;

	&_item {
		display: flex;
		padding: 1rem;
	}
}

.card {
	background-color: var(--white);
	border-radius: 0.25rem;
	box-shadow: 0 20px 40px -14px rgba(0, 0, 0, 0.25);
	display: flex;
	flex-direction: column;
	overflow: hidden;

	&_content {
		padding: 26px 16px 16px;
	}

	&_title {
		margin-bottom: 5px;
	}

	&_text {
		margin: 16px 0;
	}

	&_btn {
		background: none;
		border: none;
		cursor: pointer;
		font-size: 12px;
		text-transform: capitalize;
		font-family: var(--body-font);
		float: right;

		&:hover {
			text-decoration: underline;
		}
	}
}

/* Media queries */
@media (min-width: 40rem) {
	.cards_item {
		width: 50%;
	}
}

@media (min-width: 56rem) {
	.cards_item {
		width: 33.3333%;
	}
}

              
            
!

JS

              
                /**
 * Truncate a string
 * @param {string} text - text you want to be truncate
 * @param {number} letters - number of letters in the truncated text
 * @returns {string} - returns an truncated text within an <p> element to be insert as HTML
 */
function truncateText(text, letters = 135) {
	let truncatedText = text.slice(0, letters);
	let truncatedToHTML = `<p class="truncate">${truncatedText}...</p>`;

	return truncatedToHTML;
}

/* Select all card text containers */
let cards = document.getElementsByClassName("card_text");

/* Save texts within the cards as plain text */
let plainText = [];
for (let i = 0; i < cards.length; i++) {
	plainText[i] = cards[i].innerText;
}

/* Save the original card texts */
let htmlText = [];
for (let i = 0; i < cards.length; i++) {
	htmlText[i] = cards[i].innerHTML;
}

/* Add truncated text inside the cards */
for (let i = 0; i < cards.length; i++) {
	cards[i].innerHTML = truncateText(plainText[i]);
}

/* Add events to all buttons */
let btns = document.getElementsByClassName("card_btn");

for (let i = 0; i < cards.length; i++) {
	btns[i].addEventListener("click", function () {
		/* If the first child in the card text container has 'truncate' class... */
		if (cards[i].firstChild.className == "truncate") {
			/* Add their full text */
			cards[i].innerHTML = htmlText[i];
		} else {
			/* Add their truncated text */
			cards[i].innerHTML = truncateText(plainText[i]);
		}
	});
}

              
            
!
999px

Console