css实现响应式自适应文本大小显示效果代码

代码语言:html

所属分类:响应式

代码描述:css实现响应式自适应文本大小显示效果代码

代码标签: css 响应式 自适应 文本 大小 显示

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  


  
  
<style>
/* Always best to test responsiveness with the font(s) you use in production as all fonts vary in size rendered and usually have their own specific "quirks" */

@import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap");

/* Base font size calculation considering both viewport width (vw) and height (vh) */
body {
  font-size: calc(1vw + 1vh + 0.5vmin);
}

/* Adjust font size for viewports wider than 300px */
@media (min-width: 300px) {
  body {
    font-size: calc(0.8rem + (1.2vw + 1.2vh + 0.5vmin) * ((100vw - 300px) / 700));
  }
}

/* Adjust font size for viewports narrower than 1000px */
@media (max-width: 1000px) {
  body {
    font-size: calc(0.8rem + (1.2vw + 1.2vh + 0.5vmin) * ((1000px - 100vw) / 700));
  }
}

/* Ensure the font size stays within a readable range */
body {
  font-size: clamp(12px, calc(1vw + 1vh + 0.5vmin), 24px);
}



/* demo stuff below */
html {
  scroll-behavior: smooth;
}

h1,
h2,
h3 {
  margin-top: 20px;
}

article {
  padding-top:50px;
  padding-bottom:300px;
  width: 90%;
  max-width: 1200px;
  margin: 0 auto;
}
</style>



  
</head>

<body translate="no">
  <article>

<h1>Responsive Text (Again)</h1>
<p>
 In my endless quest for the elusive holy grail of responsive text, I've whipped up a CSS brew that's practically the eighth wonder of the digital world. By cleverly blending viewport width, height, and minimum values, this approach fixes the classic pitfalls of traditional responsive text methods. Gone are the days when your text looks splendid in width but ends up tragically squished in height. This method ensures your text remains a beacon of functionality and aesthetic brilliance, no matter what bizarre screen dimensions it encounters. 
</p>
<p>The Eighth Wonder of Responsive Text demo showcases how responsive text sizing can be achieved using CSS magic like <code>calc()</code> and <code>clamp()</code>, along with media queries.</p>
  <p>NOTE: To simplify answering a question from someone - I converted this demo  to use root variables - making it easier for you to edit and tailor to your specific design choices. Additionally, I included more detailed explanations on how this approach works, ensuring that you can fully understand and customize the responsive text for any project. </p>
  <h2>Thoughts</h2>
  <p>  <audio controls src=""></audio></p>
  <p>Responsive text is a fundamental principle in web design because it ensures that content remains readable and visually appealing across a wide range of devices and screen sizes. This adaptability is crucial in our multi-device world, where users might switch between smartphones, tablets, laptops, and desktops. Moreover, responsive text significantly enhances accessibility. For individuals with visual impairment.........完整代码请登录后点击上方下载按钮下载查看

网友评论0