Background
I want to display the icon through background-image, but if the text is too long, it will be obscured by text.
.box {
display : inline-flex;
}
.box:before {
content : '';
background-image: url('https://imgur.com/TCc5A1P');
width: 60px;
height: 60px;
margin-right: 0.2em;
display: inline-block;
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum commodi totam sit, natus dolore reiciendis. Nihil possimus, magni praesentium molestias ab vel dolorum rem. Eos autem saepe magnam pariatur.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi
</div>
Question
When I use min-width instead of width or use inline-block for .box, it will work. Can anyone tell me why min-width or inline-block works?
use min-width instead of width
.box {
display : inline-flex;
}
.box:before {
content : '';
background-image: url('https://imgur.com/TCc5A1P');
min-width: 60px;
/* width: 60px; */
height: 60px;
margin-right: 0.2em;
display: inline-block;
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum commodi totam sit, natus dolore reiciendis. Nihil possimus, magni praesentium molestias ab vel dolorum rem. Eos autem saepe magnam pariatur.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi
</div>
--
use inline-block for .box
.box {
/* display : inline-flex; */
display : inline-block;
}
.box:before {
content : '';
background-image: url('https://imgur.com/TCc5A1P');
width: 60px;
height: 60px;
margin-right: 0.2em;
display: inline-block;
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum commodi totam sit, natus dolore reiciendis. Nihil possimus, magni praesentium molestias ab vel dolorum rem. Eos autem saepe magnam pariatur.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quasi
</div>
When I use min-width instead of width or use inline-block for .box, it will work. Can anyone tell me why min-width or inline-block works?
min-width
Sets the minimum width of an element.
width
Sets the width of an element.
display: inline-flex (and flex)
An initial setting on flex items is flex-shrink: 1. This means that flex items can shrink below their defined width / height to prevent their overflow of the container. In order to prevent this behavior, you need to disable flex-shrink.
For example:
.box::before {
width: 60px;
flex-shrink: 0; <------ add this to your code
content : '';
...
...
...
}
Or, for a cleaner version (which is also recommended by the flexbox spec), use this:
.box::before {
flex: 0 0 60px; /* flex-grow, flex-shrink, flex-basis */
content : '';
...
...
...
}
Note that flex-shrink applies to width and height, but not to min-width and min-height. By disabling flex-shrink on an element, you are effectively establishing its minimum length.
For example:
width: 60px;
flex-shrink: 0;
is equivalent to:
min-width: 60px;
For a more complete explanation, see "The flex-shrink factor" section in my answer here:
What are the differences between flex-basis and width?
display: inline-block (and block)
flex-shrink (described above) does not apply in a block formatting content.
revised code
.box {
display: inline-flex;
}
.box::before {
flex: 0 0 60px;
height: 60px;
background-image: url('http://i.imgur.com/60PVLis.png');
background-size: contain;
background-repeat: no-repeat;
margin-right: 0.2em;
content: '';
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum
commodi totam sit, natus dolore reiciendis. Nihil possimus, magni
praesentium molestias ab vel dolorum rem. Eos autem saepe magnam
pariatur. Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Quasi
</div>
Width Property will change the horizontal image dimensions to the pixels you have defined (In your case 60px). Width = 60px
Min-Width will make sure that horizontal image width is greater that or equal to the pixel you have defined. Width >= 60px.
The ouput that you see in your case is because the image is taking its original dimensions. ie some value greater tha 60px.
width:
The width CSS property specifies the width of the content area of an element. The content area is inside the padding, border, and margin of the element.
min-width:
The min-width CSS property is used to set the minimum width of a given element. It prevents the used value of the width property from becoming smaller than the value specified for min-width.
max-width:
The max-width CSS property is used to set the maximum width of a given element. It prevents the used value of the width property from becoming larger than the value specified for max-width.
Please visit Info Source for more info.
Edited
this also apply with height,
to make it simpler and more general
something = 10px;
the value is neither less nor more then 10px.
min-something = 10px;
the minimum value can't below the given property value.
E.g.= 10px, 11px, 20px, 300px is above minimum level so it is accepted. So the image cant be lower then 10px in resolution.
max-something = 10px;
the maximum value can't exceed or above the given property value.
E.g.= 9px, 8px, 5px, 0px is below maximum level so it is accepted. So the image cant be higher then 10px in resolution.
Below is few example
min width and height is 100px
.box {
/*display : inline-flex;*/
display: inline-block;
}
.box:before {
content: '';
background-image: url('https://smartlogic.io/images/brand-assets/smartlogic-seal-teal-100.png');
min-width: 100px;
min-height: 100px;
margin-right: 0.2em;
display: inline-block;
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum commodi totam sit, natus dolore reiciendis. Nihil possimus, magni praesentium molestias ab vel dolorum rem. Eos autem saepe magnam pariatur. Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Quasi
<br> please note that image is 100px *100px
</div>
min width and height is 60px
.box {
/*display : inline-flex;*/
display: inline-block;
}
.box:before {
content: '';
background-image: url('https://smartlogic.io/images/brand-assets/smartlogic-seal-teal-100.png');
min-width: 60px;
min-height: 60px;
margin-right: 0.2em;
display: inline-block;
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum commodi totam sit, natus dolore reiciendis. Nihil possimus, magni praesentium molestias ab vel dolorum rem. Eos autem saepe magnam pariatur. Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Quasi
<br> please note that image is 100px * 100px
</div>
width and height is 60px
.box {
/*display : inline-flex;*/
display: inline-block;
}
.box:before {
content: '';
background-image: url('https://smartlogic.io/images/brand-assets/smartlogic-seal-teal-100.png');
width: 60px;
height: 60px;
margin-right: 0.2em;
display: inline-block;
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum commodi totam sit, natus dolore reiciendis. Nihil possimus, magni praesentium molestias ab vel dolorum rem. Eos autem saepe magnam pariatur. Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Quasi
<br> please note that image is 100px * 100px
</div>
width and height is 100px
.box {
/*display : inline-flex;*/
display: inline-block;
}
.box:before {
content: '';
background-image: url('https://smartlogic.io/images/brand-assets/smartlogic-seal-teal-100.png');
width: 100px;
height: 100px;
margin-right: 0.2em;
display: inline-block;
}
<div class="box">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iure harum commodi totam sit, natus dolore reiciendis. Nihil possimus, magni praesentium molestias ab vel dolorum rem. Eos autem saepe magnam pariatur. Lorem ipsum dolor sit amet, consectetur adipisicing
elit. Quasi
<br> please note that image is 100px * 100px
</div>
Also note that
in your case,
when you use max-width you need to define the min-width as well.
min-width will give you the space form image to show.
Related
I am trying to make a 3 column layout for a part of an interface. I am using flexbox to lay it out since I can use flex-direction: column and let it keep moving to the right. Each content box is a width of 30%, which will ideally give me the 3 columns I am looking for. The problem I am having is when the content grows too big for the container vertically, it begins to overflow in the x-direction.
See this codepen for an example of what I am trying to avoid: https://codepen.io/jtris1/pen/RwBZPNp
I tried setting the min-height of the content container to 100%, but this results in it creating a single column down the container instead of the 3 I need. I also tried setting max-width on the container to see if it would force the content down, but the content still overflows in the x-direction.
HTML
<div className='guideline-detail-overlay-container'>
<div className="overlay-info">
<h2>Title</h2>
</div>
<div className='overlay-flex-content'>
<div className="overlay-demographics overlay-content-box">
<h3>Demographics</h3>
...
</div>
<div className="overlay-med-history overlay-content-box">
<h3>Medical History</h3>
...
</div>
<div className="overlay-family-history overlay-content-box">
<h3>Family History</h3>
...
</div>
<div className="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
...
</div>
</div>
</div>
CSS
.guideline-detail-overlay-container {
/* display: none; */
position: absolute;
background-color: #ffffff;
border: 1px solid black;
width: 100%;
height: 100%;
padding: 2rem 4rem;
box-sizing: border-box;
}
.overlay-flex-content {
min-height: auto;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
}
.overlay-content-box {
width: 30%;
height: auto;
margin-bottom: 1.5rem;
}
.overlay-info {
width: 100%;
height: auto;
text-align: center;
margin-bottom: 2rem;
}
Notice that when you use a 100% height you're assign the same height that the div (guideline-detail-overlay-container) parent. Change the height to auto should fix your problem. You also should delete height property.
guideline-detail-overlay-container {
height : auto;
}
While Flexbox Layout is an easy way to define grids, sometimes it is even easier to use the 'older' CSS columns mechanism to flow a text.
Define a parent columns container (.guideline-detail-overlay-container)
Define the preferred maximum the number of columns it should hold.
Define the preferred width of a single column. This value will resize with the width of the viewport and serves as a trigger to wrap content elements and to calculate whether there is enough room given the number of columns required.
I have disabled all Flexbox Layout related properties in your CSS code, inserted columns layout alternatives and commented the CSS and left the original code in-place for comparison.
snippet
/* * { outline: 1px dashed } /* for debugging */
/* Set for all elements */
*, ::before, ::after { box-sizing: border-box }
.guideline-container {
/* position: relative; /* obsolete, has no use */
/* min-height: 70vh; /* obsolete, content will stretch the parent */
/* justify-content: center; /* obsolete, not a flexbox container */
}
.guideline-detail-overlay-container {
/* display: none; */
/* position: absolute; /* obsolete, has no use */
padding: 2rem 4rem;
background-color: #ffffff;
border: 1px solid black;
/* obsolete, stretching is automatic
width : 100%;
height: 100%;
*/
}
.overlay-flex-content {
/* Set default required columns and column width (wrap trigger) */
columns: 3 250px;
/* 'columns' mechanism will differentiate on viewport resize */
/* height: 100%; /* obsolete, stretched by content */
/*
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
*/
}
.overlay-content-box {
/*
width: 30%;
height: auto;
margin-bottom: 1.5rem;
*/
}
.overlay-content-box:first-child>h3 {
/* To remove jagged top of first item in first column */
/* A well know issue */
margin-top: 0;
}
.overlay-info {
column-span: all;
/* width: 100%; /* obsolete, handled by 'column-span' */
/* height: auto; /* obsolete, HTML default */
text-align: center;
margin-bottom: 2rem;
}
<div class='guideline-container'>
<div class='guideline-detail-overlay-container'>
<div class="overlay-info">
<h2>Title</h2>
</div>
<div class='overlay-flex-content'>
<div class="overlay-demographics overlay-content-box">
<h3>Demographics</h3>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Blanditiis possimus iure deserunt explicabo quasi quis aliquam vitae reiciendis nisi dignissimos obcaecati animi iusto cumque eum a, voluptate nesciunt assumenda corrupti?
</div>
<div class="overlay-med-history overlay-content-box">
<h3>Medical History</h3>
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Odio, dolorem reprehenderit. Saepe sunt, aut ratione quasi quaerat voluptatum, iste, fugiat autem dolor velit exercitationem blanditiis nisi. Et in odio ducimus.
</div>
<div class="overlay-family-history overlay-content-box">
<h3>Family History</h3>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Aut, neque cumque voluptatem adipisci nemo harum, hic dolorem accusamus, pariatur debitis velit est rem doloremque. Laboriosam ad corrupti sit excepturi suscipit.
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
<div class="overlay-services overlay-content-box">
<h3>Preventative Screening Services</h3>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Sit expedita saepe architecto quaerat beatae repellendus! Enim natus, totam quaerat officia non voluptatibus facilis, maiores molestias quos quam voluptates mollitia. Quae!
</div>
</div>
</div>
</div>
I am relatively new to CSS. I received a lecture on Text Styling (relative unit and absolute). And in the lecture, it was specified in the body tag that the font-size be 120% as the lecturer said most browsers default font-size is 16px (absolute unit).
He went ahead and apply an in-line styling to override the previous declaration, but made use of the “em” unit.
He explained:
2em = 240% = 38px (twice large as the parent element)
0.5em=50%...
I tried what I understood to make a two-column layout…Inserted 2 paragraphs inside a div and floated them left. I made their width 50% of the viewport –it worked fine.
Then I decided to replace the 50% by 0.5em which I think it should be half of the parent element(the div) since a div is 100% by default. I was surprised by the result…
I really don’t understand anymore!
Here is my code:
* {
box-sizing: border-box;
}
div {
background-color: #00FFFF;
margin-bottom: 10px;
}
p {
width: 50%; /* worked fine */
border: 1px solid black;
float: left;
padding: 10px;
width: .5em;
margin-top: 0em;
/* when the p width was 50%, as i scaled the browser everything adjusted...but with 18em it doesn't */
margin-top: 0%
}
#p1 {
background-color: #A52A2A;
}
#p2 {
background-color: #DEB887;
float: left;
}
section {
clear: both;
}
</style>
</head>
<body>
<h1>Two Column Design</h1>
<div>
<p id="p1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quia distinctio aliquid cupiditate perferendis fuga, sit quasi alias vero sunt non, ratione earum dolores nihil! Consequuntur pariatur totam incidunt soluta expedita.</p>
<p id="p2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta beatae voluptatibus veniam placeat iure unde assumenda porro neque voluptate esse sit magnam facilis labore odit, provident a ea! Nulla, minima.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius nemo vitae, cupiditate odio magnam reprehenderit esse eum reiciendis repellendus incidunt sequi! Autem, laudantium, accusamus. Doloribus tempora alias minima laborum, provident!</p>
<section>This is regular content continuing after the the paragraph boxes.</section>
</div>
I am trying to create a sort of text carousel.
Heres a sketch of what I want to do:
I have a card and I might have multiple of them.
If there's one I just need it to be neatly centered both vertically and horizontally.
If there's two then try to put them next to one another.
But if there's more than there can be fit on the screen, then I just want the last one to overflow off
All the cards need to retain the same size
This is what I have tried:
Firstly I tried using align-items: flex-start
http://jsfiddle.net/7pdmeh6v/
This works with the logic but the problems are that if I had one item it wouldn't be centered and here I can't change the width.
Secondly I tried using align-items: center
http://jsfiddle.net/hr8ya9fg/
Logic works but
This cuts off the top of the cards and also doesn't let me change
the size of the cards.
Thirdly I removed align-items and just left justify-content: center http://jsfiddle.net/6hdzamq5/
which works with the logic but still doesn't let me change the size of the cards AND also I noticed here that it completely disregards margins and paddings
TLDR:
Flex-box doesn't seem to let me edit the size on any occasion without another problem occurring.
A couple of initial observations:
In a row-direction flex container, which is what you're using, the align-* properties control vertical, not horizontal, space and positioning. So I'm not sure how you plan to solve your horizontal scroll problem using align-items.
The initial value of the flex-shrink property is 1. This means that flex items will shrink in order to stay within the bounds of the container. So when you tell your flex items to be width: 500px, that is not a fixed length. The items can shrink. Add flex-shrink: 0 to disable flex shrink and make the items inflexible.
.container {
display: flex;
height: 240px;
overflow: auto;
margin: 10px;
padding: 10px;
border: 3px solid #000;
}
.box {
width: 500px;
flex-shrink: 0; /* toggle between 1 and 0 to see the differences */
background-color: red;
border: 1px solid #000;
color: #000;
padding: 10px;
}
.box {
margin-left: auto;
margin-right: auto;
}
.box ~ .box {
margin-left: 10px;
}
<div class="container">
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
</div>
<div class="container">
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
</div>
<div class="container">
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
</div>
You'll notice that the last margin / padding collapses. That issue is explained here:
Last margin / padding collapsing in flexbox / grid layout
I'd suggest you looking into flex property which is a shorthand for flex-shrink, flex-grow and flex-basis properties. It would help you to define behavior of your flex-items. (https://www.w3schools.com/cssref/css3_pr_flex.asp)
See the snippet below:
#container {
display: flex;
margin: 10px;
height: 180px;
overflow: auto;
border: 3px solid;
}
.box {
flex: 0 0 320px;
margin: 10px;
background: #9a6;
border: 1px solid;
color: #000;
padding: 10px;
}
<div id="container">
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
<div class="box">
<h2>Heading</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas corporis et nesciunt esse quas eos illum, facere voluptate, iure officiis.
</div>
</div>
I created a fiddle which is working fine but whenever I am trying to add an extra left padding to lef-col, the other two divs move downside.
What I am expecting is that padding applies to the inner content and the outer boundary of div, but in this case it looks like it is adding to the div calculation. Let me know what I understand incorrectly for passing.
PS - I fixed by applying box-sizing: border-box to left-col . I am looking for e.xplanation for this cause than solution(though alternate solutions are welcome too !!)
Code -
HTML -
<div class="container">
<div class="left-col">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos illum perferendis corporis, nemo dolorem, mollitia at sequi quis corrupti. Fugit eaque dolores inventore, aliquam quisquam, saepe officiis eos quia at!</p>
</div>
<div class="mid-col">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos illum perferendis corporis, nemo dolorem, mollitia at sequi quis corrupti. Fugit eaque dolores inventore, aliquam quisquam, saepe officiis eos quia at!</p>
</div>
<div class="right-col">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos illum perferendis corporis, nemo dolorem, mollitia at sequi quis corrupti. Fugit eaque dolores inventore, aliquam quisquam, saepe officiis eos quia at!</p>
</div>
</div>
CSS -
*, html {
margin: 0;
padding: 0;
}
body {}
.container {
padding: 20px;
overflow: hidden;
}
.left-col, .mid-col, .right-col {
width: 33.3%;
float: left;
}
.left-col {
background: lightpink;
}
.mid-col {
background: lightgreen;
}
.right-col {
background: lightblue;
}
Fiddle - https://jsfiddle.net/km2brd8m/
If you add box-sizing: border-box to left-col it will work, and the reason is that when using box-sizing: border-box the padding is included in the set width.
Updated fiddle
You can also subtract the padding from the width using CSS Calc
.left-col {
width: calc(33.3% - 20px);
background: lightpink;
padding-left: 20px;
}
Updated fiddle
Add the box-sizing: border-box; property to the three columns. This will include the padding it the overall dimensions instead of outside the original dimensions as it is currently doing.
The padding increases the space used by the element, just like margin. border-box makes it use space from the element itself, instead of increasing its size.
You can read more about it here.
I have an issue on a test web-page i'm creating for practice where i have two div elements one above the other, however the top one (.item in the CSS) is 'under lapping' the bottom one (.wide and .img-span in the css) and sticks out on the other-side for some reason. I have fiddled around with my CSS and HTML for a while and i cant fix it.
CSS:
.item {
padding-left: 20%;
padding-right: 20%;
padding-top: 4px;
padding-bottom: 4px;
overflow: hidden;
margin: 0;
}
.item p {
font-size: 18px;
}
.img-span {
padding: 0;
margin: 0;
width: 100%;
}
.wide {
width: 100%;
height: 100%;
opacity: 1;
overflow: hidden;
}
HTML:
<div id="first" class="item">
<p class="para">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime distinctio sed officia, nam iure quam necessitatibus nobis non, aut quaerat autem. Quam mollitia, fugiat amet veritatis, voluptate earum quidem et! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci ex earum impedit ipsum consequatur dolor doloremque eum. Sed fugit dolor maiores pariatur nesciunt iste cupiditate consequuntur, dolore alias numquam voluptatum!
</p>
</div>
<div id="img-span">
<img src="img/board-911636.jpg" class="wide">
</div>
Setting display: block on the img fixes your problem.