简体   繁体   English

Center Div 水平带滚动 CSS

[英]Center Div Horizontally With Scroll CSS

在此处输入图像描述

I want to center the divs horizontally with an option to scroll using css.我想使用 css 滚动选项将 div 水平居中。 How can I achieve that?我怎样才能做到这一点? I saw a solution using the margin auto and max width but the div will be cut on the edges due to the maximum width which is 30rem.so I do not want that.我看到了一个使用边距自动和最大宽度的解决方案,但由于最大宽度为 30rem,div 将在边缘被切割。所以我不想要那个。 What I want is to scroll only if the contents have no space left.我想要的是只有在内容没有空间时才滚动。

Please notice how the edges are cut.请注意边缘是如何切割的。 Yes it is scrollable but since there is space available I want to use it.是的,它是可滚动的,但由于有可用空间,我想使用它。

 .az-chips-btns-parent { overflow: auto; margin: auto; max-width: 30rem; }
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"> <div class="az-chips-btns-parent"> <div class="az-chips-btns"> <div class="az-save-filters-parent" style="margin-top: 1.5px;"> <button type="button" class="btn p-0 rounded-circle az-save-filters-chip ui-btn ui-shadow ui-corner-all"> <i class="fa fa-star az-save-filters" style="font-size: 15px;"></i> </button> </div> <button type="button" class="btn az-country-chip ui-btn ui-shadow ui-corner-all"> <div>Country</div> <div class="az-country-selected-chip az-selected-chip text-danger">3 selected</div> </button> <button type="button" class="btn az-brand-chip ui-btn ui-shadow ui-corner-all"> <div>Brand</div> <div class="az-brand-selected-chip az-selected-chip text-danger"></div> </button> <button type="button" class="btn az-location-chip ui-btn ui-shadow ui-corner-all"> <div>Location</div> <div class="az-location-selected-chip az-selected-chip text-danger"></div> </button> <button type="button" class="btn az-store-chip ui-btn ui-shadow ui-corner-all"> <div>Store</div> <div class="az-store-selected-chip az-selected-chip text-danger"></div> </button> <button type="button" class="btn az-section-chip ui-btn ui-shadow ui-corner-all"> <div>Section</div> <div class="az-section-selected-chip az-selected-chip text-danger"></div> </button> </div> </div>

Flexibility rules for button and parent elements can be used to accomplish your task.按钮和父元素的灵活性规则可用于完成您的任务。

For parent class az-chips-btns , you set basic flexibility rules, such as display: flex and align-items: center (horizontal alignment of elements within the parent), as well as a gap: 10px rule that sets the spacing between children.对于父 class az-chips-btns ,您设置基本的灵活性规则,例如display: flexalign-items: center (水平 alignment 在父元素中),以及设置子元素之间间距的gap: 10px规则.

For all first-level children, you set rule flex: 1 , which allows you to accept the same width relative to each other within the parent.对于所有第一级子级,您设置规则flex: 1 ,这允许您在父级内接受相对于彼此相同的宽度。

 .az-chips-btns-parent { overflow: auto; margin: auto; max-width: 30rem; }.az-chips-btns { display: flex; align-items: center; gap: 10px; }.az-chips-btns > * { flex: 1; } button { width: 100%; }
 <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"> <div class="az-chips-btns-parent"> <div class="az-chips-btns"> <div class="az-save-filters-parent" style="margin-top: 1.5px;"> <button type="button" class="btn p-0 rounded-circle az-save-filters-chip ui-btn ui-shadow ui-corner-all"> <i class="fa fa-star az-save-filters" style="font-size: 15px;"></i> </button> </div> <button type="button" class="btn az-country-chip ui-btn ui-shadow ui-corner-all"> <div>Country</div> <div class="az-country-selected-chip az-selected-chip text-danger">3 selected</div> </button> <button type="button" class="btn az-brand-chip ui-btn ui-shadow ui-corner-all"> <div>Brand</div> <div class="az-brand-selected-chip az-selected-chip text-danger"></div> </button> <button type="button" class="btn az-location-chip ui-btn ui-shadow ui-corner-all"> <div>Location</div> <div class="az-location-selected-chip az-selected-chip text-danger"></div> </button> <button type="button" class="btn az-store-chip ui-btn ui-shadow ui-corner-all"> <div>Store</div> <div class="az-store-selected-chip az-selected-chip text-danger"></div> </button> <button type="button" class="btn az-section-chip ui-btn ui-shadow ui-corner-all"> <div>Section</div> <div class="az-section-selected-chip az-selected-chip text-danger"></div> </button> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM