水平填充容器内部空间

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

Filling space horizontally inside container

问题

我有一个容器,里面包含两个元素:一个搜索栏和下面的一个表格。在表格内部,我有一个位于左侧的侧边栏和右侧的一些内容。容器的高度是固定的,我希望侧边栏和内容在垂直方向上填充空间。

我可以让表格填充空间,但无法让侧边栏和内容填充。以下是我能够实现的最接近的效果:

<div class="projects-table">
    <div class="search">
        <h3>搜索...</h3>
    </div>
    <div class="projects-content">
        <div class="sidebar">
            <p>侧边栏</p>
            <p>侧边栏</p>
        </div>
        <p class="content">内容</p>
    </div>
</div>
* {
    font-family: "Poppins", sans-serif;
}

body {
    background-size: 100%;
    background-color: black;
    background-repeat: "no-repeat";
    background-position: 0 0;
    color: aliceblue;
    margin: 0;
    padding: 0;
}

.projects-table {
    border-style: solid;
    border-radius: 1rem;
    border-width: 0.25rem;
    border-color: slategrey;
    margin-top: 5rem;
    padding: 1rem;
    height: 20rem;
    display: flex;
    flex-direction: column;
}

.projects-table .search {
    opacity: 0.25;
    color: white;
    display: flex;
    flex-direction: row;
    justify-content: start;
    align-items: center;
    border-bottom: solid;
    border-width: 0.1rem;
}

.projects-table .search img {
    width: 1.25rem;
    height: 1.25rem;
    margin-right: 0.25rem;
}

.projects-table .projects-content {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: flex-start;
    margin-top: 1rem;
    flex-grow: 1;
    border-style: solid;
}

.projects-table .projects-content .sidebar {
    width: 10%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    border-style: solid;
    margin: 0.5rem;
}

.projects-table .projects-content .sidebar p {
    border-style: solid;
    border-width: 0.1rem;
    border-radius: 0.5rem;
    margin-top: 0;
    margin-bottom: 0.5rem;
    padding: 1rem;
}

.projects-table .projects-content .content {
    width: 100%;
    height: 100%;
    margin: 0.5rem 1rem 0.5rem 1rem;
    border-style: solid;
    border-width: 0.1rem;
    border-radius: 0.5rem;
    padding: 1rem;
}
英文:

I have a container that contains two elements: a search bar, and a table under it. Inside the table, I have a sidebar to the left and some content to the right. The container has fixed height, and I want the sidebar and content to fill the space vertically.

I can get the table to fill the space, but not the sidebar and the content. This is the closest I can get:

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

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

* {
font-family: &quot;Poppins&quot;, sans-serif;
}
body {
background-size: 100%;
/*background-image: url(&#39;./images/diego-ph-5LOhydOtTKU-unsplash.jpg&#39;);*/
background-color: black;
background-repeat: &quot;no-repeat&quot;;
background-position: 0 0;
color: aliceblue;
margin: 0;
padding: 0;
}
.projects-table {
border-style: solid;
border-radius: 1rem;
border-width: 0.25rem;
border-color: slategrey;
margin-top: 5rem;
padding: 1rem;
height: 20rem;
display: flex;
flex-direction: column;
}
.projects-table .search {
opacity: 0.25;
color: white;
display: flex;
flex-direction: row;
justify-content: start;
align-items: center;
border-bottom: solid;
border-width: 0.1rem;
}
.projects-table .search img {
width: 1.25rem;
height: 1.25rem;
margin-right: 0.25rem;
}
.projects-table .projects-content {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
margin-top: 1rem;
flex-grow: 1;
border-style: solid;
}
.projects-table .projects-content .sidebar {
width: 10%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
border-style: solid;
margin: 0.5rem;
}
.projects-table .projects-content .sidebar p {
border-style: solid;
border-width: 0.1rem;
border-radius: 0.5rem;
margin-top: 0;
margin-bottom: 0.5rem;
padding: 1rem;
}
.projects-table .projects-content .content {
width: 100%;
height: 100%;
margin: 0.5rem 1rem 0.5rem 1rem;
border-style: solid;
border-width: 0.1rem;
border-radius: 0.5rem;
padding: 1rem;
}

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

 &lt;div class=&quot;projects-table&quot;&gt;
&lt;div class=&quot;search&quot;&gt;
&lt;h3&gt;Search...&lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&quot;projects-content&quot;&gt;
&lt;div class=&quot;sidebar&quot;&gt;
&lt;p&gt;Sidebar&lt;/p&gt;
&lt;p&gt;Sidebar&lt;/p&gt;
&lt;/div&gt;
&lt;p class=&quot;content&quot;&gt;content&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

你可以尝试设置 flexbox 的 align-items: stretchflex-grow: 1 属性:

.projects-table .projects-content {
  align-items: stretch;
}
.projects-table .projects-content .content {
  flex-grow: 1;
}
英文:

You can try to set flexbox align-items:stretch and flex-grow:1 properties:

.projects-table .projects-content {
align-items: stretch;
}
.projects-table .projects-content .content {
flex-grow: 1;
}

答案2

得分: 0

以下是翻译好的部分:

"All you need to do is to give the content items a flex-grow: 1" 翻译为 "你只需要给内容项添加 flex-grow: 1"。

剩下的是代码部分,不需要翻译。

英文:

All you need to do is to give the content items a flex-grow: 1

.projects-table .projects-content .sidebar p {
flex-grow: 1;
...
}

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

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

* {
font-family: &quot;Poppins&quot;, sans-serif;
}
body {
background-size: 100%;
/*background-image: url(&#39;./images/diego-ph-5LOhydOtTKU-unsplash.jpg&#39;);*/
background-color: black;
background-repeat: &quot;no-repeat&quot;;
background-position: 0 0;
color: aliceblue;
margin: 0;
padding: 0;
}
.projects-table {
border-style: solid;
border-radius: 1rem;
border-width: 0.25rem;
border-color: slategrey;
margin-top: 5rem;
padding: 1rem;
height: 20rem;
display: flex;
flex-direction: column;
}
.projects-table .search {
opacity: 0.25;
color: white;
display: flex;
flex-direction: row;
justify-content: start;
align-items: center;
border-bottom: solid;
border-width: 0.1rem;
}
.projects-table .search img {
width: 1.25rem;
height: 1.25rem;
margin-right: 0.25rem;
}
.projects-table .projects-content {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
margin-top: 1rem;
flex-grow: 1;
border-style: solid;
}
.projects-table .projects-content .sidebar {
width: 10%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
border-style: solid;
margin: 0.5rem;
}
.projects-table .projects-content .sidebar p {
flex-grow: 1;
border-style: solid;
border-width: 0.1rem;
border-radius: 0.5rem;
margin-top: 0;
margin-bottom: 0.5rem;
padding: 1rem;
}
.projects-table .projects-content .content {
width: 100%;
height: 100%;
margin: 0.5rem 1rem 0.5rem 1rem;
border-style: solid;
border-width: 0.1rem;
border-radius: 0.5rem;
padding: 1rem;
}

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

&lt;div class=&quot;projects-table&quot;&gt;
&lt;div class=&quot;search&quot;&gt;
&lt;h3&gt;Search...&lt;/h3&gt;
&lt;/div&gt;
&lt;div class=&quot;projects-content&quot;&gt;
&lt;div class=&quot;sidebar&quot;&gt;
&lt;p&gt;Sidebar&lt;/p&gt;
&lt;p&gt;Sidebar&lt;/p&gt;
&lt;/div&gt;
&lt;p class=&quot;content&quot;&gt;content&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

这里是一种替代方法,用于执行你正在做的事情。我发现使用这样的方式可以使长期维护更容易,并且可以更容易理解复杂的定位而不需要复杂的样式设置。

我发现在一个网格中使用嵌套网格可以使复杂布局更容易。请注意,我在这里使用了一些网格区域的名称,只是为了清晰地表示每个区域的位置;还使用了一些丑陋的样式,以显示各个元素的位置等。

  • 使用一个网格作为“页面”,以解决先前的大5em边距可能用于某些内容的情况,这样可以避免一些奇怪的固定位置和大小调整的挑战。
  • 添加一些
    包装器作为“容器”。
  • 使用“container”来帮助描述每个容器块的布局与视觉样式。
<div class="page-container">
  <div class="projects-table project-container">
    <div class="search search-container">
      <div class="image-container">
        <img src="" alt="IMG">
      </div>
      <div class="search-search">
        <h3>搜索...</h3>
      </div>
    </div>
    <div class="projects-content content-container">
      <div class="sidebar">
        <p>侧边栏</p>
        <p>侧边栏</p>
      </div>
      <p class="content">内容</p>
    </div>
  </div>
</div>
* {
  font-family: "Poppins", sans-serif;
}

body {
  background-color: #000000;
  color: aliceblue;
  margin: 0;
  padding: 0;
  font-size: 16px;
  box-sizing: border-box;
}

.page-container {
  background-color: #404040;
  display: grid;
  grid-template-columns: 1fr;
  /* 先前的5em边距,但如果需要内容,允许内容在该行中 */
  grid-template-rows: 5em auto;
  grid-template-areas:
    "nothingyet"
    "projectthings";
}

.project-container {
  grid-area: projectthings;
  background-color: #004020;
  /* 告诉此容器成为自动行 */
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 5em auto;
  grid-template-areas:
    "searchc"
    "contentc";
  height: 20rem;
}

/* 其余的CSS样式请参考你的原始代码 */

注意:这只是你代码的一部分,其中包含HTML和CSS。如果需要完整的翻译或其他信息,请继续提问。

英文:

Here is a bit of an alternate way to do what you are doing. I find using something like this makes long-term maintenance easier and individual "block" styling easier to understand without complex positioning.

I find that using a grid with grids IN it makes complex layout easier. Note here I used some grid-areas names just to make things clear what was where; some ugly styling just to show what is where etc.

  • using a grid for the "page" to account for that prior large 5em margin that MGIHT be for some content? avoids some weird fix positioning and sizing challenges if so
  • adding some div wrappers for "containers"
  • used "container" to help describe layout vs visual styles for each container block

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

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

* {
font-family: &quot;Poppins&quot;, sans-serif;
}
body {
background-color: #000000;
color: aliceblue;
margin: 0;
padding: 0;
font-size: 16px;
box-sizing: border-box;
}
.page-container {
background-color: #404040;
display: grid;
grid-template-columns: 1fr;
/* the 5em nothingyet is the prior 5em margin but allows content to be in that row if needed */
grid-template-rows: 5em auto;
grid-template-areas: 
&quot;nothingyet&quot; 
&quot;projectthings&quot;;
}
.project-container {
grid-area: projectthings;
background-color: #004020;
/* tell the this container to be that auto row */
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 5em auto;
grid-template-areas: 
&quot;searchc&quot;
&quot;contentc&quot;;
height: 20rem;
}
.search-container {
grid-area: searchc;
display: grid;
grid-template-rows: afr;
grid-template-columns: 1.25em auto;
/* this 0.25em was padding before but really is a &quot;gap&quot; between image and h1 */
grid-gap: 0.25em;
grid-template-areas: &quot;imgc searchy&quot;;
align-items: center;
border-bottom: solid;
border-width: 0.1rem;
border-color: #7FFF00;
opacity: 0.25;
color: white;
}
.content-contaIner {
grid-area: contentc;
background-color: #FF0080;
display: grid;
grid-template-columns: 10% auto;
grid-template-rows: 20em;
grid-template-areas: &quot;sidebar content&quot;;
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
.image-container {
grid-area: imgc;
height: 1.25em;
}
.search-search {
grid-area: searchy;
}
/* below here is just the styling from before mostly - see inline comments */
.projects-table {
border-style: solid;
border-radius: 1rem;
border-width: 0.25rem;
border-color: slategrey;
}
/* moved to the search-container grid definition
.projects-table .search img {
width: 1.25rem;
height: 1.25rem;
margin-right: 0.25rem;
}
*/
.projects-table .search .image-container img {
/* depends on image and use intent really */
height: 100%;
width: 100%;
border: solid yellow 3px;
}
.projects-table .projects-content {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
margin-top: 1rem;
flex-grow: 1;
border-style: solid;
}
.projects-table .projects-content .sidebar {
/* moved to content-container css
width: 10%;
height: 100%;
*/
display: flex;
flex-direction: column;
align-items: center;
border-style: solid;
margin: 0.5rem;
}
.projects-table .projects-content .sidebar p {
border-style: solid;
border-width: 0.1rem;
border-radius: 0.5rem;
margin-top: 0;
margin-bottom: 0.5rem;
padding: 1rem;
}
.projects-table .projects-content .content {
/* moved to content-container css as auto
width: 100%;
height: 100%;
*/
margin: 0.5rem 1rem 0.5rem 1rem;
border-style: solid;
border-width: 0.1rem;
border-radius: 0.5rem;
padding: 1rem;
}
/* can be whatever but just to show it is contained in the container block allocated to it */
.content{width:100%;}

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

&lt;div class=&quot;page-container&quot;&gt;
&lt;div class=&quot;projects-table project-container&quot;&gt;
&lt;div class=&quot;search search-container&quot;&gt;
&lt;div class=&quot;image-container&quot;&gt;
&lt;!-- might not seem to line up but the alt text IMG is bigger than the 1.25em --&gt;
&lt;img src=&quot;&quot; alt=&quot;IMG&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;search-search&quot;&gt;
&lt;h3&gt;Search...&lt;/h3&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;projects-content content-container&quot;&gt;
&lt;div class=&quot;sidebar&quot;&gt;
&lt;p&gt;Sidebar&lt;/p&gt;
&lt;p&gt;Sidebar&lt;/p&gt;
&lt;/div&gt;
&lt;p class=&quot;content&quot;&gt;content&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 18:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580185.html
匿名

发表评论

匿名网友

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

确定