国外前端会考察哪些问题,其实CSS才是我的最大弱点,我的感觉是吃力不讨好。
CSS
1. center an element vertically
题目
This is a very basic question and good to be the first CSS problem on BFE.dev.
Suppose you have an HTML structure as below
1 | <div class="outer"> |
Please center the inner div vertically without changing their dimensions and colors.
1.Should be centered vertically

2.even if container size changes

答案
1 | /* // position relative and absolute |
2. truncate text in one line(with ellipsis)
题目
Truncate text in one line, if needed add ellipsis.
1.When no overflow

2.when there is need to truncate, add ellipsis

答案
See : https://www.w3schools.com/cssref/css3_pr_text-overflow.asp
1 | .one-line { |
3. truncate text in multiple lines(with ellipsis)
题目
Just like 2. truncate text in one line(with ellipsis), please do the same for max 3 lines.
1.short text(1 line)

2.short text (2 lines)

3. lines(no overflow)

4. more than 3 lines

答案
https://css-tricks.com/almanac/properties/l/line-clamp/
1 | /* 1. webkit-line-clamp will allow you to clamp text to 3 lines, |
4. two-column layout
题目
Implement a basic two-column layout, in which left column takes up 25% width of the container with minimum width of 100px and right column fills up the rest.
1.400x300

2.600x300

3.300x300

答案
flex布局
1 | .container { |
grid布局
1 | .container { |
5. modal with max height
题目
Let’s create a modal
- it has header(fixed height: 50px) and stretching body
- width of 300px and total max height 300px
- centered in viewport
- need mininum space of 30px to viewport top and bottom.
The HTML structure is something like
1 | <div class="modals"> |
1.400x150

2.400x300

3.400x400

4.400x600

答案
flex布局
Most discussions here are not providing context; so hopefully this will help beginners (like myself) to learn. Feel free to ask any questions
1 | .modals { |
grid布局
1 | .modals { |
6. different checkbox style
题目
The default checkbox style might not be what you want for most of the time, in this problem you are asked to create a different checkbox style.
- when unchecked, show a gray circle (
'color: gray') - when checked, show a green circle (
'color: green')
Set the circle with radius of 5px and don’t add extra padding, the HTML structure is
1 | <label class="my-checkbox"> |
1.unchecked

2.checked

答案
Most solutions proposed here are either:
- using the unreliable appearance property, or
- hiding the native input element completely from DOM tree with display:none or visibility:hidden (It reduces a11y support for visually impaired users who rely on keyboard and audio)
Here is the common practice to override native element without losing a11y support, reliably across browsers. Feel free to ask me any questions.
All credits go to Aditya Bhandari, I stumbled upon his excellent article while learning about this challenge.
1 | .my-checkbox { |
7. a row
题目
Suppose we have a row structured as below:
1 | <div class="row"></div> |
Please complete the CSS according to following requirements:
- height 50px
- set background to
#eee, but if there are adjacent rows, set even rows’ background to#ddd - set 1px border on top & bottom with color
#ccc, but if there are adjacent rows, adjacent borders should be collapsed into one
if you have to decide which row to dispplay both top and bottom border, add it to first row, ref
1.one row

2.two rows

3.three rows

答案
1 | .row { |
8. Twitter’s website layout
题目
Open Twitter’s website and change the window size, you’ll notice that layout changes responsively.
Let’s do something similar in this problem, suppose we have HTML structure as below.
1 | <div class="container"> |
Now please complete the CSS to satisfy following requirement
- when viewport width is not enough, set left column to 40px wide and middle column to stretching.
- middle column has maximum width of 240px
- when there is enough space, show right column which has width of 120px
- if there is more space, set left column to 80px
- when right column is visible, set minimum 10px space horizontally to the viewport border
It is a bit hard to explain clearly, but following screenshots might be easier to understand.
1.100 x 150

2.200 x 150

3.300 x 150

4.360 x 150

5.400 x 150

6.420 x 150

7.460 x 150

8.500 x 150

答案
媒体查询
1 | .container { |
9. multi-column text
题目
It is very common to see text in multiple columns on newspaper, please achieve something similar in this problem.
Suppose you have HTML structure like below.
1 | <div class="three-column-text"> |
Complete the CSS code according to following requirements
- divide into 3 columns
- use 1px line of
#dddas separator - adding 10px space around each column, to container border and to the separator
The text used in test doesn’t contain space but has forced line break to show padding/margin clearly.
text in 3 columns

答案
https://css-tricks.com/almanac/properties/c/columns/
1 | .three-column-text { |
10. golden-ratio rectangle
题目
Let’s create a golden-ratio rectangle, which means width/height = 1.618.
This rectangle should have the fixed ratio even when width changes.
1 | <div class="golden-ratio"></div> |
1.width: 50px

2.width: 100px

3.width: 200px

答案
https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio
1 | /* Aspect-ratio |
11. fit the image
题目
Suppose we have following image of ratio 1:4
Now in an HTML structure like below, please fit the image to the container div, just as if it is used as container’s background-image with background-size:cover.
1 | <div> |
1.50x50

2.100x100

3.200x100

4.100x200

5.300x100

6.100x300

7.700x100

答案
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
The object-fit CSS property sets how the content of a replaced element, such as an or
You can alter the alignment of the replaced element’s content object within the element’s box using the object-position property.
1 | .image { |
12. close button in CSS
题目
Create a close button in CSS.
Lines for the cross should have
- length : 3/4 of button width
- thickness: 2px
- color:
#aaa
1 | <button class="close"></button> |
1.20x20

2.40x40

3.60x60

答案
伪元素
1 | .close { |
13. list numbering
题目
Suppose we have some lists, they could be simple as
1 | <ol> |
or nested.
1 | <ol> |
please number the list as below, with color #f44336 and one space between the number and content.
1.simple list

2.nested

答案
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Counter_Styles/Using_CSS_counters
1 | ol > li::marker { |
14. 0.5px border
题目
When we set 0.5px in CSS, usually it will be rounded by browsers. But with better and better display like Retina display, 1px would sometimes looks thick.
Please add 0.5px top border of black for the following div.
1 | <div class="hairline"></div> |
should be thin enough

答案
利用trasform减低宽度
About scale: https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform-function/scale()
About transform-origin: https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform-origin
1 | .hairline { |
15. doughnut chart
题目
Let’s create a doughnut chart in CSS.
- radius: 50px
- thickness: 10px
- color:
#f44336
The percentage should be able to set by CSS variable --percentage.
For example, to create a doughnut chart with 20%:
1 | <div class="piechart" style="--percentage:20%"></div> |
A doughnut chart with 75%:
1 | <div class="piechart" style="--percentage:75%"></div> |
1.percentage: 20%

2.percentage: 75%

3.percentage: 100%

答案
To solve this task you have to make a circle using the ‘clip-path’ property instead of ‘border-radius’. In this way, the browser draws a circle using other calculations. Then your solution will be accepted.
1 | .piechart { |
16. flex layout 1
题目
Suppose we have to layout a bunch of items with below requirements.
- fill out to width:100px, but stretch to fill the available space and shrink if not enough
- stack them if needed
HTML structure is
1 | <div class="container"> |
1.width:60px

2.width:150px

3.width:250px

4.width:400px

答案
To solve this task you have to make a circle using the ‘clip-path’ property instead of ‘border-radius’. In this way, the browser draws a circle using other calculations. Then your solution will be accepted.
1 | .container { |
17. fragment style
题目
Suppose we have an <a/> tag which is streched to multiple lines, add borders to all its fragments.
- color
#7aa4f0with1pxthickness - add
5pxpadding horizontally for each line
1 | <a class="border"> |
1.width:100px

2.width:200px

3.width:500px

答案
https://developer.mozilla.org/en-US/docs/Web/CSS/box-decoration-break
1 | .border { |
18. color gradients on text
题目
Let’s give text some color gradients, from red on the left to yellow on the right.
1 | <span class="gradient-text">practice on BFE.dev, get an offer</span> |
1.short text

2.longer text

答案
https://developer.mozilla.org/en-US/docs/Web/CSS/box-decoration-break
1 | .gradient-text { |
19. color of input elements
题目
We have a slider but we want to change the default color into #F44336.
1 | <input type="range" min="0" max="10"> |
#F44336

答案
https://developer.mozilla.org/en-US/docs/Web/CSS/accent-color
1 | input[type="range"] { |
20. sticky footer
题目
“Sticky footer” is a layout pattern that
- if content is short, the footer “sticks” to the bottom
- otherwise, footer is is displayed after the content as normal.
Suppose we have some HTML structure:
1 | <div class="container"> |
Below is how Sticky Footer pattern should behave.
1.footer sticks to bottom if content is short

2.otherwise footer is displayed as normal

3.event be pushed out from viewport if enough content

答案
margin: auto actually has no specific rules and browser pretty much decides how it effects the element 😅
However, when we give any margin as auto to a child in a flex container, it basically means that margin should take the maximum possible value for that element.
In simple terms, it will push that element to opposite edge, ex. margin-left : auto , will push that element to the right edge.
If you want a little more detail, read this explanation on https://css-tricks.com/the-peculiar-magic-of-flexbox-and-auto-margins/
1 | .container{ |
21. Holy Grail Layout
题目
“Implement Holy Grail Layout which includes header, left sidebar, body, right sidebar and footer.
- it should be sticky footer
- left sidebar and right sidebar should be fixed width of 100px.
- styles of color .etc are already set for you, only layout related CSS code is needed.
1 | <div class="container"> |
1.footer sticks to bottom if content is short

2.otherwise footer is displayed as normal

3.event be pushed out from viewport if enough content

答案
grid布局
1 | .container { |
flex布局
1 | .header, .footer{ |
22. Grid Layout 1
题目
Layout a 3x3 grid and place A to I in column-first order.
- set gap in rows and columns of
10px - container size is already set.
1 | <div class="container"> |
200x200

答案
grid布局
1 | .item { |
另一种方式
1 | .container { |
flex布局也可以
1 | .item { |
23. Grid Layout 2
题目
Layout the items in a grid so that:
- items have minium width of 100px and fill up the space
- place as many items in a row as possible
- gap between items is 10px
1 | <div class="container"> |
1.width:100px

2.width:150px

3.width:250px

4.width:400px

答案
自适应grid布局
1 | .container { |
24. fluid font size
题目
Create a heading of fluid font size.
- if viewport width is smaller than 200px, use 16px
- if viewport width is bigger than 400px, use 32px
- otherwise font-size is linearly scaled. For example if viewport is 300px = (200px + 400px) / 2, then font-size is 24px = (16px + 32px) / 2
1 | <h1 class="title">BFE.dev</h1> |
1.100px

2.150px

3.200px

4.219px

5.252px

6.300px

7.379px

8.400px

9.500px

答案
clamp() is a CSS function that clamps a value between an upper and lower bound. clamp() enables selecting a middle value within a range of values between a defined minimum and maximum. It takes three parameters: a minimum value, a preferred value, and a maximum allowed value. https://developer.mozilla.org/en-US/docs/Web/CSS/clamp
1 | .title { |