It is easy to use BEM for fixed layouts. What is about css styles structure for adaptive web pages with media queries?
html sample:
<div class="t-news">
<div class="t-news__post b-post">
<div class="b-post__title"></div>
<div class="b-post__text--green"></div>
</div>
<div class="t-news__post b-post--small">
<div class="b-post__title"></div>
<div class="b-post__text--red"></div>
</div>
</div>
less sample:
.t-news {
&__post {
//some styles
}
}
.b-post {
&__title {
//some styles
}
&__text {
//some styles
&--red {
//some styles
}
&--green {
//some styles
}
}
&--small {
//some styles
}
}
.t-news - page template. It is a block that defines position of blocks inside.
.b-post - BEM block
.b-post__title - BEM element of b-post
.b-post__text--red - BEM modifier of b-post__text of b-post
Should I put media queries inside or outside my blocks?
In my experience, I realized that blocks shouldn't be responsible for their widths or margins for the sake of flexibility and modularity. Having "elastic" blocks in a project allow them to be moved around to occupy different areas (with different sizes) of a page without breaking functionality or layout. As for the margins, it's easier to keep consistent spaces between the blocks if they are defined on a higher level: a template block like, I assume, t-news is (considering the "t" is for template).
BEM is all about modularity, every piece of code that is related to a specific block stays in the block's folder in the file system, so it shouldn't be different with media queries, that are only a part of the CSS. The important thing is to know what the CSS is doing, for example: if a set of rules is defining areas and margins in a template, whether it needs media queries for that or not, these rules should be a part of the block that is responsible for these definitions.
This approach may generate a lot of media queries, and there may be a concern with rendering performance, but, according to this article, multiple media queries may affect performance only if they are different from each other. Repetitions of the same rule, like #media (max-width: 850px), will be serialized and interpreted as one.
This way, media queries related to areas and margins go in the template block, and additional media queries related to the components themselves, go in the components blocks. Since the template is responsible for sizes, I would change the "small" modifier, in your example, to the template block.
Also, I would reconsider using green and red as modifiers, since colors may change during the lifetime of a project. I suggest trying something that doesn't describe the appearance of the elements, like correct and alert.
Finally, remember that modifiers should folow element classes in the HTML, like b-post__text b-post__text--alert.
Here's your updated code:
Html:
<div class="t-news">
<div class="t-news__post b-post">
<div class="b-post__title">Title 1</div>
<div class="b-post__text">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam eget ligula eu lectus lobortis condimentum. Aliquam nonummy auctor massa. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla at risus. Quisque purus magna, auctor et, sagittis ac, posuere eu, lectus. Nam mattis, felis ut adipiscing.</div>
</div>
<div class="t-news__post b-post">
<div class="b-post__title">Title 2</div>
<div class="b-post__text b-post__text--correct">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam eget ligula eu lectus lobortis condimentum. Aliquam nonummy auctor massa. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla at risus. Quisque purus magna, auctor et, sagittis ac, posuere eu, lectus. Nam mattis, felis ut adipiscing.</div>
</div>
<div class="t-news__post t-news__post--small b-post">
<div class="b-post__title">Title 3</div>
<div class="b-post__text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
</div>
<div class="t-news__post t-news__post--small b-post">
<div class="b-post__title">Title 4</div>
<div class="b-post__text b-post__text--alert">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
</div>
</div>
Scss:
.t-news {
display: flex;
flex-flow: row wrap;
justify-content: flex-start;
margin: -0.5rem;
&__post {
margin: 0.5rem;
width: calc(50% - 1rem);
#media (max-width: 800px) { width: calc(100% - 1rem); }
&--small {
width: calc(25% - 1rem);
#media (max-width: 800px) { width: calc(50% - 1rem); }
}
}
}
.b-post {
box-sizing: border-box;
border: 1px solid #eeb;
background: #ffc;
padding: 0.5rem;
&__title {
font-size: 1.5rem;
#media (max-width: 800px) { font-size: 1.25rem; }
}
&__text {
font-size: 1rem;
&--correct {
color: green;
}
&--alert {
color: red;
}
}
&--small {
border: none;
font-style: italic;
}
}
Hope this helps.
Related
I'm new to StackOverflow, still learning fullstack web-development.
Just started creating my own website and I'm stuck at my image keeps displaying behind the next div's item, not right below the text. :(
Please help me!! Thank you :D
HTML
<div class="wrapper">
<div class="container">
<div class="row">
<div class="col-sm-8 text-container">
<p>
<h1>laoreet ante eget, vehicula ligula.</h1><br />
sed odio eu, eleifend aliquet urna. Donec ultrices dapibus ipsum.
Suspendisse ac hendrerit augue. Pellentesque massa eros, auctor ac sapien a, lacinia
luctus dolor. Proin et eleifend quam. Mauris tristique dictum tellus vitae molestie.
Praesent
auctor justo nisl, eu porta leo aliquam at.
</p>
</div>
<div class="col-sm-4">
<img src="images/picture.png" class="picture-container my-picture" alt="">
</div>
</div>
</div>
CSS
.wrapper {
width: 100%;
height: 700px;
background-color: #ed8d8d;
}
.container {
height: 500px;
padding: 80px 0px;
}
.text-container {
width: 700px;
height: 500px;
float: left;
margin-top: 80px;
text-align: right;
}
.picture-container {
overflow: hidden;
}
/* IMAGES */
.sophie-picture {
height: 450px
}
Inside your HTML code, the "picture-container" class on the IMG element should likely be moved, so it sits on the "col-sm-4"-div above.
I suspect the "my-picture" class on the IMG and the "sophie-picture" class in the CSS should be the same thing ? most likely you've renamed one of them and forgot to do the same in the other file, If so you should rename one so their names match up again.
There's a </div> missing at the end of the HTML code here, but i suspect that is likely just the case here because you only pasted part of your HTML document, and not the case in your own version.
The reason your image and text-container overlap is the use of float.
Removing that likely solves most of the issue.
But judging by the col-sm-8 style classnames i'm guessing you're using something like bootstrap ? Those classes apply a whole bunch of CSS (that you don't particularly have to worry about), but they provide the "column(s) within row; row(s) within container"-style of rapidly building a layout. If you're using those classes its best not to mix it with floats, manual width/height and margin statements, or really any significant CSS (just cosmetic only things like colors, font bold/italic, ...). Bootstrap has many classes so you effectively don't have to write any CSS yourself (classes like mb-4 or such for margins for example).
I would suggest using 1 or the other for a given container:
either building it the bootstrap-way with container/row/col and then using the margin/color/etc classes from boostrap.
or writing the CSS yourself, but then not using those bootstrap classes.
move the h1 tag above the p tag and set margin-bottom on the p tag to 0. also picture-container has no declared height so overflow:hidden won't do anything there. aplly overflow:hidden to wrapper.
.wrapper {
width: 100%;
height: 700px;
background-color: #ed8d8d;
border:solid 2px red;
overflow:hidden;
}
.container {
height: 500px;
padding: 80px 0px;
border:solid 2px blue;
}
.text-container {
width: 700px;
height: 500px;
float: left;
margin-top: 80px;
text-align: right;
border:solid green 2px;
}
.picture-container {
overflow: hidden;
}
p{
margin-bottom:0}
/* IMAGES */
.sophie-picture {
height: 450px
}
<div class="wrapper">
<div class="container">
<div class="row">
<div class="col-sm-8 text-container">
<h1>laoreet ante eget, vehicula ligula.</h1>
<p>
sed odio eu, eleifend aliquet urna. Donec ultrices dapibus ipsum.
Suspendisse ac hendrerit augue. Pellentesque massa eros, auctor ac sapien a, lacinia
luctus dolor. Proin et eleifend quam. Mauris tristique dictum tellus vitae molestie.
Praesent
auctor justo nisl, eu porta leo aliquam at.
</p>
</div>
<div class="col-sm-4">
<img src="https://via.placeholder.com/450" class="picture-container my-picture" alt="">
</div>
</div>
</div>
</div>
I have an HTML component that has an image floating to the left and text on the right. When the text's height is larger than the image, the text will wrap to the left. I want to add some padding between the image and the wrapped text. I could add a bottom padding to the image, but I don't want the padding to show up when the text is not wrapped. Here is what the component should look like when the text is no wrapped. The image should not have a bottom padding:
Here is what it should look like when the text is wrapped. There should be some padding between the image and the wrapped text:
Is there a way to do this through css?
An idea in case the image height is fixed or known:
.container {
border:2px solid;
min-height:200px; /* same as image height */
font-size:19px;
}
.container img {
float:left;
margin:0 20px 20px 0;
}
<div class="container">
<img src="https://picsum.photos/id/1014/200/200" > Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque fermentum quis mi vitae molestie. Sed scelerisque fringilla interdum. Duis ac purus nisl. Nulla vehicula vehicula turpis id convallis. Etiam nec nisl nibh. Mauris lorem mauris, vehicula nec massa in, accumsan egestas eros. Integer vehicula nulla sed enim laoreet maximus. Vestibulum at interdum sem. Sed interdum volutpat massa,
</div>
Yes, you can do it. Follow this example for HTML and css.
body {
margin: 20px;
text-align: center;
}
img {
float: left;
margin: 0px 10px 5px 10px;
}
p {
text-align: justify;
font-size: 20px;
}
<!DOCTYPE html>
<html>
<head>
<title>
Wraping an Image with the text
</title>
</head>
<body>
<div class="square">
<div>
<img src= "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/1024px-Image_created_with_a_mobile_phone.png" alt="Longtail boat in Thailand" width="300px">
</div>
<p>
How many times were you frustrated while looking
out for a good collection of programming/algorithm
/interview questions? What did you expect and what
did you get? This portal has been created to
provide well written, well thought and well
explained solutions for selected questions.
An IIT Roorkee alumnus and founder of GeeksforGeeks.
He loves to solve programming problems in most
efficient ways. Apart from GeeksforGeeks, he has
worked with DE Shaw and Co. as a software developer
and JIIT Noida as an assistant professor. It is a
good platform to learn programming. It is an
educational website. Prepare for the Recruitment
drive of product based companies like Microsoft,
Amazon, Adobe etc with a free online placement
preparation course.
</p>
</div>
</body>
</html>
Is there anyway to allow flex items to grow in width but only in height when necessary. I love flexbox but find it painful that flex items in a row all grow to the same height even when there is not content to fill them and then display additional items further down the page. Ideally I would like flex items to arrange themselves into available space when previous items don't have sufficient content to fill the box and not leave a big space.
Is this my lack of knowledge or is it just not possible? If it's not possible, could the facility be added in updates etc.
(Sorry. I tried to upload diagrams to explain but my reputation isn't enough!)
[EDIT. Code added as request. Some style left to demonstrate the white space I want to be taken up by the other flex items.]
<!doctype html>
<html>
<head>
<style>
.content {
font-size: 2em;
width: 100%;
margin: 0px;
padding: 0px;
background-color: coral;
display:flex;
flex-direction:row;
flex-wrap: wrap;
justify-content: center;
align-content: stretch;
align-items: flex-start;
}
.flex-item {
box-sizing: border-box;
flex-grow: 1;
flex-shrink: 0;
flex-basis:40vw;
background-color: rgba(255, 255, 255, 0.9);
border: 2px solid white;
border-radius: 2px;
margin: 2vmin;
padding: 2vmin;
}
</style>
</head>
<body>
<div class="content">
<div class="flex-item">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eu venenatis nisi. Sed nec purus consectetur, sodales mi vel, efficitur arcu. Vivamus id congue quam. Fusce imperdiet bibendum egestas. Mauris porttitor risus id pharetra pharetra. Vivamus et lorem erat. Nullam ac nulla ex. Nulla sit amet semper ligula. Integer augue sem, pharetra in ex ut, finibus mollis neque. Integer vulputate dolor massa, a maximus sem vehicula malesuada. Morbi a nulla ornare, egestas nisl in, ultrices est. Integer ut maximus elit. Cras ac velit condimentum, dapibus dui quis, mattis ex.
</div>
<div class="flex-item"><img src="https://pixabay.com/get/ec8630811c846e5862cb/1442266437/wheat-797086_1280.jpg" width="100%">
</div>
<div class="flex-item">Vivamus semper at tortor a lacinia. Nulla a suscipit felis. Aliquam erat volutpat. Integer dignissim suscipit nibh a accumsan.Fusce gravida nisl nec elit placerat porta. Ut feugiat feugiat lorem nec commodo. Morbi porttitor vel sapien id tincidunt. Vivamus venenatis pellentesque tempus.
</div>
<div class="flex-item"><img src="https://pixabay.com"/get/ec8630811c846e5862cb/1442266437/wheat-797086_1280.jpg" width="100%"> </div>
</div>
</body>
</html>
Apparently my question is not clear enough! I will try and expand with the sites constraints but not being allowed to post a diagram doesn't help.
There are Flex item boxes containing text, images or both. Flex item boxes containing images scale to the available space.
With high resolutions, text only boxes are the same width and height (square) so images scale ok (square) and all is chipper. However at say viewports of 400 px wide, the boxes containing just text, become long (say 200 x 1000px for sake of argument) and the image boxes are 200 x 200px (square). The next line is then display after the bottom of the flex item text leaving a big gap (say 800px high) below the image. Other flex boxes could fit in the space after the shrunk image but they don't move into the gap. Is that clear people who put the question on hold??
what you are looking for is the 'align-content' property which is default set to 'stretch' and justifies elements vertically (the cross-axis).
Opposed to 'justify-content', default 'flex-start' which justifies elements horizontally (main-axis).
'align-self', default 'auto', can be used to control individual items.
In other cases giving the max-height and height properties the same value will work too.
Which option to use depends on your personal requirement.
A very good resource for background info: Codrops CSS Reference - Flexbox
#Sharon
I believe here is your answer. Essentially everything in your solution has a relative width and height. Thus your inner box too. Giving your 'flex-item' both a min and max height will prevent height resizing. You need to do some more stuff, so have a look at the code.
body {
overflow: hidden;
/* just for testing, remove */
}
.flex-content {
width: 100%;
margin: 0;
padding: 0;
display: flex;
flex-flow: row wrap;
}
.flex-item {
flex: 1 1 40vw;
min-height: 50px;
max-height: 50px;
background-color: rgba(255, 255, 255, 0.7);
text-align: center;
border: 2px solid white;
border-radius: 2px;
margin: 2vmin;
padding: 2vmin;
background-color: #fce4ec; /* for testing*/
}
<div class="flex-content">
<div class="flex-item">some text</div>
<div class="flex-item">some text data</div>
<div class="flex-item">some more text data</div>
<div class="flex-item">again some more text data</div>
<div class="flex-item">some text</div>
<div class="flex-item">some text data</div>
<div class="flex-item">some more text data</div>
<div class="flex-item">again some more text data</div>
<div class="flex-item">some text</div>
<div class="flex-item">some text data</div>
<div class="flex-item">some more text data</div>
<div class="flex-item">again some more text data</div>
</div>
For the sake of brevity, please consult this depiction of my template (your web browser might give you a false positive about that website), that fiddle made with a chunk of my code and a sample of my H.T.M.L. file.
<html>
<!--[…]-->
<body>
<!--[…]-->
<article>
<div class="latest_article_preview">
<img class="latest_article_thumbnail" src="16x9_ratioed_picture.jpg" width="222" height="124"></img>
<div class="latest_article_headline">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
<div class="latest_article_lede">
Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehi
</div>
<div class="read_more">
Read more…
</div>
</div>
</article>
<!--[…]-->
</body>
</html>
Each latest_article_preview visually has three sub-divisions : latest_article_thumbnail, latest_article_headline and latest_article_lede. Technically, there is a fourth one (for now called "read_more") that shall visually replace latest_article_lede.
Wherever latest_article_preview gets hovered by the cursor, latest_article_lede shall be replaced by read_more.
Still, read_more is not a link to the article page as the entire latest_article_preview box is clickable (even before the to-be-animated transitions are over).
This has to be written in vanilla C.S.S., for I know two ways to do such an interaction :
The z-index transformation trick.
The content replacement.
I first tried the content replacement. As it terribly failed when I came to combine it with animations, I went to try the z-index transformation trick.
The reason I first avoided that trick is that I find it dirty. I still chose it anyhow. But I am bugging on something : how can I make the read_more flexible box take the exact same space as the latest_article_lede one ? I tried the C.S.S.'s Position property but the results were unsatisfying either (also, I remember absolute positioning being incompatible with animations).
I searched Google to know if I could base its size, positioning and alignment parameters on latest_article_lede's without finding any satisfying answer at all (despite being sure that the Flex property could help).
… After what I plan to add animations (mostly if not only fading effects), already having those. Animations that, as said earlier do not really go with the Content property.
Any samaritan to save me on this ? Help will be much appreciated.
A simple solution would be a combination of pointer-events and opacity:
section {
width: 200px;
border: 1px solid black;
}
section div {
min-height: 200px;
padding: 20px;
position: relative;
perspective: 1000px;
}
section div:after {
content: 'Read more...';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(255, 255, 255, 0.8);
line-height: 200px;
text-align: center;
opacity: 0;
pointer-events: none;
transition: all .2s;
transform: translateZ(200px);
}
section div:hover:after {
opacity: 1;
transform: translateZ(0);
pointer-events: auto;
}
<section>
<h1>Headline</h1>
<div class="content">Lorem Ipsum Dolor sit Amet!</div>
</section>
<br> won't let me to display 3 buttons inline, so i need to disable it inside div, and I can't just delete them, they are automatically there.
I have:
<div style="display:block">
<p>Some text</p>
<br>
<p>Some text</p>
<br>
</div>
and I want:
<div style="display:block">
Some text Some text
</div>
More info
I do not want to have mystyle.css file.
Of course I know that way of disabling it.
I asked how to add to divs style this br { display: none; } if it is possible.
Solution:
It is not possible to remove just <p> tags without removing content inside.
It is not possible to hide <br> directly by divs style, so I make it this way:
<style type="text/css">
.mydiv {
display: block;
width: 100%;
height: 10px;
}
.mydiv br {
display: none;
}
</style>
<div class="mydiv">
Some text
Some text
</div>
http://jsfiddle.net/cGT4E/
You could alter your CSS to render them less obtrusively, e.g.
div p,
div br {
display: inline;
}
http://jsfiddle.net/zG9Ax/
or - as my commenter points out:
div br {
display: none;
}
but then to achieve the example of what you want, you'll need to trim the p down, so:
div br {
display: none;
}
div p {
padding: 0;
margin: 0;
}
http://jsfiddle.net/zG9Ax/1
or hide any br that follows the p tag, which are obviously not wanted
p + br {
display: none;
}
I used this and it had the effect of removing the line break and showing it as a space between words. I think this is closer to what you were looking for.
br {
display: inline;
content: ' ';
padding: 0 3px;
}
<p style="color:black">Shop our collection of beautiful women's <br> <span> wedding ring in classic & modern design.</span></p>
Remove <br> effect using CSS.
<style> p br{ display:none; } </style>
I came across the same problem and solved it with display: contents
Here is the demo:
.wrapper {
background: #EEE;
overflow: hidden;
white-space: nowrap;
text-overflow:ellipsis;
}
.wrapper * {
display:contents;
}
<div class="wrapper">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit,<br/>
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. <br/>
Pretium lectus quam id leo in vitae turpis massa sed. Lorem ipsum dolor sit amet consectetur adipiscing elit.<br/>
Sagittis id consectetur purus ut faucibus pulvinar elementum. Netus et malesuada fames ac turpis egestas sed.<br/>
Eget duis at tellus at.
</p>
<p>
Faucibus vitae aliquet nec ullamcorper sit amet risus. Tortor condimentum lacinia quis vel eros donec ac.<br/>
Tellus in hac habitasse platea dictumst vestibulum. Aliquam id diam maecenas ultricies. Tortor id aliquet lectus proin nibh nisl condimentum id.<br/>
Sed enim ut sem viverra. <br/>
Morbi tristique senectus et netus et.
</p>
</div>
So according to MDN,
These elements don't produce a specific box by themselves. They are replaced by their pseudo-box and their child boxes.
What makes it worthy, (again taken from MDN)
Please note that the CSS Display Level 3 spec defines how the contents value should affect "unusual elements"
Here CSS Display Level 3 spec says, certain elements are rendered as display: none if display:contents applies to them:
<br>
<wbr>
<meter>
<progress>
<canvas>
<embed>
<object>
<audio>
<iframe>
<img>
<video>
<frame>
<frameset>
<input>
<textarea>
<select>
display: contents computes to display: none.
(source: https://drafts.csswg.org/css-display/#unbox)
You can usedisplay:contents, but avoid accessibility problems to apply this only on br tags:
br {
display:contents;
}
see https://developer.mozilla.org/en-US/docs/Web/CSS/display-box
[display:contents;] most browsers will remove from the accessibility tree any element with a display value of contents. [...] no longer be announced by screen reading technology.
I used it like this:
#media (max-width: 450px) {
br {
display: none;
}
}
nb: media query via Foundation
nb2: this is useful if one of the editor intend to use tags in his/her copy and you need to deal with it specifically under some conditions—on mobile for example.
I see people commenting just to remove from the html, but if you want to remove it only from certain type of #media screens, then this is the way to go:
div br {
display: none;
}