Ok so here something that I can't find an easy way to deal with. I have a list of links, it can be tags for exemple or anything, and I want to display all of them separated by a separator. Here is an example.
The problem is that because elements are of an unknown length and need to break on mulline (a tag can have a long name for example), sometimes the separator is the first element of the line, it's uggly. Do you have any way to prevent this wrapping before the separator span?
.list {
font-size: 0;
line-height: 0;
max-width: 290px;
}
.list > * {
font-size: 14px;
line-height: 20px;
}
.list > span {
padding: 0 5px;
}
<div class="list">
Lorem
<span>/</span>
Ipsum
<span>/</span>
Lorem dolor ipsum amet dolor long foo bar baz lorem
<span>/</span>
Ipsum dolor amet
<span>/</span>
Lorem
<span>/</span>
Ipsum
<span>/</span>
Lorem ipsum
<span>/</span>
Dolor
<span>/</span>
Lorem dolor
<span>/</span>
Ipsum dolor amet
<span>/</span>
Lorem
<span>/</span>
Ipsum
<span>/</span>
Lorem ipsum
<span>/</span>
Dolor
<span>/</span>
Lorem dolor
<span>/</span>
Ipsum dolor amet
<span>/</span>
Lorem
<span>/</span>
Ipsum
<span>/</span>
Lorem ipsum
<span>/</span>
Dolor
<span>/</span>
Lorem dolor
<span>/</span>
Ipsum dolor amet
<span>/</span>
Lorem
<span>/</span>
Ipsum
<span>/</span>
Lorem ipsum
<span>/</span>
Dolor
<span>/</span>
Lorem dolor
<span>/</span>
Ipsum dolor amet
<span>/</span>
Lorem
<span>/</span>
Ipsum
<span>/</span>
Lorem ipsum
<span>/</span>
Dolor
<span>/</span>
Lorem dolor
<span>/</span>
Ipsum dolor amet
<span>/</span>
</div>
I hope this will helps you,
.list {
font-size: 0;
line-height: 0;
}
.list > * {
font-size: 14px;
line-height: 20px;
}
.list > .wrapper {
display: inline-block;
white-space: nowrap;
}
.list > .wrapper > span {
padding: 0 5px;
}
<div class="list">
<span class="wrapper">
Lorem
<span>/</span>
</span>
<span class="wrapper">
Ipsum
<span>/</span>
</span>
<span class="wrapper">
Lorem ipsum
<span>/</span>
</span>
<span class="wrapper">
Dolor
<span>/</span>
</span>
<span class="wrapper">
Lorem dolor
<span>/</span>
</span>
<span class="wrapper">
Ipsum dolor amet
<span>/</span>
</span>
<span class="wrapper">
Lorem
<span>/</span>
</span>
<span class="wrapper">
Ipsum
<span>/</span>
</span>
<span class="wrapper">
Lorem ipsum
<span>/</span>
</span>
<span class="wrapper">
Dolor
<span>/</span>
</span>
<span class="wrapper">
Lorem dolor
<span>/</span>
</span>
<span class="wrapper">
Ipsum dolor amet
<span>/</span>
</span>
<span class="wrapper">
Lorem
<span>/</span>
</span>
<span class="wrapper">
Ipsum
<span>/</span>
</span>
<span class="wrapper">
Lorem ipsum
<span>/</span>
</span>
<span class="wrapper">
Dolor
<span>/</span>
</span>
<span class="wrapper">
Lorem dolor
<span>/</span>
</span>
<span class="wrapper">
Ipsum dolor amet
<span>/</span>
</span>
<span class="wrapper">
Lorem
<span>/</span>
</span>
<span class="wrapper">
Ipsum
<span>/</span>
</span>
<span class="wrapper">
Lorem ipsum
<span>/</span>
</span>
<span class="wrapper">
Dolor
<span>/</span>
</span>
<span class="wrapper">
Lorem dolor
<span>/</span>
</span>
<span class="wrapper">
Ipsum dolor amet
<span>/</span>
</span>
<span class="wrapper">
Lorem
<span>/</span>
</span>
<span class="wrapper">
Ipsum
<span>/</span>
</span>
<span class="wrapper">
Lorem ipsum
<span>/</span>
</span>
<span class="wrapper">
Dolor
<span>/</span>
</span>
<span class="wrapper">
Lorem dolor
<span>/</span>
</span>
<span class="wrapper">
Ipsum dolor amet
<span>/</span>
</span>
<span class="wrapper">
Lorem
<span>/</span>
</span>
<span class="wrapper">
Ipsum
<span>/</span>
</span>
<span class="wrapper">
Lorem ipsum
<span>/</span>
</span>
<span class="wrapper">
Dolor
<span>/</span>
</span>
<span class="wrapper">
Lorem dolor
<span>/</span>
</span>
<span class="wrapper">
Ipsum dolor amet
<span>/</span>
</span>
</div>
You may use ::after, for example:
<div class="list">
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
<span>Lorem</span>
<span>Ipsum</span>
<span>Lorem ipsum</span>
<span>Dolor</span>
<span>Lorem dolor</span>
<span>Ipsum dolor amet</span>
</div>
.list {
font-size: 0;
line-height: 0;
}
.list > * {
font-size: 14px;
line-height: 20px;
}
.list > span {
white-space: nowrap;
}
.list > span::after {
display: inline-block;
content: '/';
padding: 0 5px;
}
https://jsfiddle.net/Lby5u1st/2/
There are a lot of ways to handle this. I'll give a few examples. The crux of the issue is that you're depending on HTML to style your webpage. HTML will handle the flow of content, but really it should just be providing semantic meaning to the page and not providing any stylistic decoration.
The content + pseudo element method
We can remove the separators from the HTML and add them via CSS instead using content. This will also allow us to not show a separator for the last item.
Then we can set our list items to be inline-block which will still allow them to flow together, and add no-wrap for our whitespace which will prevent the separator from appearing as the first item on the list.
.list {
font-size: 0;
line-height: 0;
}
.list > * {
display: inline-block;
font-size: 14px;
line-height: 20px;
white-space: nowrap;
}
.list > *:not(:last-child)::after {
content: '/';
display: inline-block;
margin: 0 5px;
}
<div class="list">
Lorem
Ipsum
Lorem ipsum
Dolor
Lorem dolor
Ipsum dolor amet
Lorem
Ipsum
Lorem ipsum
Dolor
Lorem dolor
Ipsum dolor amet
Lorem
Ipsum
Lorem ipsum
Dolor
Lorem dolor
Ipsum dolor amet
Lorem
Ipsum
Lorem ipsum
Dolor
Lorem dolor
Ipsum dolor amet
Lorem
Ipsum
Lorem ipsum
Dolor
Lorem dolor
Ipsum dolor amet
Lorem
Ipsum
Lorem ipsum
Dolor
Lorem dolor
Ipsum dolor amet
</div>
Just the pseudo element method
There's still an issue with the above code. Though we've removed the separators from the HTML we're still using content as styling. If a screen reader was to encounter the above markup it would read something like this: "Lorem forward slash Lorem ipsum forward slash Dolor forward slash, etc". HTML is meant to provide semantic meaning, and then we use CSS to provide styling so we can push this further by styling our pseudo element as the separator and removing it from the content attribute.
.list {
font-size: 0;
line-height: 0;
}
.list > * {
display: inline-block;
font-size: 14px;
line-height: 20px;
white-space: nowrap;
}
.list > *:not(:last-child)::after {
background-color: #000;
content: '';
height: .8em;
display: inline-block;
margin: 0 5px -2px;
transform: rotate(15deg);
width: 1px;
}
<ul class="list">
<li>Lorem</li>
<li>Ipsum</li>
<li>Lorem ipsum</li>
<li>Dolor</li>
<li>Lorem dolor</li>
<li>Ipsum dolor amet</li>
<li>Lorem</li>
<li>Ipsum</li>
<li>Lorem ipsum</li>
<li>Dolor</li>
<li>Lorem dolor</li>
<li>Ipsum dolor amet</li>
<li>Lorem</li>
<li>Ipsum</li>
<li>Lorem ipsum</li>
<li>Dolor</li>
<li>Lorem dolor</li>
<li>Ipsum dolor amet</li>
<li>Lorem</li>
<li>Ipsum</li>
<li>Lorem ipsum</li>
<li>Dolor</li>
<li>Lorem dolor</li>
<li>Ipsum dolor amet</li>
<li>Lorem</li>
<li>Ipsum</li>
<li>Lorem ipsum</li>
<li>Dolor</li>
<li>Lorem dolor</li>
<li>Ipsum dolor amet</li>
<li>Lorem</li>
<li>Ipsum</li>
<li>Lorem ipsum</li>
<li>Dolor</li>
<li>Lorem dolor</li>
<li>Ipsum dolor amet</li>
</ul>
Related
I am trying to indent elements based on the heading tag they are under. I am doing this currently by using the sibling selector:
p{
padding-left: 0;
}
h1 + p, h1 + ul{
padding-left: 1em;
}
h2 + p{
padding-left: 2em;
}
However if have two or more paragraphs under a heading then the padding defaults back to zero.
Is there a way to set padding of all elements under h1 tag to the same value and all elements under the h1 tag to a different value (only using CSS)?
Thanks for the help!
You could do something like this:
h1+div {
padding-left: 1rem;
}
h2+div {
padding-left: 2rem;
}
<h1>My Title</h1>
<div>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
</div>
<h2>My Title</h2>
<div>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
<p>Lorem Ipsum Dolor sit amet</p>
</div>
There is also a way to achieve the same effect using only CSS, but I wont especially reccommend it:
h1 ~ p, h1 ~ ul {
padding-left: 1rem;
}
h2 ~ p, h2 ~ ul {
padding-left: 2rem;
}
<h1>My title</h1>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<h2>My title</h2>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
so i kinda new on web development scene and there is something i want to know how to make, here is the list that i want to make
https://i.imgur.com/PAQaJde.png
what is that bar thing called? and how to make it stay on top when i scrolled
https://i.imgur.com/ai0Vf2g.png
is this card or simple col that get styled using css?
sorry for the dumb question guys, i really kinda lost since i dont know how those thing called
1) Its called a navigation, or a navBar, and you position it absolute at 0,0 for it to stay up the top:
2) not sure what you are asking for your second question but to get it in that style. you can use CSS Grid, or FlexBox which are both native browser functionality.
Im happy to answer any further questions :)
welcome to the community
The first one is a navbar or navigation bar. You can make it stick to the top by setting position: fixed; top: 0; left:0; z-index:99;
nav {
position: fixed;
display: block;
top: 0;
left: 0;
height: 50px;
width: 100%;
padding: 10px;
background-color: #ccc;
}
.block {
display: block;
height: 1000px;
}
<nav>
Navbar
</nav>
<div class='block'>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
<p>Lorem ipsum dolor sit amet</p>
</div>
The second one you can achieve by nesting an element inst=ide another, applying a margin to the parent and border-radius to the child. It works with any layout style.
div{
border: solid 1px #ccc;
border-radius: 5px;
}
.container {
padding: 20px;
}
.card {
display: inline-block;
width: 100px;
height: 100px;
margin-right: 20px;
}
.card-wide {
display: inline-block;
width: 150px;
height: 50px;
margin-top: 30px;
margin-right: 30px;
}
<div class='container'>
<h1>Content</h1>
<div class='card'></div>
<div class='card'></div>
<div class='card'></div>
<br />
<div class='card-wide'></div>
<div class='card-wide'></div>
<div class='card-wide'></div>
<div class='card-wide'></div>
</div>
When a resize the screen, the text get another size.
I wan't to avoid this and show a scroll bar.
Could you help me please ?
This is my code :
article {
width: 100%;
height: 100%;
}
section.apropos {
background-color: white;
color: #666666;
font-family: Calibri;
height: 100%;
width: auto;
padding-top: 95px;
padding-left: 20%;
padding-right: 20%;
text-align: justify;
min-width: auto;
min-height: auto;
}
<header>
<nav>
<ul>
<li><a> Notre entreprise</a></li>
<li class="m"><a> Nous connaitre</a></li>
<li class="m"><a onclick='onLinkClick()'>Nos métiers</a></li>
<li class="m"><a>Nous contacter</a></li>
</ul>
</nav>
</header>
<body>
<article>
<section class="apropos">
<h1> A PROPOS DE NOUS </h1> <br/>
<p> Lorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consectetur</p>
<p> Lorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consectetur Lorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consectetur</p>
</section>
<section class="NotreMetier" id="NotreMetier">
</section>
</article>
</body>
Thanks in advance.
Use width: and height: to make it fixed (400px and 300px in my example).
Optionally use overflow: scroll; to give your element scrollbars if the content is bigger than its size.
article {
width: 400px;
height: 300px;
overflow: scroll;
}
section.apropos {
background-color: white;
color: #666666;
font-family: Calibri;
width: auto;
padding-top: 95px;
padding-left: 20%;
padding-right: 20%;
text-align: justify;
}
<body>
<header>
<nav>
<ul>
<li><a> Notre entreprise</a></li>
<li class="m"><a> Nous connaitre</a></li>
<li class="m"><a onclick='onLinkClick()'>Nos métiers</a></li>
<li class="m"><a>Nous contacter</a></li>
</ul>
</nav>
</header>
<article>
<section class="apropos">
<h1> A PROPOS DE NOUS </h1> <br/>
<p> Lorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consectetur</p>
<p> Lorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consectetur Lorem ipsum dolor sit amet consecteturLorem ipsum dolor sit amet consectetur</p>
</section>
<section class="NotreMetier" id="NotreMetier">
</section>
</article>
</body>
Making a flexbox child have a scroll bar instead of growing, takes a container with fixed height.
easy to create in a an isolated environment:
#container {
height: 100%;
display: flex;
flex: 0 0 auto;
flex-flow: column;
}
#list {
display: flex;
flex-flow: column;
overflow: auto;
}
https://jsfiddle.net/1yjk5ojp/
but let's say i want to use a page header.
i don't know it's exact size, so i can't use aboslute position with mergin.
i also can't use any flex component along the way, cuz it's must be fixed size.
for example - added a 100px header
<div style="height: 100px;">
<h1>Page header</h1>
</div>
https://jsfiddle.net/z6dsq822/1/
is there anything i can do to make a fixed height component after an unknown size component?
my constrains are that i don't want another scroll bar, and i want the header to be outside this structure (it's a large application with different views, the header is in a template)
Make the parent container for everything a flex column and set #container to flex-grow: 1 so it takes up the available space, then everything else works as you already had it.
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html,
body {
height: 100vh;
}
body {
display: flex;
flex-direction: column;
}
header {
flex: 0 0 100px;
}
#container {
display: flex;
flex-direction: column;
flex-grow: 1;
overflow: auto;
}
#list {
display: flex;
overflow: scroll;
flex-direction: column;
flex-grow: 1;
}
#top {
flex: 0 0 20px;
background-color: #db9277;
}
#bottom {
flex: 0 0 20px;
background-color: #db9277;
}
.row {
height: 100px;
background-color: #85d8d5;
}
<header>
header
</header>
<div id="container">
<div id="top"></div>
<div id="list">
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
<div class="row">Lorem ipsum dolor sit amet.</div>
</div>
<div id="bottom"></div>
</div>
I am trying to get a series of inner divs to scroll horizontally inside a fixed width outer div. I have it working but the only problem is the text in the inner div does not wrap and overflows the next div, because of the white-space:nowrap in the outer div, which accomplishes the scroll. How can I fix this so the inner divs continue to scroll rightward, but the text fits into the inner divs (red boxes)? Please have a look at this JSFiddle for the code:
Demo in Jsfiddle
HTML:
<div id="content">
<div id="contentHolder">
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
<div class="post">lorem ipsum dolor sit amet</div>
</div>
</div>
CSS:
#content {
margin:0px auto;
background-color:#515151;
width:600px;
border-radius:5px;
padding-top:20px;
}
#contentHolder {
color:#fff;
margin:0 auto;
width:500px;
height: 400px;
background-color:#000000;
border-radius:10px;
overflow:auto;
white-space:nowrap;
}
.post {
width:60px;
height:300px;
margin:10px;
display:inline-block;
font-size:12px;
background-color:#700;
}
Apply white-space: normal; to .post