如何将网站分成三个不等长的列。

huangapple go评论74阅读模式
英文:

How to divide a website into three unequal length columns

问题

我一直在尝试创建一个分为3列的网站,其中两列的宽度为15%,中间一列的宽度为70%。但是,我无法让这3列占用网站空间的100%宽度。以下是我的代码:

<!-- language: lang-css -->
body {
  width: 100%;
  max-width: 960px;
  margin: 0;
}

div.content {
  width: 100%;
  display: flex;
}

div.column1 {
  width: 15%;
  background-color: aqua;
}

div.column2 {
  width: 70%;
  background-color: green;
}

navbar {
  position: relative;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-pack: justify;
  justify-content: space-between;
  padding: 0.5rem 1rem;
}

.navbar-text {
  display: inline-block;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}

.nav {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  padding-left: 0;
  margin-bottom: 0;
  list-style: none;
}
<!-- language: lang-html -->
<body style="padding-top: 70px">
  <nav class="navbar fixed-top navbar-expand-lg navbar-light bg-light">
    <img class="navbar-brand" src="img/logo.png" width="80" height="50" alt="" />
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon">2</span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent1">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active"> <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a> </li>
        <li class="nav-item"> <a class="nav-link" href="#">Classes</a></li>
        <li class="nav-item"> <a class="nav-link" href="#">Gallery</a></li>
        <li class="nav-item"> <a class="nav-link" href="#">Location</a></li>
        <li class="nav-item"> <a class="nav-link" href="#">Contact</a></li>
      </ul>
    </div>
  </nav>
  <div class="content">
    <div class="column1">
      <h1> this is column 1</h1>
      tting, remairem
    </div>
    <div class="column2">
      <h1> this is column 2</h1>
      Lorem Ip Lorem
    </div>
    <div class="column1">
      <h1> this is column 1</h1>
      Lorem Ipsum ing Lorem
    </div>
  </div>
</body>

根据你提供的代码,你希望三列占用整个网页宽度。但是,你可能忽略了默认的外边距和内边距,它们会影响到宽度的计算。你可以尝试在CSS中添加以下样式来重置外边距和内边距:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

这将确保没有任何额外的外边距或内边距干扰到你的列的宽度计算。然后,你的三列应该占用整个网页的宽度。

英文:

I have been trying to create a website divided into 3 Columns of unequal length, being more specific two of 15% in the sides, and one of 70% in the centre. However I am not being able to manage that the 3 columns take 100% width of the space of my wesbite.
Here's my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
width: 100%;
max-width: 960px;
margin: 0;
}
div.content {
width: 100%;
display: flex;
}
div.column1 {
width: 15%;
background-color: aqua;
}
div.column2 {
width: 70%;
background-color: green;
}
navbar {
position: relative;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-ms-flex-align: center;
align-items: center;
-ms-flex-pack: justify;
justify-content: space-between;
padding: 0.5rem 1rem;
}
.navbar-text {
display: inline-block;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}
.nav {
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
padding-left: 0;
margin-bottom: 0;
list-style: none;
}

<!-- language: lang-html -->

&lt;body style=&quot;padding-top: 70px&quot;&gt;
&lt;nav class=&quot;navbar fixed-top navbar-expand-lg navbar-light bg-light&quot;&gt;
&lt;img class=&quot;navbar-brand&quot; src=&quot;img/logo.png&quot; width=&quot;80&quot; height=&quot;50&quot; alt=&quot;&quot; /&gt;
&lt;button class=&quot;navbar-toggler&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#navbarSupportedContent1&quot; aria-controls=&quot;navbarSupportedContent1&quot; aria-expanded=&quot;false&quot; aria-label=&quot;Toggle navigation&quot;&gt; &lt;span class=&quot;navbar-toggler-icon&quot;&gt;2&lt;/span&gt;&lt;/button&gt;
&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbarSupportedContent1&quot;&gt;
&lt;ul class=&quot;navbar-nav mr-auto&quot;&gt;
&lt;li class=&quot;nav-item active&quot;&gt; &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Home &lt;span class=&quot;sr-only&quot;&gt;(current)&lt;/span&gt;&lt;/a&gt; &lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt; &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Classes&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt; &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Gallery&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt; &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Location&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;nav-item&quot;&gt; &lt;a class=&quot;nav-link&quot; href=&quot;#&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;div class=&quot;column1&quot;&gt;
&lt;h1&gt; this is column 1&lt;/h1&gt;
tting, remairem&lt;/div&gt;
&lt;div class=&quot;column2&quot;&gt;
&lt;h1&gt; this is column 2&lt;/h1&gt;
Lorem Ip Lorem&lt;/div&gt;
&lt;div class=&quot;column1&quot;&gt;
&lt;h1&gt; this is column 1&lt;/h1&gt;
Lorem Ipsum ing Lorem&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

As you can see I have on top of my body a fixed navbar that was imported from bootstrap from dreamweaver,
and the a new div containing the three columns.

With this code I get the following output:

如何将网站分成三个不等长的列。

Do you guys know why the columns are not taking 100% width of the website?

答案1

得分: 0

100%的宽度在这里不起作用,因为div.content的父级是页面主体。如果父级已经设置了一些宽度,那么任何%的宽度都可以工作,因此CSS有一些值可以计算%。在这种情况下,由于您希望它覆盖整个页面,并且该div没有父级,最简单的做法是将%更改为vw
vw代表视窗宽度,表示整个页面宽度的%(而不是父级)。

div.content {
    width: 100vw;
    display: flex;
}
英文:

Width of 100% wont work here, because parent of div.content is page body. Width of any % works, if the parent has already some width set, so the CSS has some value to calculate the % of. In this case, since you want it to cover the whole page, and the div does not have a parent, the easiest thing to do is to change % to vw.
vw stands for viewport-width and represents % of the whole page width (not parent).

div.content{
width:100vw;
display:flex;
}

huangapple
  • 本文由 发表于 2023年1月9日 01:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定