I have an <div> container with an <img> and an ::after pseudo-element. Like so:
<div class="container" data-caption="this caption should be as wide as the image and then wrap">
<img>
</div>
.container {
display: inline-block
}
.container::after {
display: block
background: #aabbaa
content: attr(data-caption)
line-height: 40px
padding: 0 1rem
}
The container should get its width from the contained image while the ::after element should wrap its content accordingly, like this:
Instead the after element does not wrap - see this codepen.
Edited for dynamically inheriting from the image rather than the text use max-width: min-content:
.container
display: inline-block
border: 1px dashed red
max-width: min-content
&::after
display: block
background: #aabbaa
content: attr(data-caption)
line-height: 40px
padding: 0 1rem
white-space: wrap
You can use some positioning hacks, with a relative parent and absolute pseudo element. See here:
https://codepen.io/palash/pen/dJabRr
Also, white-space: wrap doesn't exist, it's white-space: normal that you are looking for, and it's is the default value. (nowrap does exist, though.)
.container
position: relative
...
&::after
...
position: absolute
left: 0
right: 0
Edit: If you don't want to use absolute positioning (so that border comes around the caption too), you can use flexbox to do it –
Updated pen: https://codepen.io/palash/pen/BYyXjq
.container
display: flex
flex-direction: column
align-items: flex-start
width: min-content
...
&::after
background: #aabbaa
content: attr(data-caption)
line-height: 40px
padding: 0 1rem
Related
I have this problem specifically when building card components.
Here's some parent container to a challenge from frontendmentor:
.grid-card {
background-color: #fff;
border-radius: 15px;
/* min-height: 100%; */
margin: 1rem;
width: 350px;
display: grid;
align-self: center;
place-items: center;
gap: 1.5rem;
}
#hero-image {
/* border-radius: 15px; */
width: 100%;
/* margin: 16px 0px; */
}
without the border-radius property applied to the element, the corners do not round.
I've wrapped the image element inside a div and a picture tag. Both resulted in the corners remaining sharp; no rounded corners. The only solution is to apply the border-radius values to both the parent container and the child image element.
Here is the link to the solution and to the repo.
https://funupulu.github.io/frontend-mentor/newbie/order-summary-component-main/index.html
https://github.com/funupulu/funupulu.github.io/tree/main/frontend-mentor/newbie/order-summary-component-main
You have to set the two following attributes to your parent element in order to reshape your child border-radius based on your parent element the CSS to add:
position: relative;
overflow: hidden;
I have my heading with arrow ::after element. After element is positioned absolute with margin-left:10px, but when i shrink down website the element wraps with text down.
Is there a way for arrow to always stick to top right of heading no matter what width of device like this?
Tried with display inline but it doesnt work. Any idea if it's possible?
I'd wrap the heading in another element and use the ::after pseudo-element of the parent. In my example, I used a <div> with display: inline;, and set the heading's margin-right to 1em to account for the width of the ::after element. My code probably isn't too different from yours, just uses different selectors.
CodePen here: https://codepen.io/the_Northway/pen/GRMyrLR
Edit: adding the Stack Snippet by suggestion - codepen is still useful to change screen size though.
div {
position: relative;
display: inline;
text-align: center;
}
div h3 {
margin-top: 0;
margin-right: 1em;
}
div::after {
position: absolute;
display: block;
width: 1em;
height: 1em;
content: ">";
color: red;
top: 0;
right: 0;
}
body {
display: flex;
height: 100vh;
width: 100vw;
align-items: center;
justify-content: center;
}
<div><h3>Looking for help?</h3></div>
I am trying to get a line over my title that lines up evenly with lines before and after my `sub-title
I looked at two references:
Line before and after title over image
CSS technique for a horizontal line with words in the middle
These helped me get started but I am not sure how to get the top line even with the before and after lines without wrapping despite the length of the title or subtitle.
<div class="title">
<h1>Testingtesting</h1>
</div>
<div class="sub-title">
<h1>Testing</h1>
</div>
<style>
#import url(http://fonts.googleapis.com/css?family=Open+Sans:300);
h1 {
width: 20%;
margin: .7em auto;
overflow: hidden;
text-align: center;
font-weight:300;
color: #000;
}
h1:before, h1:after {
content: "";
display: inline-block;
width: 50%;
margin: 0 .1em 0 -55%;
vertical-align: middle;
border-bottom: 1px solid;
}
h1:after {
margin: 0 -55% 0 .1em;
}
span {
display: inline-block;
vertical-align: middle;
}
.title h1 {
border-top: 1px solid black
}
.title h1:before, .title h1:after {
border-bottom: 0px solid;
}
</style>
You should use white-space: wrap; it should work after using it as you have set width on the element on which you are setting this.
For example,
}
.title h1:after {
content:"\A";
white-space: pre;
}
Explanation
In CSS :after is used to generate some content known as a pseudo-element. The "\A" is interpreted as a line break provided that the white space is preserved, hence you need to set white-space: pre. Finally, the element has to be inline, hence display: inline.
I believe I was able to accomplish what you want with the use of flexbox. TL;DR: see snippet below.
First, I nested div.sub-title within div.title in the HTML.
Then, I turned the div.title into a flex container with display: flex, and set the flow direction to column. Adding align-items: center centers the elements within the container.
Next, I targeted the first h1 element, adding a border-top and border-bottom. You can make it however thick you like—I put 4px. If you want to add or reduce the spacing between the borders and the title, adjust the padding.
I then targeted the div.sub-title container. I gave it a position of relative and then offset its position vertically with top: -45px. You may want to adjust this value to get it centered the way you want it. I applied a zero line-height to remove the default value which is pretty tall on a heading. To adjust the spacing between the sub-title and the line on either side, add padding to div.sub-title—I used 20px. Lastly, add a background color that matches your page's background.
While this works, it'll largely depend on how much pre-defined values you're able to use (like padding and background-color).
Another thing to note is when the screen width gets too small, and the subtitle wraps, it'll look really ugly. This is due to the line-height being set to zero. To fix, you can set a min-width on div.title to prevent the entire container from going below a certain width or reset the line-height in div.sub-title at a certain breakpoint with a media query.
.title {
display: flex;
flex-flow: column nowrap;
align-items: center;
min-width: 350px;
}
.title > h1 {
display: inline;
padding: 30px 0;
border-top: 4px solid black;
border-bottom: 4px solid black;
text-align: center;
}
.sub-title {
position: relative;
top: -45px;
/* reset this w/ a media query when screen size gets too small */
line-height: 0px;
padding: 0 20px;
background-color: #fff;
}
<body>
<div class="title">
<h1>Tomorrow Or Something Longer</h1>
<div class="sub-title">
<h1>Today or something</h1>
</div>
</div>
</body>
I would like to make an anchor that is in a lower z-index clickable, here's a fiddle:
http://jsfiddle.net/mjooh3gv/
Setting the z-index only for the anchor (that's different from the partent div) does not work.
.underlay a {
position: absolute;
z-index: 5000;
}
What's the point in layering the two DIVs on top of each other in the first place?
If you want to position elements at the outer edges of a container, you can use several methods:
postion: relative on the container and postition: absolute on the children, or
float: left and float: right on the children (with clearing the container), or
using display: inline-block and width: 50% on the children, or
a layout <table> with two columns (Yeah, I know. Sue me.), or
using display: table-row on the container and display: table-cell on the children, or
display: flex, see https://css-tricks.com/snippets/css/a-guide-to-flexbox/ and http://caniuse.com/#feat=flexbox
The latter would work like this:
.container {
border: 5px inset red;
width: 300px;
padding: 5px;
display: flex;
}
.overlay {
margin-right: auto;
}
.underlay {
margin-left: auto;
}
<div class="container">
<div class="overlay">
Link 1
</div>
<div class="underlay">
Link 2
</div>
</div>
Here is the jsfiddle
In my example, giving either of the children elements a bottom margin causes its sibling to be pushed down by whatever margin I specify; I hadn't anticipated seeing anything move since the container is larger than each div. Why is this the case?
HTML
<div class=container>
<section></section>
<aside></aside>
</div>
CSS
.container {
background: whitesmoke;
height: 12em;
width: 12em;
}
.container section {
background: slategray;
display: inline-block;
height: 04em;
margin-bottom: 20px;
width: 04em;
}
.container aside {
background: gold;
display: inline-block;
height: 04em;
width: 04em;
}
Add vertical-align: top to your section element. As these elements are ìnline-block, they are not simply behaving as boxes anymore - they have flowing text properties. It is not really the margin that is pushing down the other element, it is the default vertical-align property they have.
jsFiddle Demo
Other Demo that shows the effect with text - the key is vertical-align