I have a primary and a secondary headline.
The primary headline is longer and should be centered.
The second headline should start exactly where the first one starts.
So if this is my setup:
<h1 className="sofortcheck__headline--mobile">
Check your situation for free
</h1>
<h2 className="sofortcheck__subheader--mobile">
Only <span>2 minutes</span>
</h2>
I want both of those lines to start at exactly the same spot (on all devices) after I center the first line.
Is there a way to do this in pure CSS? So far I can only think of getting the left offset in JavaScript and then setting that for the second headline, but I feel like there should be a way to achieve this in CSS only.
When I center both elements the second one starts later than the first one because it's shorter.
When I use px margins it doesn't work for different screens.
This is fairly easy to do in the markup. You left-justify both the headline and subheadline in an element, then center that element relative to your page.
h1 { font-size: 0.75em; }
h2 { font-size: 0.5em; }
.headline-container {
display: flex;
}
.headline {
display: inline-block;
margin: 0 auto;
}
<div class="headline-container">
<div class="headline">
<h1>The primary headline which is longer and determines the centering.</h1>
<h2>The subheadline which is left-justified to the primary.</h2>
</div>
</div>
CSS-Grid can do that.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
::before,
::after {
box-sizing: inherit;
}
body {
display: grid;
grid-template-columns: 1fr auto 1fr;
position: relative;
align-items: center;
}
body::before {
content: "";
position: absolute;
left: 50%;
height: 100vh;
width: 1px;
background: green;
}
.sofortcheck__headline--mobile {
grid-column: 2;
border: 1px solid red;
}
.sofortcheck__subheader--mobile {
grid-column: 2;
grid-row: 2;
border: 1px solid grey;
}
<h1 class="sofortcheck__headline--mobile">
Check your situation for free
</h1>
<h2 class="sofortcheck__subheader--mobile">
Only <span>2 minutes</span>
</h2>
Related
I know there have been a quite a few like this on here and other sites already, but am kicking myself over this and really should have solved this issue by now - so here it is:
https://jsfiddle.net/p3reauLf/1/
.container {
float: left;
position: relative;
padding-right: 10px;
overflow: hidden;
background-color: red;
}
.vert-text {
float: left;
position: relative;
writing-mode: vertical-lr;
text-orientation: upright;
background-color: black;
color: white;
margin-right: 5px;
padding: 5px;
height: 100%;
}
.info {
position: relative;
white-space: nowrap;
display: flow-root;
}
<div class="container">
<div class="vert-text"><strong>TRAX</strong></div>
<div class="info"><strong>A1.</strong> Activator (Untitled Mix 1)<br>
<strong>B1.</strong> Activator (Untitled Mix 2)<br>
<strong>B2.</strong> Activator (Untitled Mix 3)<br></div>
</div>
<div class="container">
<div class="vert-text"><strong>DATA</strong></div>
<div class="info"><strong>Label:</strong> Vicious Muzik Records<br>
<strong>Released:</strong> 1994<br>
<strong>Condition:</strong> VG+ to NM-<br></div>
</div>
In this JS fiddle you can see the 2 boxes side by side with text (dynamic on my web site) where the text appears to be overlapped by the second box - and i have no clue how to make the width of the boxes adjust to size of the text within -
of course i would like the box on the right to remain to the right of the first box, unless of course the page gets to narrow in which case it bumps down under the 1st box -
at least that is what i am trying to achieve - live page here:
on this live page i have adjusted to the width to be fixed, but sometimes text is longer or much shorter and it either gets truncated or is too long and looks off...
appreciate all the help i can get ! thanks
You can use the modern CSS grid layout mode, and create two columns in a media query with the breakpoint of your choosing:
body {
display: grid;
grid-template-columns: 1fr;
}
.container {
float: left;
position: relative;
padding-right: 10px;
overflow: hidden;
background-color: red;
}
.vert-text {
float: left;
position: relative;
writing-mode: vertical-lr;
text-orientation: upright;
background-color: black;
color: white;
margin-right: 5px;
padding: 5px;
height: 100%;
}
.info {
position: relative;
white-space: nowrap;
display: flow-root;
}
#media (min-width: 35rem) {
body {
grid-template-columns: 1fr 1fr;
}
}
<div class="container">
<div class="vert-text"><strong>TRAX</strong></div>
<div class="info"><strong>A1.</strong> Activator (Untitled Mix 1)<br>
<strong>B1.</strong> Activator (Untitled Mix 2)<br>
<strong>B2.</strong> Activator (Untitled Mix 3)<br></div>
</div>
<div class="container">
<div class="vert-text"><strong>DATA</strong></div>
<div class="info"><strong>Label:</strong> Vicious Muzik Records<br>
<strong>Released:</strong> 1994<br>
<strong>Condition:</strong> VG+ to NM-<br></div>
</div>
Sample code:
a {
background: tan;
}
div {
width: 100px;
height: 200px;
background: green;
}
Link 1
This is link 2.
3rd
<div></div>
This is the effect I wish to achieve:
It's easy to vertically align the anchor elements by something like display: block, but I have no idea how to put the <div> element next to them.
Note 1: The above is just a sample code. My actual links can be any width.
Note 2: The <div> needs to be the anchors' next sibling. It can be any height.
I would go for a grid:
You can set it to adapt automatically to the inner dimensions.
You only need to set some row and column properties to locate the inner elements
a {
background: tan;
grid-column: 1;
}
a:nth-child(2) {
grid-row: 2;
}
#mydiv {
width: 100px;
height: 200px;
background: green;
grid-column: 2;
grid-row: 1 / 5;
}
.container {
display: grid;
grid-template-columns: max-content auto;
grid-template-rows: auto auto auto 1fr;
}
<div class="container">
Link 1
This is link 2.
3rd
<div id="mydiv"></div>
</div>
It will work also in the case of a huge margin bottom on anchors
a {
background: tan;
grid-column: 1;
margin-bottom: 200px;
}
a:nth-child(2) {
grid-row: 2;
}
#mydiv {
width: 100px;
height: 200px;
background: green;
grid-column: 2;
grid-row: 1 / 5;
}
.container {
display: grid;
grid-template-columns: max-content auto;
grid-template-rows: auto auto auto 1fr;
}
<div class="container">
Link 1
This is link 2, now longer.
3rd
<div id="mydiv"></div>
</div>
Try this solution
First we need two columns, one column for links, second for the DIV
tag. I preferred to take the grid.
Used fit-content to for fit background on links.
The DIV needs to set as position: absolute, because without
absolute position, div will be resize only the first row in the
second column.
For adjustment width of the div, i was create a variable. This
resizes both the second column of the grid and width of the div.
:root {
--second-column: 100px;
}
body {
width: min-content;
display: grid;
grid-template-columns: max-content var(--second-column);
grid-auto-rows: min-content;
row-gap: 1rem;
column-gap: 2rem;
background-color: hsl(201, 27%, 10%);
position: relative;
}
a {
grid-column: 1;
width: fit-content;
background: tan;
}
div {
grid-column: 2;
grid-row: 1 / -1;
width: var(--second-column);
height: 200px;
position: absolute;
background: green;
}
Link 1
This is link 2.
3rd
<div></div>
The good old float
a {
background: tan;
margin: 5px;
/* all the links at the left
above each other */
float: left;
clear: left;
}
div {
width: 100px;
height: 200px;
background: green;
overflow: auto; /* do not overlap the links and stay at the right */
}
Link 1
This is link 2.
3rd
<div></div>
Here, check this out.
a {
background: tan;
display: block;
margin-bottom: 5px;
}
div {
width: 100px;
height: 100px;
display: inline-block;
vertical-align: top;
}
div.div2 {
background: green;
}
<div>
Link 1
This is link 2.
3rd
</div>
<div class="div2"></div>
a {
background: tan;
}
#child-1 {
display: flex;
flex-direction: column;
align-items: flex-start;
}
#child-2 {
width: 100px;
height: 100px;
background: green;
}
#parent {
display: flex;
}
<div id="parent">
<div id="child-1">
Link 1
This is link 2.
3rd
</div>
<div id="child-2"></div>
</div>
Easiest is to use float:left; on all links as mentioned before. but we probably don't really like float for situations where we also might want to do more complex stuff.
Also you might want to add markup for semantic purposes anyway (like <nav> for the links). The only reason I could make up for the links having to be siblings of the div is javaScript, and bet it will be possible to tune it more performant with slightly more markup around.
To do so you might want to have a look at flexbox like so:
.container{
display:flex;
flex-direction: row;
justify-content: space-between;
border: 1px solid blue;
}
ul{
flex: 1 1 auto;
align-self: flex-start;
display:flex;
flex-direction: column;
padding: 0;
margin: 0;
}
li{
border: 1px solid red;
flex: 1 1 auto;
}
a {
background: tan;
}
div.main {
flex: 0 1 100px;
height: 200px;
background: green;
}
<div class="container">
<ul>
<li>Link 1</li>
<li>This is link 2.</li>
<li>3rd</li>
</ul>
<div class="main">lore ipsum</div>
</div>
No matter how you do it you need a little more markup to solve this if you want a generic answer but not use float. There might be a way around this in certain situations but there is none in a genric scenario. That is if you do not allow javaScript to fix it (which would most likely be an unresasonable solution).
The li of cause is unnecessary for your question but in general its a good idea not to position a tags directly but to wrap them. a-tags also tend to have unforeseen bugs when positioned with flexbox.
The surrounding div.container could be the documents body tag instead. Then you could simplify to:
body{
display:flex;
flex-direction: row;
justify-content: space-between;
border: 1px solid blue;
}
nav{
flex: 1 1 auto;
align-self: flex-start;
display:flex;
flex-direction: column;
padding: 0;
margin: 0;
}
a {
background: tan;
}
div {
flex: 0 1 100px;
height: 200px;
background: green;
}
<nav>
Link 1
This is link 2.
3rd
</nav>
<div>lorem ipsum</div>
i used and change this solution inside my answer.
div element gives links higtht and stays after theme.
body {
display: grid;
grid-template-columns: 1fr max-content;
width: max-content;
}
a {
background: tan;
margin: 50px;
grid-column: 1;
width: fit-content;
}
div {
width: 100px;
min-height: 200px;
background: green;
grid-column: 2;
grid-row: 1 / 9999;
}
Link 1
This is link 2.
3rd
4th long long and too long link. f it Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate nostrum quibusdam cupiditate deleniti possimus sit!
<div></div>
You have to use position property and display inline-block or you can use display flex
a {
background: tan;
display:block;
margin-bottom: 16px;
width: 100px;
}
.main {
width: 100px;
height: 100px;
background: green;
position: absolute;
left: 20%;
top: 8px;
}
Link 1
This is link 2.
3rd
<div class="main"></div>
Use CSS clear Property to make anchor elements not allowed to Float on the left side in relation to other element
Using CSS Overflow, the overflow of the div is clipped, and the rest of the content will be invisible, I set it to use hidden, you can also use auto, but auto adds scrollbars only when necessary
a {
background: tan;
float: left;
clear: left;
}
div {
width: 100px;
height: 200px;
background: green;
overflow: hidden;
}
Link 1
This is link 2.
3rd
This is a long string for testing
<div></div>
Flexbox seems to be a good approach. Wrap all the links (anchor tags) in a div element and then wrap that div along with the other div you want to be side by side in another div. See below code example:
CSS
.parentDiv {
display: flex;
}
.links {
display: flex;
flex-direction: column;
}
a {
background: tan;
}
.otherDiv {
width: 100px;
height: 200px;
background: green;
}
HTML
<div class="parentDiv">
<div class="links">
Link 1
This is link 2.
3rd
</div>
<div class="otherDiv"></div>
</div>
As a fan of using flexbox, I came up with this approach
HTML
<div class="parent">
<div>
Link 1
This is link 2.
3rd
</div>
<div></div>
</div>
CSS
a {
background: tan;
}
.parent {
display: flex;
}
.parent div:first-child {
display: flex;
flex-direction: column;
margin-right: 20px;
}
.parent div:first-child a {
margin-bottom: 10px;
}
.parent div:last-child {
width: 100px;
height: 200px;
background: green;
}
Just wrapper your content with one parent class with Flex property and it's done. It will solve your problem.
Refer to below snippet code
.parent {
display: flex;
}
a {
background: tan;
}
.otherclass {
width: 100px;
height: auto;
background: green;
flex: 0 0 1;
}
<div class="parent">
<ul>
<li>
Link 1</li>
<li>
This is link 2.</li>
<li>
3rd</li>
<li>
Link 1</li>
<li>
This is link 2.</li>
<li>
3rd</li>
</ul>
<div class='otherclass'></div>
</div>
I'm struggling to make myself clear, so first, let me show you what I have already done:
Here's an extract of my HTML page :
<div id="random_div">
random text message
</div>
<p>
warning 1
</p>
<p>
warning 2
</p>
and my CSS:
#random_div {
background: #fff;
padding: 20px;
font-size: 25px;
margin: 15px;
width: 150px;
float: left;
}
p{
background-color: orange;
}
which give the output :
And I'd want something like that :
Where there is no overlap between the div and the 2 p.
edit
I'd want to keep the margin of the div, and generally speaking, how the elements are placed, and if it's possible to add margin between the div and the two p, that's even better :)
This is because you are not clearing floats. Wrap the floats with a clearfix class.
#random_div {
background: #fff;
padding: 20px;
font-size: 25px;
margin-right: 15px;
width: 150px;
float: left;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
p{
background-color: orange;
}
<div class="clearfix">
<div id="random_div">
random text message
</div>
<div>
<p>
warning 1
</p>
<p>
warning 2
</p>
</div>
</div>
I made you a simple solution using css grid.
The only change is that instead of using floats, this way you can actually modify the layout much easier. Check the code here: https://codepen.io/lukagurovic/pen/zYYQJNv
<div id="content">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
#content {
max-width: 960px;
margin: auto;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, minmax(150px, auto));
grid-gap: 10px;
}
.one {
grid-row: 1 / 3;
}
.two {
grid-column: 2 / 5;
}
.three {
grid-column: 2 / 5;
}
Okay, So a friend told me an acceptable solution, I don't really want to take the credit, but well, I have to close this.
The solution is to use flex-box instead of float, which are not really meant for what I wanted to do.
html:
<div class='wrapper'>
<div id="random_div">
random text message
</div>
<div class='message'>
<p>
warning 1
</p>
<p>
warning 2
</p>
</div>
</div>
css:
#random_div {
background: #fff;
padding: 20px;
font-size: 25px;
width: 150px;
margin: 15px;
}
p{
background-color: orange;
margin-right: 5px;
}
.wrapper {
display: flex;
align-items: center;
}
.message {
flex-grow: 1;
}
result:
And now I just need to work a bit with the margin to have the desired output :)
Is this layout remotely possible in IE11 (very crude example)?
$(document).on('click', '.js-toggle-hide', function(e){
e.preventDefault();
$(this).parent().parent().find('.js-hide').toggle();
});
#charset "UTF-8";
.c-sidebar {
display: grid;
grid-template-columns: 120px auto;
outline: 1px solid #ccc;
overflow: auto;
}
.c-cat {
display: grid;
grid-template-columns: 120px auto;
grid-template-rows: auto;
}
.c-cat__name {
grid-row: 1 / 1000;
}
.c-cat__toggle {
grid-row: 1 / 999;
min-width: 120px;
}
.c-cat__subcat {
grid-column: 3 / 4;
min-width: 120px;
}
.c-cat__subcat--all {
grid-column: 2 / 4;
}
/* decoration */
body {
padding: 15px;
background: #eee;
font-size: 11px;
}
.c-cell {
background: #fff;
outline: 1px solid #ccc;
padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="c-sidebar">
<div class="c-cell">All Categories</div>
<div>
<div class="c-cat">
<div class="c-cat__name c-cell">Sales</div>
<div class="c-cat__toggle c-cell">
All items
</div>
<div class="c-cat__subcat c-cell js-hide">Export sales</div>
<div class="c-cat__subcat c-cell js-hide">Other sales</div>
<div class="c-cat__subcat c-cell js-hide">Product sales</div>
<div class="c-cat__subcat--all c-cell js-hide"><b>All items</b></div>
</div>
</div>
</div>
Note that the number of items in the right columns is undefined (types of sales), client can add/remove them.
Are there any css tricks I'm not aware of?
The alternative is to use tables with complex js/jquery code (this is just a part of the code but it represents my dilemma perfectly) and that would be very tedious work.
My recommendation is if you want to be grid modern and still support something like IE11....then start with in display: flex and then do a media support to add your grid like this:
#supports (display: grid) {
#_your css grid here _#
}
So then you can add all your grid there....you are not necessarily repeating if you do this you are just doing the grid part here(grid-template, columns, rows, etc) and bypass any other styling like color, font, px and etc...
Can I create a layout like on the picture below, while setting the fixed width only on the parent container? I also cannot use position: absolute; left: 0; right: 0; on Full screen width child, as I cannot remove it from the flow, because it's size is dynamic.
I can't change the markup.
The only solution I can think of is setting the fixed width on every Fixed-width child separately, but as I have a lot of them, that's not the most comfortable solution - means adding a class for every child that I add into the parent container.
Here is an example markup you can post a solution to.
HTML
<div class="fixed-width-container">
<div class="regular-child"></div>
<div class="full-screen-width-child"></div>
<div class="regular-child"></div>
<div class="regular-child"></div>
</div>
CSS
.fixed-width-container {
width: <some-fixed-width>;
}
you can give a try to the flex layout : https://css-tricks.com/snippets/css/a-guide-to-flexbox/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-align: center;
}
body {
margin: 0;
}
div {
border: 1px solid #333;
}
.fixed-width-container {
width: 400px;/* any width set */
margin: auto;
padding: 10px 10px 0;
background: yellow;
display: flex;
flex-flow: column;
align-items: center;
}
.fixed-width-container>div {
height: 3em;
margin-bottom: 10px;
background: lightblue;
min-width: 100%;
}
.full-screen-width-child {
width: 99vw;/* 100vw is fine too */
}
<div class="fixed-width-container">
<div class="regular-child">Fixed-width child</div>
<div class="full-screen-width-child">Full screen width child with dynamic contents</div>
<div class="regular-child">Fixed-width child</div>
<div class="regular-child">Fixed-width child</div>
</div>
codepen to test and play with
This is just an attempt, and probably not a very good one. But maybe it will spawn some more sophisticated solutions by others, or even yourself.
Idea: negative margins for the full-width child.
* {
box-sizing: border-box;
text-align: center;
}
body {
width: 100%;
background: #fff;
}
div {
border: 1px solid #333;
margin-bottom: 10px;
}
div:last-child {
margin-bottom: 0;
}
.fixed-width-container {
width: 70%;
margin: auto;
padding: 10px;
background: LightYellow;
}
.regular-child,
.full-screen-width-child {
height: 45px;
line-height: 45px;
background: LightBlue;
}
.full-screen-width-child {
margin-left: -24%;
margin-right: -24%;
background: LightGreen;
}
<div class="fixed-width-container">
<div class="regular-child">Fixed-width child</div>
<div class="full-screen-width-child">Full screen width child with dynamic contents</div>
<div class="regular-child">Fixed-width child</div>
<div class="regular-child">Fixed-width child</div>
</div>
The problematic part here is the dimension of the negative margins. If you use %, it will relate to the width of the fixed-width-container. Here, I chose width: 70% for it. Given a body width of 625px (as is the case for the Stack Snippet preview) and a margin of -24%, that would give a negative margin of 625px * 0.7 * 0.24 = 105px. I'm not sure what's the best approach of making this work for any configuration.