html - 带有文本和按钮的响应式标题

标签 html css responsive

我正在制作一个带有文本和按钮的简单标题,但是当我使用移动设备时,一切都没有响应。我尝试使用 %'s 但没有任何效果。当我在移动设备上滚动时,我的 CSS 会上下跳动。这是代码笔,https://codepen.io/Religion/pen/ZEQerQZ .下面是 CSS 。 HTML 你可以在 codepen 上查看,因为帖子说它主要是代码。 CSS:

@media only screen and (max-width: 767px) {
.header1{
  padding: 15%;
  text-align: center;

  background-image: url('https://manchesterdental.org/img/dentalimg/welcomenote.jpg');
   background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  color: white;
  font-size: 30px;
  position: relative;

}
.header-text1 h2{
  margin-top:20%;
  font-size:1.8rem;
  width:45vh; 
  margin-left:-20%;
  color:white;



 
}
.header-text1 p{
 
    font-size:1.1rem;
  width:44vh; 
  margin-left:-20%;
  padding-top:40px;
  color:white;
  font-weight: 500;



}
.header-text{
   position: absolute;
   bottom:0;
   left:0;

   
   
   width:1px;



}
.button {
  background-color: #00aeef; /* Green */
  
  color: black;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
  margin-left: 2%;
  width:95%;
  margin-bottom: 20px;
  
}
}

@media (min-width:900px) {
  .header1{
  padding: 60px;
  text-align: center;

  background-image: url("{% static 'dentist/img/bg-img/header.jpg' %}");
   background-position: center center;
  background-size: cover;
  background-repeat: no-repeat;
  color: white;
  font-size: 30px;
  height:80vh;
 
}
.header-text1 h2{
  margin-top:10%;
  color:white;
font-size: 3rem;


 
}
.header-text1 p{
 padding-top:40px;
 font-size:1.5rem;
 max-width:80%;
 margin-left:10%;
 
 
  color:white;
  font-weight: 500;



}
.button {
  background-color: #00aeef; /* Green */
  
  color: black;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
  margin-left: 2%;
  width:45%;
  font-family: "Comic Sans MS", cursive, sans-serif;
  font-weight:800;

  
}
}

HTML:

<div class="header1">
  <div class = "header-text1">
  <h2>We Believe Everyone Should Have Easy Access To Great Dental Care</h2>
  <p >As a leading industry innovater, * Dental Business* is opening up exciting new opportunities for dental professionals, investors, employees & suppliers. Contact us to find out what we have to offer you.</p>
  <br>
 <a href="{% url 'contact' %}"><button class="button button1">Contact Us!</button></a>

  </div>
</div>

enter image description here

最佳答案

要使文本元素在 div 中居中,您可以使用 flexbox。只需将以下代码添加到媒体查询之前的 CSS 文件顶部:

.header-text1 {
  display: flex;
  flex-direction: column;
  justify-content: center;
 
  width: 100%;

此外,无需将此包含在您的媒体查询中,因为无论视口(viewport)大小如何,flexbox 都会处理所有内容。

Codepen更新了代码 - 还更改了第二个媒体查询,以便 CSS 不会像以前那样在 767px 和 900px 之间中断。

关于html - 带有文本和按钮的响应式标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62504170/

相关文章:

jquery - Bootstrap 3 更新会在 Bootstrap 对话框打开或关闭时摇动 UI。我怎样才能摆脱这种行为?

html - Firefox 问题 100% 宽度的表格标题超出父元素

html - spring mvc 向收件人发送时尚且格式化的电子邮件

html - 单击导航按钮时如何关闭导航栏?

php - WordPress 输出消息

html - 在元素中创建响应式对 Angular 线

javascript - Chrome 扩展程序 - 单击扩展程序、新选项卡和重定向

javascript - 如何防止用户使用 CTRL & + 进行缩放

html - 使下拉菜单与导航栏宽度相同

css - 更改响应式表格的悬停颜色