css - 没有父元素的 100% 宽度

标签 css google-chrome width element viewport

我有点困惑。我正在构建一个待办事项列表应用程序,我正在尝试将标题设置为屏幕宽度的 100%。我相信我有正确的元标记,但将标题设置为 100% 宽度会将元素拉伸(stretch)到屏幕右侧。我做错了什么吗?

<!DOCTYPE html>
<html>
 <head>

 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Todo List App</title>

 <link rel="stylesheet" text="text/css" href="style.css">


 </head>
 <body>
     <header>

         <input type="text" placeholder="Enter an activity...">

         <button></button>

     </header>
 </body>
</html>

@charset "UTF-8";

header {
width: 100%;
height: 80px;
position: fixed;
padding: 15px;
top: 0px;
left: 0px;
z-index: 5;
background: #25b99a;
box-shadow:0px 2px 4px rgba(44, 62, 80, 0.15);
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
}

header input {
width: 100%;
height: 50px;
float: left;
background: rgba(255,255,255,0.2);
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
border-top-right-radius: 25px;
border-bottom-right-radius: 25px;
border-radius: 5px;
border: 0px;
box-shadow: none;
outline: none;
}

最佳答案

您需要包含 box-sizing 属性。

Info from MDN

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  margin: 0;
}

header {
  width: 100%;
  height: 80px;
  position: fixed;
  padding: 15px;
  top: 0px;
  left: 0px;
  z-index: 5;
  background: #25b99a;
  box-shadow: 0px 2px 4px rgba(44, 62, 80, 0.15);
  border-bottom-right-radius: 10px;
  border-bottom-left-radius: 10px;
}

header input {
  width: 100%;
  height: 50px;
  float: left;
  background: rgba(255, 255, 255, 0.2);
  border-top-left-radius: 5px;
  border-bottom-left-radius: 5px;
  border-top-right-radius: 25px;
  border-bottom-right-radius: 25px;
  border-radius: 5px;
  border: 0px;
  box-shadow: none;
  outline: none;
}
<header>
  <input type="text" placeholder="Enter an activity...">
  <button></button>
</header>

关于css - 没有父元素的 100% 宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47331907/

相关文章:

css 保持悬停菜单项悬停

javascript - Chrome 扩展 -> 执行脚本 : function call doesn' work

android - 无法在样式中设置资源形状的高度和宽度

macos - Applescript 将启动 Chrome(具体内容)

ios - UICollectionView 宽度以编程方式与 Storyboard初始化冲突

html - Div 背景颜色在带有文档类型的视口(viewport)处被截断

css - UL 占用全宽,不设置宽度

html - 将悬停事件链接到 2 个或更多元素

html - 使用引导网格将两个按钮定位在 div 的边缘

javascript - 在 Chrome 上添加了多个类,但在 Edge 或 IE 上未添加